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