-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot_Code.ino
More file actions
186 lines (148 loc) · 5.2 KB
/
Robot_Code.ino
File metadata and controls
186 lines (148 loc) · 5.2 KB
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
//===================================================================
//===================================================================
// Masonville Dynamics (Warman) - Snr Design WEC 2021 Source Code
// Developed by Mason K., Samson H., Adrian K, Owen K.,
// Jan 30th, 2021
//===================================================================
//===================================================================
//===================================================================
// Pin Declarations
//===================================================================
//Motors
#define leftMotor 11
#define hMotor 12
#define rightMotor 13
#define revolverMotor 6
#define extenderMotor 5
//Sensors
#define leftEcho A0
#define leftTrig 4
#define rightEcho A1
#define rightTrig 3
#define forwardEcho A2
#define forwardTrig 2
#define backwardEcho A3
#define backwardTrig 7
//Initialized Values
int Room;
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int safe_distance = 5; // safe distance set to 5 cm
bool safe = true; //Safety declaration
//=========================================================
//Object Detection Variables
//=========================================================
#define LT_Sides digitalRead(2) //IR sensor (left and right)
#define LT_M digitalRead(3) //IR Sensor (middle)
//=========================================================
//Finite State Machine
//=========================================================
//Enumeration for States (Stop, Idle, And Run)
enum RobotState {stop, idle, run};
enum RobotState current_state = stop;
//=========================================================
//Functions
//=========================================================
//checkObstacle() - This code determines whether there is any obstacles and returns true or false
bool checkObstacle(){
//State to detect obstacle interference
bool rightDetect = false;
bool leftDetect = false;
bool forwardDetect = false;
bool backwardDetect = false;
//Check each of the four sensors to determine whether there is any obstacle interference
//Uses a helper function to ping
rightDetect = ping(rightEcho, rightTrig);
leftDetect = ping(leftEcho, leftTrig);
forwardDetect = ping(forwardEcho, forwardTrig);
backwardDetect = ping(backwardEcho, backwardTrig);
//I am bad at bitshifting, so I will use two nested OR loops
if((rightDetect||leftDetect)||(forwardDetect || backwardDetect)){
return false;
}else{
return true;
}
}
//ping() is a helper function for checkObstacle(), it takes in a trigger and echo and returns whether there is an obstacle interference
bool ping(int echo, int trigger){
int duration = 0;
//Ping ultrasonic signal
digitalWrite(trigger, LOW);
delayMicroseconds(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
//Determine the time for bounce
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
//Divide by duration by 58 to get cm
duration = duration/58;
//If distance is less than 25cm, stop the robot to be safe
if(duration < 50){
return true;
}
else{
return false;
}
}
//=========================================================
//=========================================================
// Setup
//=========================================================
//=========================================================
void setup()
{
//Chooses a 9600 baud rate
Serial.begin(9600);
//Motor Pin Declarations
pinMode(leftMotor, OUTPUT);
pinMode(hMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
pinMode(revolverMotor, OUTPUT);
pinMode(extenderMotor, OUTPUT);
//Sensor Pin Declarations
pinMode(leftEcho, INPUT);
pinMode(rightEcho, INPUT);
pinMode(forwardEcho, INPUT);
pinMode(backwardEcho, INPUT);
pinMode(leftTrig, OUTPUT);
pinMode(rightTrig, OUTPUT);
pinMode(forwardTrig, OUTPUT);
pinMode(backwardTrig, OUTPUT);
}
//=========================================================
//=========================================================
// Loop
//=========================================================
//=========================================================
void loop()
{
//=========================================================
//Safety Check - Checks to see if there is any interference
//=========================================================
safe = checkObstacle();
//Enums are buggy in arduino so we had to use a boolean lol
if(safe){
current_state = idle;
}else{
current_state = stop;
}
//===============================================================================
//Command Check - Checks to see if there is any instructions from the transmitter
//===============================================================================
// TODO: Implement reciever and transmitter code in here
//Changes state based on the two checks
switch (current_state){
case stop:
//All the code the robot should be running when stopped
Serial.println("Robot is currently stopped");
break;
case idle:
//All the code the robot should run when idle
Serial.println("Robot await instructions");
break;
case run:
//
break;
}
}