An auto car with 3 IR sensors.

Dependencies:   Ping

Committer:
cudaChen
Date:
Sat Jun 30 11:36:15 2018 +0000
Revision:
10:a14380381d86
Parent:
9:ce1dd89ff5e8
Child:
11:3e9d4c345ebd
[add] add three new function: forward, turnLeft, and turnRight

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cudaChen 0:190c4784b6f4 1 #include "mbed.h"
cudaChen 0:190c4784b6f4 2
cudaChen 7:4edd049209a6 3 // trigger button and triggered LED
cudaChen 0:190c4784b6f4 4 DigitalOut led1(LED1);
cudaChen 7:4edd049209a6 5 DigitalIn pb(PC_13);
cudaChen 7:4edd049209a6 6 int lastButtonState = 0;
cudaChen 7:4edd049209a6 7 bool ledState = false;
cudaChen 0:190c4784b6f4 8
cudaChen 0:190c4784b6f4 9 Serial pc(SERIAL_TX, SERIAL_RX);
cudaChen 0:190c4784b6f4 10
cudaChen 0:190c4784b6f4 11 // used for motor
cudaChen 0:190c4784b6f4 12 //AnalogOut M1_enable(D6);
cudaChen 0:190c4784b6f4 13 //AnalogOut M2_enable(D5);
cudaChen 0:190c4784b6f4 14 DigitalOut M1_enable(D6);
cudaChen 0:190c4784b6f4 15 DigitalOut M2_enable(D5);
cudaChen 9:ce1dd89ff5e8 16 DigitalOut M1_in1(D3);
cudaChen 9:ce1dd89ff5e8 17 DigitalOut M1_in2(D2);
cudaChen 9:ce1dd89ff5e8 18 DigitalOut M2_in3(D1);
cudaChen 9:ce1dd89ff5e8 19 DigitalOut M2_in4(D0);
cudaChen 10:a14380381d86 20 const int MOTOR_M1 = 0;
cudaChen 10:a14380381d86 21 const int MOTOR_M2 = 1;
cudaChen 10:a14380381d86 22 const int DIR_FORWARD = 0;
cudaChen 10:a14380381d86 23 const int DIR_BACKWARD = 1;
cudaChen 0:190c4784b6f4 24 const int PWR_STOP = 0;
cudaChen 0:190c4784b6f4 25
cudaChen 0:190c4784b6f4 26 // used for IR sensors
cudaChen 0:190c4784b6f4 27 AnalogIn leftIR(A1);
cudaChen 0:190c4784b6f4 28 AnalogIn middleIR(A3);
cudaChen 0:190c4784b6f4 29 AnalogIn rightIR(A5);
cudaChen 0:190c4784b6f4 30
cudaChen 10:a14380381d86 31 // used for controlling the direction of auto car
cudaChen 10:a14380381d86 32 void forward();
cudaChen 10:a14380381d86 33 void turnLeft();
cudaChen 10:a14380381d86 34 void turnRight();
cudaChen 10:a14380381d86 35
cudaChen 0:190c4784b6f4 36 long map(long x, long in_min, long in_max, long out_min, long out_max)
cudaChen 0:190c4784b6f4 37 {
cudaChen 0:190c4784b6f4 38 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
cudaChen 0:190c4784b6f4 39 }
cudaChen 0:190c4784b6f4 40
cudaChen 0:190c4784b6f4 41 void DriveSingleMotor(int m, int speed, int dir);
cudaChen 5:25faf509ee9b 42 void driveMotor(bool left, bool middle, bool right);
cudaChen 10:a14380381d86 43 void init();
cudaChen 0:190c4784b6f4 44
cudaChen 0:190c4784b6f4 45 // main() runs in its own thread in the OS
cudaChen 0:190c4784b6f4 46 int main()
cudaChen 0:190c4784b6f4 47 {
cudaChen 0:190c4784b6f4 48 bool left = false;
cudaChen 0:190c4784b6f4 49 bool middle = false;
cudaChen 0:190c4784b6f4 50 bool right = false;
cudaChen 4:76b9213714cc 51
cudaChen 2:e0a553b64315 52 // here I use 500 as threshold
cudaChen 2:e0a553b64315 53 int threshold = 500;
cudaChen 0:190c4784b6f4 54
cudaChen 10:a14380381d86 55 // set two motors to stop
cudaChen 10:a14380381d86 56 init();
cudaChen 10:a14380381d86 57
cudaChen 4:76b9213714cc 58 while (true) {
cudaChen 7:4edd049209a6 59 int reading1 = pb.read();
cudaChen 10:a14380381d86 60
cudaChen 7:4edd049209a6 61 if(reading1 != lastButtonState) {
cudaChen 7:4edd049209a6 62 wait_ms(20);
cudaChen 10:a14380381d86 63
cudaChen 7:4edd049209a6 64 int reading2 = pb.read();
cudaChen 10:a14380381d86 65
cudaChen 7:4edd049209a6 66 if(reading2 == reading1) {
cudaChen 7:4edd049209a6 67 lastButtonState = reading2;
cudaChen 7:4edd049209a6 68 }
cudaChen 10:a14380381d86 69
cudaChen 7:4edd049209a6 70 if(lastButtonState == 1) {
cudaChen 7:4edd049209a6 71 ledState = !ledState;
cudaChen 7:4edd049209a6 72 }
cudaChen 7:4edd049209a6 73 }
cudaChen 10:a14380381d86 74
cudaChen 7:4edd049209a6 75 led1.write(ledState);
cudaChen 10:a14380381d86 76
cudaChen 7:4edd049209a6 77 if(ledState) {
cudaChen 10:a14380381d86 78 // not on track: > 500
cudaChen 10:a14380381d86 79 // on track (black): < 500
cudaChen 10:a14380381d86 80 left = leftIR.read_u16() < threshold ? true : false;
cudaChen 10:a14380381d86 81 middle = middleIR.read_u16() < threshold ? true : false;
cudaChen 10:a14380381d86 82 right = rightIR.read_u16() < threshold ? true : false;
cudaChen 0:190c4784b6f4 83
cudaChen 10:a14380381d86 84 driveMotor(left, middle, right);
cudaChen 7:4edd049209a6 85 }
cudaChen 0:190c4784b6f4 86 }
cudaChen 0:190c4784b6f4 87 }
cudaChen 0:190c4784b6f4 88
cudaChen 0:190c4784b6f4 89 // m: 0 => M1 1 => M2
cudaChen 0:190c4784b6f4 90 // power 0~10 dir: 0=>正向 1=>反向
cudaChen 0:190c4784b6f4 91 void DriveSingleMotor(int m, int speed, int dir)
cudaChen 0:190c4784b6f4 92 {
cudaChen 0:190c4784b6f4 93 /*int _speed = 0;
cudaChen 0:190c4784b6f4 94
cudaChen 0:190c4784b6f4 95 // 設定速度
cudaChen 0:190c4784b6f4 96 if (speed>10) speed=10;
cudaChen 0:190c4784b6f4 97 _speed = map(speed, 1, 10, 100, 255);
cudaChen 0:190c4784b6f4 98
cudaChen 0:190c4784b6f4 99 if (speed<=0) {
cudaChen 0:190c4784b6f4 100 speed=0;
cudaChen 0:190c4784b6f4 101 _speed=0;
cudaChen 0:190c4784b6f4 102 } else
cudaChen 0:190c4784b6f4 103 _speed = map(speed, 1, 10, 100, 255);*/
cudaChen 0:190c4784b6f4 104
cudaChen 0:190c4784b6f4 105 if (m == MOTOR_M1) {
cudaChen 0:190c4784b6f4 106 // 設定方向
cudaChen 0:190c4784b6f4 107 if (speed == PWR_STOP) {
cudaChen 0:190c4784b6f4 108 M1_in1 = 0;
cudaChen 0:190c4784b6f4 109 M1_in2 = 0;
cudaChen 0:190c4784b6f4 110 } else if (dir == DIR_FORWARD) {
cudaChen 0:190c4784b6f4 111 // right wheel go forward
cudaChen 0:190c4784b6f4 112 M1_in1 = 1;
cudaChen 0:190c4784b6f4 113 M1_in2 = 0;
cudaChen 0:190c4784b6f4 114 } else {
cudaChen 0:190c4784b6f4 115 // right wheel go backward
cudaChen 0:190c4784b6f4 116 M1_in1 = 0;
cudaChen 0:190c4784b6f4 117 M1_in2 = 1;
cudaChen 0:190c4784b6f4 118 }
cudaChen 0:190c4784b6f4 119 //M1_enable.write_u16(_speed); // 驅動馬達 右輪
cudaChen 0:190c4784b6f4 120 M1_enable.write(1);
cudaChen 0:190c4784b6f4 121
cudaChen 0:190c4784b6f4 122 } else if (m == MOTOR_M2) {
cudaChen 0:190c4784b6f4 123 // 設定方向
cudaChen 0:190c4784b6f4 124 if (speed == PWR_STOP) {
cudaChen 0:190c4784b6f4 125 M2_in3 = 0;
cudaChen 0:190c4784b6f4 126 M2_in4 = 0;
cudaChen 0:190c4784b6f4 127 } else if (dir == DIR_FORWARD) {
cudaChen 0:190c4784b6f4 128 //左輪前進
cudaChen 0:190c4784b6f4 129 M2_in3 = 1;
cudaChen 0:190c4784b6f4 130 M2_in4 = 0;
cudaChen 0:190c4784b6f4 131 } else {
cudaChen 0:190c4784b6f4 132 //左輪倒轉
cudaChen 0:190c4784b6f4 133 M2_in3 = 0;
cudaChen 0:190c4784b6f4 134 M2_in4 = 1;
cudaChen 0:190c4784b6f4 135 }
cudaChen 0:190c4784b6f4 136 //M2_enable.write_u16(_speed); // 驅動馬達 左輪
cudaChen 0:190c4784b6f4 137 M2_enable.write(1);
cudaChen 0:190c4784b6f4 138 } else {
cudaChen 0:190c4784b6f4 139 //M1_enable.write_u16(PWR_STOP); // right wheel
cudaChen 0:190c4784b6f4 140 //M2_enable.write_u16(PWR_STOP); // left wheel
cudaChen 0:190c4784b6f4 141 M1_enable.write(0);
cudaChen 0:190c4784b6f4 142 M2_enable.write(0);
cudaChen 0:190c4784b6f4 143 }
cudaChen 0:190c4784b6f4 144 }
cudaChen 0:190c4784b6f4 145
cudaChen 5:25faf509ee9b 146 void driveMotor(bool left, bool middle, bool right)
cudaChen 5:25faf509ee9b 147 {
cudaChen 6:f34538eea724 148 int status = left * 4 + middle * 2 + right;
cudaChen 6:f34538eea724 149 switch(status) {
cudaChen 6:f34538eea724 150 case 7: // go straight
cudaChen 6:f34538eea724 151 pc.printf("7. go straight\r\n");
cudaChen 10:a14380381d86 152 forward();
cudaChen 6:f34538eea724 153 break;
cudaChen 6:f34538eea724 154 case 6: // turn left
cudaChen 6:f34538eea724 155 pc.printf("6. turn left\r\n");
cudaChen 10:a14380381d86 156 turnLeft();
cudaChen 6:f34538eea724 157 break;
cudaChen 6:f34538eea724 158 case 5: // go straight
cudaChen 6:f34538eea724 159 pc.printf("5. go straight\r\n");
cudaChen 10:a14380381d86 160 forward();
cudaChen 6:f34538eea724 161 break;
cudaChen 6:f34538eea724 162 case 4: // turn left
cudaChen 6:f34538eea724 163 pc.printf("4. turn left\r\n");
cudaChen 10:a14380381d86 164 turnLeft();
cudaChen 6:f34538eea724 165 break;
cudaChen 6:f34538eea724 166 case 3: // turn right
cudaChen 6:f34538eea724 167 pc.printf("3. turn right\r\n");
cudaChen 10:a14380381d86 168 turnRight();
cudaChen 6:f34538eea724 169 break;
cudaChen 6:f34538eea724 170 case 2: // go straight
cudaChen 6:f34538eea724 171 pc.printf("2. go straight\r\n");
cudaChen 10:a14380381d86 172 forward();
cudaChen 6:f34538eea724 173 break;
cudaChen 6:f34538eea724 174 case 1: // turn right
cudaChen 6:f34538eea724 175 pc.printf("1. turn right\r\n");
cudaChen 10:a14380381d86 176 turnRight();
cudaChen 6:f34538eea724 177 break;
cudaChen 6:f34538eea724 178 case 0: // go straight
cudaChen 6:f34538eea724 179 pc.printf("0. go straight\r\n");
cudaChen 10:a14380381d86 180 forward();
cudaChen 6:f34538eea724 181 break;
cudaChen 6:f34538eea724 182 default:
cudaChen 6:f34538eea724 183 pc.printf("invalid\r\n");
cudaChen 6:f34538eea724 184 break;
cudaChen 6:f34538eea724 185 }
cudaChen 5:25faf509ee9b 186 }
cudaChen 5:25faf509ee9b 187
cudaChen 10:a14380381d86 188 void init()
cudaChen 10:a14380381d86 189 {
cudaChen 10:a14380381d86 190 DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
cudaChen 10:a14380381d86 191 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
cudaChen 10:a14380381d86 192 }
cudaChen 10:a14380381d86 193
cudaChen 10:a14380381d86 194 void forward()
cudaChen 10:a14380381d86 195 {
cudaChen 10:a14380381d86 196 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
cudaChen 10:a14380381d86 197 DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
cudaChen 10:a14380381d86 198 }
cudaChen 10:a14380381d86 199
cudaChen 10:a14380381d86 200 void turnLeft()
cudaChen 10:a14380381d86 201 {
cudaChen 10:a14380381d86 202 DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
cudaChen 10:a14380381d86 203 DriveSingleMotor(MOTOR_M2, 2, DIR_FORWARD);
cudaChen 10:a14380381d86 204 }
cudaChen 10:a14380381d86 205
cudaChen 10:a14380381d86 206 void turnRight()
cudaChen 10:a14380381d86 207 {
cudaChen 10:a14380381d86 208 DriveSingleMotor(MOTOR_M1, 2, DIR_FORWARD);
cudaChen 10:a14380381d86 209 DriveSingleMotor(MOTOR_M2, PWR_STOP, DIR_FORWARD);
cudaChen 10:a14380381d86 210 }
cudaChen 10:a14380381d86 211
cudaChen 10:a14380381d86 212