Michael Ernst Peter / Mbed OS Test_GPS

Dependencies:   Eigen

Committer:
pmic
Date:
Fri Jun 03 18:20:08 2022 +0200
Revision:
57:6c87f4989616
Parent:
55:fbd59767e175
Child:
58:f1b47b30f7c4
Introduced seperate timer for logging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 36:8c75783c1eca 1 #include <mbed.h>
pmic 36:8c75783c1eca 2
pmic 41:1627f3e4640d 3 // GNSS and Compass test programm for Mateksys GNSS&Compass M9N-5883
pmic 40:1581715116a8 4
pmic 43:f459f6efaf5c 5 // #include "Eigen/Dense.h"
pmic 43:f459f6efaf5c 6 #include "QMC5883L.h"
pmic 43:f459f6efaf5c 7 #include "LinearCharacteristics.h"
pmic 49:76fcaffb92ef 8 #include "NEOM9N.h"
pmic 6:e1fa1a2d7483 9
pmic 24:86f1a63e35a0 10 // logical variable main task
pmic 45:5d65b27c9293 11 bool do_execute_main_task = true; // this variable will be toggled via the user button (blue button) to or not to execute the main task
pmic 17:c19b471f05cb 12
pmic 24:86f1a63e35a0 13 // user button on nucleo board
pmic 24:86f1a63e35a0 14 Timer user_button_timer; // create Timer object which we use to check if user button was pressed for a certain time (robust against signal bouncing)
pmic 24:86f1a63e35a0 15 InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR)
pmic 24:86f1a63e35a0 16 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 17 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 18
pmic 1:93d997d6b232 19 int main()
pmic 23:26b3a25fc637 20 {
pmic 36:8c75783c1eca 21 // while loop gets executed every main_task_period_ms milliseconds
pmic 55:fbd59767e175 22 const int main_task_period_ms = 40; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second
pmic 53:50ecf1240eba 23 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
pmic 57:6c87f4989616 24 main_task_timer.start();
pmic 57:6c87f4989616 25
pmic 57:6c87f4989616 26 Timer run_timer;
pmic 57:6c87f4989616 27 run_timer.start();
pmic 36:8c75783c1eca 28
pmic 36:8c75783c1eca 29 // led on nucleo board
pmic 36:8c75783c1eca 30 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 36:8c75783c1eca 31
pmic 43:f459f6efaf5c 32 // create QMC5883L compass object
pmic 47:c74d09a252d4 33 I2C i2c(PB_9, PB_8); // I2C1
pmic 43:f459f6efaf5c 34 QMC5883L mag(i2c);
pmic 43:f459f6efaf5c 35 LinearCharacteristics raw_mx2mx, raw_my2my, raw_mz2mz;
pmic 43:f459f6efaf5c 36 raw_mx2mx.setup(0.9991f, 0.0088f);
pmic 43:f459f6efaf5c 37 raw_my2my.setup(0.9982f, 0.2092f);
pmic 43:f459f6efaf5c 38 raw_mz2mz.setup(1.0027f, -0.0903f);
pmic 43:f459f6efaf5c 39 float mag_val[3] = {0.0f, 0.0f, 0.0f};
pmic 47:c74d09a252d4 40
pmic 47:c74d09a252d4 41 // create object for GNSS Sensor NEO-M9N
pmic 49:76fcaffb92ef 42 NEOM9N neom9n(PA_9, PA_10); // UART1
pmic 55:fbd59767e175 43
pmic 24:86f1a63e35a0 44 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 45 user_button.fall(&user_button_pressed_fcn);
pmic 57:6c87f4989616 46 user_button.rise(&user_button_released_fcn);
pmic 6:e1fa1a2d7483 47
pmic 24:86f1a63e35a0 48 while (true) { // this loop will run forever
pmic 6:e1fa1a2d7483 49
pmic 24:86f1a63e35a0 50 main_task_timer.reset();
pmic 6:e1fa1a2d7483 51
pmic 55:fbd59767e175 52 // update magnetometer
pmic 52:4c282feb57eb 53 mag.readMag(); // this needs approx 2450 mus
pmic 55:fbd59767e175 54 mag_val[0] = raw_mx2mx.evaluate( mag.magX() );
pmic 55:fbd59767e175 55 mag_val[1] = raw_my2my.evaluate( mag.magY() );
pmic 55:fbd59767e175 56 mag_val[2] = raw_mz2mz.evaluate( mag.magZ() );
pmic 52:4c282feb57eb 57
pmic 55:fbd59767e175 58 // update GNSS
pmic 52:4c282feb57eb 59 neom9n.update();
pmic 43:f459f6efaf5c 60
pmic 24:86f1a63e35a0 61 if (do_execute_main_task) {
pmic 17:c19b471f05cb 62
pmic 1:93d997d6b232 63 } else {
pmic 6:e1fa1a2d7483 64
pmic 1:93d997d6b232 65 }
pmic 6:e1fa1a2d7483 66
pmic 24:86f1a63e35a0 67 user_led = !user_led;
pmic 24:86f1a63e35a0 68
pmic 55:fbd59767e175 69 //int main_task_elapsed_time_mus = std::chrono::duration_cast<std::chrono::microseconds>(main_task_timer.elapsed_time()).count();
pmic 52:4c282feb57eb 70
pmic 24:86f1a63e35a0 71 // do only output via serial what's really necessary (this makes your code slow)
pmic 55:fbd59767e175 72 printf("%0.3f, %0.3f, %0.3f, ", // 1: 3 : scaled with 10.0f
pmic 55:fbd59767e175 73 mag_val[0] * 10.0f,
pmic 55:fbd59767e175 74 mag_val[1] * 10.0f,
pmic 55:fbd59767e175 75 mag_val[2] * 10.0f);
pmic 55:fbd59767e175 76 printf("%0.3f, %0.3f, %0.3f, ", // 4: 6 : scaled with 10.0f
pmic 55:fbd59767e175 77 neom9n.getVelN() * 10.0f,
pmic 55:fbd59767e175 78 neom9n.getVelE() * 10.0f,
pmic 55:fbd59767e175 79 neom9n.getVelD() * 10.0f);
pmic 55:fbd59767e175 80 printf("%0.3f, %0.3f, %0.3f, %0.3f, ", // 7:10 : scaled with 10.0f
pmic 55:fbd59767e175 81 neom9n.getGroundSpeed() * 10.0f,
pmic 55:fbd59767e175 82 neom9n.getSpeedAcc() * 10.0f,
pmic 55:fbd59767e175 83 neom9n.getHeading() * 10.0f,
pmic 55:fbd59767e175 84 neom9n.getHeadAcc() * 10.0f);
pmic 55:fbd59767e175 85 printf("%0.6f, %0.6f, %0.3f, %0.3f, ", // 11:14 : scaled with 10.0f
pmic 55:fbd59767e175 86 neom9n.getLon() * 10.0f,
pmic 55:fbd59767e175 87 neom9n.getLat() * 10.0f,
pmic 55:fbd59767e175 88 neom9n.getHeight() * 10.0f,
pmic 55:fbd59767e175 89 neom9n.getHeightMSL() * 10.0f);
pmic 57:6c87f4989616 90 printf("%0.3f, %d, ", // 15:16 : no scaling
pmic 55:fbd59767e175 91 neom9n.getSatTime(),
pmic 55:fbd59767e175 92 neom9n.getNumSat());
pmic 55:fbd59767e175 93
pmic 57:6c87f4989616 94 int run_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(run_timer.elapsed_time()).count();
pmic 57:6c87f4989616 95 printf("%d\r\n", run_elapsed_time_ms);
pmic 17:c19b471f05cb 96
pmic 24:86f1a63e35a0 97 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 98 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 57:6c87f4989616 99
pmic 24:86f1a63e35a0 100 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 101 }
pmic 1:93d997d6b232 102 }
pmic 6:e1fa1a2d7483 103
pmic 24:86f1a63e35a0 104 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 105 {
pmic 26:28693b369945 106 user_button_timer.start();
pmic 6:e1fa1a2d7483 107 user_button_timer.reset();
pmic 6:e1fa1a2d7483 108 }
pmic 6:e1fa1a2d7483 109
pmic 24:86f1a63e35a0 110 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 111 {
pmic 24:86f1a63e35a0 112 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 113 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 114 user_button_timer.stop();
pmic 24:86f1a63e35a0 115 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 116 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 117 }
pmic 52:4c282feb57eb 118 }