forked from rricklic/arduino.uctronics.robot.car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuctronics_robot_car.ino
287 lines (252 loc) · 6.07 KB
/
uctronics_robot_car.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <Servo.h>
//HC-SR04 (5V) - ultrasonic sensor to measure distance
#define SONAR_TRIG_PIN A5
#define SONAR_ECHO_PIN 2
#define SONAR_INTERRUPT 0 //Note: pin 2
volatile int distance = -1;
volatile long distCalcStartTime = 0;
//L293D Motor Drive
#define MOTOR1_PWM_PIN 11
#define MOTOR2_PWM_PIN 3
#define MOTOR3_PWM_PIN 6
#define MOTOR4_PWM_PIN 5
#define SHIFTREG_LATCH_PIN 12
#define SHIFTREG_CLK_PIN 4
#define SHIFTREG_ENABLE_PIN 7
#define SHIFTREG_DATA_PIN 8
//74HC595 shift register - used to set the direction of the bridge driver
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6
//Codes for motor usage
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4
#define MAX_PWM 255
#define MIN_PWM 0
#define NO_PWM -1
#define LEFT_MOTOR 3
#define RIGHT_MOTOR 4
//SG90 9G Mini Servo (5V)
#define SERVO_PIN 10
//#define SERVO2_PIN 9 (not used... yet)
Servo servo;
void setup()
{
//HC-SR04
pinMode(SONAR_TRIG_PIN, OUTPUT);
pinMode(SONAR_ECHO_PIN, INPUT);
//interrupt when SONAR_ECHO_PIN changes; used instead of pulseIn(SONAR_ECHO_PIN, HIGH);
attachInterrupt(SONAR_INTERRUPT, calculateDistance, CHANGE);
//L293D Motor Drive - shift register
pinMode(SHIFTREG_LATCH_PIN, OUTPUT);
pinMode(SHIFTREG_ENABLE_PIN, OUTPUT);
pinMode(SHIFTREG_DATA_PIN, OUTPUT);
pinMode(SHIFTREG_CLK_PIN, OUTPUT);
digitalWrite(SHIFTREG_DATA_PIN, LOW);
digitalWrite(SHIFTREG_LATCH_PIN, LOW);
digitalWrite(SHIFTREG_CLK_PIN, LOW);
digitalWrite(SHIFTREG_ENABLE_PIN, LOW);
//SG90
servo.attach(SERVO_PIN);
moveServo(90);
Serial.begin(9600);
}
void moveMotor(int motorNumber, int command, int speed)
{
int motorA, motorB;
switch(motorNumber)
{
case 1:
motorA = MOTOR1_A;
motorB = MOTOR1_B;
break;
case 2:
motorA = MOTOR2_A;
motorB = MOTOR2_B;
break;
case 3:
motorA = MOTOR3_A;
motorB = MOTOR3_B;
break;
case 4:
motorA = MOTOR4_A;
motorB = MOTOR4_B;
break;
default:
return;
}
switch(command)
{
case FORWARD:
setMotorOutput(motorA, HIGH, speed);
setMotorOutput(motorB, LOW, NO_PWM);
break;
case BACKWARD:
setMotorOutput(motorA, LOW, speed);
setMotorOutput(motorB, HIGH, NO_PWM);
break;
case BRAKE:
setMotorOutput(motorA, LOW, MAX_PWM);
setMotorOutput(motorB, LOW, NO_PWM);
break;
case RELEASE:
setMotorOutput(motorA, LOW, MIN_PWM);
setMotorOutput(motorB, LOW, NO_PWM);
break;
default:
break;
}
}
void setMotorOutput(int output, int highOrLow, int speed)
{
int motorPWMPin;
switch(output)
{
case MOTOR1_A:
case MOTOR1_B:
motorPWMPin = MOTOR1_PWM_PIN;
break;
case MOTOR2_A:
case MOTOR2_B:
motorPWMPin = MOTOR2_PWM_PIN;
break;
case MOTOR3_A:
case MOTOR3_B:
motorPWMPin = MOTOR3_PWM_PIN;
break;
case MOTOR4_A:
case MOTOR4_B:
motorPWMPin = MOTOR4_PWM_PIN;
break;
default:
return;
}
//Set motor direction with the shift register
shiftWrite(output, highOrLow);
if(speed >= MIN_PWM && speed <= MAX_PWM) {
analogWrite(motorPWMPin, speed);
}
}
void shiftWrite(int output, int highOrLow)
{
static int latchCopy = 0;
bitWrite(latchCopy, output, highOrLow);
//Shift SHIFTREG_CLK_PIN bits as clock pulse.
//The 74HC595 shift register uses most-significant-bit (MSB) first.
shiftOut(SHIFTREG_DATA_PIN, SHIFTREG_CLK_PIN, MSBFIRST, latchCopy);
//Generate a latch pulse with SHIFTREG_LATCH_PIN.
delayMicroseconds(5);
digitalWrite(SHIFTREG_LATCH_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(SHIFTREG_LATCH_PIN, LOW);
}
void moveForward(int speed)
{
moveMotor(LEFT_MOTOR, FORWARD, speed);
moveMotor(RIGHT_MOTOR, FORWARD, speed);
}
void moveBackward(int speed)
{
moveMotor(LEFT_MOTOR, BACKWARD, speed);
moveMotor(RIGHT_MOTOR, BACKWARD, speed);
}
void turnLeft(int speed)
{
moveMotor(LEFT_MOTOR, BACKWARD, speed);
moveMotor(RIGHT_MOTOR, FORWARD, speed);
}
void turnRight(int speed)
{
moveMotor(LEFT_MOTOR, FORWARD, speed);
moveMotor(RIGHT_MOTOR, BACKWARD, speed);
}
void stopMoving()
{
moveMotor(LEFT_MOTOR, RELEASE, MIN_PWM);
moveMotor(RIGHT_MOTOR, RELEASE, MIN_PWM);
}
void moveServo(int angle)
{
servo.write(angle);
}
void startDistanceCalculation()
{
//Clear the SONAR_TRIG_PIN
digitalWrite(SONAR_TRIG_PIN, LOW);
delayMicroseconds(2);
//Set the SONAR_TRIG_PIN on HIGH state for 10 micro seconds
digitalWrite(SONAR_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(SONAR_TRIG_PIN, LOW);
}
void calculateDistance()
{
switch(digitalRead(SONAR_ECHO_PIN))
{
//Start of pulse
case HIGH:
distCalcStartTime = micros();
break;
//Pulse done; calculate distance in cm
case LOW:
distance = (micros() - distCalcStartTime)*0.034/2;
distCalcStartTime = 0;
break;
}
}
long calculateDistanceNow()
{
startDistanceCalculation();
long duration = pulseIn(SONAR_ECHO_PIN, HIGH);
return (duration/2) / 29.1;
}
void loop()
{
//Calculate distance
Serial.print("Distance: ");
Serial.println(distance);
if(!distCalcStartTime) {
startDistanceCalculation();
}
if(distance > 40) {
//No obstacles detected - full speed ahead!
moveForward(MAX_PWM);
delay(15);
}
else if(distance <= 40 && distance > 20) {
//Upcoming obstacle detected - slow down
moveForward(MAX_PWM/2);
delay(15);
}
else {
//Detcted obstacle way too close for comfort - backup and turn
stopMoving();
//Scan area
moveServo(120);
delay(250);
long leftDist = calculateDistanceNow();
moveServo(60);
delay(250);
long rightDist = calculateDistanceNow();
moveServo(90);
moveBackward(MAX_PWM);
delay(500);
if(leftDist >= rightDist) {
turnLeft(MAX_PWM);
}
else {
turnRight(MAX_PWM);
}
delay(350);
stopMoving();
calculateDistanceNow();
delay(15);
}
}