Example project for the Line Follower robot.
Dependencies: PM2_Libary Eigen
main.cpp@46:fd580fa68618, 2022-05-13 (annotated)
- Committer:
- pmic
- Date:
- Fri May 13 20:53:37 2022 +0000
- Revision:
- 46:fd580fa68618
- Parent:
- 45:5e1dd4117ed2
- Child:
- 47:5ce234723e3a
Motion integrated into SpeedController and PositionController class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 33:cff70742569d | 1 | #include <mbed.h> |
pmic | 33:cff70742569d | 2 | #include <math.h> |
pmic | 46:fd580fa68618 | 3 | //#include <vector> |
pmic | 33:cff70742569d | 4 | |
pmic | 17:c19b471f05cb | 5 | #include "PM2_Libary.h" |
pmic | 40:eb7f8dce5787 | 6 | #include "Eigen/Dense.h" |
pmic | 6:e1fa1a2d7483 | 7 | |
pmic | 46:fd580fa68618 | 8 | #include "Motion.h" |
pmic | 46:fd580fa68618 | 9 | |
pmic | 34:702246639f02 | 10 | # define M_PI 3.14159265358979323846 // number pi |
pmic | 33:cff70742569d | 11 | |
pmic | 24:86f1a63e35a0 | 12 | // logical variable main task |
pmic | 24:86f1a63e35a0 | 13 | 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 | 14 | |
pmic | 24:86f1a63e35a0 | 15 | // user button on nucleo board |
pmic | 24:86f1a63e35a0 | 16 | 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 | 17 | 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 | 18 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 24:86f1a63e35a0 | 19 | void user_button_released_fcn(); |
pmic | 6:e1fa1a2d7483 | 20 | |
pmic | 45:5e1dd4117ed2 | 21 | // controller functions |
pmic | 45:5e1dd4117ed2 | 22 | float ang_cntrl_fcn(const float& Kp, const float& Kp_nl, const float& angle); |
pmic | 45:5e1dd4117ed2 | 23 | float vel_cntrl_v1_fcn(const float& vel_max, const float& vel_min, const float& ang_max, const float& angle); |
pmic | 45:5e1dd4117ed2 | 24 | float vel_cntrl_v2_fcn(const float& wheel_speed_max, const float& b, const float& robot_omega, const Eigen::Matrix2f& Cwheel2robot); |
pmic | 38:6d11788e14c0 | 25 | |
pmic | 46:fd580fa68618 | 26 | float speed = 0.0f; |
pmic | 46:fd580fa68618 | 27 | float motionspeed = 0.0f; |
pmic | 46:fd580fa68618 | 28 | |
pmic | 1:93d997d6b232 | 29 | int main() |
pmic | 46:fd580fa68618 | 30 | { |
pmic | 45:5e1dd4117ed2 | 31 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 45:5e1dd4117ed2 | 32 | 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 | 45:5e1dd4117ed2 | 33 | 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 | 34 | |
pmic | 45:5e1dd4117ed2 | 35 | // led on nucleo board |
pmic | 45:5e1dd4117ed2 | 36 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 45:5e1dd4117ed2 | 37 | |
pmic | 45:5e1dd4117ed2 | 38 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 45:5e1dd4117ed2 | 39 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 45:5e1dd4117ed2 | 40 | 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 | 41 | |
pmic | 45:5e1dd4117ed2 | 42 | // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB |
pmic | 45:5e1dd4117ed2 | 43 | DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors |
pmic | 45:5e1dd4117ed2 | 44 | |
pmic | 45:5e1dd4117ed2 | 45 | // create SpeedController objects, default parametrization is for 78.125:1 gear box |
pmic | 45:5e1dd4117ed2 | 46 | FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity) |
pmic | 45:5e1dd4117ed2 | 47 | FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity) |
pmic | 45:5e1dd4117ed2 | 48 | EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values |
pmic | 45:5e1dd4117ed2 | 49 | EncoderCounter encoder_M2(PB_6, PB_7); |
pmic | 45:5e1dd4117ed2 | 50 | 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 | 51 | const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio |
pmic | 45:5e1dd4117ed2 | 52 | const float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 46:fd580fa68618 | 53 | //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 | 46:fd580fa68618 | 54 | //const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 44:340cdc4b6e47 | 55 | |
pmic | 46:fd580fa68618 | 56 | SpeedController* speedControllers[1]; |
pmic | 45:5e1dd4117ed2 | 57 | speedControllers[0] = new SpeedController(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1); |
pmic | 46:fd580fa68618 | 58 | //speedControllers[1] = new SpeedController(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); |
pmic | 46:fd580fa68618 | 59 | speedControllers[0]->setMaxAccelerationRPM(22.0f * max_voltage * kn * 0.05f); |
pmic | 46:fd580fa68618 | 60 | //speedControllers[1]->setMaxAccelerationRPM(22.0f * max_voltage * kn * 0.1f); |
pmic | 46:fd580fa68618 | 61 | PositionController* positionController = new PositionController(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); |
pmic | 46:fd580fa68618 | 62 | positionController->setMaxAccelerationRPM(22.0f * max_voltage * kn * 0.05f); |
pmic | 46:fd580fa68618 | 63 | positionController->setMaxVelocityRPS(2.0f); |
pmic | 45:5e1dd4117ed2 | 64 | //std::vector<SpeedController*> speedControllers; |
pmic | 45:5e1dd4117ed2 | 65 | //speedControllers.push_back( new SpeedController(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1) ); |
pmic | 45:5e1dd4117ed2 | 66 | //speedControllers.push_back( new SpeedController(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2) ); |
pmic | 45:5e1dd4117ed2 | 67 | |
pmic | 45:5e1dd4117ed2 | 68 | // create SensorBar object for sparkfun line follower array |
pmic | 45:5e1dd4117ed2 | 69 | I2C i2c(PB_9, PB_8); |
pmic | 45:5e1dd4117ed2 | 70 | SensorBar sensor_bar(i2c, 0.1175f); |
pmic | 45:5e1dd4117ed2 | 71 | |
pmic | 45:5e1dd4117ed2 | 72 | // robot kinematics |
pmic | 45:5e1dd4117ed2 | 73 | const float r_wheel = 0.0358f / 2.0f; |
pmic | 45:5e1dd4117ed2 | 74 | const float L_wheel = 0.143f; |
pmic | 45:5e1dd4117ed2 | 75 | Eigen::Matrix2f Cwheel2robot; // transform wheel to robot |
pmic | 45:5e1dd4117ed2 | 76 | Eigen::Matrix2f Crobot2wheel; // transform robot to wheel |
pmic | 45:5e1dd4117ed2 | 77 | Eigen::Vector2f robot_coord; // contains v and w (robot translational and rotational velocities) |
pmic | 45:5e1dd4117ed2 | 78 | Eigen::Vector2f wheel_speed; // w1 w2 (wheel speed) |
pmic | 46:fd580fa68618 | 79 | Cwheel2robot << r_wheel / 2.0f, r_wheel / 2.0f, |
pmic | 46:fd580fa68618 | 80 | r_wheel / L_wheel, -r_wheel / L_wheel; |
pmic | 45:5e1dd4117ed2 | 81 | Crobot2wheel << 1.0f / r_wheel, L_wheel / (2.0f * r_wheel), |
pmic | 46:fd580fa68618 | 82 | 1.0f / r_wheel, -L_wheel / (2.0f * r_wheel); |
pmic | 45:5e1dd4117ed2 | 83 | robot_coord.setZero(); |
pmic | 45:5e1dd4117ed2 | 84 | wheel_speed.setZero(); |
pmic | 46:fd580fa68618 | 85 | |
pmic | 24:86f1a63e35a0 | 86 | // attach button fall and rise functions to user button object |
pmic | 24:86f1a63e35a0 | 87 | user_button.fall(&user_button_pressed_fcn); |
pmic | 24:86f1a63e35a0 | 88 | user_button.rise(&user_button_released_fcn); |
pmic | 17:c19b471f05cb | 89 | |
pmic | 29:d6f1ccf42a31 | 90 | // start timer |
pmic | 24:86f1a63e35a0 | 91 | main_task_timer.start(); |
pmic | 6:e1fa1a2d7483 | 92 | |
pmic | 38:6d11788e14c0 | 93 | // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled |
pmic | 38:6d11788e14c0 | 94 | enable_motors = 1; |
pmic | 6:e1fa1a2d7483 | 95 | |
pmic | 46:fd580fa68618 | 96 | |
pmic | 46:fd580fa68618 | 97 | Motion motion; |
pmic | 46:fd580fa68618 | 98 | motion.setProfileVelocity(10.0f); |
pmic | 46:fd580fa68618 | 99 | motion.setProfileAcceleration(5.0f); |
pmic | 46:fd580fa68618 | 100 | motion.setProfileDeceleration(5.0f); |
pmic | 46:fd580fa68618 | 101 | |
pmic | 24:86f1a63e35a0 | 102 | while (true) { // this loop will run forever |
pmic | 6:e1fa1a2d7483 | 103 | |
pmic | 24:86f1a63e35a0 | 104 | main_task_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 105 | |
pmic | 24:86f1a63e35a0 | 106 | if (do_execute_main_task) { |
pmic | 34:702246639f02 | 107 | |
pmic | 42:b54a4f294aa9 | 108 | // read SensorBar |
pmic | 43:5648b7083fe5 | 109 | 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 | 110 | if (sensor_bar.isAnyLedActive()) { |
pmic | 42:b54a4f294aa9 | 111 | sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad(); |
pmic | 42:b54a4f294aa9 | 112 | } |
pmic | 42:b54a4f294aa9 | 113 | |
pmic | 45:5e1dd4117ed2 | 114 | const static float Kp = 2.0f; |
pmic | 45:5e1dd4117ed2 | 115 | const static float Kp_nl = 17.0f; |
pmic | 45:5e1dd4117ed2 | 116 | robot_coord(1) = ang_cntrl_fcn(Kp, Kp_nl, sensor_bar_avgAngleRad); |
pmic | 42:b54a4f294aa9 | 117 | |
pmic | 43:5648b7083fe5 | 118 | // nonlinear controllers version 1 (whatever came to my mind) |
pmic | 43:5648b7083fe5 | 119 | /* |
pmic | 43:5648b7083fe5 | 120 | const static float vel_max = 0.3374f; //0.10f; |
pmic | 43:5648b7083fe5 | 121 | const static float vel_min = 0.00f; //0.02f; |
pmic | 43:5648b7083fe5 | 122 | const static float ang_max = 27.0f * M_PI / 180.0f; |
pmic | 45:5e1dd4117ed2 | 123 | robot_coord(0) = vel_cntrl_v1_fcn(vel_max, vel_min, ang_max, sensor_bar_avgAngleRad); |
pmic | 43:5648b7083fe5 | 124 | */ |
pmic | 43:5648b7083fe5 | 125 | |
pmic | 43:5648b7083fe5 | 126 | // nonlinear controllers version 2 (one wheel always at full speed controller) |
pmic | 43:5648b7083fe5 | 127 | ///* |
pmic | 45:5e1dd4117ed2 | 128 | const static float wheel_speed_max = max_voltage * kn / 60.0f * 2.0f * M_PI; |
pmic | 45:5e1dd4117ed2 | 129 | const static float b = L_wheel / (2.0f * r_wheel); |
pmic | 45:5e1dd4117ed2 | 130 | robot_coord(0) = vel_cntrl_v2_fcn(wheel_speed_max, b, robot_coord(1), Cwheel2robot); |
pmic | 43:5648b7083fe5 | 131 | //*/ |
pmic | 43:5648b7083fe5 | 132 | |
pmic | 42:b54a4f294aa9 | 133 | // transform to robot coordinates |
pmic | 42:b54a4f294aa9 | 134 | wheel_speed = Crobot2wheel * robot_coord; |
pmic | 42:b54a4f294aa9 | 135 | |
pmic | 38:6d11788e14c0 | 136 | // read analog input |
pmic | 38:6d11788e14c0 | 137 | ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; |
pmic | 38:6d11788e14c0 | 138 | |
pmic | 46:fd580fa68618 | 139 | //speedControllers[0]->setDesiredSpeedRPS(wheel_speed(0) / (2.0f * M_PI)); // set a desired speed for speed controlled dc motors M1 |
pmic | 46:fd580fa68618 | 140 | //speedControllers[1]->setDesiredSpeedRPS(wheel_speed(1) / (2.0f * M_PI)); // set a desired speed for speed controlled dc motors M2 |
pmic | 46:fd580fa68618 | 141 | speedControllers[0]->setDesiredSpeedRPS(speed); |
pmic | 46:fd580fa68618 | 142 | //speedControllers[1]->setDesiredSpeedRPS(speed); |
pmic | 46:fd580fa68618 | 143 | positionController->setDesiredRotation(speed); |
pmic | 34:702246639f02 | 144 | |
pmic | 1:93d997d6b232 | 145 | } else { |
pmic | 6:e1fa1a2d7483 | 146 | |
pmic | 38:6d11788e14c0 | 147 | ir_distance_mV = 0.0f; |
pmic | 38:6d11788e14c0 | 148 | |
pmic | 46:fd580fa68618 | 149 | speedControllers[0]->setDesiredSpeedRPS(speed); |
pmic | 46:fd580fa68618 | 150 | //speedControllers[1]->setDesiredSpeedRPS(speed); |
pmic | 46:fd580fa68618 | 151 | positionController->setDesiredRotation(speed); |
pmic | 46:fd580fa68618 | 152 | |
pmic | 46:fd580fa68618 | 153 | |
pmic | 46:fd580fa68618 | 154 | |
pmic | 33:cff70742569d | 155 | } |
pmic | 6:e1fa1a2d7483 | 156 | |
pmic | 24:86f1a63e35a0 | 157 | user_led = !user_led; |
pmic | 24:86f1a63e35a0 | 158 | |
pmic | 24:86f1a63e35a0 | 159 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 46:fd580fa68618 | 160 | //printf("%d, %d\r\n", sensor_bar_raw_value_time_ms, sensor_bar_position_time_ms); |
pmic | 46:fd580fa68618 | 161 | //printf("%f, %f\r\n", speedControllers[0]->getSpeedRPS(), speedControllers[1]->getSpeedRPS()); |
pmic | 46:fd580fa68618 | 162 | |
pmic | 46:fd580fa68618 | 163 | motion.incrementToPosition(motionspeed, static_cast<float>(main_task_period_ms) * 1.0e-3f); |
pmic | 46:fd580fa68618 | 164 | float motionpositionactual = motion.getPosition(); |
pmic | 46:fd580fa68618 | 165 | float motionspeedactual = motion.getVelocity(); |
pmic | 46:fd580fa68618 | 166 | printf("%f, %f, %f, %f, %f\r\n", speedControllers[0]->getSpeedRPS(), positionController->getRotation(), positionController->getSpeedRPS(), motionpositionactual, motionspeedactual); |
pmic | 17:c19b471f05cb | 167 | |
pmic | 24:86f1a63e35a0 | 168 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 24:86f1a63e35a0 | 169 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 24:86f1a63e35a0 | 170 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
pmic | 1:93d997d6b232 | 171 | } |
pmic | 1:93d997d6b232 | 172 | } |
pmic | 6:e1fa1a2d7483 | 173 | |
pmic | 24:86f1a63e35a0 | 174 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 175 | { |
pmic | 26:28693b369945 | 176 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 177 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 178 | } |
pmic | 6:e1fa1a2d7483 | 179 | |
pmic | 24:86f1a63e35a0 | 180 | void user_button_released_fcn() |
pmic | 6:e1fa1a2d7483 | 181 | { |
pmic | 24:86f1a63e35a0 | 182 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 183 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 184 | user_button_timer.stop(); |
pmic | 24:86f1a63e35a0 | 185 | if (user_button_elapsed_time_ms > 200) { |
pmic | 24:86f1a63e35a0 | 186 | do_execute_main_task = !do_execute_main_task; |
pmic | 46:fd580fa68618 | 187 | if(do_execute_main_task) { |
pmic | 46:fd580fa68618 | 188 | motionspeed = 12.0f; |
pmic | 46:fd580fa68618 | 189 | speed = 2.7f; |
pmic | 46:fd580fa68618 | 190 | } else { |
pmic | 46:fd580fa68618 | 191 | motionspeed = -12.0f; |
pmic | 46:fd580fa68618 | 192 | speed = -2.7f; |
pmic | 46:fd580fa68618 | 193 | } |
pmic | 8:9bb806a7f585 | 194 | } |
pmic | 42:b54a4f294aa9 | 195 | } |
pmic | 42:b54a4f294aa9 | 196 | |
pmic | 45:5e1dd4117ed2 | 197 | float ang_cntrl_fcn(const float& Kp, const float& Kp_nl, const float& angle) |
pmic | 43:5648b7083fe5 | 198 | { |
pmic | 45:5e1dd4117ed2 | 199 | static float retval = 0.0f; |
pmic | 43:5648b7083fe5 | 200 | if (angle > 0) { |
pmic | 43:5648b7083fe5 | 201 | retval = Kp * angle + Kp_nl * angle * angle; |
pmic | 45:5e1dd4117ed2 | 202 | } else if (angle <= 0) { |
pmic | 43:5648b7083fe5 | 203 | retval = Kp * angle - Kp_nl * angle * angle; |
pmic | 43:5648b7083fe5 | 204 | } |
pmic | 43:5648b7083fe5 | 205 | return retval; |
pmic | 43:5648b7083fe5 | 206 | } |
pmic | 43:5648b7083fe5 | 207 | |
pmic | 45:5e1dd4117ed2 | 208 | float vel_cntrl_v1_fcn(const float& vel_max, const float& vel_min, const float& ang_max, const float& angle) |
pmic | 42:b54a4f294aa9 | 209 | { |
pmic | 42:b54a4f294aa9 | 210 | const static float gain = (vel_min - vel_max) / ang_max; |
pmic | 42:b54a4f294aa9 | 211 | const static float offset = vel_max; |
pmic | 43:5648b7083fe5 | 212 | return gain * fabs(angle) + offset; |
pmic | 42:b54a4f294aa9 | 213 | } |
pmic | 42:b54a4f294aa9 | 214 | |
pmic | 45:5e1dd4117ed2 | 215 | float vel_cntrl_v2_fcn(const float& wheel_speed_max, const float& b, const float& robot_omega, const Eigen::Matrix2f& Cwheel2robot) |
pmic | 42:b54a4f294aa9 | 216 | { |
pmic | 43:5648b7083fe5 | 217 | static Eigen::Matrix<float, 2, 2> _wheel_speed; |
pmic | 43:5648b7083fe5 | 218 | static Eigen::Matrix<float, 2, 2> _robot_coord; |
pmic | 43:5648b7083fe5 | 219 | if (robot_omega > 0) { |
pmic | 43:5648b7083fe5 | 220 | _wheel_speed(0) = wheel_speed_max; |
pmic | 43:5648b7083fe5 | 221 | _wheel_speed(1) = wheel_speed_max - 2*b*robot_omega; |
pmic | 43:5648b7083fe5 | 222 | } else { |
pmic | 43:5648b7083fe5 | 223 | _wheel_speed(0) = wheel_speed_max + 2*b*robot_omega; |
pmic | 43:5648b7083fe5 | 224 | _wheel_speed(1) = wheel_speed_max; |
pmic | 42:b54a4f294aa9 | 225 | } |
pmic | 43:5648b7083fe5 | 226 | _robot_coord = Cwheel2robot * _wheel_speed; |
pmic | 43:5648b7083fe5 | 227 | return _robot_coord(0); |
pmic | 6:e1fa1a2d7483 | 228 | } |