-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller_1.cpp
358 lines (314 loc) · 7.6 KB
/
Controller_1.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
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
#include <SoftwareSerial.h>
#include <Wire.h>
#include "Roboteq.h"
#include "maxon.h"
#include "disp.h"
#include "turbine.h"
//#include "FIMU_ADXL345.h"
//#include "FIMU_ITG3200.h"
//#include "FreeSixIMU.h"
//Define the serial port pins
#define SLAM_RX 17
#define SLAM_TX 16
#define ROBOTEQ_RX 19
#define ROBOTEQ_TX 18
//Define serial communications
SoftwareSerial serial2(ROBOTEQ_RX, ROBOTEQ_TX);
SoftwareSerial serial3(SLAM_RX, SLAM_TX);
//Call the constructors
roboteq myRoboteq = roboteq();
maxon myMaxon = maxon();
disp myDisp = disp();
turbine myTurbine = turbine();
//FreeSixIMU imu = FreeSixIMU();
//Global variables
//Commands
int commaIdx[4];
int idx_start;
int idx_end;
int idx;
bool data;
String str = "";
//Robot Status
float pose[10];
String _status;
//Maxon Commands
String speed_l;
String speed_r;
//Turbine LA
int dist;
int ti_double_guh_er;
int eyeore;
String robin;
//Dispensation LA: 0: DOWN, 1: TRANSIENT_DOWN, 2: UP
int bin_state;
//IMU Commands
float values[6];
int t0;
int t1;
int delta;
void setup()
{
/* Connections:
* ODroid - Serial1(9600)
* Maxon - Analog, Digital x4
* Roboteq - Serial(115200), Analog, Digital
* Disp. LA - Digital(PWM), Digital x4, Analog x2
* Tubine LA - Digital(PWM), Digital x4, Analog x2
*/
//Roboteq pinout
pinMode(ROBOTEQ_SPEED, OUTPUT);
pinMode(ROBOTEQ_DIR, OUTPUT);
pinMode(ROBOTEQ_BRAKE, OUTPUT);
//Maxon pinout
pinMode(MAXON_SPEED, OUTPUT);
pinMode(MAXON_DIR, OUTPUT);
pinMode(MAXON_BRAKE, OUTPUT);
pinMode(MAXON_DISABLE, OUTPUT);
pinMode(MAXON_HALL, INPUT);
//Dispensation LA pinout
pinMode(DISP_SPEED, OUTPUT);
pinMode(DISP_DIR, OUTPUT);
pinMode(DISP_BRAKE, OUTPUT);
pinMode(DISP_THERMAL, INPUT);
pinMode(DISP_HALL, INPUT);
pinMode(DISP_POT, INPUT);
pinMode(DISP_CURRENT, INPUT);
digitalWrite(A0, HIGH); //Set the pullup resistor
//Paddle LA pinout
pinMode(ARM_SPEED, OUTPUT);
pinMode(ARM_DIR, OUTPUT);
pinMode(ARM_BRAKE, OUTPUT);
pinMode(ARM_THERMAL, INPUT);
pinMode(ARM_POT, INPUT);
pinMode(ARM_CURRENT, INPUT);
//SLAM serial port
pinMode(SLAM_RX, INPUT);
pinMode(SLAM_TX, OUTPUT);
//Roboteq serial port
pinMode(ROBOTEQ_RX, INPUT);
pinMode(ROBOTEQ_TX, OUTPUT);
//Serial communications
Serial.begin(115200); //ODroid Serial
serial2.begin(115200); //Roboteq Serial
//serial3.begin(115200); //SLAM Serial
//I2C communications
Wire.begin();
idx_start = 0;
idx_end = 0;
/*
//Initialize IMU
imu.init();
t0 = 0;
t1 = 0;
delta = 0;
*/
ti_double_guh_er = 0;
eyeore = 0;
bin_state = 0;
Serial.println("Ready");
}
void loop()
{
//Wait for data to arrive (Format: "spd_l,spd_r,paddle_LA,bin_LA,maxon#!")
while(Serial.available() > 0)
{
str = "";
str = Serial.readStringUntil('!');
idx_start = 0;
idx_end = str.length();
// Check we got good data
data = str.endsWith("#");
//Print the data back
if (!(Serial.available() > 0))
{
Serial.write(str.c_str(), str.length());
Serial.flush();
}
}
if(data)
{
//Find the index of the commas
commaIdx[0] = str.indexOf(',', idx_start);
commaIdx[1] = str.indexOf(',', commaIdx[0]+1);
commaIdx[2] = str.indexOf(',', commaIdx[1]+1);
commaIdx[3] = str.indexOf(',', commaIdx[2]+1);
//Handle left motor commands
speed_l = myRoboteq.roboteq_set_speed_left(str.substring(idx_start, commaIdx[0]));
serial2.write(speed_l.c_str(), speed_l.length());
serial2.flush();
//Handle right motor commands
speed_r = myRoboteq.roboteq_set_speed_right(str.substring(commaIdx[0]+1, commaIdx[1]));
serial2.write(speed_r.c_str(), speed_r.length());
serial2.flush();
//TEST TEST TEST TEST
ti_double_guh_er = millis();
if((ti_double_guh_er - eyeore) > 50)
{
robin = "\n";
robin += String(analogRead(A14));
robin += "\n";
Serial.write(robin.c_str(), robin.length());
Serial.flush();
}
eyeore = millis();
//Handle paddle LA commands
if(str[commaIdx[2]-1] == '0')
{
dist = analogRead(A14);
/*
//Potentiometer stop check
if(dist > 190)
{
//Actuator fully extended
myTurbine.stop();
Serial.write("Turbine extended");
Serial.flush();
}
else
{
myTurbine.extendArm();
}*/
myTurbine.extendArm();
}
else if(str[commaIdx[2]-1] == '1')
{
myTurbine.stop();
}
else if(str[commaIdx[2]-1] == '2')
{
dist = analogRead(A14);
//Potentiometer stop check
if(dist < 140)
{
myTurbine.stop();
Serial.write("Turbine retracted");
Serial.flush();
}
else
{
myTurbine.retractArm();
}
}
//Handle dispensation LA commands
if(str[commaIdx[3]-1] == '0')
{
if(analogRead(A0) < 100)
{
if(bin_state == 1)
{
//Hit limit switch
bin_state = 0;
//Serial.write("Bin down");
//Serial.flush();
myDisp.stop();
}
else if(bin_state == 0)
{
myDisp.stop();
//Serial.write("Bin down");
//Serial.flush();
}
else
{
//Erraneous results
//Serial.write("Erraneous limit switch result");
//Serial.flush();
myDisp.retractDisp();
}
}
else
{
bin_state = 1;
myDisp.retractDisp();
}
/*
if(analogRead(A0) < 100)
{
//Hit limit switch
bin_state = 0;
if(bin_state == false)
{
myDisp.stop();
Serial.write("Bin state: Down");
}
}
else
{
myDisp.retractDisp();
}
//myDisp.retractDisp();
*/
}
else if(str[commaIdx[3]-1] == '1')
{
myDisp.stop();
}
else if(str[commaIdx[3]-1] == '2')
{
myDisp.extendDisp();
bin_state = 2;
}
//Handle maxon commands
if(str[str.length()-2] == '1')
{
myMaxon.on();
}
else
{
myMaxon.off();
}
data = false;
}
/*Get IMU data
t1 = micros()
imu.getValues(values);
imu.getYawPitchRoll(&values[3]);
//Compute x, y, z positions
delta = t1 - t0;
values[0] *= delta * delta;
values[1] *= delta * delta;
values[2] *= delta * delta;
//Normalize positions to flat plane
values[0] *= cos(values[3]);
values[1] *= cos(values[4]);
//Update timestep
t0 = micros();*/
//Update the robot status
//Format: "x, y, theta, x_error, y_error, theta_error, v_x, v_theta, disp_act, turbine_act, maxon"
pose[0] = values[0];
pose[1] = values[1];
pose[2] = values[3];
pose[3] = 0;
pose[4] = 0;
pose[5] = 0;
pose[6] = 0;
pose[7] = str[commaIdx[3]-1];
pose[8] = str[commaIdx[2]-1];
pose[9] = str[str.length()-2];
/*
//Create the string to send
_status += String(pose[0]);
_status += ",";
_status += String(pose[1]);
_status += ",";
_status += String(pose[2]);
_status += ",";
_status += String(pose[3]);
_status += ",";
_status += String(pose[4]);
_status += ",";
_status += String(pose[5]);
_status += ",";
_status += String(pose[6]);
_status += ",";
_status += String(pose[7]);
_status += ",";
_status += String(pose[8]);
_status += ",";
_status += String(pose[9]);
_status += "#!";
Serial.write(_status.c_str(), _status.length());
Serial.flush();
_status = "";*/
}