-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild.ino
203 lines (170 loc) · 4.43 KB
/
child.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
#include <Wire.h>
int GREEN = 2;
int RED = 3;
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
char messageBuffer[80];
struct stackdef {
String tokens[80];
};
class Parser {
public:
int tokens;
stackdef msg;
int index = 0;
Parser(stackdef &msg, int tokens) {
this-> msg = msg;
this-> tokens = tokens;
parseTokens();
}
void startEngine(char a) {
Serial.println("Starting engine");
analogWrite(E1, a); // PWM Speed Control
analogWrite(E2, a); // PWM Speed Control
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
}
void turnRight (char a, char b) //Turn Right
{
analogWrite (E1, a);
digitalWrite(M1, HIGH);
analogWrite (E2, b);
digitalWrite(M2, LOW);
}
void turnLeft (char a, char b) //Turn Right
{
analogWrite (E1, a);
digitalWrite(M1, LOW);
analogWrite (E2, b);
digitalWrite(M2, HIGH);
}
void stopEngine(void) {
Serial.println("Stopping Engine");
digitalWrite(E1, LOW);
digitalWrite(E2, LOW);
}
void uwbFuncs() {
if (msg.tokens[index] == "start") {
startUWB();
index++;
}
else if (msg.tokens[index] == "stop") {
stopUWB();
index++;
}
}
void startUWB() {
Serial.println("Starting Ultra Wide Band...");
}
void stopUWB() {
Serial.println("Stopping Ultra Wide Band...");
}
void reverse (char a, char b) //Move backward
{
analogWrite (E1, a);
digitalWrite(M1, LOW);
analogWrite (E2, b);
digitalWrite(M2, LOW);
}
void engineFuncs() {
if (msg.tokens[index] == "start") {
startEngine(255);
index++;
} else if (msg.tokens[index] == "stop") {
stopEngine();
index++;
} else if (msg.tokens[index] == "right"){
turnRight(250, 250);
index++;
} else if(msg.tokens[index] == "reverse"){
reverse(250, 250);
index++;
} else if(msg.tokens[index] == "left"){
turnLeft(250, 250);
index++;
}
}
void parseTokens() {
while (index < tokens) {
if (msg.tokens[index] == "engine") {
index++;
engineFuncs();
} else if (msg.tokens[index] == "uwb") {
index++;
uwbFuncs();
} else if (msg.tokens[index] == ";") {
index++;
} else {
Serial.println("Invalid input. "
"Commands are written in a noun verb structure."
"ie noun: 'engine' verb: 'start'");
index++;
break;
}
}
}
};
class Tokenizer {
public:
int index = 0;
int count = 0;
stackdef message;
char* msg;
Tokenizer(char msg[]) {
this -> msg = msg;
getTokens();
}
void addToStack(String &str) {
message.tokens[index] = str;
index++;
count++;
str = "";
}
void advance(String &str, char c ) {
str+= c;
count++;
}
void getTokens() {
String str = "";
char c = msg[count];
while (count < strlen(msg) +1) {
if (isspace(c)) {
skipWhitespace();
addToStack(str);
c = msg[count];
} else if (c == '\0') {
addToStack(str);
} else {
advance(str, c);
c = msg[count];
}
}
}
char peek() {
char peekValue = msg[count+1];
return peekValue;
}
void skipWhitespace() {
while (isspace(peek())) {
count++;
}
}
};
void receiveEvent(int howMany) {
byte index = 0;
while (Wire.available() && (index < 80)){ // Make sure that only a max of 8 bytes are read {
messageBuffer[index++] = Wire.read(); // Read one byte to index and increment (++) index
}
Tokenizer tokens(messageBuffer);
Parser parser(tokens.message, tokens.index);
memset(messageBuffer, 0, sizeof(messageBuffer));
}
void setup() {
Serial.begin(115200);
Wire.begin(9);
Wire.onReceive(receiveEvent);
}
void loop() {
}