robot code for summer school

Dependencies:   PM2_Libary Eigen

Fork of PM2_Example_Summer_School by Alex Hawkins

Committer:
eversonrosed
Date:
Tue May 31 10:44:16 2022 +0200
Revision:
76:2302f2b51e63
Parent:
74:76c7a805f63d
Child:
77:19cf9072bc22
works even better

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