forked from mirobot/mirobot-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirobot.cpp
282 lines (252 loc) · 5.84 KB
/
Mirobot.cpp
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
#include "Arduino.h"
#include "Mirobot.h"
HotStepper motor1(&PORTB, 0b00011101);
HotStepper motor2(&PORTD, 0b11110000);
CmdProcessor::CmdProcessor p;
// Pointer to the bootloader memory location
void* bl = (void *) 0x3c00;
Mirobot::Mirobot(){
blocking = true;
mainState = POWERED_UP;
lastLedChange = millis();
}
void Mirobot::setup(){
HotStepper::setup(TIMER1INT);
// Set up the pen arm servo
pinMode(SERVO_PIN, OUTPUT);
// Set up the collision sensor inputs and state
pinMode(LEFT_COLLIDE_SENSOR, INPUT_PULLUP);
pinMode(RIGHT_COLLIDE_SENSOR, INPUT_PULLUP);
collideState = NORMAL;
//setPenState(UP);
// Set up the status LED
pinMode(STATUS_LED, OUTPUT);
}
void Mirobot::setup(Stream &s){
HotStepper::setup(TIMER1INT);
// Set up the pen arm servo
pinMode(SERVO_PIN, OUTPUT);
// Set up the collision sensor inputs and state
pinMode(LEFT_COLLIDE_SENSOR, INPUT_PULLUP);
pinMode(RIGHT_COLLIDE_SENSOR, INPUT_PULLUP);
collideState = NORMAL;
setPenState(UP);
// We will be non-blocking so we can continue to process serial input
blocking = false;
// Set up the command processor
p.setup(s, self());
// Set up the status LED
pinMode(STATUS_LED, OUTPUT);
// Set up the ready pin to communicate with the WiFi module
pinMode(WIFI_READY, INPUT); //nReady
penup();
initHwVersion();
}
void Mirobot::initHwVersion(){
if(EEPROM.read(0) == MAGIC_BYTE_1 && EEPROM.read(1) == MAGIC_BYTE_2){
// We've previously written something valid to the EEPROM
hwVersion.major = EEPROM.read(2);
hwVersion.minor = EEPROM.read(3);
}else{
hwVersion.major = 0;
hwVersion.minor = 0;
}
}
void Mirobot::setHwVersion(char &version){
char v[4];
char i;
char v_ptr = 0;
char *ptr = &version;
for(i = 0; i < 9; i++){
if(ptr[i] >= 0x30 && ptr[i] <= 0x39){
v[v_ptr++] = ptr[i];
}
if(ptr[i] == '.'){
v[v_ptr++] = '\0';
break;
}
}
hwVersion.major = atoi(v);
v_ptr = 0;
for(i = i; i < 9; i++){
if(ptr[i] >= 0x30 && ptr[i] <= 0x39){
v[v_ptr++] = ptr[i];
}
if(ptr[i] == '\0'){
v[v_ptr++] = '\0';
break;
}
}
v[v_ptr] = '\0';
hwVersion.minor = atoi(v);
EEPROM.write(0, MAGIC_BYTE_1);
EEPROM.write(1, MAGIC_BYTE_2);
EEPROM.write(2, hwVersion.major);
EEPROM.write(3, hwVersion.minor);
}
void Mirobot::forward(int distance){
motor1.turn(distance * STEPS_PER_MM, FORWARD);
motor2.turn(distance * STEPS_PER_MM, BACKWARD);
wait();
}
void Mirobot::back(int distance){
motor1.turn(distance * STEPS_PER_MM, BACKWARD);
motor2.turn(distance * STEPS_PER_MM, FORWARD);
wait();
}
void Mirobot::left(int angle){
motor1.turn(angle * STEPS_PER_DEGREE, FORWARD);
motor2.turn(angle * STEPS_PER_DEGREE, FORWARD);
wait();
}
void Mirobot::right(int angle){
motor1.turn(angle * STEPS_PER_DEGREE, BACKWARD);
motor2.turn(angle * STEPS_PER_DEGREE, BACKWARD);
wait();
}
void Mirobot::penup(){
setPenState(UP);
wait();
}
void Mirobot::pendown(){
setPenState(DOWN);
wait();
}
void Mirobot::pause(){
motor1.pause();
motor2.pause();
paused = true;
}
void Mirobot::resume(){
motor1.resume();
motor2.resume();
paused = false;
}
void Mirobot::stop(){
motor1.stop();
motor2.stop();
following = false;
colliding = false;
}
void Mirobot::reset(){
// Give the response message time to get out
delay(100);
goto *bl;
}
void Mirobot::follow(){
following = true;
}
void Mirobot::collide(){
colliding = true;
}
void Mirobot::beep(int duration){
tone(SPEAKER_PIN, NOTE_C4, duration);
}
boolean Mirobot::ready(){
return (motor1.ready() && motor2.ready() && !servo_pulses_left);
}
void Mirobot::wait(){
if(blocking){
while(!ready()){
if(servo_pulses_left){
servoHandler();
}
}
}
}
void Mirobot::setPenState(penState_t state){
penState = state;
servo_pulses_left = SERVO_PULSES;
next_servo_pulse = 0;
}
void Mirobot::checkState(){
if(!digitalRead(WIFI_READY)){
mainState = CONNECTED;
}else{
mainState = POWERED_UP;
}
}
void Mirobot::followHandler(){
if(motor1.ready() && motor2.ready()){
int diff = analogRead(LEFT_LINE_SENSOR) - analogRead(RIGHT_LINE_SENSOR);
if(diff > 5){
right(1);
}else if(diff < -5){
left(1);
}else{
forward(5);
}
}
}
void Mirobot::collideHandler(){
boolean collideLeft = !digitalRead(LEFT_COLLIDE_SENSOR);
boolean collideRight = !digitalRead(RIGHT_COLLIDE_SENSOR);
if(collideState == NORMAL){
if(collideLeft){
collideState = LEFT_REVERSE;
back(50);
}else if(collideRight){
collideState = RIGHT_REVERSE;
back(50);
}else{
forward(10);
}
}else if(motor1.ready() && motor2.ready()){
switch(collideState){
case LEFT_REVERSE :
collideState = LEFT_TURN;
right(90);
break;
case RIGHT_REVERSE :
collideState = RIGHT_TURN;
left(90);
break;
case LEFT_TURN :
case RIGHT_TURN :
collideState = NORMAL;
}
}
}
void Mirobot::ledHandler(){
checkState();
switch(mainState){
case POWERED_UP:
if(millis() - lastLedChange > 250){
lastLedChange = millis();
digitalWrite(STATUS_LED, !digitalRead(STATUS_LED));
}
break;
case CONNECTED:
digitalWrite(STATUS_LED, HIGH);
break;
}
}
void Mirobot::servoHandler(){
if(servo_pulses_left){
if(micros() >= next_servo_pulse){
servo_pulses_left--;
digitalWrite(SERVO_PIN, HIGH);
if(penState == UP){
next_servo_pulse = micros() + 10800;
delayMicroseconds(1200);
}else{
next_servo_pulse = micros() + 10000;
delayMicroseconds(2000);
}
digitalWrite(SERVO_PIN, LOW);
}
}
}
void Mirobot::autoHandler(){
if(following){
followHandler();
}else if(colliding){
collideHandler();
}
}
void Mirobot::process(){
ledHandler();
servoHandler();
autoHandler();
p.process();
}