-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboRoarZ_July_2024_EDITIED.ino
234 lines (214 loc) · 5.57 KB
/
RoboRoarZ_July_2024_EDITIED.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
#include "HUSKYLENS.h" // Library for HuskyLens
#include <smorphi_single.h> // Library for Smorphi
#include "SoftwareSerial.h" // Library for Software Serial
Smorphi_single my_robot;
HUSKYLENS huskylens;
//SoftwareSerial myHuskySerial(26, 27);
SoftwareSerial myHuskySerial(16, 17);
// please dont change this
#define straightSpeed 70
#define leftSpeed 300
#define rightSpeed 300
#define angV 80
#define speedBoost 100
#define the180Speed 290 //unused
// dont change this else we cooked
int right_sensor_status = my_robot.module1_sensor_status(4);
int bottom_right_sensor_status = my_robot.module1_sensor_status(2);
int left_sensor_status = my_robot.module1_sensor_status(10);
int bottom_left_sensor_status = my_robot.module1_sensor_status(0);
bool isGoingStraight = 0;
bool isGoingLeft = 0;
bool isGoingRight = 0;
bool is180ing = 0;
bool isSpeedBoost = 0;
// int is180flipStatus = 0;
unsigned long startTime = 0;
unsigned long detectTime = 0;
int color_signature;
int command_block;
void setup() {
//Initialize Serial Monitor
Serial.begin(115200);
myHuskySerial.begin(9600); //Baud rate for HuskyLens communication. Can change from GUI of the HuskyLens
my_robot.BeginSmorphi_single();
while (!huskylens.begin(myHuskySerial)) {
Serial.println(F("Begin failed!"));
delay(100);
}
my_robot.BeginSmorphi_single();
pinMode(25,OUTPUT);
pinMode(26,OUTPUT);
pinMode(27,OUTPUT);
// DO NOT DELETE / COMMENT THESE LINES !!!!
red_on();
delay(1000);
led_off();
green_on();
delay(1000);
led_off();
blue_on();
delay(1000);
led_off();
white_on();
delay(1000);
}
void loop() {
Serial.println("isGoingStraight " + isGoingStraight);
Serial.println("isSpeedBoost " + isSpeedBoost);
// DO NOT DELETE / COMMENT THESE LINES !!!!
int x = 0;
if (huskylens.request()) {
if (huskylens.available()) {
// HUSKYLENSResult result = huskylens.read();
// Serial.println(F("###########"));
HUSKYLENSResult result = huskylens.read();
printResult(result);
if (result.command == COMMAND_RETURN_BLOCK) {
x=result.ID;
}
}
}else{
Serial.println("Error!");
}
if ((isGoingLeft || isGoingRight) && ((millis() - startTime) > 5000)) {
isGoingStraight = 0;
isGoingRight = 0;
isGoingLeft = 0;
isSpeedBoost = 1; // VROOM. GAS GAS GAS
detectTime = millis();
led_off();
// if (is180flipStatus == 1) {
// is180flipStatus = 2;
// }
// if (is180flipStatus > 1)
// {
// digitalWrite(27, HIGH);
// digitalWrite(26, HIGH);
// }
}
else if (isGoingStraight && ((millis() - startTime) > 3500))
{
isGoingStraight = 0;
isGoingRight = 0;
isGoingLeft = 0;
is180ing = 0;
isSpeedBoost = 0; // speedboost turns off with the 180 part regardless of any turns
led_off();
}
else if (is180ing && ((millis() - startTime) > 3430)) {
isGoingStraight = 0;
isGoingRight = 0;
isGoingLeft = 0;
is180ing = 0;
// is180flipStatus = 1;
led_off();
}
// check for speedboost; turn it off after X seconds
if (isSpeedBoost && ((millis() - detectTime) > 2000)) {
isSpeedBoost = 0;
}
switch (x){
case 1:
red_on();
break;
case 2:
green_on();
break;
case 3:
blue_on();
break;
case 4:
// LEFT
led_off();
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
isGoingLeft = 1;
isGoingRight = 0;
isGoingStraight = 0;
is180ing = 0;
startTime = millis();
break;
case 5:
// FORWARD + BOOST
led_off();
digitalWrite(27, HIGH);
digitalWrite(26, HIGH);
isGoingStraight = 1;
isSpeedBoost = 1; // FOR THE FORWARD PART near the 180 segment
isGoingRight = 0;
isGoingLeft = 0;
is180ing = 0;
startTime = millis();
break;
case 6:
//RIGHT
led_off();
digitalWrite(27, HIGH);
digitalWrite(25, HIGH);
isGoingRight = 1;
isGoingLeft = 0;
isGoingStraight = 0;
is180ing = 0;
startTime = millis();
break;
case 7:
// round and round we go
white_on();
isGoingRight = 0;
isGoingLeft = 0;
isGoingStraight = 0;
is180ing = 1;
startTime = millis();
detectTime = millis();
break;
case 8:
// NOTHING
break;
case 9:
// NOTHING
break;
default:
1==1; //DUMMY COMMAND (CAN IGNORE)
}
sensor_initialisation();
}
void printResult(HUSKYLENSResult result){
// DO NOT DELETE / COMMENT THESE LINES !!!!
if (result.command == COMMAND_RETURN_BLOCK){
// Serial.println(String()+F("Block:xCenter=")+result.xCenter+F(",yCenter=")+result.yCenter+F(",width=")+result.width+F(",height=")+result.height+F(",ID=")+result.ID);
Serial.println(result.ID);
}
else if (result.command == COMMAND_RETURN_ARROW){
Serial.println("Wrong mode");
// Serial.println(String()+F("Arrow:xOrigin=")+result.xOrigin+F(",yOrigin=")+result.yOrigin+F(",xTarget=")+result.xTarget+F(",yTarget=")+result.ID);
}
else{
Serial.println("Object unknown!");
}
}
void red_on(){
digitalWrite(25, HIGH);
digitalWrite(26, LOW);
digitalWrite(27, LOW);
}
void green_on(){
digitalWrite(25, LOW);
digitalWrite(26, HIGH);
digitalWrite(27, LOW);
}
void blue_on(){
digitalWrite(25, LOW);
digitalWrite(26, LOW);
digitalWrite(27, HIGH);
}
void white_on(){
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
}
void led_off(){
digitalWrite(25, LOW);
digitalWrite(26, LOW);
digitalWrite(27, LOW);
}