Workshop 1

Dependencies:   PM2_Libary Eigen

Committer:
pmic
Date:
Tue May 17 12:35:58 2022 +0000
Revision:
40:7e6b7aec3947
Parent:
39:c6475c899b61
Child:
41:8a63b01edd7e
Create example project for Summer School 2022

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 36:8c75783c1eca 1 #include <mbed.h>
pmic 36:8c75783c1eca 2
pmic 17:c19b471f05cb 3 #include "PM2_Libary.h"
pmic 36:8c75783c1eca 4 #include "Eigen/Dense.h"
pmic 36:8c75783c1eca 5
pmic 36:8c75783c1eca 6 # define M_PI 3.14159265358979323846 // number pi
pmic 6:e1fa1a2d7483 7
pmic 24:86f1a63e35a0 8 // logical variable main task
pmic 24:86f1a63e35a0 9 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
pmic 17:c19b471f05cb 10
pmic 24:86f1a63e35a0 11 // user button on nucleo board
pmic 24:86f1a63e35a0 12 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 13 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 14 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 15 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 16
pmic 1:93d997d6b232 17 int main()
pmic 23:26b3a25fc637 18 {
pmic 36:8c75783c1eca 19 // while loop gets executed every main_task_period_ms milliseconds
pmic 40:7e6b7aec3947 20 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 21 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 22
pmic 40:7e6b7aec3947 23 // a coutner
pmic 40:7e6b7aec3947 24 uint32_t main_task_cntr = 0;
pmic 40:7e6b7aec3947 25
pmic 36:8c75783c1eca 26 // led on nucleo board
pmic 36:8c75783c1eca 27 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 36:8c75783c1eca 28
pmic 36:8c75783c1eca 29 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
pmic 36:8c75783c1eca 30 float ir_distance_mV = 0.0f; // define variable to store measurement
pmic 36:8c75783c1eca 31 AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 36:8c75783c1eca 32
pmic 40:7e6b7aec3947 33 // create SensorBar object for sparkfun line follower array, only use this if it is connected (blocking your code if not)
pmic 40:7e6b7aec3947 34 float sensor_bar_avgAngleRad = 0.0f;
pmic 40:7e6b7aec3947 35 I2C i2c(PB_9, PB_8);
pmic 40:7e6b7aec3947 36 //SensorBar sensor_bar(i2c, 0.1175f); // second input argument is distance from bar to wheel axis
pmic 40:7e6b7aec3947 37
pmic 36:8c75783c1eca 38 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 36:8c75783c1eca 39 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 36:8c75783c1eca 40
pmic 40:7e6b7aec3947 41 FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity)
pmic 40:7e6b7aec3947 42 FastPWM pwm_M2(PA_9); // motor M2 is closed-loop position controlled (angle controlled)
pmic 36:8c75783c1eca 43
pmic 36:8c75783c1eca 44 EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values
pmic 36:8c75783c1eca 45 EncoderCounter encoder_M2(PB_6, PB_7);
pmic 36:8c75783c1eca 46
pmic 36:8c75783c1eca 47 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
pmic 36:8c75783c1eca 48 const float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
pmic 36:8c75783c1eca 49 const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio
pmic 36:8c75783c1eca 50 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
pmic 40:7e6b7aec3947 51 //const float k_gear = 100.0f / 78.125f; // define additional ratio in case you are using a dc motor with a different gear box, e.g. 100:1
pmic 40:7e6b7aec3947 52 //const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
pmic 36:8c75783c1eca 53
pmic 40:7e6b7aec3947 54 SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1); // default 78.125:1 gear box with default contoller parameters
pmic 40:7e6b7aec3947 55 //SpeedController speedController_M1(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear
pmic 36:8c75783c1eca 56
pmic 40:7e6b7aec3947 57 PositionController positionController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear with default contoller parameters
pmic 40:7e6b7aec3947 58 //PositionController positionController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
pmic 40:7e6b7aec3947 59 //positionController_M2.setSpeedCntrlGain(kp * k_gear);
pmic 40:7e6b7aec3947 60 // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage
pmic 40:7e6b7aec3947 61 float max_speed_rps = 0.5f;
pmic 40:7e6b7aec3947 62 positionController_M2.setMaxVelocityRPS(max_speed_rps);
pmic 36:8c75783c1eca 63
pmic 24:86f1a63e35a0 64 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 65 user_button.fall(&user_button_pressed_fcn);
pmic 24:86f1a63e35a0 66 user_button.rise(&user_button_released_fcn);
pmic 17:c19b471f05cb 67
pmic 29:d6f1ccf42a31 68 // start timer
pmic 24:86f1a63e35a0 69 main_task_timer.start();
pmic 6:e1fa1a2d7483 70
pmic 24:86f1a63e35a0 71 // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled
pmic 10:c5d85e35758c 72 enable_motors = 1;
pmic 17:c19b471f05cb 73
pmic 24:86f1a63e35a0 74 while (true) { // this loop will run forever
pmic 6:e1fa1a2d7483 75
pmic 24:86f1a63e35a0 76 main_task_timer.reset();
pmic 40:7e6b7aec3947 77
pmic 40:7e6b7aec3947 78 // read analog input
pmic 40:7e6b7aec3947 79 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
pmic 40:7e6b7aec3947 80
pmic 40:7e6b7aec3947 81 // read SensorBar, only use this if it is connected (blocking your code if not)
pmic 40:7e6b7aec3947 82 //if (sensor_bar.isAnyLedActive()) {
pmic 40:7e6b7aec3947 83 // sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad();
pmic 40:7e6b7aec3947 84 //}
pmic 6:e1fa1a2d7483 85
pmic 24:86f1a63e35a0 86 if (do_execute_main_task) {
pmic 17:c19b471f05cb 87
pmic 40:7e6b7aec3947 88 speedController_M1.setDesiredSpeedRPS(0.5f);
pmic 40:7e6b7aec3947 89 positionController_M2.setDesiredRotation(1.5f);
pmic 9:f10b974d01e0 90
pmic 1:93d997d6b232 91 } else {
pmic 6:e1fa1a2d7483 92
pmic 40:7e6b7aec3947 93 speedController_M1.setDesiredSpeedRPS(0.0f);
pmic 40:7e6b7aec3947 94 positionController_M2.setDesiredRotation(0.0f);
pmic 40:7e6b7aec3947 95
pmic 1:93d997d6b232 96 }
pmic 6:e1fa1a2d7483 97
pmic 40:7e6b7aec3947 98 // user_led is switching its state every 100 runs
pmic 40:7e6b7aec3947 99 if ( (main_task_cntr%(1000 / main_task_cntr) == 0) && (main_task_cntr!=0) ) {
pmic 40:7e6b7aec3947 100 user_led = !user_led;
pmic 40:7e6b7aec3947 101 }
pmic 40:7e6b7aec3947 102 main_task_cntr++;
pmic 40:7e6b7aec3947 103
pmic 24:86f1a63e35a0 104 // do only output via serial what's really necessary (this makes your code slow)
pmic 40:7e6b7aec3947 105 printf("IR sensor (mV): %3.3f, SensorBar angle (rad): %3.3f, Speed M1 (rps) %3.3f, Position M2 (rot): %3.3f\r\n",
pmic 24:86f1a63e35a0 106 ir_distance_mV,
pmic 40:7e6b7aec3947 107 sensor_bar_avgAngleRad,
pmic 40:7e6b7aec3947 108 speedController_M1.getSpeedRPS(),
pmic 40:7e6b7aec3947 109 positionController_M2.getRotation());
pmic 17:c19b471f05cb 110
pmic 24:86f1a63e35a0 111 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 112 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 113 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 114 }
pmic 1:93d997d6b232 115 }
pmic 6:e1fa1a2d7483 116
pmic 24:86f1a63e35a0 117 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 118 {
pmic 26:28693b369945 119 user_button_timer.start();
pmic 6:e1fa1a2d7483 120 user_button_timer.reset();
pmic 6:e1fa1a2d7483 121 }
pmic 6:e1fa1a2d7483 122
pmic 24:86f1a63e35a0 123 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 124 {
pmic 24:86f1a63e35a0 125 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 126 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 127 user_button_timer.stop();
pmic 24:86f1a63e35a0 128 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 129 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 130 }
pmic 6:e1fa1a2d7483 131 }