Advertisement
exnon

Motiondetector ISSP2

Nov 26th, 2020 (edited)
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file    main.cpp
  4.  * @author  CLab
  5.  * @version V1.0.0
  6.  * @date    2-December-2016
  7.  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1
  8.  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
  9.  ******************************************************************************
  10.  * @attention
  11.  *
  12.  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without modification,
  15.  * are permitted provided that the following conditions are met:
  16.  *   1. Redistributions of source code must retain the above copyright notice,
  17.  *      this list of conditions and the following disclaimer.
  18.  *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.  *      this list of conditions and the following disclaimer in the documentation
  20.  *      and/or other materials provided with the distribution.
  21.  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.  *      may be used to endorse or promote products derived from this software
  23.  *      without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  *
  36.  ******************************************************************************
  37. */
  38.  
  39. /* Includes */
  40. #include "mbed.h"
  41. #include "XNucleoIKS01A2.h"
  42.  
  43. /* Instantiate the expansion board */
  44. static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
  45.  
  46. /* Retrieve the composing elements of the expansion board */
  47. static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
  48. static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
  49. static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
  50. static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
  51. static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
  52.  
  53. /* Helper function for printing floats & doubles */
  54. static char *print_double(char* str, double v, int decimalDigits=2)
  55. {
  56.   int i = 1;
  57.   int intPart, fractPart;
  58.   int len;
  59.   char *ptr;
  60.  
  61.   /* prepare decimal digits multiplicator */
  62.   for (;decimalDigits!=0; i*=10, decimalDigits--);
  63.  
  64.   /* calculate integer & fractinal parts */
  65.   intPart = (int)v;
  66.   fractPart = (int)((v-(double)(int)v)*i);
  67.  
  68.   /* fill in integer part */
  69.   sprintf(str, "%i.", intPart);
  70.  
  71.   /* prepare fill in of fractional part */
  72.   len = strlen(str);
  73.   ptr = &str[len];
  74.  
  75.   /* fill in leading fractional zeros */
  76.   for (i/=10;i>1; i/=10, ptr++) {
  77.     if (fractPart >= i) {
  78.       break;
  79.     }
  80.     *ptr = '0';
  81.   }
  82.  
  83.   /* fill in (rest of) fractional part */
  84.   sprintf(ptr, "%i", fractPart);
  85.  
  86.   return str;
  87. }
  88.  
  89. /* Simple main function */
  90. int main() {
  91.   uint8_t id;
  92.   float value1, value2;
  93.   char buffer1[32], buffer2[32];
  94.   int32_t axes[3];
  95.  
  96.   /* Enable all sensors */
  97.   hum_temp->enable();
  98.   press_temp->enable();
  99.   magnetometer->enable();
  100.   accelerometer->enable();
  101.   acc_gyro->enable_x();
  102.   acc_gyro->enable_g();
  103.  
  104.   printf("\r\n--- Starting new run ---\r\n");
  105.  
  106.   hum_temp->read_id(&id);
  107.   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
  108.   press_temp->read_id(&id);
  109.   printf("LPS22HB  pressure & temperature   = 0x%X\r\n", id);
  110.   magnetometer->read_id(&id);
  111.   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
  112.   accelerometer->read_id(&id);
  113.   printf("LSM303AGR accelerometer           = 0x%X\r\n", id);
  114.   acc_gyro->read_id(&id);
  115.   printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
  116. //  int32_t xMagneto, yMagneto, zMagneto, xAccelero, yAccelero, zAccelero, xLSMAcceleto, yLSMAcceleto, zLSMAcceleto, xGyro, yGyro, zGyro;
  117.     int32_t recentData[12], currData[12], diffData[12];
  118.     acc_gyro->get_g_axes(axes);
  119.     recentData[9] = axes[0];
  120.     recentData[10] = axes[1];
  121.     int32_t joyX, joyY =0;
  122.   while(1) {
  123.     printf("\r\n");
  124.  
  125.     hum_temp->get_temperature(&value1);
  126.     hum_temp->get_humidity(&value2);
  127.     printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
  128.    
  129.     press_temp->get_temperature(&value1);
  130.     press_temp->get_pressure(&value2);
  131.     printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
  132.         int32_t xDiff, yDiff;
  133.     printf("---\r\n");
  134.  
  135.     magnetometer->get_m_axes(axes);
  136.         printf("LSM303AGR [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
  137.         for(int i=0; i<3;i++){
  138.             if (axes[i] < 0){
  139.                 axes[i] = -axes[i];
  140.             }
  141.             currData[i] = axes[i];
  142.         }
  143.     accelerometer->get_x_axes(axes);
  144.     printf("LSM303AGR [acc/mg]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
  145.                 for(int i=0; i<3;i++){
  146.                     if (axes[i] < 0){
  147.                         axes[i] = -axes[i];
  148.             }
  149.             currData[3+i] = axes[i];
  150.         }
  151.     acc_gyro->get_x_axes(axes);
  152.     printf("LSM6DSL [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
  153.                 for(int i=0; i<3;i++){
  154.                     if (axes[i] < 0){
  155.                         axes[i] = -axes[i];
  156.                     }
  157.             currData[6+i] = axes[i];
  158.         }
  159.     acc_gyro->get_g_axes(axes);
  160.     printf("LSM6DSL [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
  161.                 for(int i=0; i<3;i++){
  162.                     if (axes[i] < 0){
  163.                         axes[i] = -axes[i];
  164.                     }
  165.                 currData[9+i] = axes[i];
  166.         }
  167.                 xDiff = axes[0]-recentData[0];
  168.         if (xDiff > 100){ //Differenz der Daten vergleichen und wenn es positiv über den Schwellwert von 100 ist wird unsere Koordinate um 1 erhöht.
  169.             joyX++;
  170.         }
  171.             else if (xDiff < -100){
  172.                 joyX--;
  173.             }
  174.                 yDiff = axes[1]-recentData[1];
  175.         if (yDiff < -2){
  176.             joyY--;
  177.         }
  178.             else if (yDiff > 2){
  179.                 joyY++;
  180.             }
  181.             printf("Joystick x und y Ausgabe:  %d, %d.\r\n", joyX, joyY);
  182.         recentData[9] = axes[0];
  183.         recentData[10] = axes[1];
  184.         for(int i=0; i<12;i++){
  185.             diffData[i] = currData[i]-recentData[i];
  186.         }
  187.         for (int i=0; i<3;i++){
  188.             //Checkt ob die Bewegung auf allen 4 Sensoren entdeckt wurde.
  189.             if (diffData[i] > 0 && diffData[i+3] >0 && diffData[i+6] >0  && diffData[i+9]>0){
  190.                     printf("Movement = 1");
  191.             }
  192.         }
  193.         for (int i=0; i<12;i++){
  194.             recentData[i] = currData[i];
  195.         }
  196.         // Alle 12 Werte auf eine Zeile
  197.             magnetometer->get_m_axes(axes);
  198.     printf("LSM303AGR [mag/mgauss]:  %6ld, %6ld, %6ld. ", axes[0], axes[1], axes[2]);
  199.    
  200.     accelerometer->get_x_axes(axes);
  201.     printf("LSM303AGR [acc/mg]:  %6ld, %6ld, %6ld. ", axes[0], axes[1], axes[2]);
  202.  
  203.     acc_gyro->get_x_axes(axes);
  204.     printf("LSM6DSL [acc/mg]:      %6ld, %6ld, %6ld. ", axes[0], axes[1], axes[2]);
  205.  
  206.     acc_gyro->get_g_axes(axes);
  207.     printf("LSM6DSL [gyro/mdps]:   %6ld, %6ld, %6ld.", axes[0], axes[1], axes[2]);
  208.  
  209.     wait(1.5);
  210.   }
  211. }
  212.    
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement