Code for break using sensor
Dependencies: SEGGER_RTT mbed-dev
Fork of DigitalOut_HelloWorld by
main.cpp@2:8f53e653f9b1, 2017-12-18 (annotated)
- Committer:
- Anythingconnected
- Date:
- Mon Dec 18 10:18:59 2017 +0000
- Revision:
- 2:8f53e653f9b1
- Parent:
- 0:b5a9e0614efd
Getting byte by byte read to work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anythingconnected | 2:8f53e653f9b1 | 1 | #include <mbed.h> |
Anythingconnected | 2:8f53e653f9b1 | 2 | #include "MPU9250.h" |
Anythingconnected | 2:8f53e653f9b1 | 3 | #include "SEGGER_RTT.h" |
Anythingconnected | 2:8f53e653f9b1 | 4 | #include "SEGGER_RTT.c" |
Anythingconnected | 2:8f53e653f9b1 | 5 | #include "SEGGER_RTT_printf.c" |
Anythingconnected | 2:8f53e653f9b1 | 6 | #include "nrf_drv_clock.h" |
Anythingconnected | 2:8f53e653f9b1 | 7 | |
Anythingconnected | 2:8f53e653f9b1 | 8 | #define NRF_CLK_SRC NRF_CLOCK_LFCLKSRC_SYNTH_250_PPM |
Anythingconnected | 2:8f53e653f9b1 | 9 | |
Anythingconnected | 2:8f53e653f9b1 | 10 | //MPU-9250 Settings |
Anythingconnected | 2:8f53e653f9b1 | 11 | #define MPU9250_PIN_SCL 14 //P0.14 |
Anythingconnected | 2:8f53e653f9b1 | 12 | #define MPU9250_PIN_SDA 13 //P0.13 |
Anythingconnected | 2:8f53e653f9b1 | 13 | #define MPU9250_PIN_INT 12 //P0.12 |
Anythingconnected | 2:8f53e653f9b1 | 14 | #define MPU9250_I2C_ADDRESS 0b1101001 //AD0 pin for LSB in address is pulled up to logic high |
Anythingconnected | 2:8f53e653f9b1 | 15 | |
Anythingconnected | 2:8f53e653f9b1 | 16 | //Define digital outputs |
Anythingconnected | 2:8f53e653f9b1 | 17 | DigitalOut LED = p0; |
Anythingconnected | 2:8f53e653f9b1 | 18 | DigitalOut VIBR = p1; |
Anythingconnected | 2:8f53e653f9b1 | 19 | |
Anythingconnected | 2:8f53e653f9b1 | 20 | int acc[4], gyr[3], mag[3], temp, tmp; |
Anythingconnected | 2:8f53e653f9b1 | 21 | |
Anythingconnected | 2:8f53e653f9b1 | 22 | MPU9250 mpu9250Sensor(MPU9250_PIN_SCL,MPU9250_PIN_SDA,MPU9250_I2C_ADDRESS); |
mbed_official | 0:b5a9e0614efd | 23 | |
Anythingconnected | 2:8f53e653f9b1 | 24 | void updateSensorValue() { |
Anythingconnected | 2:8f53e653f9b1 | 25 | MPU9250_ACCEL_DATA accel = mpu9250Sensor.readAccel(); |
Anythingconnected | 2:8f53e653f9b1 | 26 | MPU9250_ACCEL_DATA accelnew = mpu9250Sensor.readAccelNew(); |
Anythingconnected | 2:8f53e653f9b1 | 27 | MPU9250_GYRO_DATA gyro = mpu9250Sensor.readGyro(); |
Anythingconnected | 2:8f53e653f9b1 | 28 | MPU9250_MAG_DATA magn = mpu9250Sensor.readMag(); |
Anythingconnected | 2:8f53e653f9b1 | 29 | MPU9250_TEMP_DATA temp = mpu9250Sensor.readTemp(); |
Anythingconnected | 2:8f53e653f9b1 | 30 | |
Anythingconnected | 2:8f53e653f9b1 | 31 | float accfactor = 1.0; |
Anythingconnected | 2:8f53e653f9b1 | 32 | float gyrfactor = 1.0; |
Anythingconnected | 2:8f53e653f9b1 | 33 | float magfactor = 1.0; |
Anythingconnected | 2:8f53e653f9b1 | 34 | |
Anythingconnected | 2:8f53e653f9b1 | 35 | acc[0] = static_cast<int>(accel.x * accfactor); |
Anythingconnected | 2:8f53e653f9b1 | 36 | acc[1] = static_cast<int>(accel.y * accfactor); |
Anythingconnected | 2:8f53e653f9b1 | 37 | acc[2] = static_cast<int>(accel.z * accfactor); |
Anythingconnected | 2:8f53e653f9b1 | 38 | acc[4] = static_cast<int>(accelnew.x * accfactor); |
Anythingconnected | 2:8f53e653f9b1 | 39 | gyr[0] = static_cast<int>(gyro.x * gyrfactor); |
Anythingconnected | 2:8f53e653f9b1 | 40 | gyr[1] = static_cast<int>(gyro.y * gyrfactor); |
Anythingconnected | 2:8f53e653f9b1 | 41 | gyr[2] = static_cast<int>(gyro.z * gyrfactor); |
Anythingconnected | 2:8f53e653f9b1 | 42 | mag[0] = static_cast<int>(magn.x * magfactor); |
Anythingconnected | 2:8f53e653f9b1 | 43 | mag[1] = static_cast<int>(magn.y * magfactor); |
Anythingconnected | 2:8f53e653f9b1 | 44 | mag[2] = static_cast<int>(magn.z * magfactor); |
Anythingconnected | 2:8f53e653f9b1 | 45 | tmp = temp.temperature; |
Anythingconnected | 2:8f53e653f9b1 | 46 | } |
Anythingconnected | 2:8f53e653f9b1 | 47 | |
Anythingconnected | 2:8f53e653f9b1 | 48 | //Runs once on boot (called from main method) |
Anythingconnected | 2:8f53e653f9b1 | 49 | void setup() { |
Anythingconnected | 2:8f53e653f9b1 | 50 | NRF_CLOCK->XTALFREQ = 0xFF; //Reset to default (use external 16MHz crystal) |
Anythingconnected | 2:8f53e653f9b1 | 51 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos); |
Anythingconnected | 2:8f53e653f9b1 | 52 | //NRF_CLOCK->LFCLKSRC = 0; // (NRF_CLOCK_LFCLKSRC_SYNTH_250_PPM) synthesize 32KHz clock from main frequency; |
Anythingconnected | 2:8f53e653f9b1 | 53 | |
Anythingconnected | 2:8f53e653f9b1 | 54 | NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; |
Anythingconnected | 2:8f53e653f9b1 | 55 | NRF_CLOCK->TASKS_HFCLKSTART = 1; |
Anythingconnected | 2:8f53e653f9b1 | 56 | |
Anythingconnected | 2:8f53e653f9b1 | 57 | while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {} |
Anythingconnected | 2:8f53e653f9b1 | 58 | |
Anythingconnected | 2:8f53e653f9b1 | 59 | mpu9250Sensor.init(); |
Anythingconnected | 2:8f53e653f9b1 | 60 | } |
mbed_official | 0:b5a9e0614efd | 61 | |
mbed_official | 0:b5a9e0614efd | 62 | int main() { |
Anythingconnected | 2:8f53e653f9b1 | 63 | setup(); |
Anythingconnected | 2:8f53e653f9b1 | 64 | SEGGER_RTT_WriteString(0, "Start output \n"); |
Anythingconnected | 2:8f53e653f9b1 | 65 | |
Anythingconnected | 2:8f53e653f9b1 | 66 | while (true) { |
Anythingconnected | 2:8f53e653f9b1 | 67 | updateSensorValue(); |
Anythingconnected | 2:8f53e653f9b1 | 68 | SEGGER_RTT_printf(0, "Acc X:%d, Y:%d, Z:%d\n", acc[0],acc[1],acc[2]); |
Anythingconnected | 2:8f53e653f9b1 | 69 | SEGGER_RTT_printf(0, "AccNew X:%d\n", acc[4]); |
Anythingconnected | 2:8f53e653f9b1 | 70 | SEGGER_RTT_printf(0, "Gyr X:%d, Y:%d, Z:%d\n", gyr[0],gyr[1],gyr[2]); |
Anythingconnected | 2:8f53e653f9b1 | 71 | SEGGER_RTT_printf(0, "Mag X:%d, Y:%d, Z:%d\n", mag[0],mag[1],mag[2]); |
Anythingconnected | 2:8f53e653f9b1 | 72 | SEGGER_RTT_printf(0, "Temp:%d\n", temp); |
Anythingconnected | 2:8f53e653f9b1 | 73 | SEGGER_RTT_WriteString(0, "\n"); |
Anythingconnected | 2:8f53e653f9b1 | 74 | wait(0.5); |
Anythingconnected | 2:8f53e653f9b1 | 75 | /*int tapThresh = 120; |
Anythingconnected | 2:8f53e653f9b1 | 76 | if (acc[0] > tapThresh){ |
Anythingconnected | 2:8f53e653f9b1 | 77 | LED = 1; |
Anythingconnected | 2:8f53e653f9b1 | 78 | VIBR = 1; |
Anythingconnected | 2:8f53e653f9b1 | 79 | wait(0.3); |
Anythingconnected | 2:8f53e653f9b1 | 80 | LED = !LED; |
Anythingconnected | 2:8f53e653f9b1 | 81 | VIBR = !VIBR; |
Anythingconnected | 2:8f53e653f9b1 | 82 | wait(2); |
Anythingconnected | 2:8f53e653f9b1 | 83 | }*/ |
mbed_official | 0:b5a9e0614efd | 84 | } |
Anythingconnected | 2:8f53e653f9b1 | 85 | } |