-
Notifications
You must be signed in to change notification settings - Fork 0
/
light-sensing-robot.ino
429 lines (362 loc) · 11.4 KB
/
light-sensing-robot.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/********************************************************************
ECEN 240/301 Lab Code
Light-Following Robot
The approach of this code is to use an architecture that employs
three different processes:
Perception
Planning
Action
By separating these processes, this allows one to focus on the
individual elements needed to do these tasks that are general
to most robotics.
Version History
1.1.3 11 January 2023 Creation by Dr. Mazzeo and TAs from 2022 version
********************************************************************/
#include "Arduino.h"
#include <CapacitiveSensor.h>
#include <Servo.h>
#include <NewPing.h>
#define PHOTODIODE_1 A1 // left diode, turn left
#define PHOTODIODE_2 A2 // front diode, move down
#define PHOTODIODE_3 A3 // right diode, turn right
#define PHOTODIODE_4 A4 // back diode, move up
#define H_BRIDGE_ENA 3
#define LED_3 4
#define H_BRIDGE_ENB 5
// Capacitive sensor pins - Lab 4
#define CAP_SENSOR_SEND 11
#define CAP_SENSOR_RECEIVE 10
// Ultrasonic sensor pin - Lab 6
#define TRIGGER_PIN 8
#define ECHO_PIN 7
// Configuration parameter definitions
#define BUTTON_THRESHOLD 2.5
// Voltage at which a photodiode voltage is considered to be present - Lab 5
#define PHOTODIODE_LIGHT_THRESHOLD 2.7
// Number of samples that the capacitor sensor will use in a measurement - Lab 4
#define CAP_SENSOR_SAMPLES 40
#define CAP_SENSOR_TAU_THRESHOLD 100
// The speeds for the wheels from the capactance - Lab 4
#define SPEED_STOP 0
#define SPEED_LOW (int) (255 * 0.45)
#define SPEED_MED (int) (255 * 0.75)
#define SPEED_HIGH (int) (255 * 1)
// Servo pin - Lab 6
#define SERVO_PIN 9
// Parameters for servo control as well as instantiation - Lab 6
#define SERVO_START_ANGLE 90
#define SERVO_UP_LIMIT 160
#define SERVO_DOWN_LIMIT 20
static Servo myServo;
// Parameters for ultrasonic sensor and instantiation - Lab 6
#define MAX_DISTANCE 200 // centimeters
static NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Parameter to define when the ultrasonic sensor detects a collision - Lab 6
#define COLLISION_DISTANCE 15
/***********************************************************/
// Definitions that allow one to set states
#define DETECTION_NO 0
#define DETECTION_YES 1
#define COLLISION_ON 0
#define COLLISION_OFF 1
#define DRIVE_STOP 0
#define DRIVE_LEFT 1
#define DRIVE_RIGHT 2
#define DRIVE_STRAIGHT 3
#define SERVO_MOVE_STOP 0
#define SERVO_MOVE_UP 1
#define SERVO_MOVE_DOWN 2
// Global variables for PERCEPTION
int SensedCollision = DETECTION_NO;
int SensedLightRight = DETECTION_NO;
int SensedLightLeft = DETECTION_NO;
int SensedLightUp = DETECTION_NO;
int SensedLightDown = DETECTION_NO;
int SensedCapacitiveTouch = DETECTION_NO;
// Global variables for ACTION
int ActionCollision = COLLISION_OFF;
int ActionRobotDrive = DRIVE_STOP;
int ActionRobotSpeed = SPEED_STOP;
int ActionServoMove = SERVO_MOVE_STOP;
void setup() {
Serial.begin(9600);
pinMode(H_BRIDGE_ENA, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(H_BRIDGE_ENB, OUTPUT);
pinMode(PHOTODIODE_1, INPUT);
pinMode(PHOTODIODE_2, INPUT);
pinMode(PHOTODIODE_3, INPUT);
pinMode(PHOTODIODE_4, INPUT);
pinMode(CAP_SENSOR_RECEIVE, INPUT);
pinMode(CAP_SENSOR_SEND, OUTPUT);
//Set up servo - Lab 6
myServo.attach(SERVO_PIN);
myServo.write(SERVO_START_ANGLE);
pinMode(TRIGGER_PIN, OUTPUT); // pulse sent out through TRIGGER_PIN
pinMode(ECHO_PIN, INPUT); // return signal read through ECHO_PIN
}
/********************************************************************
Main LOOP function - this gets executed in an infinite loop until
power off or reset. - Notice: PERCEPTION, PLANNING, ACTION
********************************************************************/
void loop() {
RobotPerception();
RobotPlanning();
RobotAction();
}
void RobotPerception() {
// Photodiode Sensing
if (isLight(PHOTODIODE_1)){
SensedLightLeft = DETECTION_YES;
} else {
SensedLightLeft = DETECTION_NO;
}
if (isLight(PHOTODIODE_3)) {
SensedLightRight = DETECTION_YES;
} else {
SensedLightRight = DETECTION_NO;
}
// Add sensing for light up and down
if (isButtonPushed(PHOTODIODE_4)){
SensedLightUp = DETECTION_YES;
} else {
SensedLightUp = DETECTION_NO;
}
if (isButtonPushed(PHOTODIODE_2)) {
SensedLightDown = DETECTION_YES;
} else {
SensedLightDown = DETECTION_NO;
}
// Collision Sensing
if (isCollision()) {
SensedCollision = DETECTION_YES;
} else {
SensedCollision = DETECTION_NO;
}
if (isCapacitiveSensorTouched()) {
SensedCapacitiveTouch = DETECTION_YES;
}
else {
SensedCapacitiveTouch = DETECTION_NO;
}
}
float getPinVoltage(int pin) {
return 5 * (float)analogRead(pin) / 1024;
}
bool isButtonPushed(int button_pin) {
return getPinVoltage(button_pin) >= BUTTON_THRESHOLD;
}
bool isCollision() {
int sonar_distance = sonar.ping_cm(); // If the distance is too big, it returns 0.
if(sonar_distance != 0){
return (sonar_distance < COLLISION_DISTANCE);
} else {
return false;
}
}
bool isCapacitiveSensorTouched() {
static CapacitiveSensor sensor
= CapacitiveSensor(CAP_SENSOR_SEND, CAP_SENSOR_RECEIVE);
long tau
= sensor.capacitiveSensor(CAP_SENSOR_SAMPLES);
return tau > CAP_SENSOR_TAU_THRESHOLD;
}
bool isLight(int pin) {
float light = getPinVoltage(pin);
return (light > PHOTODIODE_LIGHT_THRESHOLD);
}
void RobotPlanning(void) {
fsmCollisionDetection();
fsmSteerRobot();
fsmMoveServoUpAndDown();
fsmCapacitiveSensorSpeedControl();
}
void fsmCollisionDetection() {
static int collisionDetectionState = 0;
switch (collisionDetectionState) {
case 0:
ActionCollision = COLLISION_ON;
if (SensedCollision == DETECTION_NO) {
collisionDetectionState = 1;
}
break;
case 1:
ActionCollision = COLLISION_OFF;
fsmSteerRobot();
if (SensedCollision == DETECTION_YES) {
collisionDetectionState = 0;
}
break;
}
}
void fsmSteerRobot() {
static int steerRobotState = 0;
// Add a check to ensure that driving actions are not allowed if a collision has been detected.
if (ActionCollision == COLLISION_ON) {
// If a collision is detected, ensure the robot does not attempt to drive left or right.
ActionRobotDrive = DRIVE_STOP;
return; // Exit the function early as no further steering action should be taken.
}
switch (steerRobotState) {
case 0: // No light detected
ActionRobotDrive = DRIVE_STOP;
if (SensedLightLeft == DETECTION_YES) {
steerRobotState = 1; // Light is on the left, prepare to steer left
} else if (SensedLightRight == DETECTION_YES) {
steerRobotState = 2; // Light is on the right, prepare to steer right
}
break;
case 1: // Light is to the left of the robot
ActionRobotDrive = DRIVE_LEFT;
if (SensedLightRight == DETECTION_YES) {
steerRobotState = 3; // Light detected on both sides, consider stopping or going straight
} else if (SensedLightLeft == DETECTION_NO) {
steerRobotState = 0; // No light detected, stop
}
break;
case 2: // Light is to the right of the robot
ActionRobotDrive = DRIVE_RIGHT;
if (SensedLightLeft == DETECTION_YES) {
steerRobotState = 3; // Light detected on both sides, consider stopping or going straight
} else if (SensedLightRight == DETECTION_NO) {
steerRobotState = 0; // No light detected, stop
}
break;
case 3: // Light detected on both sides
ActionRobotDrive = DRIVE_STRAIGHT;
if (SensedLightLeft == DETECTION_NO && SensedLightRight == DETECTION_NO) {
steerRobotState = 0; // No light detected, stop
} else if (SensedLightRight == DETECTION_NO) {
steerRobotState = 1; // Only light on the left, turn left
} else if (SensedLightLeft == DETECTION_NO) {
steerRobotState = 2; // Only light on the right, turn right
}
break;
}
}
void fsmMoveServoUpAndDown() {
static int moveServoState = 0;
switch (moveServoState) {
case 0:
ActionServoMove = SERVO_MOVE_STOP;
if (SensedLightUp == DETECTION_YES && SensedLightDown == DETECTION_NO) {
moveServoState = 1;
} else if (SensedLightUp == DETECTION_NO && SensedLightDown == DETECTION_YES) {
moveServoState = 2;
}
break;
case 1:
ActionServoMove = SERVO_MOVE_UP;
if ((SensedLightUp == DETECTION_YES && SensedLightDown == DETECTION_YES) || (SensedLightUp == DETECTION_NO && SensedLightDown == DETECTION_NO)) {
moveServoState = 0;
}
break;
case 2:
ActionServoMove = SERVO_MOVE_DOWN;
if ((SensedLightUp == DETECTION_YES && SensedLightDown == DETECTION_YES) || (SensedLightUp == DETECTION_NO && SensedLightDown == DETECTION_NO)) {
moveServoState = 0;
}
break;
}
}
////////////////////////////////////////////////////////////////////
// State machine for detecting when the capacitive sensor is
// touched, and changing the robot's speed.
////////////////////////////////////////////////////////////////////
void fsmCapacitiveSensorSpeedControl() {
static int capacitiveSensorState = 0;
switch (capacitiveSensorState) {
case 0: // Wait for button press
if (SensedCapacitiveTouch == DETECTION_YES) {
capacitiveSensorState = 1;
}
break;
case 1: // Wait for release
if (SensedCapacitiveTouch == DETECTION_NO) {
capacitiveSensorState = 2;
}
break;
case 2: // Toggle speed! (fsmChangeSpeed)
fsmChangeSpeed();
capacitiveSensorState = 0;
break;
}
}
////////////////////////////////////////////////////////////////////
// State machine for cycling through the robot's speeds.
////////////////////////////////////////////////////////////////////
void fsmChangeSpeed() {
static int changeSpeedState = 0;
/*Implement in lab 4*/
switch(changeSpeedState) {
case 0: // Speed stop
ActionRobotSpeed = SPEED_STOP;
changeSpeedState = 1;
break;
case 1: // Speed low
ActionRobotSpeed = SPEED_LOW;
changeSpeedState = 2;
break;
case 2: //Speed medium
ActionRobotSpeed = SPEED_MED;
changeSpeedState = 3;
break;
case 3: // Speed high;
ActionRobotSpeed = SPEED_HIGH;
changeSpeedState = 0;
break;
}
}
void RobotAction() {
switch(ActionCollision) {
case COLLISION_OFF:
doTurnLedOff(LED_3);
break;
case COLLISION_ON:
doTurnLedOn(LED_3);
break;
}
switch(ActionRobotDrive) {
case DRIVE_STOP:
analogWrite(H_BRIDGE_ENB, 0);
analogWrite(H_BRIDGE_ENA, 0);
break;
case DRIVE_STRAIGHT:
analogWrite(H_BRIDGE_ENA, ActionRobotSpeed);
analogWrite(H_BRIDGE_ENB, ActionRobotSpeed);
break;
case DRIVE_LEFT:
analogWrite(H_BRIDGE_ENB, 0);
analogWrite(H_BRIDGE_ENA, ActionRobotSpeed);
break;
case DRIVE_RIGHT:
analogWrite(H_BRIDGE_ENB, ActionRobotSpeed);
analogWrite(H_BRIDGE_ENA, 0);
break;
}
MoveServo();
}
void MoveServo() {
static int servoAngle = SERVO_START_ANGLE;
switch(ActionServoMove) {
case SERVO_MOVE_STOP:
break;
case SERVO_MOVE_UP:
if (servoAngle < SERVO_UP_LIMIT) {
servoAngle++;
}
break;
case SERVO_MOVE_DOWN:
if (servoAngle > SERVO_DOWN_LIMIT) {
servoAngle--;
}
break;
}
myServo.write(servoAngle);
}
void doTurnLedOn(int led_pin) {
digitalWrite(led_pin, HIGH);
}
void doTurnLedOff(int led_pin) {
digitalWrite(led_pin, LOW);
}