Version of the IKS01A1 library for this coursework
Dependencies: X_NUCLEO_COMMON
Fork of X_NUCLEO_IKS01A1 by
main.cpp@89:c55aa4c1187f, 2017-08-01 (annotated)
- Committer:
- noutram
- Date:
- Tue Aug 01 23:46:04 2017 +0000
- Revision:
- 89:c55aa4c1187f
updated for 2017
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 89:c55aa4c1187f | 1 | /** |
| noutram | 89:c55aa4c1187f | 2 | ****************************************************************************** |
| noutram | 89:c55aa4c1187f | 3 | * @file main.cpp |
| noutram | 89:c55aa4c1187f | 4 | * @author AST / EST |
| noutram | 89:c55aa4c1187f | 5 | * @version V0.0.1 |
| noutram | 89:c55aa4c1187f | 6 | * @date 14-August-2015 |
| noutram | 89:c55aa4c1187f | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
| noutram | 89:c55aa4c1187f | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
| noutram | 89:c55aa4c1187f | 9 | ****************************************************************************** |
| noutram | 89:c55aa4c1187f | 10 | * @attention |
| noutram | 89:c55aa4c1187f | 11 | * |
| noutram | 89:c55aa4c1187f | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
| noutram | 89:c55aa4c1187f | 13 | * |
| noutram | 89:c55aa4c1187f | 14 | * Redistribution and use in source and binary forms, with or without modification, |
| noutram | 89:c55aa4c1187f | 15 | * are permitted provided that the following conditions are met: |
| noutram | 89:c55aa4c1187f | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
| noutram | 89:c55aa4c1187f | 17 | * this list of conditions and the following disclaimer. |
| noutram | 89:c55aa4c1187f | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| noutram | 89:c55aa4c1187f | 19 | * this list of conditions and the following disclaimer in the documentation |
| noutram | 89:c55aa4c1187f | 20 | * and/or other materials provided with the distribution. |
| noutram | 89:c55aa4c1187f | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| noutram | 89:c55aa4c1187f | 22 | * may be used to endorse or promote products derived from this software |
| noutram | 89:c55aa4c1187f | 23 | * without specific prior written permission. |
| noutram | 89:c55aa4c1187f | 24 | * |
| noutram | 89:c55aa4c1187f | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| noutram | 89:c55aa4c1187f | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| noutram | 89:c55aa4c1187f | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| noutram | 89:c55aa4c1187f | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| noutram | 89:c55aa4c1187f | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| noutram | 89:c55aa4c1187f | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| noutram | 89:c55aa4c1187f | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| noutram | 89:c55aa4c1187f | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| noutram | 89:c55aa4c1187f | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| noutram | 89:c55aa4c1187f | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| noutram | 89:c55aa4c1187f | 35 | * |
| noutram | 89:c55aa4c1187f | 36 | ****************************************************************************** |
| noutram | 89:c55aa4c1187f | 37 | |
| noutram | 89:c55aa4c1187f | 38 | |
| noutram | 89:c55aa4c1187f | 39 | HINTS: |
| noutram | 89:c55aa4c1187f | 40 | |
| noutram | 89:c55aa4c1187f | 41 | 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 |
| noutram | 89:c55aa4c1187f | 42 | Many functions in MBED are thread-safe - check the online docs |
| noutram | 89:c55aa4c1187f | 43 | |
| noutram | 89:c55aa4c1187f | 44 | For buffering, use an Array (of structures) and the producer-consumer pattern (or a variant of it). |
| noutram | 89:c55aa4c1187f | 45 | DO NOT use a mailbox or queue to perform the buffering |
| noutram | 89:c55aa4c1187f | 46 | |
| noutram | 89:c55aa4c1187f | 47 | Perform serial comms on another thread |
| noutram | 89:c55aa4c1187f | 48 | |
| noutram | 89:c55aa4c1187f | 49 | 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 |
| noutram | 89:c55aa4c1187f | 50 | |
| noutram | 89:c55aa4c1187f | 51 | In terms of diagnostics, consider the following type of information: |
| noutram | 89:c55aa4c1187f | 52 | |
| noutram | 89:c55aa4c1187f | 53 | An indication that the sampling is running (not every sample maybe, but a heart-beat type indication) |
| noutram | 89:c55aa4c1187f | 54 | An error if the buffer is full |
| noutram | 89:c55aa4c1187f | 55 | An warning if the buffer is empty |
| noutram | 89:c55aa4c1187f | 56 | Anything that helps diagnose a deadlock (e.g. output a message / toggle an LED before a lock is taken and after it is released) |
| noutram | 89:c55aa4c1187f | 57 | |
| noutram | 89:c55aa4c1187f | 58 | 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. |
| noutram | 89:c55aa4c1187f | 59 | 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 |
| noutram | 89:c55aa4c1187f | 60 | */ |
| noutram | 89:c55aa4c1187f | 61 | |
| noutram | 89:c55aa4c1187f | 62 | /* Includes */ |
| noutram | 89:c55aa4c1187f | 63 | #include "mbed.h" |
| noutram | 89:c55aa4c1187f | 64 | //#include "rtos.h" |
| noutram | 89:c55aa4c1187f | 65 | #include "x_nucleo_iks01a1.h" |
| noutram | 89:c55aa4c1187f | 66 | |
| noutram | 89:c55aa4c1187f | 67 | /* Instantiate the expansion board */ |
| noutram | 89:c55aa4c1187f | 68 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
| noutram | 89:c55aa4c1187f | 69 | |
| noutram | 89:c55aa4c1187f | 70 | /* Retrieve the composing elements of the expansion board */ |
| noutram | 89:c55aa4c1187f | 71 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
| noutram | 89:c55aa4c1187f | 72 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
| noutram | 89:c55aa4c1187f | 73 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
| noutram | 89:c55aa4c1187f | 74 | |
| noutram | 89:c55aa4c1187f | 75 | extern char *printDouble(char* str, double v, int decimalDigits=2); |
| noutram | 89:c55aa4c1187f | 76 | |
| noutram | 89:c55aa4c1187f | 77 | typedef struct { |
| noutram | 89:c55aa4c1187f | 78 | int32_t x; |
| noutram | 89:c55aa4c1187f | 79 | int32_t y; |
| noutram | 89:c55aa4c1187f | 80 | int32_t z; |
| noutram | 89:c55aa4c1187f | 81 | } AccelData; |
| noutram | 89:c55aa4c1187f | 82 | |
| noutram | 89:c55aa4c1187f | 83 | /* Simple main function */ |
| noutram | 89:c55aa4c1187f | 84 | int main() { |
| noutram | 89:c55aa4c1187f | 85 | uint8_t id; |
| noutram | 89:c55aa4c1187f | 86 | int32_t axes[3]; |
| noutram | 89:c55aa4c1187f | 87 | |
| noutram | 89:c55aa4c1187f | 88 | printf("\r\n--- Starting new run ---\r\n"); |
| noutram | 89:c55aa4c1187f | 89 | |
| noutram | 89:c55aa4c1187f | 90 | accelerometer->ReadID(&id); |
| noutram | 89:c55aa4c1187f | 91 | printf("LSM6DS0 Accelerometer = 0x%X\r\n", id); |
| noutram | 89:c55aa4c1187f | 92 | magnetometer->ReadID(&id); |
| noutram | 89:c55aa4c1187f | 93 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
| noutram | 89:c55aa4c1187f | 94 | gyroscope->ReadID(&id); |
| noutram | 89:c55aa4c1187f | 95 | printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
| noutram | 89:c55aa4c1187f | 96 | |
| noutram | 89:c55aa4c1187f | 97 | wait(3); |
| noutram | 89:c55aa4c1187f | 98 | |
| noutram | 89:c55aa4c1187f | 99 | while(1) { |
| noutram | 89:c55aa4c1187f | 100 | printf("\r\n"); |
| noutram | 89:c55aa4c1187f | 101 | |
| noutram | 89:c55aa4c1187f | 102 | magnetometer->Get_M_Axes(axes); |
| noutram | 89:c55aa4c1187f | 103 | printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
| noutram | 89:c55aa4c1187f | 104 | |
| noutram | 89:c55aa4c1187f | 105 | accelerometer->Get_X_Axes(axes); |
| noutram | 89:c55aa4c1187f | 106 | |
| noutram | 89:c55aa4c1187f | 107 | AccelData d; |
| noutram | 89:c55aa4c1187f | 108 | d.x = axes[0]; |
| noutram | 89:c55aa4c1187f | 109 | d.y = axes[1]; |
| noutram | 89:c55aa4c1187f | 110 | d.z = axes[2]; |
| noutram | 89:c55aa4c1187f | 111 | |
| noutram | 89:c55aa4c1187f | 112 | printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", d.x, d.y, d.z); |
| noutram | 89:c55aa4c1187f | 113 | |
| noutram | 89:c55aa4c1187f | 114 | gyroscope->Get_G_Axes(axes); |
| noutram | 89:c55aa4c1187f | 115 | printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
| noutram | 89:c55aa4c1187f | 116 | |
| noutram | 89:c55aa4c1187f | 117 | //This is not a good way to do accurate sampling - I reccomend a Ticker |
| noutram | 89:c55aa4c1187f | 118 | wait(1.5); |
| noutram | 89:c55aa4c1187f | 119 | } |
| noutram | 89:c55aa4c1187f | 120 | } |
