Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Control/drivecontrol.cpp@16:c26d8e007df5, 2017-05-20 (annotated)
- Committer:
- kolanery
- Date:
- Sat May 20 21:15:30 2017 +0000
- Revision:
- 16:c26d8e007df5
- Parent:
- 15:151e59899221
- Child:
- 17:043ed1d0196f
add drive one cell.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kolanery | 0:cb667de3a336 | 1 | #include "drivecontrol.h" |
kolanery | 0:cb667de3a336 | 2 | #include "Cell.h" |
kolanery | 0:cb667de3a336 | 3 | #include "ir_sensor.h" |
kolanery | 2:619b02232144 | 4 | #include "left_motor.h" |
kolanery | 2:619b02232144 | 5 | #include "right_motor.h" |
szh66 | 6:1bcfda49e146 | 6 | #include "pin_assignment.h" |
kolanery | 0:cb667de3a336 | 7 | |
szh66 | 6:1bcfda49e146 | 8 | IRSensor leftIR(PA_8, PC_5); |
szh66 | 6:1bcfda49e146 | 9 | IRSensor rightIR(PB_0, PA_4); |
szh66 | 8:4a32fc9ee939 | 10 | //Encoder rightEncoder(PA_1, PA_0); |
szh66 | 8:4a32fc9ee939 | 11 | //Encoder leftEncoder(PC_9, PC_8); |
kolanery | 2:619b02232144 | 12 | LeftMotor * leftMotor; |
kolanery | 2:619b02232144 | 13 | RightMotor * rightMotor; |
kolanery | 0:cb667de3a336 | 14 | Cell * curr_cell; |
szh66 | 6:1bcfda49e146 | 15 | Serial pc(PA_9, PA_10); |
szh66 | 6:1bcfda49e146 | 16 | |
kolanery | 13:a1a3418c07f3 | 17 | // Sensor offsets |
kolanery | 15:151e59899221 | 18 | float FRONT_SENSOR_THRES = 7.0f, SENSOR_ERROR_OFFSET = 0.0f; |
kolanery | 16:c26d8e007df5 | 19 | float LEFT_WALL_THRES = 0.626f, RIGHT_WALL_THRES = 0.161f; |
kolanery | 16:c26d8e007df5 | 20 | float RIGHT_SIDE_WALL_THRES = 0.5f, LEFT_SIDE_WALL_THRES = 0.5f; |
kolanery | 13:a1a3418c07f3 | 21 | |
kolanery | 13:a1a3418c07f3 | 22 | // Motor speed offsets |
kolanery | 13:a1a3418c07f3 | 23 | float left_speed, right_speed, motor_speed; |
kolanery | 14:a646667ac9ea | 24 | float MOTOR_BASE_SPEED = 10.0f; |
kolanery | 14:a646667ac9ea | 25 | float cool_down_offset = 0.0f; |
kolanery | 7:7215adbae3da | 26 | |
kolanery | 16:c26d8e007df5 | 27 | // Encoder offsets |
kolanery | 16:c26d8e007df5 | 28 | int ENCODER_TURN_UNIT = 16000; |
kolanery | 16:c26d8e007df5 | 29 | |
kolanery | 13:a1a3418c07f3 | 30 | namespace pid_controller { |
kolanery | 13:a1a3418c07f3 | 31 | // PID Constants |
kolanery | 13:a1a3418c07f3 | 32 | float error_p = 0.0f, old_error_p = 0.0f, old_error_d = 0.0f, error_d = 0.0f; |
kolanery | 13:a1a3418c07f3 | 33 | float total_error = 0.0f; |
kolanery | 15:151e59899221 | 34 | float P = 18.0f, D = 5.0f; |
kolanery | 13:a1a3418c07f3 | 35 | |
kolanery | 7:7215adbae3da | 36 | void navigate() { |
kolanery | 7:7215adbae3da | 37 | bool has_left_wall = leftDiagonalIR.readIR() > LEFT_WALL_THRES; |
kolanery | 7:7215adbae3da | 38 | bool has_right_wall = rightDiagonalIR.readIR() > RIGHT_WALL_THRES; |
kolanery | 7:7215adbae3da | 39 | |
kolanery | 7:7215adbae3da | 40 | if (has_left_wall && has_right_wall) { |
kolanery | 7:7215adbae3da | 41 | if (rightDiagonalIR - RIGHT_WALL_THRES < 0) { |
kolanery | 7:7215adbae3da | 42 | error_p = rightDiagonalIR - RIGHT_WALL_THRES; |
kolanery | 7:7215adbae3da | 43 | } |
kolanery | 7:7215adbae3da | 44 | else if (leftDiagonalIR - LEFT_WALL_THRES < 0) { |
kolanery | 7:7215adbae3da | 45 | error_p = leftDiagonalIR - LEFT_WALL_THRES; |
kolanery | 7:7215adbae3da | 46 | } |
kolanery | 7:7215adbae3da | 47 | else{ |
kolanery | 7:7215adbae3da | 48 | error_p = rightDiagonalIR - leftDiagonalIR; |
kolanery | 7:7215adbae3da | 49 | } |
kolanery | 7:7215adbae3da | 50 | error_d = error_p - old_error_p; |
kolanery | 7:7215adbae3da | 51 | } |
kolanery | 7:7215adbae3da | 52 | else if (has_left_wall) { |
kolanery | 7:7215adbae3da | 53 | error_p = 2 * (LEFT_WALL_THRES - leftDiagonalIR.readIR()); |
kolanery | 7:7215adbae3da | 54 | error_d = error_p - old_error_p; |
kolanery | 7:7215adbae3da | 55 | } |
kolanery | 7:7215adbae3da | 56 | else if (has_right_wall) { |
kolanery | 7:7215adbae3da | 57 | error_p = 2 * (rightDiagonalIR.readIR() - RIGHT_WALL_THRES); |
kolanery | 7:7215adbae3da | 58 | error_d = error_p - old_error_p; |
kolanery | 7:7215adbae3da | 59 | } |
kolanery | 7:7215adbae3da | 60 | else if (!has_left_wall && !has_right_wall) { |
kolanery | 7:7215adbae3da | 61 | error_p = 0; |
kolanery | 7:7215adbae3da | 62 | error_d = 0; |
kolanery | 7:7215adbae3da | 63 | } |
kolanery | 7:7215adbae3da | 64 | total_error = P * error_p + D * (error_d - old_error_d); |
kolanery | 7:7215adbae3da | 65 | old_error_p = error_p; |
kolanery | 7:7215adbae3da | 66 | old_error_d = error_d; |
kolanery | 7:7215adbae3da | 67 | |
kolanery | 14:a646667ac9ea | 68 | //MOTOR_BASE_SPEED -= cool_down_offset; |
kolanery | 16:c26d8e007df5 | 69 | if(total_error < 7.5f){ |
kolanery | 7:7215adbae3da | 70 | leftMotor -> speed(MOTOR_BASE_SPEED - total_error); |
kolanery | 7:7215adbae3da | 71 | rightMotor -> speed(MOTOR_BASE_SPEED + total_error); |
kolanery | 14:a646667ac9ea | 72 | |
kolanery | 14:a646667ac9ea | 73 | //leftMotor -> speed(MOTOR_BASE_SPEED - total_error); |
kolanery | 14:a646667ac9ea | 74 | //rightMotor -> speed(MOTOR_BASE_SPEED + total_error); |
kolanery | 7:7215adbae3da | 75 | } |
kolanery | 7:7215adbae3da | 76 | else{ |
kolanery | 7:7215adbae3da | 77 | leftMotor->speed(MOTOR_BASE_SPEED); |
kolanery | 7:7215adbae3da | 78 | rightMotor->speed(MOTOR_BASE_SPEED); |
kolanery | 14:a646667ac9ea | 79 | //leftMotor->speed(MOTOR_BASE_SPEED); |
kolanery | 14:a646667ac9ea | 80 | //rightMotor->speed(MOTOR_BASE_SPEED); |
kolanery | 14:a646667ac9ea | 81 | } |
kolanery | 14:a646667ac9ea | 82 | |
kolanery | 14:a646667ac9ea | 83 | |
kolanery | 14:a646667ac9ea | 84 | } |
kolanery | 14:a646667ac9ea | 85 | |
kolanery | 16:c26d8e007df5 | 86 | void clear_pid(){ |
kolanery | 16:c26d8e007df5 | 87 | error_p = 0.0f, old_error_p = 0.0f, old_error_d = 0.0f, error_d = 0.0f; |
kolanery | 16:c26d8e007df5 | 88 | total_error = 0.0f; |
kolanery | 16:c26d8e007df5 | 89 | } |
kolanery | 16:c26d8e007df5 | 90 | |
kolanery | 14:a646667ac9ea | 91 | void cool_down_speed() { |
kolanery | 14:a646667ac9ea | 92 | if (cool_down_offset < 5.0f) { |
kolanery | 14:a646667ac9ea | 93 | cool_down_offset += 2.0f; |
kolanery | 7:7215adbae3da | 94 | } |
ryan_whr | 12:6f48afe41cd9 | 95 | } |
ryan_whr | 12:6f48afe41cd9 | 96 | |
kolanery | 13:a1a3418c07f3 | 97 | void one_cell_turned(){ |
ryan_whr | 12:6f48afe41cd9 | 98 | leftEncoder.reset(); |
ryan_whr | 12:6f48afe41cd9 | 99 | rightEncoder.reset(); |
ryan_whr | 12:6f48afe41cd9 | 100 | while(leftEncoder.getEncoderDistance(1)<-46000 & leftEncoder.getEncoderDistance(1)<46000){ |
ryan_whr | 12:6f48afe41cd9 | 101 | //Do nothing |
ryan_whr | 12:6f48afe41cd9 | 102 | } |
kolanery | 7:7215adbae3da | 103 | } |
kolanery | 7:7215adbae3da | 104 | |
kolanery | 13:a1a3418c07f3 | 105 | // should use this method as the exit condition |
kolanery | 13:a1a3418c07f3 | 106 | // in the pid_controller::navigate() method |
kolanery | 13:a1a3418c07f3 | 107 | // resets the pid, encoders, etc. |
kolanery | 13:a1a3418c07f3 | 108 | void one_cell_traversed() { |
kolanery | 13:a1a3418c07f3 | 109 | leftEncoder.reset(); |
kolanery | 13:a1a3418c07f3 | 110 | rightEncoder.reset(); |
kolanery | 13:a1a3418c07f3 | 111 | old_error_p = old_error_d = total_error = 0.0f; |
kolanery | 13:a1a3418c07f3 | 112 | } |
kolanery | 13:a1a3418c07f3 | 113 | |
kolanery | 13:a1a3418c07f3 | 114 | // TODO |
kolanery | 14:a646667ac9ea | 115 | void turn (bool turn_right) { |
kolanery | 16:c26d8e007df5 | 116 | float MOTOR_TURN_SPEED = 14.0f; |
kolanery | 15:151e59899221 | 117 | |
kolanery | 15:151e59899221 | 118 | if (turn_right) { |
kolanery | 14:a646667ac9ea | 119 | leftMotor -> speed(MOTOR_TURN_SPEED); |
kolanery | 15:151e59899221 | 120 | rightMotor -> speed(-MOTOR_TURN_SPEED); |
kolanery | 7:7215adbae3da | 121 | } |
kolanery | 14:a646667ac9ea | 122 | else { |
kolanery | 15:151e59899221 | 123 | leftMotor -> speed(-MOTOR_TURN_SPEED); |
kolanery | 14:a646667ac9ea | 124 | rightMotor -> speed(MOTOR_TURN_SPEED); |
kolanery | 7:7215adbae3da | 125 | } |
kolanery | 7:7215adbae3da | 126 | } |
kolanery | 7:7215adbae3da | 127 | } |
kolanery | 7:7215adbae3da | 128 | |
kolanery | 0:cb667de3a336 | 129 | // Currently only have the x, y position fields for |
kolanery | 0:cb667de3a336 | 130 | // each cell. |
kolanery | 0:cb667de3a336 | 131 | DriveControl::DriveControl (int start_x, int start_y) { |
kolanery | 0:cb667de3a336 | 132 | curr_cell = new Cell (start_x, start_y); |
kolanery | 2:619b02232144 | 133 | leftMotor= new LeftMotor(); |
kolanery | 2:619b02232144 | 134 | rightMotor = new RightMotor(); |
kolanery | 0:cb667de3a336 | 135 | } |
kolanery | 0:cb667de3a336 | 136 | |
kolanery | 0:cb667de3a336 | 137 | // Defines the next cell to traverse. |
kolanery | 0:cb667de3a336 | 138 | Cell * next_cell() { |
kolanery | 0:cb667de3a336 | 139 | return curr_cell; |
kolanery | 0:cb667de3a336 | 140 | } |
kolanery | 0:cb667de3a336 | 141 | |
kolanery | 0:cb667de3a336 | 142 | int DriveControl::get_next_direction() { |
kolanery | 0:cb667de3a336 | 143 | // TODO: Define the direction based on heuristic eval. |
kolanery | 0:cb667de3a336 | 144 | return 1; |
kolanery | 0:cb667de3a336 | 145 | } |
kolanery | 0:cb667de3a336 | 146 | |
kolanery | 0:cb667de3a336 | 147 | int DriveControl::get_next_state(int state) { |
kolanery | 2:619b02232144 | 148 | // Simply drives the mouse for testing |
kolanery | 15:151e59899221 | 149 | //return DRIVE; |
kolanery | 0:cb667de3a336 | 150 | } |
kolanery | 0:cb667de3a336 | 151 | |
kolanery | 15:151e59899221 | 152 | void DriveControl::print_serial_ports(){ |
kolanery | 15:151e59899221 | 153 | pc.printf("LEFT Encoder Reading %d\n\r", leftEncoder.getEncoderDistance(1)); |
kolanery | 15:151e59899221 | 154 | pc.printf("RIGHT Encoder Reading %d\n\r", rightEncoder.getEncoderDistance(0)); |
kolanery | 14:a646667ac9ea | 155 | pc.printf("FRONT TOP RIGHT IRSensor %f\n\r", rightFrontIR.readIR() * 1000); |
kolanery | 15:151e59899221 | 156 | pc.printf("FRONT TOP LEFT IRSensor %f\n\r", leftFrontIR.readIR() * 10); |
kolanery | 14:a646667ac9ea | 157 | pc.printf("LEFT Diagonal Sensor %f\n\r", leftDiagonalIR.readIR()); |
kolanery | 14:a646667ac9ea | 158 | pc.printf("RIGHT Diagonal Sensor %f\n\r", rightDiagonalIR.readIR()); |
kolanery | 14:a646667ac9ea | 159 | pc.printf("LEFT SIDE Sensor %f\n\r", leftIR.readIR()); |
kolanery | 14:a646667ac9ea | 160 | pc.printf("RIGHT SIDE Sensor %f\n\r", rightIR.readIR()); |
kolanery | 16:c26d8e007df5 | 161 | } |
kolanery | 16:c26d8e007df5 | 162 | |
kolanery | 16:c26d8e007df5 | 163 | void DriveControl::turn_left() { |
kolanery | 16:c26d8e007df5 | 164 | // TODO: Add PID Control |
kolanery | 16:c26d8e007df5 | 165 | pid_controller::turn(false); |
szh66 | 8:4a32fc9ee939 | 166 | } |
szh66 | 8:4a32fc9ee939 | 167 | |
kolanery | 0:cb667de3a336 | 168 | void DriveControl::turn_right() { |
kolanery | 4:73510c7fa316 | 169 | // TODO: Add PID Control |
kolanery | 14:a646667ac9ea | 170 | pid_controller::turn(true); |
kolanery | 7:7215adbae3da | 171 | } |
kolanery | 7:7215adbae3da | 172 | |
kolanery | 7:7215adbae3da | 173 | void DriveControl::turn_around() { |
kolanery | 7:7215adbae3da | 174 | // TODO: Add PID Control |
kolanery | 15:151e59899221 | 175 | //pid_controller::turn(TURN_AROUND); |
kolanery | 16:c26d8e007df5 | 176 | pid_controller::turn(true); |
kolanery | 0:cb667de3a336 | 177 | } |
kolanery | 0:cb667de3a336 | 178 | |
kolanery | 0:cb667de3a336 | 179 | void DriveControl::stop() { |
kolanery | 2:619b02232144 | 180 | leftMotor->stop(); |
kolanery | 2:619b02232144 | 181 | rightMotor->stop(); |
kolanery | 0:cb667de3a336 | 182 | } |
kolanery | 0:cb667de3a336 | 183 | |
kolanery | 14:a646667ac9ea | 184 | void DriveControl::resetEncoders() { |
kolanery | 14:a646667ac9ea | 185 | leftEncoder.resetEncoders(); |
kolanery | 14:a646667ac9ea | 186 | rightEncoder.resetEncoders(); |
kolanery | 14:a646667ac9ea | 187 | } |
kolanery | 14:a646667ac9ea | 188 | |
kolanery | 7:7215adbae3da | 189 | void DriveControl::drive_forward() { |
kolanery | 7:7215adbae3da | 190 | pid_controller::navigate(); |
kolanery | 14:a646667ac9ea | 191 | |
kolanery | 14:a646667ac9ea | 192 | } |
kolanery | 14:a646667ac9ea | 193 | |
kolanery | 14:a646667ac9ea | 194 | bool DriveControl::should_stop_drive_forward() { |
kolanery | 14:a646667ac9ea | 195 | float SHOULD_STOP = 7.0f; |
kolanery | 14:a646667ac9ea | 196 | return (rightFrontIR.readIR() * 1000) > SHOULD_STOP; |
kolanery | 14:a646667ac9ea | 197 | } |
kolanery | 14:a646667ac9ea | 198 | |
kolanery | 14:a646667ac9ea | 199 | bool DriveControl::should_finish_turn_right() { |
kolanery | 16:c26d8e007df5 | 200 | int max = |
kolanery | 16:c26d8e007df5 | 201 | (leftEncoder.getEncoderDistance(1) < rightEncoder.getEncoderDistance(0)) |
kolanery | 16:c26d8e007df5 | 202 | ? rightEncoder.getEncoderDistance(0):leftEncoder.getEncoderDistance(1); |
kolanery | 16:c26d8e007df5 | 203 | return max < - ENCODER_TURN_UNIT; |
kolanery | 14:a646667ac9ea | 204 | } |
kolanery | 14:a646667ac9ea | 205 | |
kolanery | 14:a646667ac9ea | 206 | bool DriveControl::should_finish_turn_left() { |
kolanery | 16:c26d8e007df5 | 207 | int min_two = |
kolanery | 16:c26d8e007df5 | 208 | (leftEncoder.getEncoderDistance(1) > rightEncoder.getEncoderDistance(0)) |
kolanery | 16:c26d8e007df5 | 209 | ? rightEncoder.getEncoderDistance(0):leftEncoder.getEncoderDistance(1); |
kolanery | 16:c26d8e007df5 | 210 | return min_two > ENCODER_TURN_UNIT; |
kolanery | 16:c26d8e007df5 | 211 | } |
kolanery | 16:c26d8e007df5 | 212 | |
kolanery | 16:c26d8e007df5 | 213 | bool DriveControl::should_finish_drive_forward() { |
kolanery | 16:c26d8e007df5 | 214 | int max_two = |
kolanery | 16:c26d8e007df5 | 215 | (- leftEncoder.getEncoderDistance(1) < rightEncoder.getEncoderDistance(0)) |
kolanery | 16:c26d8e007df5 | 216 | ? rightEncoder.getEncoderDistance(0):- leftEncoder.getEncoderDistance(1); |
kolanery | 16:c26d8e007df5 | 217 | return max_two > 46000; |
kolanery | 4:73510c7fa316 | 218 | } |
kolanery | 4:73510c7fa316 | 219 | |
kolanery | 0:cb667de3a336 | 220 | bool DriveControl::has_front_wall() { |
kolanery | 15:151e59899221 | 221 | bool right_front_wall = (rightFrontIR.readIR() * 1000) > FRONT_SENSOR_THRES; |
kolanery | 15:151e59899221 | 222 | bool left_front_wall = (leftFrontIR.readIR() * 10) > FRONT_SENSOR_THRES; |
kolanery | 15:151e59899221 | 223 | return right_front_wall || left_front_wall; |
kolanery | 0:cb667de3a336 | 224 | } |
kolanery | 0:cb667de3a336 | 225 | |
kolanery | 0:cb667de3a336 | 226 | bool DriveControl::has_left_wall() { |
kolanery | 15:151e59899221 | 227 | return leftIR.readIR() > LEFT_SIDE_WALL_THRES; |
kolanery | 0:cb667de3a336 | 228 | } |
kolanery | 0:cb667de3a336 | 229 | |
kolanery | 0:cb667de3a336 | 230 | bool DriveControl::has_right_wall() { |
kolanery | 15:151e59899221 | 231 | return rightIR.readIR() > RIGHT_SIDE_WALL_THRES; |
kolanery | 0:cb667de3a336 | 232 | } |
kolanery | 16:c26d8e007df5 | 233 | |
kolanery | 16:c26d8e007df5 | 234 | void DriveControl::clear_pid() { |
kolanery | 16:c26d8e007df5 | 235 | pid_controller::clear_pid(); |
kolanery | 16:c26d8e007df5 | 236 | } |