An auto car with 3 IR sensors.

Dependencies:   Ping

Committer:
cudaChen
Date:
Wed Jul 18 12:38:54 2018 +0000
Revision:
18:d7509436e9ef
Parent:
15:1d440beb24d3
Child:
19:d06f5a3ed0bc
[add] add obstacle avoidance feature

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cudaChen 12:e95ed962be7a 1 #include "mbed.h"
cudaChen 12:e95ed962be7a 2
cudaChen 18:d7509436e9ef 3 #include "Ping.h"
cudaChen 12:e95ed962be7a 4 #include "autocar.h"
cudaChen 12:e95ed962be7a 5
cudaChen 12:e95ed962be7a 6 const int MOTOR_M1 = 0;
cudaChen 12:e95ed962be7a 7 const int MOTOR_M2 = 1;
cudaChen 12:e95ed962be7a 8 const int DIR_FORWARD = 0;
cudaChen 12:e95ed962be7a 9 const int DIR_BACKWARD = 1;
cudaChen 12:e95ed962be7a 10 const int PWR_STOP = 0;
cudaChen 12:e95ed962be7a 11
cudaChen 12:e95ed962be7a 12 // used for IR sensors
cudaChen 13:87cd0ae37e06 13 int left, middle, right;
cudaChen 13:87cd0ae37e06 14
cudaChen 13:87cd0ae37e06 15 // PID factors
cudaChen 13:87cd0ae37e06 16 int interror = 0;
cudaChen 13:87cd0ae37e06 17 int olderror = 0;
cudaChen 15:1d440beb24d3 18 int limit = 4095; // 12-bit ADC in STM32
cudaChen 12:e95ed962be7a 19
cudaChen 12:e95ed962be7a 20 // used for output message via serial
cudaChen 12:e95ed962be7a 21 Serial pc(SERIAL_TX, SERIAL_RX);
cudaChen 12:e95ed962be7a 22
cudaChen 12:e95ed962be7a 23 long map(long x, long in_min, long in_max, long out_min, long out_max)
cudaChen 12:e95ed962be7a 24 {
cudaChen 12:e95ed962be7a 25 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
cudaChen 12:e95ed962be7a 26 }
cudaChen 12:e95ed962be7a 27
cudaChen 12:e95ed962be7a 28 void readIR(bool* left, bool* middle, bool* right, int threshold)
cudaChen 12:e95ed962be7a 29 {
cudaChen 12:e95ed962be7a 30 // not on track: > 500
cudaChen 12:e95ed962be7a 31 // on track (black): < 500
cudaChen 12:e95ed962be7a 32 *left = leftIR.read_u16() < threshold ? true : false;
cudaChen 12:e95ed962be7a 33 *middle = middleIR.read_u16() < threshold ? true : false;
cudaChen 12:e95ed962be7a 34 *right = rightIR.read_u16() < threshold ? true : false;
cudaChen 12:e95ed962be7a 35 }
cudaChen 12:e95ed962be7a 36
cudaChen 13:87cd0ae37e06 37 int readIRValues()
cudaChen 13:87cd0ae37e06 38 {
cudaChen 13:87cd0ae37e06 39 left = leftIR.read_u16();
cudaChen 13:87cd0ae37e06 40 middle = middleIR.read_u16();
cudaChen 13:87cd0ae37e06 41 right = rightIR.read_u16();
cudaChen 13:87cd0ae37e06 42
cudaChen 13:87cd0ae37e06 43 int ret = left * (-1) + middle * 0 + right * 1;
cudaChen 13:87cd0ae37e06 44
cudaChen 14:28cae134a304 45 pc.printf("left middle right: %d %d %d\r\n", left, middle, right);
cudaChen 13:87cd0ae37e06 46 pc.printf("IR values: %d\r\n", ret);
cudaChen 13:87cd0ae37e06 47
cudaChen 13:87cd0ae37e06 48 return ret;
cudaChen 13:87cd0ae37e06 49 }
cudaChen 13:87cd0ae37e06 50
cudaChen 18:d7509436e9ef 51 void readSensor(bool* left, bool* middle, bool* right, bool* hasObstacle, int threshold, int range)
cudaChen 18:d7509436e9ef 52 {
cudaChen 18:d7509436e9ef 53 int distance;
cudaChen 18:d7509436e9ef 54 ultrasonic.Send();
cudaChen 18:d7509436e9ef 55 wait_ms(30);
cudaChen 18:d7509436e9ef 56 distance = ultrasonic.Read_cm();
cudaChen 18:d7509436e9ef 57 *hasObstacle = distance < range ? true : false;
cudaChen 18:d7509436e9ef 58
cudaChen 18:d7509436e9ef 59 // not on track: > 500
cudaChen 18:d7509436e9ef 60 // on track (black): < 500
cudaChen 18:d7509436e9ef 61 *left = leftIR.read_u16() < threshold ? true : false;
cudaChen 18:d7509436e9ef 62 *middle = middleIR.read_u16() < threshold ? true : false;
cudaChen 18:d7509436e9ef 63 *right = rightIR.read_u16() < threshold ? true : false;
cudaChen 18:d7509436e9ef 64 }
cudaChen 18:d7509436e9ef 65
cudaChen 12:e95ed962be7a 66 // m: 0 => M1 1 => M2
cudaChen 12:e95ed962be7a 67 // power 0~10 dir: 0=>正向 1=>反向
cudaChen 12:e95ed962be7a 68 void DriveSingleMotor(int m, int speed, int dir)
cudaChen 12:e95ed962be7a 69 {
cudaChen 12:e95ed962be7a 70 /*int _speed = 0;
cudaChen 12:e95ed962be7a 71
cudaChen 12:e95ed962be7a 72 // 設定速度
cudaChen 12:e95ed962be7a 73 if (speed>10) speed=10;
cudaChen 12:e95ed962be7a 74 _speed = map(speed, 1, 10, 100, 255);
cudaChen 12:e95ed962be7a 75
cudaChen 12:e95ed962be7a 76 if (speed<=0) {
cudaChen 12:e95ed962be7a 77 speed=0;
cudaChen 12:e95ed962be7a 78 _speed=0;
cudaChen 12:e95ed962be7a 79 } else
cudaChen 12:e95ed962be7a 80 _speed = map(speed, 1, 10, 100, 255);*/
cudaChen 12:e95ed962be7a 81
cudaChen 12:e95ed962be7a 82 if (m == MOTOR_M1) {
cudaChen 12:e95ed962be7a 83 // 設定方向
cudaChen 12:e95ed962be7a 84 if (speed == PWR_STOP) {
cudaChen 12:e95ed962be7a 85 M1_in1 = 0;
cudaChen 12:e95ed962be7a 86 M1_in2 = 0;
cudaChen 12:e95ed962be7a 87 } else if (dir == DIR_FORWARD) {
cudaChen 12:e95ed962be7a 88 // right wheel go forward
cudaChen 12:e95ed962be7a 89 M1_in1 = 1;
cudaChen 12:e95ed962be7a 90 M1_in2 = 0;
cudaChen 12:e95ed962be7a 91 } else {
cudaChen 12:e95ed962be7a 92 // right wheel go backward
cudaChen 12:e95ed962be7a 93 M1_in1 = 0;
cudaChen 12:e95ed962be7a 94 M1_in2 = 1;
cudaChen 12:e95ed962be7a 95 }
cudaChen 12:e95ed962be7a 96 //M1_enable.write_u16(_speed); // 驅動馬達 右輪
cudaChen 12:e95ed962be7a 97 M1_enable.write(1);
cudaChen 12:e95ed962be7a 98
cudaChen 12:e95ed962be7a 99 } else if (m == MOTOR_M2) {
cudaChen 12:e95ed962be7a 100 // 設定方向
cudaChen 12:e95ed962be7a 101 if (speed == PWR_STOP) {
cudaChen 12:e95ed962be7a 102 M2_in3 = 0;
cudaChen 12:e95ed962be7a 103 M2_in4 = 0;
cudaChen 12:e95ed962be7a 104 } else if (dir == DIR_FORWARD) {
cudaChen 12:e95ed962be7a 105 //左輪前進
cudaChen 12:e95ed962be7a 106 M2_in3 = 1;
cudaChen 12:e95ed962be7a 107 M2_in4 = 0;
cudaChen 12:e95ed962be7a 108 } else {
cudaChen 12:e95ed962be7a 109 //左輪倒轉
cudaChen 12:e95ed962be7a 110 M2_in3 = 0;
cudaChen 12:e95ed962be7a 111 M2_in4 = 1;
cudaChen 12:e95ed962be7a 112 }
cudaChen 12:e95ed962be7a 113 //M2_enable.write_u16(_speed); // 驅動馬達 左輪
cudaChen 12:e95ed962be7a 114 M2_enable.write(1);
cudaChen 12:e95ed962be7a 115 } else {
cudaChen 12:e95ed962be7a 116 //M1_enable.write_u16(PWR_STOP); // right wheel
cudaChen 12:e95ed962be7a 117 //M2_enable.write_u16(PWR_STOP); // left wheel
cudaChen 12:e95ed962be7a 118 M1_enable.write(0);
cudaChen 12:e95ed962be7a 119 M2_enable.write(0);
cudaChen 12:e95ed962be7a 120 }
cudaChen 12:e95ed962be7a 121 }
cudaChen 12:e95ed962be7a 122
cudaChen 12:e95ed962be7a 123 void driveMotor(bool left, bool middle, bool right)
cudaChen 12:e95ed962be7a 124 {
cudaChen 12:e95ed962be7a 125 int status = left * 4 + middle * 2 + right;
cudaChen 12:e95ed962be7a 126 switch(status) {
cudaChen 12:e95ed962be7a 127 case 7: // go straight
cudaChen 12:e95ed962be7a 128 pc.printf("7. go straight\r\n");
cudaChen 12:e95ed962be7a 129 forward();
cudaChen 12:e95ed962be7a 130 break;
cudaChen 12:e95ed962be7a 131 case 6: // turn left
cudaChen 12:e95ed962be7a 132 pc.printf("6. turn left\r\n");
cudaChen 12:e95ed962be7a 133 turnLeft();
cudaChen 12:e95ed962be7a 134 break;
cudaChen 12:e95ed962be7a 135 case 5: // go straight
cudaChen 12:e95ed962be7a 136 pc.printf("5. go straight\r\n");
cudaChen 12:e95ed962be7a 137 forward();
cudaChen 12:e95ed962be7a 138 break;
cudaChen 12:e95ed962be7a 139 case 4: // turn left
cudaChen 12:e95ed962be7a 140 pc.printf("4. turn left\r\n");
cudaChen 12:e95ed962be7a 141 turnLeft();
cudaChen 12:e95ed962be7a 142 break;
cudaChen 12:e95ed962be7a 143 case 3: // turn right
cudaChen 12:e95ed962be7a 144 pc.printf("3. turn right\r\n");
cudaChen 12:e95ed962be7a 145 turnRight();
cudaChen 12:e95ed962be7a 146 break;
cudaChen 12:e95ed962be7a 147 case 2: // go straight
cudaChen 12:e95ed962be7a 148 pc.printf("2. go straight\r\n");
cudaChen 12:e95ed962be7a 149 forward();
cudaChen 12:e95ed962be7a 150 break;
cudaChen 12:e95ed962be7a 151 case 1: // turn right
cudaChen 12:e95ed962be7a 152 pc.printf("1. turn right\r\n");
cudaChen 12:e95ed962be7a 153 turnRight();
cudaChen 12:e95ed962be7a 154 break;
cudaChen 12:e95ed962be7a 155 case 0: // go straight
cudaChen 12:e95ed962be7a 156 pc.printf("0. go straight\r\n");
cudaChen 12:e95ed962be7a 157 forward();
cudaChen 12:e95ed962be7a 158 break;
cudaChen 12:e95ed962be7a 159 default:
cudaChen 12:e95ed962be7a 160 pc.printf("invalid\r\n");
cudaChen 12:e95ed962be7a 161 break;
cudaChen 12:e95ed962be7a 162 }
cudaChen 12:e95ed962be7a 163 }
cudaChen 12:e95ed962be7a 164
cudaChen 18:d7509436e9ef 165 void driveMotor(bool left, bool middle, bool right, bool hasObstacle)
cudaChen 18:d7509436e9ef 166 {
cudaChen 18:d7509436e9ef 167 if(hasObstacle)
cudaChen 18:d7509436e9ef 168 {
cudaChen 18:d7509436e9ef 169 stop();
cudaChen 18:d7509436e9ef 170 wait_ms(500);
cudaChen 18:d7509436e9ef 171 turnRight();
cudaChen 18:d7509436e9ef 172 wait_ms(200);
cudaChen 18:d7509436e9ef 173 stop();
cudaChen 18:d7509436e9ef 174 wait_ms(500);
cudaChen 18:d7509436e9ef 175 forward();
cudaChen 18:d7509436e9ef 176 wait_ms(500);
cudaChen 18:d7509436e9ef 177 stop();
cudaChen 18:d7509436e9ef 178 wait_ms(500);
cudaChen 18:d7509436e9ef 179
cudaChen 18:d7509436e9ef 180 turnLeft();
cudaChen 18:d7509436e9ef 181 wait_ms(200);
cudaChen 18:d7509436e9ef 182 stop();
cudaChen 18:d7509436e9ef 183 wait_ms(500);
cudaChen 18:d7509436e9ef 184 forward();
cudaChen 18:d7509436e9ef 185 wait_ms(500);
cudaChen 18:d7509436e9ef 186 stop();
cudaChen 18:d7509436e9ef 187 wait_ms(500);
cudaChen 18:d7509436e9ef 188 turnRight();
cudaChen 18:d7509436e9ef 189 wait_ms(200);
cudaChen 18:d7509436e9ef 190 stop();
cudaChen 18:d7509436e9ef 191 wait_ms(500);
cudaChen 18:d7509436e9ef 192
cudaChen 18:d7509436e9ef 193 return;
cudaChen 18:d7509436e9ef 194 }
cudaChen 18:d7509436e9ef 195
cudaChen 18:d7509436e9ef 196 int status = left * 4 + middle * 2 + right;
cudaChen 18:d7509436e9ef 197 switch(status) {
cudaChen 18:d7509436e9ef 198 case 7: // go straight
cudaChen 18:d7509436e9ef 199 pc.printf("7. go straight\r\n");
cudaChen 18:d7509436e9ef 200 forward();
cudaChen 18:d7509436e9ef 201 break;
cudaChen 18:d7509436e9ef 202 case 6: // turn left
cudaChen 18:d7509436e9ef 203 pc.printf("6. turn left\r\n");
cudaChen 18:d7509436e9ef 204 turnLeft();
cudaChen 18:d7509436e9ef 205 break;
cudaChen 18:d7509436e9ef 206 case 5: // go straight
cudaChen 18:d7509436e9ef 207 pc.printf("5. go straight\r\n");
cudaChen 18:d7509436e9ef 208 forward();
cudaChen 18:d7509436e9ef 209 break;
cudaChen 18:d7509436e9ef 210 case 4: // turn left
cudaChen 18:d7509436e9ef 211 pc.printf("4. turn left\r\n");
cudaChen 18:d7509436e9ef 212 turnLeft();
cudaChen 18:d7509436e9ef 213 break;
cudaChen 18:d7509436e9ef 214 case 3: // turn right
cudaChen 18:d7509436e9ef 215 pc.printf("3. turn right\r\n");
cudaChen 18:d7509436e9ef 216 turnRight();
cudaChen 18:d7509436e9ef 217 break;
cudaChen 18:d7509436e9ef 218 case 2: // go straight
cudaChen 18:d7509436e9ef 219 pc.printf("2. go straight\r\n");
cudaChen 18:d7509436e9ef 220 forward();
cudaChen 18:d7509436e9ef 221 break;
cudaChen 18:d7509436e9ef 222 case 1: // turn right
cudaChen 18:d7509436e9ef 223 pc.printf("1. turn right\r\n");
cudaChen 18:d7509436e9ef 224 turnRight();
cudaChen 18:d7509436e9ef 225 break;
cudaChen 18:d7509436e9ef 226 case 0: // go straight
cudaChen 18:d7509436e9ef 227 pc.printf("0. go straight\r\n");
cudaChen 18:d7509436e9ef 228 forward();
cudaChen 18:d7509436e9ef 229 break;
cudaChen 18:d7509436e9ef 230 default:
cudaChen 18:d7509436e9ef 231 pc.printf("invalid\r\n");
cudaChen 18:d7509436e9ef 232 break;
cudaChen 18:d7509436e9ef 233 }
cudaChen 18:d7509436e9ef 234 }
cudaChen 18:d7509436e9ef 235
cudaChen 18:d7509436e9ef 236 void driveMotorPID(float values, float Kp, float Ki, float Kd)
cudaChen 13:87cd0ae37e06 237 {
cudaChen 13:87cd0ae37e06 238 int error = values; // 'P'
cudaChen 13:87cd0ae37e06 239 interror += error;
cudaChen 13:87cd0ae37e06 240 int lasterror = error - olderror;
cudaChen 13:87cd0ae37e06 241 olderror = error; // 注意olderror的順序
cudaChen 13:87cd0ae37e06 242 int power = error * Kp + interror * Ki + lasterror * Kd;
cudaChen 13:87cd0ae37e06 243 /* error(P值)、interror(I值)、lasterror(D值)、olderror用來取D值的 */
cudaChen 13:87cd0ae37e06 244
cudaChen 13:87cd0ae37e06 245 // limit PID output value (for 12-bit ADC in STM32)
cudaChen 13:87cd0ae37e06 246 if(power > limit) {
cudaChen 13:87cd0ae37e06 247 power = limit;
cudaChen 13:87cd0ae37e06 248 } else if(power < -limit) {
cudaChen 13:87cd0ae37e06 249 power = -limit;
cudaChen 13:87cd0ae37e06 250 }
cudaChen 13:87cd0ae37e06 251
cudaChen 13:87cd0ae37e06 252 // control the direction of auto car
cudaChen 18:d7509436e9ef 253 if(power < -700) {
cudaChen 13:87cd0ae37e06 254 turnLeft();
cudaChen 18:d7509436e9ef 255 } else if (power > 700) {
cudaChen 13:87cd0ae37e06 256 turnRight();
cudaChen 13:87cd0ae37e06 257 } else {
cudaChen 13:87cd0ae37e06 258 forward();
cudaChen 13:87cd0ae37e06 259 }
cudaChen 13:87cd0ae37e06 260 }
cudaChen 13:87cd0ae37e06 261
cudaChen 12:e95ed962be7a 262 void init()
cudaChen 12:e95ed962be7a 263 {
cudaChen 12:e95ed962be7a 264 DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
cudaChen 12:e95ed962be7a 265 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
cudaChen 12:e95ed962be7a 266 }
cudaChen 12:e95ed962be7a 267
cudaChen 18:d7509436e9ef 268 void stop()
cudaChen 18:d7509436e9ef 269 {
cudaChen 18:d7509436e9ef 270 DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
cudaChen 18:d7509436e9ef 271 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
cudaChen 18:d7509436e9ef 272 }
cudaChen 18:d7509436e9ef 273
cudaChen 12:e95ed962be7a 274 void forward()
cudaChen 12:e95ed962be7a 275 {
cudaChen 12:e95ed962be7a 276 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
cudaChen 12:e95ed962be7a 277 DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
cudaChen 12:e95ed962be7a 278 }
cudaChen 12:e95ed962be7a 279
cudaChen 12:e95ed962be7a 280 void turnLeft()
cudaChen 12:e95ed962be7a 281 {
cudaChen 12:e95ed962be7a 282 DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
cudaChen 12:e95ed962be7a 283 DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
cudaChen 12:e95ed962be7a 284 }
cudaChen 12:e95ed962be7a 285
cudaChen 12:e95ed962be7a 286 void turnRight()
cudaChen 12:e95ed962be7a 287 {
cudaChen 12:e95ed962be7a 288 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
cudaChen 12:e95ed962be7a 289 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
cudaChen 12:e95ed962be7a 290 }