robot code for summer school

Dependencies:   PM2_Libary Eigen

Fork of PM2_Example_Summer_School by Alex Hawkins

Committer:
eversonrosed
Date:
Wed Jun 01 16:18:25 2022 +0200
Revision:
78:d53f1d68ca65
Parent:
77:19cf9072bc22
have we gone anywhere in 24 hours?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seas726 49:7da71f479dac 1 #include "robot.h"
seas726 49:7da71f479dac 2 #include "EncoderCounter.h"
seas726 49:7da71f479dac 3 #include "PeripheralNames.h"
seas726 49:7da71f479dac 4 #include "PinNames.h"
eversonrosed 73:667d568da72a 5 #include <array>
seas726 56:3fce0a9bb6df 6 #include <cstdint>
seas726 49:7da71f479dac 7 #include <cstdio>
eversonrosed 73:667d568da72a 8 #include <cmath>
eversonrosed 73:667d568da72a 9
eversonrosed 74:76c7a805f63d 10 static float ComputeAngle(std::uint8_t raw, float default_val);
eversonrosed 74:76c7a805f63d 11 static float Multiplier(float angle);
seas726 49:7da71f479dac 12
eversonrosed 60:85a40e69ced6 13 Robot::Robot()
eversonrosed 60:85a40e69ced6 14 : dist(PB_1), // initialize all of the physical ports
eversonrosed 60:85a40e69ced6 15 bit0(PH_1), bit1(PC_2), bit2(PC_3),
eversonrosed 60:85a40e69ced6 16 ir_sensor_0(dist, bit0, bit1, bit2, 0), // one IR sensor
eversonrosed 60:85a40e69ced6 17 i2c2(PB_9, PB_8), // line sensor
eversonrosed 73:667d568da72a 18 line_sensor(i2c2), enable_motors(PB_15), pwm_M1(PB_13),
eversonrosed 73:667d568da72a 19 pwm_M2(PA_9), // motors + encoders
eversonrosed 78:d53f1d68ca65 20 encoder_M1(PA_6, PC_7), encoder_M2(PB_6, PB_7)
eversonrosed 78:d53f1d68ca65 21 // theta(0.0f), target_theta(0.0f),
eversonrosed 78:d53f1d68ca65 22 // robot_x(0.0f), target_x(0.0f),
eversonrosed 78:d53f1d68ca65 23 // robot_y(0.0f), target_y(0.0f)
eversonrosed 78:d53f1d68ca65 24 {
eversonrosed 60:85a40e69ced6 25 // initialize all variables
eversonrosed 76:2302f2b51e63 26 wheel_to_robot << WHEEL_RADIUS / 2.0f, -WHEEL_RADIUS / 2.0f,
eversonrosed 60:85a40e69ced6 27 -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS,
eversonrosed 60:85a40e69ced6 28 -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS; // transformation matrix
eversonrosed 60:85a40e69ced6 29 robot_to_wheel = wheel_to_robot.inverse();
seas726 56:3fce0a9bb6df 30
eversonrosed 60:85a40e69ced6 31 robot_speed_desired.setZero(); // zero out all speeds
eversonrosed 60:85a40e69ced6 32 wheel_speed_desired.setZero();
eversonrosed 60:85a40e69ced6 33 wheel_speed_smooth.setZero();
eversonrosed 60:85a40e69ced6 34 robot_speed_actual.setZero();
eversonrosed 60:85a40e69ced6 35 wheel_speed_actual.setZero();
eversonrosed 60:85a40e69ced6 36
eversonrosed 60:85a40e69ced6 37 // MOTORS + MOTION
seas726 49:7da71f479dac 38
eversonrosed 60:85a40e69ced6 39 // TRAJECTORY PLANNERS
eversonrosed 60:85a40e69ced6 40 trajectoryPlanners[0] = new Motion();
eversonrosed 60:85a40e69ced6 41 trajectoryPlanners[1] = new Motion();
seas726 49:7da71f479dac 42
eversonrosed 60:85a40e69ced6 43 trajectoryPlanners[0]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f);
eversonrosed 60:85a40e69ced6 44 trajectoryPlanners[1]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f);
eversonrosed 60:85a40e69ced6 45 trajectoryPlanners[0]->setProfileAcceleration(10.0f);
eversonrosed 60:85a40e69ced6 46 trajectoryPlanners[1]->setProfileAcceleration(10.0f);
eversonrosed 60:85a40e69ced6 47 trajectoryPlanners[0]->setProfileDeceleration(10.0f);
eversonrosed 60:85a40e69ced6 48 trajectoryPlanners[1]->setProfileDeceleration(10.0f);
seas726 49:7da71f479dac 49
eversonrosed 60:85a40e69ced6 50 // SPEED CONTROLLERS
eversonrosed 60:85a40e69ced6 51 speedControllers[0] = new SpeedController(
eversonrosed 60:85a40e69ced6 52 COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M1, encoder_M1);
eversonrosed 60:85a40e69ced6 53 speedControllers[1] = new SpeedController(
eversonrosed 60:85a40e69ced6 54 COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M2, encoder_M2);
seas726 49:7da71f479dac 55
eversonrosed 60:85a40e69ced6 56 speedControllers[0]->setSpeedCntrlGain(
eversonrosed 60:85a40e69ced6 57 0.04f); // adjust speed controller gains
eversonrosed 60:85a40e69ced6 58 speedControllers[1]->setSpeedCntrlGain(0.04f);
eversonrosed 60:85a40e69ced6 59 speedControllers[0]->setMaxAccelerationRPS(
eversonrosed 60:85a40e69ced6 60 999.0f); // adjust max. acceleration for smooth movement
eversonrosed 60:85a40e69ced6 61 speedControllers[1]->setMaxAccelerationRPS(999.0f);
seas726 49:7da71f479dac 62 }
seas726 49:7da71f479dac 63
eversonrosed 60:85a40e69ced6 64 Robot::~Robot() {
eversonrosed 60:85a40e69ced6 65 delete trajectoryPlanners[0];
eversonrosed 60:85a40e69ced6 66 delete trajectoryPlanners[1];
eversonrosed 60:85a40e69ced6 67 delete speedControllers[0];
eversonrosed 60:85a40e69ced6 68 delete speedControllers[1];
eversonrosed 54:b442660523df 69 }
eversonrosed 54:b442660523df 70
seas726 49:7da71f479dac 71 void Robot::Update() {
seas726 49:7da71f479dac 72
eversonrosed 60:85a40e69ced6 73 controller.Update();
eversonrosed 73:667d568da72a 74
eversonrosed 73:667d568da72a 75 wheel_speed_actual << speedControllers[0]->getSpeedRPS(),
eversonrosed 73:667d568da72a 76 speedControllers[1]->getSpeedRPS();
eversonrosed 67:90f378806cbe 77 robot_speed_actual = wheel_to_robot * wheel_speed_actual;
seas726 49:7da71f479dac 78
eversonrosed 60:85a40e69ced6 79 switch (state) {
eversonrosed 73:667d568da72a 80 case INITIAL:
eversonrosed 73:667d568da72a 81 Initial();
eversonrosed 73:667d568da72a 82 break;
eversonrosed 73:667d568da72a 83 case IDLE:
eversonrosed 73:667d568da72a 84 Idle();
eversonrosed 73:667d568da72a 85 break;
eversonrosed 73:667d568da72a 86 case FOLLOWING_LINE:
eversonrosed 73:667d568da72a 87 FollowingLine();
eversonrosed 73:667d568da72a 88 break;
eversonrosed 73:667d568da72a 89 case AVOIDING_OBSTACLE:
eversonrosed 73:667d568da72a 90 AvoidObstacle();
eversonrosed 73:667d568da72a 91 break;
eversonrosed 73:667d568da72a 92 default:
eversonrosed 73:667d568da72a 93 state = IDLE; // on default, stop the car
eversonrosed 60:85a40e69ced6 94 }
eversonrosed 73:667d568da72a 95
eversonrosed 76:2302f2b51e63 96 if (!controller.GetTurnedOn()) {
eversonrosed 76:2302f2b51e63 97 state = IDLE;
eversonrosed 76:2302f2b51e63 98 return;
eversonrosed 76:2302f2b51e63 99 }
eversonrosed 76:2302f2b51e63 100
eversonrosed 67:90f378806cbe 101 wheel_speed_desired = robot_to_wheel * robot_speed_desired;
eversonrosed 67:90f378806cbe 102
eversonrosed 67:90f378806cbe 103 // smooth desired wheel_speeds
eversonrosed 73:667d568da72a 104 trajectoryPlanners[0]->incrementToVelocity(
eversonrosed 73:667d568da72a 105 wheel_speed_desired(0) / (2.0f * M_PI), controller.Period());
eversonrosed 73:667d568da72a 106 trajectoryPlanners[1]->incrementToVelocity(
eversonrosed 73:667d568da72a 107 wheel_speed_desired(1) / (2.0f * M_PI), controller.Period());
eversonrosed 73:667d568da72a 108 wheel_speed_smooth << trajectoryPlanners[0]->getVelocity(),
eversonrosed 73:667d568da72a 109 trajectoryPlanners[1]->getVelocity();
eversonrosed 67:90f378806cbe 110
eversonrosed 67:90f378806cbe 111 // command speedController objects
eversonrosed 67:90f378806cbe 112 speedControllers[0]->setDesiredSpeedRPS(wheel_speed_smooth(0));
eversonrosed 67:90f378806cbe 113 speedControllers[1]->setDesiredSpeedRPS(wheel_speed_smooth(1));
seas726 49:7da71f479dac 114 }
seas726 49:7da71f479dac 115
eversonrosed 60:85a40e69ced6 116 void Robot::Initial() {
eversonrosed 60:85a40e69ced6 117 // initialize the robot.
eversonrosed 72:9325748d2d02 118 enable_motors = 0;
eversonrosed 60:85a40e69ced6 119 // motors_enabled = false;
eversonrosed 60:85a40e69ced6 120 robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero
eversonrosed 60:85a40e69ced6 121 robot_speed_desired(1) = 0.0f;
seas726 49:7da71f479dac 122
eversonrosed 60:85a40e69ced6 123 if (controller.GetTurnedOn()) // check to see if blue button is toggled
eversonrosed 60:85a40e69ced6 124 {
eversonrosed 72:9325748d2d02 125 enable_motors = 1;
eversonrosed 60:85a40e69ced6 126 state = FOLLOWING_LINE;
eversonrosed 60:85a40e69ced6 127 }
seas726 49:7da71f479dac 128 }
seas726 49:7da71f479dac 129
eversonrosed 60:85a40e69ced6 130 void Robot::Idle() {
eversonrosed 72:9325748d2d02 131 enable_motors = 0;
eversonrosed 60:85a40e69ced6 132 robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero
eversonrosed 60:85a40e69ced6 133 robot_speed_desired(1) = 0.0f;
eversonrosed 73:667d568da72a 134 if (controller.GetTurnedOn()) {
eversonrosed 72:9325748d2d02 135 enable_motors = 1;
eversonrosed 72:9325748d2d02 136 state = FOLLOWING_LINE;
eversonrosed 72:9325748d2d02 137 }
seas726 49:7da71f479dac 138 }
seas726 49:7da71f479dac 139
seas726 56:3fce0a9bb6df 140 void Robot::FollowingLine() // Updates once per cycle.
seas726 49:7da71f479dac 141 {
eversonrosed 73:667d568da72a 142 uint8_t raw = line_sensor.getRaw();
eversonrosed 78:d53f1d68ca65 143 if (raw == 0xFF) {
eversonrosed 78:d53f1d68ca65 144 raw = 0x80; // turn left at crossroads
eversonrosed 78:d53f1d68ca65 145 // raw = 0x01; // turn right at crossroads
eversonrosed 78:d53f1d68ca65 146 }
eversonrosed 74:76c7a805f63d 147 float angle = ComputeAngle(raw, previous_error_value);
eversonrosed 60:85a40e69ced6 148
eversonrosed 60:85a40e69ced6 149 // if(IsSharpTurn(binary_sensor_data)) { return; } // check if the sensor
eversonrosed 60:85a40e69ced6 150 // reads in any sharp turns. if so, exit the PID movement and turn sharply.
eversonrosed 60:85a40e69ced6 151 // first test PID movement. it is possible that PID movement works just as
eversonrosed 60:85a40e69ced6 152 // well.
eversonrosed 60:85a40e69ced6 153
eversonrosed 73:667d568da72a 154 PID_Move(angle); // move the robot smoothly with error
eversonrosed 73:667d568da72a 155 // calculation and stuff?
seas726 56:3fce0a9bb6df 156 }
seas726 56:3fce0a9bb6df 157
eversonrosed 73:667d568da72a 158 void Robot::AvoidObstacle() {
eversonrosed 70:0e9e3c6223d1 159 // TODO
eversonrosed 70:0e9e3c6223d1 160 }
eversonrosed 70:0e9e3c6223d1 161
eversonrosed 73:667d568da72a 162 void Robot::PID_Move(float errval) // for following smooth lines ONLY
seas726 56:3fce0a9bb6df 163 {
eversonrosed 60:85a40e69ced6 164 float dt = controller.Period();
eversonrosed 60:85a40e69ced6 165
eversonrosed 60:85a40e69ced6 166 integral_error += dt * errval;
seas726 49:7da71f479dac 167
eversonrosed 73:667d568da72a 168 float integral_term = k_int * integral_error;
eversonrosed 74:76c7a805f63d 169 float derivative_term = k_deriv * robot_speed_actual(1);
seas726 49:7da71f479dac 170
eversonrosed 73:667d568da72a 171 float control_input = k_prop * errval + integral_term + derivative_term;
eversonrosed 74:76c7a805f63d 172 float multiplier = sqrt(Multiplier(errval));
eversonrosed 60:85a40e69ced6 173
eversonrosed 74:76c7a805f63d 174 robot_speed_desired(0) = multiplier * TRANSLATIONAL_VELOCITY;
eversonrosed 76:2302f2b51e63 175 robot_speed_desired(1) = control_input * ROTATIONAL_VELOCITY / multiplier;
eversonrosed 60:85a40e69ced6 176 previous_error_value = errval;
eversonrosed 60:85a40e69ced6 177
eversonrosed 60:85a40e69ced6 178 // Delay total_error/2. not exactly sure why.
seas726 56:3fce0a9bb6df 179 }
seas726 56:3fce0a9bb6df 180
eversonrosed 60:85a40e69ced6 181 bool Robot::IsSharpTurn(int binary_sensor_data) {
eversonrosed 60:85a40e69ced6 182 return binary_sensor_data & 0b11110000 || binary_sensor_data & 0b00001111;
seas726 56:3fce0a9bb6df 183 }
seas726 56:3fce0a9bb6df 184
eversonrosed 60:85a40e69ced6 185 void Robot::PID_Delay(int ms) {
eversonrosed 60:85a40e69ced6 186 // add in delay ?
eversonrosed 60:85a40e69ced6 187 // implement
seas726 56:3fce0a9bb6df 188 }
eversonrosed 73:667d568da72a 189
eversonrosed 74:76c7a805f63d 190 static float ComputeAngle(std::uint8_t raw, float default_val) {
eversonrosed 73:667d568da72a 191 const int line_sensor_count = 8;
eversonrosed 73:667d568da72a 192 uint8_t line_sensor_bits_count;
eversonrosed 73:667d568da72a 193 std::array<float, line_sensor_count> line_sensor_weight{
eversonrosed 73:667d568da72a 194 3.5, 2.5, 1.5, 0.5, -0.5, -1.5, -2.5, -3.5}; // [m]
eversonrosed 73:667d568da72a 195 std::array<float, line_sensor_count> line_sensor_value{};
eversonrosed 73:667d568da72a 196 float line_sensor_position; // [m]
eversonrosed 73:667d568da72a 197 const float line_sensor_distance = 12.5e-3; // [m]
eversonrosed 73:667d568da72a 198
eversonrosed 73:667d568da72a 199 // fill bits into the sensor array
eversonrosed 73:667d568da72a 200 // also count the sensors detecting a black line
eversonrosed 73:667d568da72a 201 line_sensor_bits_count = 0;
eversonrosed 73:667d568da72a 202 for (int i = 0; i < line_sensor_count; i++) {
eversonrosed 73:667d568da72a 203 line_sensor_value[i] = (raw >> i) & 0x01;
eversonrosed 73:667d568da72a 204 line_sensor_bits_count += line_sensor_value[i];
eversonrosed 73:667d568da72a 205 }
eversonrosed 73:667d568da72a 206
eversonrosed 73:667d568da72a 207 // getting the line position as a weighted mean
eversonrosed 74:76c7a805f63d 208 if (line_sensor_bits_count == 8) {
eversonrosed 74:76c7a805f63d 209 return default_val;
eversonrosed 74:76c7a805f63d 210 } else if (line_sensor_bits_count > 0) {
eversonrosed 74:76c7a805f63d 211 for (int i = 0; i < line_sensor_count; i++) {
eversonrosed 73:667d568da72a 212 line_sensor_position += line_sensor_value[i] * line_sensor_weight[i] /
eversonrosed 73:667d568da72a 213 float(line_sensor_bits_count);
eversonrosed 73:667d568da72a 214 }
eversonrosed 76:2302f2b51e63 215 } else { // line_sensor_bits_count = 0
eversonrosed 76:2302f2b51e63 216 return default_val;
eversonrosed 73:667d568da72a 217 }
eversonrosed 73:667d568da72a 218 line_sensor_position *= line_sensor_distance;
eversonrosed 73:667d568da72a 219
eversonrosed 73:667d568da72a 220 float line_sensor_angle; // [rad]
eversonrosed 73:667d568da72a 221 const float line_sensor_center_distance = 40e-3; // [m] Set this parameter yourself
eversonrosed 73:667d568da72a 222
eversonrosed 73:667d568da72a 223 // getting an angle out of the position
eversonrosed 73:667d568da72a 224 line_sensor_angle = atan2(line_sensor_position, line_sensor_center_distance);
eversonrosed 73:667d568da72a 225 return line_sensor_angle;
eversonrosed 73:667d568da72a 226 }
eversonrosed 74:76c7a805f63d 227
eversonrosed 74:76c7a805f63d 228 static float Multiplier(float angle)
eversonrosed 74:76c7a805f63d 229 {
eversonrosed 76:2302f2b51e63 230 return 1 / (1 + 1.5 * angle * angle);
eversonrosed 74:76c7a805f63d 231 }