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
- Committer:
- pmic
- Date:
- 2022-06-02
- Revision:
- 49:76fcaffb92ef
- Parent:
- 48:77009dca23a8
- Child:
- 50:84723ac07ea5
File content as of revision 49:76fcaffb92ef:
#include <mbed.h>
// GNSS and Compass test programm for Mateksys GNSS&Compass M9N-5883
// #include "Eigen/Dense.h"
#include "QMC5883L.h"
#include "LinearCharacteristics.h"
#include "NEOM9N.h"
// logical variable main task
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
// user button on nucleo board
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)
InterruptIn user_button(PC_13);     // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR)
void user_button_pressed_fcn();     // custom functions which gets executed when user button gets pressed and released, definition below
void user_button_released_fcn();
int main()
{
    // while loop gets executed every main_task_period_ms milliseconds
    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
    Timer main_task_timer;                // create Timer object which we use to run the main task every main task period time in ms
    // led on nucleo board
    DigitalOut user_led(LED1);      // create DigitalOut object to command user led
    // create QMC5883L compass object
    I2C i2c(PB_9, PB_8); // I2C1
    QMC5883L mag(i2c);
    LinearCharacteristics raw_mx2mx, raw_my2my, raw_mz2mz;
    raw_mx2mx.setup(0.9991f, 0.0088f);
    raw_my2my.setup(0.9982f, 0.2092f);
    raw_mz2mz.setup(1.0027f, -0.0903f);
    float mag_val[3] = {0.0f, 0.0f, 0.0f};
    
    // create object for GNSS Sensor NEO-M9N
    NEOM9N neom9n(PA_9, PA_10); // UART1
    //NEOM9N neom9n(PA_2, PA_3); // UART2
    // attach button fall and rise functions to user button object
    user_button.fall(&user_button_pressed_fcn);
    user_button.rise(&user_button_released_fcn);
    // start timer
    main_task_timer.start();
    while (true) { // this loop will run forever
        main_task_timer.reset();
        mag.readMag();
        if (do_execute_main_task) {
            mag_val[0] = raw_mx2mx.evaluate(mag.magX());
            mag_val[1] = raw_my2my.evaluate(mag.magY());
            mag_val[2] = raw_mz2mz.evaluate(mag.magZ());
        } else {
            for (uint8_t i = 0; i <= 3; i++) {
                mag_val[i] = 0;
            }
        }
        user_led = !user_led;
        // do only output via serial what's really necessary (this makes your code slow)
        printf("%f, %f, %f\r\n", mag_val[0], mag_val[1], mag_val[2]);
        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);
        // read timer and make the main thread sleep for the remaining time span (non blocking)
        int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
        thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
    }
}
void user_button_pressed_fcn()
{
    user_button_timer.start();
    user_button_timer.reset();
}
void user_button_released_fcn()
{
    // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
    int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
    user_button_timer.stop();
    if (user_button_elapsed_time_ms > 200) {
        do_execute_main_task = !do_execute_main_task;
    }
}