Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@49:76fcaffb92ef, 2022-06-02 (annotated)
- Committer:
- pmic
- Date:
- Thu Jun 02 09:42:26 2022 +0000
- Revision:
- 49:76fcaffb92ef
- Parent:
- 48:77009dca23a8
- Child:
- 50:84723ac07ea5
Changes applied, no GPS data
Who changed what in which revision?
| User | Revision | Line number | New 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 | 47:c74d09a252d4 | 22 | const int main_task_period_ms = 100; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
| pmic | 36:8c75783c1eca | 23 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
| pmic | 36:8c75783c1eca | 24 | |
| pmic | 36:8c75783c1eca | 25 | // led on nucleo board |
| pmic | 36:8c75783c1eca | 26 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
| pmic | 36:8c75783c1eca | 27 | |
| pmic | 43:f459f6efaf5c | 28 | // create QMC5883L compass object |
| pmic | 47:c74d09a252d4 | 29 | I2C i2c(PB_9, PB_8); // I2C1 |
| pmic | 43:f459f6efaf5c | 30 | QMC5883L mag(i2c); |
| pmic | 43:f459f6efaf5c | 31 | LinearCharacteristics raw_mx2mx, raw_my2my, raw_mz2mz; |
| pmic | 43:f459f6efaf5c | 32 | raw_mx2mx.setup(0.9991f, 0.0088f); |
| pmic | 43:f459f6efaf5c | 33 | raw_my2my.setup(0.9982f, 0.2092f); |
| pmic | 43:f459f6efaf5c | 34 | raw_mz2mz.setup(1.0027f, -0.0903f); |
| pmic | 43:f459f6efaf5c | 35 | float mag_val[3] = {0.0f, 0.0f, 0.0f}; |
| pmic | 47:c74d09a252d4 | 36 | |
| pmic | 47:c74d09a252d4 | 37 | // create object for GNSS Sensor NEO-M9N |
| pmic | 49:76fcaffb92ef | 38 | NEOM9N neom9n(PA_9, PA_10); // UART1 |
| pmic | 48:77009dca23a8 | 39 | //NEOM9N neom9n(PA_2, PA_3); // UART2 |
| pmic | 36:8c75783c1eca | 40 | |
| pmic | 24:86f1a63e35a0 | 41 | // attach button fall and rise functions to user button object |
| pmic | 24:86f1a63e35a0 | 42 | user_button.fall(&user_button_pressed_fcn); |
| pmic | 24:86f1a63e35a0 | 43 | user_button.rise(&user_button_released_fcn); |
| pmic | 17:c19b471f05cb | 44 | |
| pmic | 29:d6f1ccf42a31 | 45 | // start timer |
| pmic | 24:86f1a63e35a0 | 46 | main_task_timer.start(); |
| 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 | 43:f459f6efaf5c | 52 | mag.readMag(); |
| pmic | 43:f459f6efaf5c | 53 | |
| pmic | 24:86f1a63e35a0 | 54 | if (do_execute_main_task) { |
| pmic | 17:c19b471f05cb | 55 | |
| pmic | 43:f459f6efaf5c | 56 | mag_val[0] = raw_mx2mx.evaluate(mag.magX()); |
| pmic | 43:f459f6efaf5c | 57 | mag_val[1] = raw_my2my.evaluate(mag.magY()); |
| pmic | 43:f459f6efaf5c | 58 | mag_val[2] = raw_mz2mz.evaluate(mag.magZ()); |
| pmic | 9:f10b974d01e0 | 59 | |
| pmic | 1:93d997d6b232 | 60 | } else { |
| pmic | 6:e1fa1a2d7483 | 61 | |
| pmic | 43:f459f6efaf5c | 62 | for (uint8_t i = 0; i <= 3; i++) { |
| pmic | 43:f459f6efaf5c | 63 | mag_val[i] = 0; |
| pmic | 43:f459f6efaf5c | 64 | } |
| pmic | 6:e1fa1a2d7483 | 65 | |
| pmic | 1:93d997d6b232 | 66 | } |
| pmic | 6:e1fa1a2d7483 | 67 | |
| pmic | 24:86f1a63e35a0 | 68 | user_led = !user_led; |
| pmic | 24:86f1a63e35a0 | 69 | |
| pmic | 24:86f1a63e35a0 | 70 | // do only output via serial what's really necessary (this makes your code slow) |
| pmic | 48:77009dca23a8 | 71 | printf("%f, %f, %f\r\n", mag_val[0], mag_val[1], mag_val[2]); |
| pmic | 49:76fcaffb92ef | 72 | printf("GPS time: %d, num sat: %d, lat: %d, lon: %d, speed: %d, heading: %d\r\n", neom9n.actualPVT.itow, neom9n.actualPVT.numSV, neom9n.actualPVT.lat, neom9n.actualPVT.lon, neom9n.actualPVT.speed, neom9n.actualPVT.headMot); |
| pmic | 17:c19b471f05cb | 73 | |
| pmic | 24:86f1a63e35a0 | 74 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
| pmic | 24:86f1a63e35a0 | 75 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
| pmic | 24:86f1a63e35a0 | 76 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
| pmic | 1:93d997d6b232 | 77 | } |
| pmic | 1:93d997d6b232 | 78 | } |
| pmic | 6:e1fa1a2d7483 | 79 | |
| pmic | 24:86f1a63e35a0 | 80 | void user_button_pressed_fcn() |
| pmic | 25:ea1d6e27c895 | 81 | { |
| pmic | 26:28693b369945 | 82 | user_button_timer.start(); |
| pmic | 6:e1fa1a2d7483 | 83 | user_button_timer.reset(); |
| pmic | 6:e1fa1a2d7483 | 84 | } |
| pmic | 6:e1fa1a2d7483 | 85 | |
| pmic | 24:86f1a63e35a0 | 86 | void user_button_released_fcn() |
| pmic | 6:e1fa1a2d7483 | 87 | { |
| pmic | 24:86f1a63e35a0 | 88 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
| pmic | 24:86f1a63e35a0 | 89 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
| pmic | 6:e1fa1a2d7483 | 90 | user_button_timer.stop(); |
| pmic | 24:86f1a63e35a0 | 91 | if (user_button_elapsed_time_ms > 200) { |
| pmic | 24:86f1a63e35a0 | 92 | do_execute_main_task = !do_execute_main_task; |
| pmic | 8:9bb806a7f585 | 93 | } |
| pmic | 6:e1fa1a2d7483 | 94 | } |