Example to sweep a servo according to MEMS sensors position

Dependencies:   Servo X_NUCLEO_IKS01A1 mbed

Fork of HelloWorld_IKS01A1 by ST

Committer:
mfr16
Date:
Thu Oct 06 10:11:06 2016 +0000
Revision:
9:10f36a418bc0
Parent:
4:b1526d074d83
Example to sweep a servo according to MEMS sensors position

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:c71c9af137dd 1 /**
Wolfgang Betz 0:c71c9af137dd 2 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 3 * @file main.cpp
Wolfgang Betz 0:c71c9af137dd 4 * @author AST / EST
Wolfgang Betz 0:c71c9af137dd 5 * @version V0.0.1
Wolfgang Betz 0:c71c9af137dd 6 * @date 14-August-2015
Wolfgang Betz 0:c71c9af137dd 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A1
Wolfgang Betz 0:c71c9af137dd 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
Wolfgang Betz 0:c71c9af137dd 9 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 10 * @attention
Wolfgang Betz 0:c71c9af137dd 11 *
Wolfgang Betz 0:c71c9af137dd 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 0:c71c9af137dd 13 *
Wolfgang Betz 0:c71c9af137dd 14 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 0:c71c9af137dd 15 * are permitted provided that the following conditions are met:
Wolfgang Betz 0:c71c9af137dd 16 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 17 * this list of conditions and the following disclaimer.
Wolfgang Betz 0:c71c9af137dd 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 19 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 0:c71c9af137dd 20 * and/or other materials provided with the distribution.
Wolfgang Betz 0:c71c9af137dd 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 0:c71c9af137dd 22 * may be used to endorse or promote products derived from this software
Wolfgang Betz 0:c71c9af137dd 23 * without specific prior written permission.
Wolfgang Betz 0:c71c9af137dd 24 *
Wolfgang Betz 0:c71c9af137dd 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 0:c71c9af137dd 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 0:c71c9af137dd 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 0:c71c9af137dd 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 0:c71c9af137dd 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 0:c71c9af137dd 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 0:c71c9af137dd 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 0:c71c9af137dd 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 0:c71c9af137dd 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 0:c71c9af137dd 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 0:c71c9af137dd 35 *
Wolfgang Betz 0:c71c9af137dd 36 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 37 */
Wolfgang Betz 0:c71c9af137dd 38
Wolfgang Betz 0:c71c9af137dd 39 /* Includes */
Wolfgang Betz 0:c71c9af137dd 40 #include "mbed.h"
Wolfgang Betz 0:c71c9af137dd 41 #include "x_nucleo_iks01a1.h"
Wolfgang Betz 0:c71c9af137dd 42
mfr16 9:10f36a418bc0 43 #include "Servo.h"
mfr16 9:10f36a418bc0 44
mfr16 9:10f36a418bc0 45 Servo myservo(D3);
mfr16 9:10f36a418bc0 46
Wolfgang Betz 0:c71c9af137dd 47 /* Instantiate the expansion board */
Wolfgang Betz 4:b1526d074d83 48 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
Wolfgang Betz 0:c71c9af137dd 49
Wolfgang Betz 0:c71c9af137dd 50 /* Retrieve the composing elements of the expansion board */
Wolfgang Betz 0:c71c9af137dd 51 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
Wolfgang Betz 0:c71c9af137dd 52 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
Wolfgang Betz 0:c71c9af137dd 53 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
Wolfgang Betz 0:c71c9af137dd 54 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 55 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 56 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 57 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 58
Wolfgang Betz 0:c71c9af137dd 59 /* Helper function for printing floats & doubles */
Wolfgang Betz 0:c71c9af137dd 60 static char *printDouble(char* str, double v, int decimalDigits=2)
Wolfgang Betz 0:c71c9af137dd 61 {
Wolfgang Betz 0:c71c9af137dd 62 int i = 1;
Wolfgang Betz 0:c71c9af137dd 63 int intPart, fractPart;
Wolfgang Betz 0:c71c9af137dd 64 int len;
Wolfgang Betz 0:c71c9af137dd 65 char *ptr;
Wolfgang Betz 0:c71c9af137dd 66
Wolfgang Betz 0:c71c9af137dd 67 /* prepare decimal digits multiplicator */
Wolfgang Betz 0:c71c9af137dd 68 for (;decimalDigits!=0; i*=10, decimalDigits--);
Wolfgang Betz 0:c71c9af137dd 69
Wolfgang Betz 0:c71c9af137dd 70 /* calculate integer & fractinal parts */
Wolfgang Betz 0:c71c9af137dd 71 intPart = (int)v;
Wolfgang Betz 0:c71c9af137dd 72 fractPart = (int)((v-(double)(int)v)*i);
Wolfgang Betz 0:c71c9af137dd 73
Wolfgang Betz 0:c71c9af137dd 74 /* fill in integer part */
Wolfgang Betz 0:c71c9af137dd 75 sprintf(str, "%i.", intPart);
Wolfgang Betz 0:c71c9af137dd 76
Wolfgang Betz 0:c71c9af137dd 77 /* prepare fill in of fractional part */
Wolfgang Betz 0:c71c9af137dd 78 len = strlen(str);
Wolfgang Betz 0:c71c9af137dd 79 ptr = &str[len];
Wolfgang Betz 0:c71c9af137dd 80
Wolfgang Betz 0:c71c9af137dd 81 /* fill in leading fractional zeros */
Wolfgang Betz 0:c71c9af137dd 82 for (i/=10;i>1; i/=10, ptr++) {
Wolfgang Betz 0:c71c9af137dd 83 if(fractPart >= i) break;
Wolfgang Betz 0:c71c9af137dd 84 *ptr = '0';
Wolfgang Betz 0:c71c9af137dd 85 }
Wolfgang Betz 0:c71c9af137dd 86
Wolfgang Betz 0:c71c9af137dd 87 /* fill in (rest of) fractional part */
Wolfgang Betz 0:c71c9af137dd 88 sprintf(ptr, "%i", fractPart);
Wolfgang Betz 0:c71c9af137dd 89
Wolfgang Betz 0:c71c9af137dd 90 return str;
Wolfgang Betz 0:c71c9af137dd 91 }
Wolfgang Betz 0:c71c9af137dd 92
Wolfgang Betz 0:c71c9af137dd 93
Wolfgang Betz 0:c71c9af137dd 94 /* Simple main function */
Wolfgang Betz 0:c71c9af137dd 95 int main() {
Wolfgang Betz 0:c71c9af137dd 96 uint8_t id;
Wolfgang Betz 0:c71c9af137dd 97 float value1, value2;
Wolfgang Betz 0:c71c9af137dd 98 char buffer1[32], buffer2[32];
Wolfgang Betz 0:c71c9af137dd 99 int32_t axes[3];
Wolfgang Betz 0:c71c9af137dd 100
Wolfgang Betz 0:c71c9af137dd 101 printf("\r\n--- Starting new run ---\r\n");
Wolfgang Betz 0:c71c9af137dd 102
Wolfgang Betz 0:c71c9af137dd 103 humidity_sensor->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 104 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 105 pressure_sensor->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 106 printf("LPS25H pressure & temperature = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 107 magnetometer->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 108 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 109 gyroscope->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 110 printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 111
Wolfgang Betz 0:c71c9af137dd 112 wait(3);
Wolfgang Betz 0:c71c9af137dd 113
Wolfgang Betz 0:c71c9af137dd 114 while(1) {
Wolfgang Betz 0:c71c9af137dd 115 printf("\r\n");
Wolfgang Betz 0:c71c9af137dd 116
Wolfgang Betz 0:c71c9af137dd 117 temp_sensor1->GetTemperature(&value1);
Wolfgang Betz 0:c71c9af137dd 118 humidity_sensor->GetHumidity(&value2);
Wolfgang Betz 0:c71c9af137dd 119 printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 120
Wolfgang Betz 0:c71c9af137dd 121 temp_sensor2->GetFahrenheit(&value1);
Wolfgang Betz 0:c71c9af137dd 122 pressure_sensor->GetPressure(&value2);
Wolfgang Betz 0:c71c9af137dd 123 printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 124
Wolfgang Betz 0:c71c9af137dd 125 printf("---\r\n");
Wolfgang Betz 0:c71c9af137dd 126
Wolfgang Betz 0:c71c9af137dd 127 magnetometer->Get_M_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 128 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 129
Wolfgang Betz 0:c71c9af137dd 130 accelerometer->Get_X_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 131 printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 132
mfr16 9:10f36a418bc0 133 myservo = (1000.0-axes[2])/1000.0;
mfr16 9:10f36a418bc0 134
Wolfgang Betz 0:c71c9af137dd 135 gyroscope->Get_G_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 136 printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 137
mfr16 9:10f36a418bc0 138 wait(0.2);
Wolfgang Betz 0:c71c9af137dd 139 }
Wolfgang Betz 0:c71c9af137dd 140 }