Example project for the Line Follower robot.

Dependencies:   PM2_Libary Eigen

Committer:
pmic
Date:
Sat May 14 15:28:15 2022 +0200
Revision:
50:fec2ffc2a443
Parent:
48:3ae406d7554a
Child:
51:b27f2beaf7b3
Working solution, only update mbed left

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 33:cff70742569d 1 #include <mbed.h>
pmic 33:cff70742569d 2 #include <math.h>
pmic 33:cff70742569d 3
pmic 17:c19b471f05cb 4 #include "PM2_Libary.h"
pmic 40:eb7f8dce5787 5 #include "Eigen/Dense.h"
pmic 6:e1fa1a2d7483 6
pmic 34:702246639f02 7 # define M_PI 3.14159265358979323846 // number pi
pmic 33:cff70742569d 8
pmic 24:86f1a63e35a0 9 // logical variable main task
pmic 24:86f1a63e35a0 10 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 11
pmic 24:86f1a63e35a0 12 // user button on nucleo board
pmic 24:86f1a63e35a0 13 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 14 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 15 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 16 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 17
pmic 45:5e1dd4117ed2 18 // controller functions
pmic 45:5e1dd4117ed2 19 float ang_cntrl_fcn(const float& Kp, const float& Kp_nl, const float& angle);
pmic 45:5e1dd4117ed2 20 float vel_cntrl_v1_fcn(const float& vel_max, const float& vel_min, const float& ang_max, const float& angle);
pmic 45:5e1dd4117ed2 21 float vel_cntrl_v2_fcn(const float& wheel_speed_max, const float& b, const float& robot_omega, const Eigen::Matrix2f& Cwheel2robot);
pmic 38:6d11788e14c0 22
pmic 1:93d997d6b232 23 int main()
pmic 46:fd580fa68618 24 {
pmic 45:5e1dd4117ed2 25 // while loop gets executed every main_task_period_ms milliseconds
pmic 45:5e1dd4117ed2 26 const int main_task_period_ms = 10; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second
pmic 48:3ae406d7554a 27 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
pmic 45:5e1dd4117ed2 28
pmic 45:5e1dd4117ed2 29 // led on nucleo board
pmic 45:5e1dd4117ed2 30 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 45:5e1dd4117ed2 31
pmic 45:5e1dd4117ed2 32 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
pmic 45:5e1dd4117ed2 33 float ir_distance_mV = 0.0f; // define variable to store measurement
pmic 45:5e1dd4117ed2 34 AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 45:5e1dd4117ed2 35
pmic 45:5e1dd4117ed2 36 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 45:5e1dd4117ed2 37 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 45:5e1dd4117ed2 38
pmic 50:fec2ffc2a443 39 // create SpeedController objects
pmic 45:5e1dd4117ed2 40 FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity)
pmic 45:5e1dd4117ed2 41 FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity)
pmic 45:5e1dd4117ed2 42 EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values
pmic 45:5e1dd4117ed2 43 EncoderCounter encoder_M2(PB_6, PB_7);
pmic 45:5e1dd4117ed2 44 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 45:5e1dd4117ed2 45 const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio
pmic 45:5e1dd4117ed2 46 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
pmic 50:fec2ffc2a443 47
pmic 48:3ae406d7554a 48 SpeedController* speedControllers[2];
pmic 45:5e1dd4117ed2 49 speedControllers[0] = new SpeedController(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1);
pmic 48:3ae406d7554a 50 speedControllers[1] = new SpeedController(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2);
pmic 50:fec2ffc2a443 51
pmic 45:5e1dd4117ed2 52 // create SensorBar object for sparkfun line follower array
pmic 45:5e1dd4117ed2 53 I2C i2c(PB_9, PB_8);
pmic 48:3ae406d7554a 54 SensorBar sensor_bar(i2c, 0.1175f); // second input argument is distance from bar to wheel axis
pmic 45:5e1dd4117ed2 55
pmic 45:5e1dd4117ed2 56 // robot kinematics
pmic 50:fec2ffc2a443 57 const float r_wheel = 0.0766f / 2.0f; // wheel radius
pmic 50:fec2ffc2a443 58 const float L_wheel = 0.176f; // distance from wheel to wheel
pmic 45:5e1dd4117ed2 59 Eigen::Matrix2f Cwheel2robot; // transform wheel to robot
pmic 50:fec2ffc2a443 60 //Eigen::Matrix2f Crobot2wheel; // transform robot to wheel
pmic 50:fec2ffc2a443 61 Cwheel2robot << r_wheel / 2.0f , r_wheel / 2.0f ,
pmic 50:fec2ffc2a443 62 r_wheel / L_wheel, -r_wheel / L_wheel;
pmic 50:fec2ffc2a443 63 //Crobot2wheel << 1.0f / r_wheel, L_wheel / (2.0f * r_wheel),
pmic 50:fec2ffc2a443 64 // 1.0f / r_wheel, -L_wheel / (2.0f * r_wheel);
pmic 48:3ae406d7554a 65 Eigen::Vector2f robot_coord; // contains v and w (robot translational and rotational velocities)
pmic 50:fec2ffc2a443 66 Eigen::Vector2f wheel_speed; // w1 w2 (wheel speed)
pmic 45:5e1dd4117ed2 67 robot_coord.setZero();
pmic 45:5e1dd4117ed2 68 wheel_speed.setZero();
pmic 46:fd580fa68618 69
pmic 24:86f1a63e35a0 70 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 71 user_button.fall(&user_button_pressed_fcn);
pmic 24:86f1a63e35a0 72 user_button.rise(&user_button_released_fcn);
pmic 17:c19b471f05cb 73
pmic 29:d6f1ccf42a31 74 // start timer
pmic 24:86f1a63e35a0 75 main_task_timer.start();
pmic 6:e1fa1a2d7483 76
pmic 24:86f1a63e35a0 77 while (true) { // this loop will run forever
pmic 6:e1fa1a2d7483 78
pmic 24:86f1a63e35a0 79 main_task_timer.reset();
pmic 6:e1fa1a2d7483 80
pmic 24:86f1a63e35a0 81 if (do_execute_main_task) {
pmic 34:702246639f02 82
pmic 48:3ae406d7554a 83 // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled
pmic 48:3ae406d7554a 84 enable_motors = 1;
pmic 47:5ce234723e3a 85
pmic 42:b54a4f294aa9 86 // read SensorBar
pmic 43:5648b7083fe5 87 static float sensor_bar_avgAngleRad = 0.0f; // by making this static it will not be overwritten (only fist time set to zero)
pmic 42:b54a4f294aa9 88 if (sensor_bar.isAnyLedActive()) {
pmic 42:b54a4f294aa9 89 sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad();
pmic 42:b54a4f294aa9 90 }
pmic 42:b54a4f294aa9 91
pmic 48:3ae406d7554a 92 const static float Kp = 2.0f; // by making this const static it will not be overwritten and only initiliazed once
pmic 45:5e1dd4117ed2 93 const static float Kp_nl = 17.0f;
pmic 45:5e1dd4117ed2 94 robot_coord(1) = ang_cntrl_fcn(Kp, Kp_nl, sensor_bar_avgAngleRad);
pmic 42:b54a4f294aa9 95
pmic 43:5648b7083fe5 96 // nonlinear controllers version 1 (whatever came to my mind)
pmic 43:5648b7083fe5 97 /*
pmic 43:5648b7083fe5 98 const static float vel_max = 0.3374f; //0.10f;
pmic 43:5648b7083fe5 99 const static float vel_min = 0.00f; //0.02f;
pmic 43:5648b7083fe5 100 const static float ang_max = 27.0f * M_PI / 180.0f;
pmic 45:5e1dd4117ed2 101 robot_coord(0) = vel_cntrl_v1_fcn(vel_max, vel_min, ang_max, sensor_bar_avgAngleRad);
pmic 43:5648b7083fe5 102 */
pmic 43:5648b7083fe5 103
pmic 43:5648b7083fe5 104 // nonlinear controllers version 2 (one wheel always at full speed controller)
pmic 43:5648b7083fe5 105 ///*
pmic 45:5e1dd4117ed2 106 const static float wheel_speed_max = max_voltage * kn / 60.0f * 2.0f * M_PI;
pmic 45:5e1dd4117ed2 107 const static float b = L_wheel / (2.0f * r_wheel);
pmic 45:5e1dd4117ed2 108 robot_coord(0) = vel_cntrl_v2_fcn(wheel_speed_max, b, robot_coord(1), Cwheel2robot);
pmic 43:5648b7083fe5 109 //*/
pmic 43:5648b7083fe5 110
pmic 50:fec2ffc2a443 111 // transform robot coordinates to wheel speed
pmic 50:fec2ffc2a443 112 wheel_speed = Cwheel2robot.inverse() * robot_coord;
pmic 42:b54a4f294aa9 113
pmic 38:6d11788e14c0 114 // read analog input
pmic 38:6d11788e14c0 115 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
pmic 38:6d11788e14c0 116
pmic 50:fec2ffc2a443 117 // command speedController objects
pmic 48:3ae406d7554a 118 speedControllers[0]->setDesiredSpeedRPS(wheel_speed(0) / (2.0f * M_PI)); // set a desired speed for speed controlled dc motors M1
pmic 48:3ae406d7554a 119 speedControllers[1]->setDesiredSpeedRPS(wheel_speed(1) / (2.0f * M_PI)); // set a desired speed for speed controlled dc motors M2
pmic 34:702246639f02 120
pmic 1:93d997d6b232 121 } else {
pmic 6:e1fa1a2d7483 122
pmic 48:3ae406d7554a 123 enable_motors = 0;
pmic 48:3ae406d7554a 124
pmic 38:6d11788e14c0 125 ir_distance_mV = 0.0f;
pmic 38:6d11788e14c0 126
pmic 48:3ae406d7554a 127 speedControllers[0]->setDesiredSpeedRPS(0.0f);
pmic 48:3ae406d7554a 128 speedControllers[1]->setDesiredSpeedRPS(0.0f);
pmic 46:fd580fa68618 129
pmic 33:cff70742569d 130 }
pmic 6:e1fa1a2d7483 131
pmic 24:86f1a63e35a0 132 user_led = !user_led;
pmic 24:86f1a63e35a0 133
pmic 24:86f1a63e35a0 134 // do only output via serial what's really necessary (this makes your code slow)
pmic 46:fd580fa68618 135 //printf("%d, %d\r\n", sensor_bar_raw_value_time_ms, sensor_bar_position_time_ms);
pmic 46:fd580fa68618 136 //printf("%f, %f\r\n", speedControllers[0]->getSpeedRPS(), speedControllers[1]->getSpeedRPS());
pmic 46:fd580fa68618 137
pmic 24:86f1a63e35a0 138 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 139 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 140 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 141 }
pmic 1:93d997d6b232 142 }
pmic 6:e1fa1a2d7483 143
pmic 24:86f1a63e35a0 144 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 145 {
pmic 26:28693b369945 146 user_button_timer.start();
pmic 6:e1fa1a2d7483 147 user_button_timer.reset();
pmic 6:e1fa1a2d7483 148 }
pmic 6:e1fa1a2d7483 149
pmic 24:86f1a63e35a0 150 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 151 {
pmic 24:86f1a63e35a0 152 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 153 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 154 user_button_timer.stop();
pmic 24:86f1a63e35a0 155 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 156 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 157 }
pmic 42:b54a4f294aa9 158 }
pmic 42:b54a4f294aa9 159
pmic 45:5e1dd4117ed2 160 float ang_cntrl_fcn(const float& Kp, const float& Kp_nl, const float& angle)
pmic 43:5648b7083fe5 161 {
pmic 45:5e1dd4117ed2 162 static float retval = 0.0f;
pmic 43:5648b7083fe5 163 if (angle > 0) {
pmic 43:5648b7083fe5 164 retval = Kp * angle + Kp_nl * angle * angle;
pmic 45:5e1dd4117ed2 165 } else if (angle <= 0) {
pmic 43:5648b7083fe5 166 retval = Kp * angle - Kp_nl * angle * angle;
pmic 43:5648b7083fe5 167 }
pmic 43:5648b7083fe5 168 return retval;
pmic 43:5648b7083fe5 169 }
pmic 43:5648b7083fe5 170
pmic 45:5e1dd4117ed2 171 float vel_cntrl_v1_fcn(const float& vel_max, const float& vel_min, const float& ang_max, const float& angle)
pmic 42:b54a4f294aa9 172 {
pmic 42:b54a4f294aa9 173 const static float gain = (vel_min - vel_max) / ang_max;
pmic 42:b54a4f294aa9 174 const static float offset = vel_max;
pmic 43:5648b7083fe5 175 return gain * fabs(angle) + offset;
pmic 42:b54a4f294aa9 176 }
pmic 42:b54a4f294aa9 177
pmic 45:5e1dd4117ed2 178 float vel_cntrl_v2_fcn(const float& wheel_speed_max, const float& b, const float& robot_omega, const Eigen::Matrix2f& Cwheel2robot)
pmic 42:b54a4f294aa9 179 {
pmic 43:5648b7083fe5 180 static Eigen::Matrix<float, 2, 2> _wheel_speed;
pmic 43:5648b7083fe5 181 static Eigen::Matrix<float, 2, 2> _robot_coord;
pmic 43:5648b7083fe5 182 if (robot_omega > 0) {
pmic 43:5648b7083fe5 183 _wheel_speed(0) = wheel_speed_max;
pmic 43:5648b7083fe5 184 _wheel_speed(1) = wheel_speed_max - 2*b*robot_omega;
pmic 43:5648b7083fe5 185 } else {
pmic 43:5648b7083fe5 186 _wheel_speed(0) = wheel_speed_max + 2*b*robot_omega;
pmic 43:5648b7083fe5 187 _wheel_speed(1) = wheel_speed_max;
pmic 42:b54a4f294aa9 188 }
pmic 43:5648b7083fe5 189 _robot_coord = Cwheel2robot * _wheel_speed;
pmic 43:5648b7083fe5 190 return _robot_coord(0);
pmic 6:e1fa1a2d7483 191 }