IKS01A1 Demo
Dependencies: X_NUCLEO_IKS01A1
main.cpp@12:504236c45846, 2019-09-11 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 11 13:07:56 2019 +0000
- Revision:
- 12:504236c45846
- Parent:
- 11:c613d631c1e9
Now works with F429
Who changed what in which revision?
| User | Revision | Line number | New 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>© 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" |
| noutram | 12:504236c45846 | 42 | void doSample(); |
| noutram | 12:504236c45846 | 43 | void samplerThread(); |
| noutram | 11:c613d631c1e9 | 44 | |
| Wolfgang Betz |
0:c71c9af137dd | 45 | /* Instantiate the expansion board */ |
| Wolfgang Betz |
4:b1526d074d83 | 46 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
| Wolfgang Betz |
0:c71c9af137dd | 47 | |
| Wolfgang Betz |
0:c71c9af137dd | 48 | /* Retrieve the composing elements of the expansion board */ |
| Wolfgang Betz |
0:c71c9af137dd | 49 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
| Wolfgang Betz |
0:c71c9af137dd | 50 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
| Wolfgang Betz |
0:c71c9af137dd | 51 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
| Wolfgang Betz |
0:c71c9af137dd | 52 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
| Wolfgang Betz |
3:a2d2342526db | 53 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
| Wolfgang Betz |
0:c71c9af137dd | 54 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
| Wolfgang Betz |
3:a2d2342526db | 55 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
| Wolfgang Betz |
0:c71c9af137dd | 56 | |
| noutram | 12:504236c45846 | 57 | Serial pc(USBTX, USBRX); |
| noutram | 12:504236c45846 | 58 | Thread sampler(osPriorityRealtime); |
| noutram | 12:504236c45846 | 59 | EventQueue queue; |
| Wolfgang Betz |
0:c71c9af137dd | 60 | |
| noutram | 11:c613d631c1e9 | 61 | DigitalIn sw1(USER_BUTTON); |
| noutram | 11:c613d631c1e9 | 62 | int32_t m_axes[3]; |
| noutram | 11:c613d631c1e9 | 63 | int32_t a_axes[3]; |
| noutram | 11:c613d631c1e9 | 64 | int32_t g_axes[3]; |
| noutram | 12:504236c45846 | 65 | uint8_t id; |
| noutram | 12:504236c45846 | 66 | |
| noutram | 12:504236c45846 | 67 | const unsigned Fs = 100; |
| noutram | 12:504236c45846 | 68 | const double T = 1.0 / (double)Fs; |
| noutram | 12:504236c45846 | 69 | const unsigned Tms = (unsigned)(T*1000.0); |
| noutram | 12:504236c45846 | 70 | unsigned long tt = 0; |
| noutram | 12:504236c45846 | 71 | |
| noutram | 12:504236c45846 | 72 | void samplerThread() |
| noutram | 12:504236c45846 | 73 | { |
| noutram | 12:504236c45846 | 74 | queue.dispatch(); |
| noutram | 12:504236c45846 | 75 | } |
| noutram | 11:c613d631c1e9 | 76 | void doSample() |
| noutram | 11:c613d631c1e9 | 77 | { |
| noutram | 11:c613d631c1e9 | 78 | magnetometer->get_m_axes(m_axes); |
| noutram | 11:c613d631c1e9 | 79 | accelerometer->get_x_axes(a_axes); |
| noutram | 11:c613d631c1e9 | 80 | gyroscope->get_g_axes(g_axes); |
| noutram | 11:c613d631c1e9 | 81 | |
| noutram | 11:c613d631c1e9 | 82 | pc.printf("%lu,",tt); tt+=Tms; |
| noutram | 11:c613d631c1e9 | 83 | pc.printf("%d,%d,%d,", m_axes[0], m_axes[1], m_axes[2]); |
| noutram | 11:c613d631c1e9 | 84 | pc.printf("%d,%d,%d,", a_axes[0], a_axes[1], a_axes[2]); |
| noutram | 11:c613d631c1e9 | 85 | pc.printf("%d,%d,%d\r\n", g_axes[0], g_axes[1], g_axes[2]); |
| noutram | 12:504236c45846 | 86 | } |
| Wolfgang Betz |
0:c71c9af137dd | 87 | |
| noutram | 12:504236c45846 | 88 | /* Simple main function */ |
| noutram | 12:504236c45846 | 89 | int main() { |
| noutram | 12:504236c45846 | 90 | |
| noutram | 12:504236c45846 | 91 | pc.baud(926100); |
| noutram | 12:504236c45846 | 92 | |
| noutram | 12:504236c45846 | 93 | humidity_sensor->read_id(&id); |
| noutram | 12:504236c45846 | 94 | pressure_sensor->read_id(&id); |
| noutram | 12:504236c45846 | 95 | magnetometer->read_id(&id); |
| noutram | 12:504236c45846 | 96 | gyroscope->read_id(&id); |
| noutram | 12:504236c45846 | 97 | |
| noutram | 12:504236c45846 | 98 | pc.printf("Press the blue button to start\n\rThen press the black reset button to stop\n\r"); |
| noutram | 12:504236c45846 | 99 | |
| noutram | 12:504236c45846 | 100 | while (sw1 == 0); |
| noutram | 12:504236c45846 | 101 | wait_ms(250.0); |
| noutram | 12:504236c45846 | 102 | while (sw1 == 1); |
| noutram | 12:504236c45846 | 103 | wait_ms(250.0); |
| noutram | 12:504236c45846 | 104 | |
| noutram | 12:504236c45846 | 105 | pc.printf("time[ms],x[mag/mgauss],y[mag/mgauss],z[mag/mgauss],x[acc/mg],y[acc/mg],z[acc/mg],x[gyro/mdps],y[gyro/mdps],z[gyro/mdps]\n\r"); |
| noutram | 12:504236c45846 | 106 | |
| noutram | 12:504236c45846 | 107 | sampler.start(samplerThread); |
| noutram | 12:504236c45846 | 108 | queue.call_every(Tms, doSample); |
| noutram | 12:504236c45846 | 109 | sampler.join(); |
| noutram | 12:504236c45846 | 110 | |
| noutram | 12:504236c45846 | 111 | while(true) { |
| noutram | 12:504236c45846 | 112 | pc.printf("Should never get here!\n\r"); |
| noutram | 12:504236c45846 | 113 | wait(1.0); |
| noutram | 11:c613d631c1e9 | 114 | } |
| noutram | 11:c613d631c1e9 | 115 | |
| noutram | 11:c613d631c1e9 | 116 | } |
| Wolfgang Betz |
0:c71c9af137dd | 117 | |
| Wolfgang Betz |
0:c71c9af137dd | 118 |