Example project for the Line Follower robot.

Dependencies:   PM2_Libary Eigen

main.cpp

Committer:
pmic
Date:
2022-05-05
Revision:
33:cff70742569d
Parent:
31:1b2a1bd1bccb
Child:
34:702246639f02

File content as of revision 33:cff70742569d:

#include <mbed.h>
#include <math.h>

#include "PM2_Libary.h"

# define M_PI 3.14159265358979323846  /* pi */

// logical variable main task
bool do_execute_main_task = false;  // 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();

// while loop gets executed every main_task_period_ms milliseconds
int main_task_period_ms = 200;   // 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

I2C i2c(PB_9, PB_8); // I2C (PinName sda, PinName scl)
SensorBar sensor_bar(i2c, 0.1175f);
// PinName sda = PB_9;
// PinName scl = PB_8;
// SensorBar sensor_bar = SensorBar(0.1175f);

int main()
{
    // 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();

    // sensor_bar.setBarStrobe();
    // sensor_bar.clearBarStrobe();  // to illuminate all the time
    // sensor_bar.clearInvertBits(); // to make the bar look for a dark line on a reflective surface
    // sensor_bar.begin();

    while (true) { // this loop will run forever

        main_task_timer.reset();

        /*
        if (do_execute_main_task) {
        } else {

        }
        */

        // sensor_bar.update();

        printf("---\r\n");

        uint8_t sensor_bar_raw_value = sensor_bar.getRaw();
        for( int i = 7; i >= 0; i-- ) {
            printf("%d", (sensor_bar_raw_value >> i) & 0x01);
        }
        printf("\r\n");

        int8_t sensor_bar_binaryPosition = sensor_bar.getBinaryPosition();       
        printf("%d\r\n", sensor_bar_binaryPosition);

        uint8_t sensor_bar_nrOfLedsActive = sensor_bar.getNrofLedsActive();
        printf("%d\r\n", sensor_bar_nrOfLedsActive);
        
        float sensor_bar_angleRad = sensor_bar.getAngleRad();
        printf("%f\r\n", sensor_bar_angleRad * 180.0f / M_PI);

        user_led = !user_led;

        // do only output via serial what's really necessary (this makes your code slow)
        // printf("%d, %d\r\n", sensor_bar_raw_value_time_ms, sensor_bar_position_time_ms);

        // 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;
    }
}