tiz

Dependencies:   TextLCD X_NUCLEO_IKS01A1 func mbed-src mbed

Fork of mas by esproj

Committer:
herenvarno
Date:
Thu Dec 10 18:51:28 2015 +0000
Revision:
1:1df80fe13928
Parent:
0:99767aa66bce
Child:
2:2ca3d4f57b88
Child:
3:4355890e55b4
add func

Who changed what in which revision?

UserRevisionLine numberNew contents of line
herenvarno 0:99767aa66bce 1 /**
herenvarno 0:99767aa66bce 2 ******************************************************************************
herenvarno 0:99767aa66bce 3 * @file main.cpp
herenvarno 0:99767aa66bce 4 * @author AST / EST
herenvarno 0:99767aa66bce 5 * @version V0.0.1
herenvarno 0:99767aa66bce 6 * @date 14-August-2015
herenvarno 0:99767aa66bce 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A1
herenvarno 0:99767aa66bce 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
herenvarno 0:99767aa66bce 9 ******************************************************************************
herenvarno 0:99767aa66bce 10 * @attention
herenvarno 0:99767aa66bce 11 *
herenvarno 0:99767aa66bce 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
herenvarno 0:99767aa66bce 13 *
herenvarno 0:99767aa66bce 14 * Redistribution and use in source and binary forms, with or without modification,
herenvarno 0:99767aa66bce 15 * are permitted provided that the following conditions are met:
herenvarno 0:99767aa66bce 16 * 1. Redistributions of source code must retain the above copyright notice,
herenvarno 0:99767aa66bce 17 * this list of conditions and the following disclaimer.
herenvarno 0:99767aa66bce 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
herenvarno 0:99767aa66bce 19 * this list of conditions and the following disclaimer in the documentation
herenvarno 0:99767aa66bce 20 * and/or other materials provided with the distribution.
herenvarno 0:99767aa66bce 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
herenvarno 0:99767aa66bce 22 * may be used to endorse or promote products derived from this software
herenvarno 0:99767aa66bce 23 * without specific prior written permission.
herenvarno 0:99767aa66bce 24 *
herenvarno 0:99767aa66bce 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
herenvarno 0:99767aa66bce 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
herenvarno 0:99767aa66bce 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
herenvarno 0:99767aa66bce 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
herenvarno 0:99767aa66bce 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
herenvarno 0:99767aa66bce 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
herenvarno 0:99767aa66bce 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
herenvarno 0:99767aa66bce 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
herenvarno 0:99767aa66bce 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
herenvarno 0:99767aa66bce 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
herenvarno 0:99767aa66bce 35 *
herenvarno 0:99767aa66bce 36 ******************************************************************************
herenvarno 0:99767aa66bce 37 */
herenvarno 0:99767aa66bce 38
herenvarno 0:99767aa66bce 39 /* Includes */
herenvarno 0:99767aa66bce 40 #include "mbed.h"
herenvarno 0:99767aa66bce 41 #include "x_nucleo_iks01a1.h"
herenvarno 1:1df80fe13928 42 #include "func/Error.h"
herenvarno 1:1df80fe13928 43 #include "func/Config.h"
herenvarno 0:99767aa66bce 44
herenvarno 0:99767aa66bce 45 /* Instantiate the expansion board */
herenvarno 0:99767aa66bce 46 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
herenvarno 0:99767aa66bce 47
herenvarno 0:99767aa66bce 48 /* Retrieve the composing elements of the expansion board */
herenvarno 0:99767aa66bce 49 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
herenvarno 0:99767aa66bce 50 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
herenvarno 0:99767aa66bce 51 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
herenvarno 0:99767aa66bce 52 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
herenvarno 0:99767aa66bce 53 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
herenvarno 0:99767aa66bce 54 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
herenvarno 0:99767aa66bce 55 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
herenvarno 0:99767aa66bce 56
herenvarno 0:99767aa66bce 57 /* Helper function for printing floats & doubles */
herenvarno 0:99767aa66bce 58 static char *printDouble(char* str, double v, int decimalDigits=2)
herenvarno 0:99767aa66bce 59 {
herenvarno 0:99767aa66bce 60 int i = 1;
herenvarno 0:99767aa66bce 61 int intPart, fractPart;
herenvarno 0:99767aa66bce 62 int len;
herenvarno 0:99767aa66bce 63 char *ptr;
herenvarno 0:99767aa66bce 64
herenvarno 0:99767aa66bce 65 /* prepare decimal digits multiplicator */
herenvarno 0:99767aa66bce 66 for (;decimalDigits!=0; i*=10, decimalDigits--);
herenvarno 0:99767aa66bce 67
herenvarno 0:99767aa66bce 68 /* calculate integer & fractinal parts */
herenvarno 0:99767aa66bce 69 intPart = (int)v;
herenvarno 0:99767aa66bce 70 fractPart = (int)((v-(double)(int)v)*i);
herenvarno 0:99767aa66bce 71
herenvarno 0:99767aa66bce 72 /* fill in integer part */
herenvarno 0:99767aa66bce 73 sprintf(str, "%i.", intPart);
herenvarno 0:99767aa66bce 74
herenvarno 0:99767aa66bce 75 /* prepare fill in of fractional part */
herenvarno 0:99767aa66bce 76 len = strlen(str);
herenvarno 0:99767aa66bce 77 ptr = &str[len];
herenvarno 0:99767aa66bce 78
herenvarno 0:99767aa66bce 79 /* fill in leading fractional zeros */
herenvarno 0:99767aa66bce 80 for (i/=10;i>1; i/=10, ptr++) {
herenvarno 0:99767aa66bce 81 if(fractPart >= i) break;
herenvarno 0:99767aa66bce 82 *ptr = '0';
herenvarno 0:99767aa66bce 83 }
herenvarno 0:99767aa66bce 84
herenvarno 0:99767aa66bce 85 /* fill in (rest of) fractional part */
herenvarno 0:99767aa66bce 86 sprintf(ptr, "%i", fractPart);
herenvarno 0:99767aa66bce 87
herenvarno 0:99767aa66bce 88 return str;
herenvarno 0:99767aa66bce 89 }
herenvarno 0:99767aa66bce 90
herenvarno 0:99767aa66bce 91 DigitalOut myled(LED1);
herenvarno 0:99767aa66bce 92 DigitalIn mybutton(USER_BUTTON);
herenvarno 0:99767aa66bce 93
herenvarno 1:1df80fe13928 94 //LocalFileSystem local("local");
herenvarno 1:1df80fe13928 95
herenvarno 0:99767aa66bce 96 /* Simple main function */
herenvarno 0:99767aa66bce 97 int main() {
herenvarno 0:99767aa66bce 98 myled=0;
herenvarno 0:99767aa66bce 99 uint8_t func=0;
herenvarno 0:99767aa66bce 100 uint8_t button_mem=0;
herenvarno 0:99767aa66bce 101
herenvarno 0:99767aa66bce 102 uint8_t id;
herenvarno 0:99767aa66bce 103 float value1, value2;
herenvarno 0:99767aa66bce 104 char buffer1[32], buffer2[32];
herenvarno 0:99767aa66bce 105 int32_t axes[3];
herenvarno 0:99767aa66bce 106 int16_t axxx[3];
herenvarno 0:99767aa66bce 107
herenvarno 0:99767aa66bce 108 // set time
herenvarno 0:99767aa66bce 109 set_time(1387188323);
herenvarno 0:99767aa66bce 110
herenvarno 0:99767aa66bce 111
herenvarno 1:1df80fe13928 112 //raise_error(ERROR_CONFIG);
herenvarno 0:99767aa66bce 113
herenvarno 1:1df80fe13928 114 //Config cfg = Config("/local/mas.cfg");
herenvarno 1:1df80fe13928 115 //cfg.write();
herenvarno 0:99767aa66bce 116
herenvarno 0:99767aa66bce 117
herenvarno 0:99767aa66bce 118 printf("\r\n--- Starting new run ---\r\n");
herenvarno 0:99767aa66bce 119
herenvarno 0:99767aa66bce 120 humidity_sensor->ReadID(&id);
herenvarno 0:99767aa66bce 121 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
herenvarno 0:99767aa66bce 122 pressure_sensor->ReadID(&id);
herenvarno 0:99767aa66bce 123 printf("LPS25H pressure & temperature = 0x%X\r\n", id);
herenvarno 0:99767aa66bce 124 magnetometer->ReadID(&id);
herenvarno 0:99767aa66bce 125 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
herenvarno 0:99767aa66bce 126 gyroscope->ReadID(&id);
herenvarno 0:99767aa66bce 127 printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
herenvarno 0:99767aa66bce 128
herenvarno 0:99767aa66bce 129 wait(3);
herenvarno 1:1df80fe13928 130
herenvarno 1:1df80fe13928 131 printf("START CALIBRATION\n");
herenvarno 1:1df80fe13928 132 int32_t m_max[3]={0};
herenvarno 1:1df80fe13928 133 int32_t m_min[3]={0};
herenvarno 1:1df80fe13928 134 for (int i=0; i<1000; i++)
herenvarno 1:1df80fe13928 135 {
herenvarno 1:1df80fe13928 136
herenvarno 1:1df80fe13928 137 magnetometer->Get_M_Axes(axes);
herenvarno 1:1df80fe13928 138 if(i==0)
herenvarno 1:1df80fe13928 139 {
herenvarno 1:1df80fe13928 140 for(int j=0; j<3; j++)
herenvarno 1:1df80fe13928 141 {
herenvarno 1:1df80fe13928 142 m_max[j]=axes[j];
herenvarno 1:1df80fe13928 143 m_min[j]=axes[j];
herenvarno 1:1df80fe13928 144 }
herenvarno 1:1df80fe13928 145 }
herenvarno 1:1df80fe13928 146 else
herenvarno 1:1df80fe13928 147 {
herenvarno 1:1df80fe13928 148 for(int j=0; j<3; j++)
herenvarno 1:1df80fe13928 149 {
herenvarno 1:1df80fe13928 150 if(axes[j]>m_max[j]) m_max[j]=axes[j];
herenvarno 1:1df80fe13928 151 if(axes[j]<m_min[j]) m_min[j]=axes[j];
herenvarno 1:1df80fe13928 152 }
herenvarno 1:1df80fe13928 153 }
herenvarno 1:1df80fe13928 154 wait(0.05);
herenvarno 1:1df80fe13928 155 }
herenvarno 1:1df80fe13928 156
herenvarno 1:1df80fe13928 157 printf("MAX : %d, %d, %d\n", m_max[0], m_max[1], m_max[2]);
herenvarno 1:1df80fe13928 158 printf("MIN : %d, %d, %d\n", m_min[0], m_min[1], m_min[2]);
herenvarno 1:1df80fe13928 159
herenvarno 0:99767aa66bce 160
herenvarno 0:99767aa66bce 161 while(1) {
herenvarno 0:99767aa66bce 162 if((mybutton!=0) && (button_mem==0))
herenvarno 0:99767aa66bce 163 {
herenvarno 0:99767aa66bce 164 func++;
herenvarno 0:99767aa66bce 165 if(func>=6)
herenvarno 0:99767aa66bce 166 func=0;
herenvarno 0:99767aa66bce 167 }
herenvarno 0:99767aa66bce 168 button_mem=mybutton;
herenvarno 0:99767aa66bce 169 printf("func: %d\r\n", func);
herenvarno 0:99767aa66bce 170 printf("\r\n");
herenvarno 0:99767aa66bce 171
herenvarno 0:99767aa66bce 172 if(func==0)
herenvarno 0:99767aa66bce 173 {
herenvarno 0:99767aa66bce 174 time_t seconds = time(NULL);
herenvarno 0:99767aa66bce 175 printf("Time as a basic string = %s\r\n", ctime(&seconds));
herenvarno 0:99767aa66bce 176 }
herenvarno 0:99767aa66bce 177 else if(func==1)
herenvarno 0:99767aa66bce 178 {
herenvarno 0:99767aa66bce 179 temp_sensor1->GetTemperature(&value1);
herenvarno 0:99767aa66bce 180 humidity_sensor->GetHumidity(&value2);
herenvarno 0:99767aa66bce 181
herenvarno 0:99767aa66bce 182 if(value2 > 70)
herenvarno 0:99767aa66bce 183 myled=1;
herenvarno 0:99767aa66bce 184 else
herenvarno 0:99767aa66bce 185 myled=0;
herenvarno 0:99767aa66bce 186 printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
herenvarno 0:99767aa66bce 187 }
herenvarno 0:99767aa66bce 188 else if(func==2)
herenvarno 0:99767aa66bce 189 {
herenvarno 0:99767aa66bce 190 temp_sensor2->GetFahrenheit(&value1);
herenvarno 0:99767aa66bce 191 pressure_sensor->GetPressure(&value2);
herenvarno 0:99767aa66bce 192 printf("LPS25H: [temp] %7s F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
herenvarno 0:99767aa66bce 193 printf("Altitude: %s m\r\n", printDouble(buffer1, 44300-44300*pow((value2/1013.25), 0.00019)));
herenvarno 0:99767aa66bce 194 }
herenvarno 0:99767aa66bce 195 else if(func==3)
herenvarno 0:99767aa66bce 196 {
herenvarno 0:99767aa66bce 197 magnetometer->Get_M_Axes(axes);
herenvarno 1:1df80fe13928 198 axxx[0]=axes[0]-((m_max[0]+m_min[0])/2);
herenvarno 1:1df80fe13928 199 axxx[1]=axes[1]-((m_max[1]+m_min[1])/2);
herenvarno 1:1df80fe13928 200 axxx[2]=axes[2]-((m_max[2]+m_min[2])/2);
herenvarno 0:99767aa66bce 201 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axxx[0], axxx[1], axxx[2]);
herenvarno 1:1df80fe13928 202 printf("DIR: %s deg\r\n", printDouble(buffer1, 90-atan(float(axxx[0])/float(axxx[1])/3.1416)));
herenvarno 0:99767aa66bce 203 }
herenvarno 0:99767aa66bce 204 else if(func==4)
herenvarno 0:99767aa66bce 205 {
herenvarno 0:99767aa66bce 206 accelerometer->Get_X_Axes(axes);
herenvarno 0:99767aa66bce 207 printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
herenvarno 0:99767aa66bce 208 }
herenvarno 0:99767aa66bce 209 else if(func==5)
herenvarno 0:99767aa66bce 210 {
herenvarno 0:99767aa66bce 211 gyroscope ->Get_G_Axes(axes);
herenvarno 0:99767aa66bce 212 printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
herenvarno 0:99767aa66bce 213 }
herenvarno 0:99767aa66bce 214 wait(1.5);
herenvarno 0:99767aa66bce 215 }
herenvarno 0:99767aa66bce 216 }