Stage-1 Students SoCEM / Mbed OS ReferredCoursework2016

Dependencies:   X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-August-2015
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 
00038 
00039  HINTS:
00040  
00041  Use a Ticker for accurate sampling, but do NOT use printf or locks inside an ISR. Instead, use a MailBox to safely move data across from an ISR to a Thread
00042  Many functions in MBED are thread-safe - check the online docs
00043  
00044  For buffering, use an Array (of structures) and the producer-consumer pattern (or a variant of it).
00045  DO NOT use a mailbox or queue to perform the buffering
00046  
00047  Perform serial comms on another thread
00048  
00049  Beware of a thread running out of stack space. If you have to use a lot of local variable data, consider increasing the size of the stack for the respective thread. See the constructor for Thread in the docs
00050  
00051  In terms of diagnostics, consider the following type of information:
00052  
00053  An indication that the sampling is running (not every sample maybe, but a heart-beat type indication)
00054  An error if the buffer is full
00055  An warning if the buffer is empty
00056  Anything that helps diagnose a deadlock (e.g. output a message / toggle an LED before a lock is taken and after it is released)
00057  
00058  For high marks in the logging aspect, remember that although printf is thread safe (not interrupt safe), printf from multiple threads will result in interleaved text.
00059  To solve this, have a logging thread that queues up whole messages and write them to the serial interface one at a time - this is ambitious but can be done
00060 */ 
00061 
00062 /* Includes */
00063 #include "mbed.h"
00064 //#include "rtos.h"
00065 #include "x_nucleo_iks01a1.h"
00066 
00067 /* Instantiate the expansion board */
00068 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00069 
00070 /* Retrieve the composing elements of the expansion board */
00071 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00072 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00073 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00074 
00075 extern char *printDouble(char* str, double v, int decimalDigits=2);
00076 
00077 typedef struct  {
00078     int32_t x;
00079     int32_t y;
00080     int32_t z;
00081 } AccelData;
00082 
00083 /* Simple main function */
00084 int main() {
00085   uint8_t id;
00086   int32_t axes[3];
00087   
00088   printf("\r\n--- Starting new run ---\r\n");
00089   
00090   accelerometer->ReadID(&id);
00091   printf("LSM6DS0 Accelerometer             = 0x%X\r\n", id);
00092   magnetometer->ReadID(&id);
00093   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00094   gyroscope->ReadID(&id);
00095   printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
00096   
00097   wait(3);
00098  
00099   while(1) {
00100     printf("\r\n");
00101 
00102     magnetometer->Get_M_Axes(axes);
00103     printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00104 
00105     accelerometer->Get_X_Axes(axes);
00106     
00107     AccelData d;
00108     d.x = axes[0];
00109     d.y = axes[1];
00110     d.z = axes[2];
00111     
00112     printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", d.x, d.y, d.z);
00113     
00114     gyroscope->Get_G_Axes(axes);
00115     printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00116 
00117     //This is not a good way to do accurate sampling - I reccomend a Ticker
00118     wait(1.5);
00119   }
00120 }