robot code for summer school
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
Robot_Library/robot.cpp@68:61aad0993a29, 2022-05-30 (annotated)
- Committer:
- eversonrosed
- Date:
- Mon May 30 11:13:44 2022 +0200
- Revision:
- 68:61aad0993a29
- Parent:
- 67:90f378806cbe
- Child:
- 70:0e9e3c6223d1
delete error_scale
Who changed what in which revision?
User | Revision | Line number | New 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" |
seas726 | 56:3fce0a9bb6df | 5 | #include <cstdint> |
seas726 | 49:7da71f479dac | 6 | #include <cstdio> |
seas726 | 49:7da71f479dac | 7 | |
eversonrosed | 60:85a40e69ced6 | 8 | Robot::Robot() |
eversonrosed | 60:85a40e69ced6 | 9 | : dist(PB_1), // initialize all of the physical ports |
eversonrosed | 60:85a40e69ced6 | 10 | bit0(PH_1), bit1(PC_2), bit2(PC_3), |
eversonrosed | 60:85a40e69ced6 | 11 | ir_sensor_0(dist, bit0, bit1, bit2, 0), // one IR sensor |
eversonrosed | 60:85a40e69ced6 | 12 | i2c2(PB_9, PB_8), // line sensor |
eversonrosed | 60:85a40e69ced6 | 13 | line_sensor(i2c2), pwm_M1(PA_9), // motors + encoders |
eversonrosed | 60:85a40e69ced6 | 14 | pwm_M2(PA_8), encoder_M1(PA_6, PC_7), encoder_M2(PB_6, PB_7) { |
eversonrosed | 60:85a40e69ced6 | 15 | // initialize all variables |
eversonrosed | 60:85a40e69ced6 | 16 | wheel_to_robot << -WHEEL_RADIUS / 2.0f, WHEEL_RADIUS / 2.0f, |
eversonrosed | 60:85a40e69ced6 | 17 | -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS, |
eversonrosed | 60:85a40e69ced6 | 18 | -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS; // transformation matrix |
eversonrosed | 60:85a40e69ced6 | 19 | robot_to_wheel = wheel_to_robot.inverse(); |
seas726 | 56:3fce0a9bb6df | 20 | |
eversonrosed | 60:85a40e69ced6 | 21 | robot_speed_desired.setZero(); // zero out all speeds |
eversonrosed | 60:85a40e69ced6 | 22 | wheel_speed_desired.setZero(); |
eversonrosed | 60:85a40e69ced6 | 23 | wheel_speed_smooth.setZero(); |
eversonrosed | 60:85a40e69ced6 | 24 | robot_speed_actual.setZero(); |
eversonrosed | 60:85a40e69ced6 | 25 | wheel_speed_actual.setZero(); |
eversonrosed | 60:85a40e69ced6 | 26 | |
eversonrosed | 60:85a40e69ced6 | 27 | // MOTORS + MOTION |
seas726 | 49:7da71f479dac | 28 | |
eversonrosed | 60:85a40e69ced6 | 29 | // TRAJECTORY PLANNERS |
eversonrosed | 60:85a40e69ced6 | 30 | trajectoryPlanners[0] = new Motion(); |
eversonrosed | 60:85a40e69ced6 | 31 | trajectoryPlanners[1] = new Motion(); |
seas726 | 49:7da71f479dac | 32 | |
eversonrosed | 60:85a40e69ced6 | 33 | trajectoryPlanners[0]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f); |
eversonrosed | 60:85a40e69ced6 | 34 | trajectoryPlanners[1]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f); |
eversonrosed | 60:85a40e69ced6 | 35 | trajectoryPlanners[0]->setProfileAcceleration(10.0f); |
eversonrosed | 60:85a40e69ced6 | 36 | trajectoryPlanners[1]->setProfileAcceleration(10.0f); |
eversonrosed | 60:85a40e69ced6 | 37 | trajectoryPlanners[0]->setProfileDeceleration(10.0f); |
eversonrosed | 60:85a40e69ced6 | 38 | trajectoryPlanners[1]->setProfileDeceleration(10.0f); |
seas726 | 49:7da71f479dac | 39 | |
eversonrosed | 60:85a40e69ced6 | 40 | // SPEED CONTROLLERS |
eversonrosed | 60:85a40e69ced6 | 41 | speedControllers[0] = new SpeedController( |
eversonrosed | 60:85a40e69ced6 | 42 | COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M1, encoder_M1); |
eversonrosed | 60:85a40e69ced6 | 43 | speedControllers[1] = new SpeedController( |
eversonrosed | 60:85a40e69ced6 | 44 | COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M2, encoder_M2); |
seas726 | 49:7da71f479dac | 45 | |
eversonrosed | 60:85a40e69ced6 | 46 | speedControllers[0]->setSpeedCntrlGain( |
eversonrosed | 60:85a40e69ced6 | 47 | 0.04f); // adjust speed controller gains |
eversonrosed | 60:85a40e69ced6 | 48 | speedControllers[1]->setSpeedCntrlGain(0.04f); |
eversonrosed | 60:85a40e69ced6 | 49 | speedControllers[0]->setMaxAccelerationRPS( |
eversonrosed | 60:85a40e69ced6 | 50 | 999.0f); // adjust max. acceleration for smooth movement |
eversonrosed | 60:85a40e69ced6 | 51 | speedControllers[1]->setMaxAccelerationRPS(999.0f); |
seas726 | 49:7da71f479dac | 52 | } |
seas726 | 49:7da71f479dac | 53 | |
eversonrosed | 60:85a40e69ced6 | 54 | Robot::~Robot() { |
eversonrosed | 60:85a40e69ced6 | 55 | delete trajectoryPlanners[0]; |
eversonrosed | 60:85a40e69ced6 | 56 | delete trajectoryPlanners[1]; |
eversonrosed | 60:85a40e69ced6 | 57 | delete speedControllers[0]; |
eversonrosed | 60:85a40e69ced6 | 58 | delete speedControllers[1]; |
eversonrosed | 54:b442660523df | 59 | } |
eversonrosed | 54:b442660523df | 60 | |
seas726 | 49:7da71f479dac | 61 | void Robot::Update() { |
seas726 | 49:7da71f479dac | 62 | |
eversonrosed | 60:85a40e69ced6 | 63 | controller.Update(); |
eversonrosed | 67:90f378806cbe | 64 | |
eversonrosed | 67:90f378806cbe | 65 | wheel_speed_actual << speedControllers[0]->getSpeedRPS(), speedControllers[1]->getSpeedRPS(); |
eversonrosed | 67:90f378806cbe | 66 | robot_speed_actual = wheel_to_robot * wheel_speed_actual; |
seas726 | 49:7da71f479dac | 67 | |
eversonrosed | 60:85a40e69ced6 | 68 | printf("STATE: %d \r\n", state); |
eversonrosed | 60:85a40e69ced6 | 69 | switch (state) { |
eversonrosed | 60:85a40e69ced6 | 70 | case INITIAL: |
eversonrosed | 60:85a40e69ced6 | 71 | Initial(); |
eversonrosed | 60:85a40e69ced6 | 72 | break; |
eversonrosed | 60:85a40e69ced6 | 73 | case IDLE: |
eversonrosed | 60:85a40e69ced6 | 74 | Idle(); |
eversonrosed | 60:85a40e69ced6 | 75 | break; |
eversonrosed | 60:85a40e69ced6 | 76 | case FOLLOWING_LINE: |
eversonrosed | 60:85a40e69ced6 | 77 | FollowingLine(); |
eversonrosed | 60:85a40e69ced6 | 78 | break; |
eversonrosed | 60:85a40e69ced6 | 79 | case RIGHT_TURN_90: |
eversonrosed | 60:85a40e69ced6 | 80 | RightTurn_90(); |
eversonrosed | 60:85a40e69ced6 | 81 | break; |
eversonrosed | 60:85a40e69ced6 | 82 | case LEFT_TURN_90: |
eversonrosed | 60:85a40e69ced6 | 83 | LeftTurn_90(); |
eversonrosed | 60:85a40e69ced6 | 84 | break; |
eversonrosed | 60:85a40e69ced6 | 85 | default: |
eversonrosed | 60:85a40e69ced6 | 86 | state = IDLE; // on default, stop the car |
eversonrosed | 60:85a40e69ced6 | 87 | } |
eversonrosed | 67:90f378806cbe | 88 | |
eversonrosed | 67:90f378806cbe | 89 | wheel_speed_desired = robot_to_wheel * robot_speed_desired; |
eversonrosed | 67:90f378806cbe | 90 | |
eversonrosed | 67:90f378806cbe | 91 | // smooth desired wheel_speeds |
eversonrosed | 67:90f378806cbe | 92 | trajectoryPlanners[0]->incrementToVelocity(wheel_speed_desired(0) / (2.0f * M_PI), controller.Period()); |
eversonrosed | 67:90f378806cbe | 93 | trajectoryPlanners[1]->incrementToVelocity(wheel_speed_desired(1) / (2.0f * M_PI), controller.Period()); |
eversonrosed | 67:90f378806cbe | 94 | wheel_speed_smooth << trajectoryPlanners[0]->getVelocity(), trajectoryPlanners[1]->getVelocity(); |
eversonrosed | 67:90f378806cbe | 95 | |
eversonrosed | 67:90f378806cbe | 96 | // command speedController objects |
eversonrosed | 67:90f378806cbe | 97 | speedControllers[0]->setDesiredSpeedRPS(wheel_speed_smooth(0)); |
eversonrosed | 67:90f378806cbe | 98 | speedControllers[1]->setDesiredSpeedRPS(wheel_speed_smooth(1)); |
seas726 | 49:7da71f479dac | 99 | } |
seas726 | 49:7da71f479dac | 100 | |
eversonrosed | 60:85a40e69ced6 | 101 | void Robot::Initial() { |
eversonrosed | 60:85a40e69ced6 | 102 | printf("Initial State\n"); // TODO: REMOVE PRINT |
eversonrosed | 60:85a40e69ced6 | 103 | // initialize the robot. |
eversonrosed | 60:85a40e69ced6 | 104 | // enable_motors = 1; |
eversonrosed | 60:85a40e69ced6 | 105 | // motors_enabled = false; |
eversonrosed | 60:85a40e69ced6 | 106 | robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero |
eversonrosed | 60:85a40e69ced6 | 107 | robot_speed_desired(1) = 0.0f; |
seas726 | 49:7da71f479dac | 108 | |
eversonrosed | 60:85a40e69ced6 | 109 | if (controller.GetTurnedOn()) // check to see if blue button is toggled |
eversonrosed | 60:85a40e69ced6 | 110 | { |
eversonrosed | 60:85a40e69ced6 | 111 | state = FOLLOWING_LINE; |
eversonrosed | 60:85a40e69ced6 | 112 | } |
seas726 | 49:7da71f479dac | 113 | } |
seas726 | 49:7da71f479dac | 114 | |
eversonrosed | 60:85a40e69ced6 | 115 | void Robot::Idle() { |
eversonrosed | 60:85a40e69ced6 | 116 | printf("Idle\n"); // TODO: REMOVE PRINT |
eversonrosed | 60:85a40e69ced6 | 117 | robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero |
eversonrosed | 60:85a40e69ced6 | 118 | robot_speed_desired(1) = 0.0f; |
seas726 | 49:7da71f479dac | 119 | } |
seas726 | 49:7da71f479dac | 120 | |
seas726 | 56:3fce0a9bb6df | 121 | void Robot::FollowingLine() // Updates once per cycle. |
seas726 | 49:7da71f479dac | 122 | { |
eversonrosed | 60:85a40e69ced6 | 123 | if (!controller.GetTurnedOn()) { |
eversonrosed | 60:85a40e69ced6 | 124 | state = IDLE; |
eversonrosed | 60:85a40e69ced6 | 125 | return; |
eversonrosed | 60:85a40e69ced6 | 126 | } |
seas726 | 56:3fce0a9bb6df | 127 | |
eversonrosed | 60:85a40e69ced6 | 128 | printf("FollowingLine\n"); // TODO: REMOVE PRINT |
eversonrosed | 60:85a40e69ced6 | 129 | printf("%d", line_sensor.getRaw()); // print raw line sensor data |
eversonrosed | 60:85a40e69ced6 | 130 | uint8_t binary_sensor_data = |
eversonrosed | 60:85a40e69ced6 | 131 | line_sensor.getRaw(); // convert line sensor data into binary |
eversonrosed | 60:85a40e69ced6 | 132 | // representation of it |
eversonrosed | 60:85a40e69ced6 | 133 | |
eversonrosed | 60:85a40e69ced6 | 134 | // if(IsSharpTurn(binary_sensor_data)) { return; } // check if the sensor |
eversonrosed | 60:85a40e69ced6 | 135 | // reads in any sharp turns. if so, exit the PID movement and turn sharply. |
eversonrosed | 60:85a40e69ced6 | 136 | // first test PID movement. it is possible that PID movement works just as |
eversonrosed | 60:85a40e69ced6 | 137 | // well. |
eversonrosed | 60:85a40e69ced6 | 138 | |
eversonrosed | 60:85a40e69ced6 | 139 | PID_Move(binary_sensor_data); // move the robot smoothly with error |
eversonrosed | 60:85a40e69ced6 | 140 | // calculation and stuff? |
seas726 | 56:3fce0a9bb6df | 141 | } |
seas726 | 56:3fce0a9bb6df | 142 | |
eversonrosed | 60:85a40e69ced6 | 143 | void Robot::RightTurn_90() { |
eversonrosed | 60:85a40e69ced6 | 144 | // count encoder values and turn until the motor has rotated ~ 90 degrees |
eversonrosed | 60:85a40e69ced6 | 145 | // im actually not sure if we need this, try testing with just the PID system |
eversonrosed | 60:85a40e69ced6 | 146 | // first |
seas726 | 56:3fce0a9bb6df | 147 | } |
seas726 | 56:3fce0a9bb6df | 148 | |
eversonrosed | 60:85a40e69ced6 | 149 | void Robot::LeftTurn_90() { |
eversonrosed | 60:85a40e69ced6 | 150 | // count encoder values and turn until the motor has rotated ~ 90 degrees |
eversonrosed | 60:85a40e69ced6 | 151 | // im actually not sure if we need this, try testing with just the PID system |
eversonrosed | 60:85a40e69ced6 | 152 | // first |
seas726 | 56:3fce0a9bb6df | 153 | } |
seas726 | 56:3fce0a9bb6df | 154 | |
eversonrosed | 60:85a40e69ced6 | 155 | void Robot::PID_Move(std::uint8_t s_binary) // for following smooth lines ONLY |
seas726 | 56:3fce0a9bb6df | 156 | { |
seas726 | 56:3fce0a9bb6df | 157 | |
eversonrosed | 60:85a40e69ced6 | 158 | float errval = 0; |
seas726 | 56:3fce0a9bb6df | 159 | |
eversonrosed | 60:85a40e69ced6 | 160 | if (s_binary & 0b00000001) |
eversonrosed | 60:85a40e69ced6 | 161 | errval += error_angles[3]; |
eversonrosed | 60:85a40e69ced6 | 162 | else if (s_binary & 0b00000010) |
eversonrosed | 60:85a40e69ced6 | 163 | errval += error_angles[2]; |
eversonrosed | 60:85a40e69ced6 | 164 | else if (s_binary & 0b00000100) |
eversonrosed | 60:85a40e69ced6 | 165 | errval += error_angles[1]; |
eversonrosed | 60:85a40e69ced6 | 166 | else if (s_binary & 0b00001000) |
eversonrosed | 60:85a40e69ced6 | 167 | errval += error_angles[0]; |
seas726 | 49:7da71f479dac | 168 | |
eversonrosed | 60:85a40e69ced6 | 169 | if (s_binary & 0b10000000) |
eversonrosed | 60:85a40e69ced6 | 170 | errval -= error_angles[3]; |
eversonrosed | 60:85a40e69ced6 | 171 | else if (s_binary & 0b01000000) |
eversonrosed | 60:85a40e69ced6 | 172 | errval -= error_angles[2]; |
eversonrosed | 60:85a40e69ced6 | 173 | else if (s_binary & 0b00100000) |
eversonrosed | 60:85a40e69ced6 | 174 | errval -= error_angles[1]; |
eversonrosed | 60:85a40e69ced6 | 175 | else if (s_binary & 0b00010000) |
eversonrosed | 60:85a40e69ced6 | 176 | errval -= error_angles[0]; |
seas726 | 56:3fce0a9bb6df | 177 | |
eversonrosed | 60:85a40e69ced6 | 178 | float dt = controller.Period(); |
eversonrosed | 60:85a40e69ced6 | 179 | |
eversonrosed | 60:85a40e69ced6 | 180 | integral_error += dt * errval; |
seas726 | 49:7da71f479dac | 181 | |
eversonrosed | 60:85a40e69ced6 | 182 | float integral_term = integral_error / t_int; |
eversonrosed | 60:85a40e69ced6 | 183 | float derivative_term = t_deriv * (errval - previous_error_value) / dt; |
seas726 | 49:7da71f479dac | 184 | |
eversonrosed | 68:61aad0993a29 | 185 | float control_input = k_prop * (errval + integral_term + derivative_term); |
eversonrosed | 60:85a40e69ced6 | 186 | |
eversonrosed | 60:85a40e69ced6 | 187 | robot_speed_desired(0) = TRANSLATIONAL_VELOCITY; |
eversonrosed | 60:85a40e69ced6 | 188 | robot_speed_desired(1) = ROTATIONAL_VELOCITY * control_input; |
eversonrosed | 60:85a40e69ced6 | 189 | previous_error_value = errval; |
eversonrosed | 60:85a40e69ced6 | 190 | |
eversonrosed | 60:85a40e69ced6 | 191 | // Delay total_error/2. not exactly sure why. |
seas726 | 56:3fce0a9bb6df | 192 | } |
seas726 | 56:3fce0a9bb6df | 193 | |
eversonrosed | 60:85a40e69ced6 | 194 | bool Robot::IsSharpTurn(int binary_sensor_data) { |
eversonrosed | 60:85a40e69ced6 | 195 | return binary_sensor_data & 0b11110000 || binary_sensor_data & 0b00001111; |
seas726 | 56:3fce0a9bb6df | 196 | } |
seas726 | 56:3fce0a9bb6df | 197 | |
eversonrosed | 60:85a40e69ced6 | 198 | void Robot::PID_Delay(int ms) { |
eversonrosed | 60:85a40e69ced6 | 199 | // add in delay ? |
eversonrosed | 60:85a40e69ced6 | 200 | // implement |
seas726 | 56:3fce0a9bb6df | 201 | } |