-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmindsensors-lineleader-test5.c
409 lines (343 loc) · 10.4 KB
/
mindsensors-lineleader-test5.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#pragma config(Sensor, S1, LLEADER, sensorI2CCustomFastSkipStates9V)
#pragma config(Sensor, S4, NUMPAD, sensorI2CCustomFastSkipStates)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: mindsensors-lineleader-test5.c 133 2013-03-10 15:15:38Z xander $
*/
/** \file mindsensors-lineleader-test5.c
* \brief Mindsensors LineLeader Sensor demo program
*
* MSLL-test5.c is a demo program for the Mindsensors LineLeader Sensor.
*
* Changelog:
* - 0.1: Initial release
* - 0.2: Reworked to use new driver API
* - 0.3: More comments<br>
* Use clip() instead of manual clipping
* - 0.4: Removed common.h from includes\n
* Replaced arrays with new type that does not use structs
*
* Credits:
* - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
*
* License: You may use this code as you wish, provided you give credit where its due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 14 February 2011
* version 0.4
*/
#define MSLL_I2C_ADDR 0x02
#include "drivers/mindsensors-lineleader.h"
#include "drivers/mindsensors-numericpad.h"
// The PID constants and base speed
#define DEFAULT_SP 50
// The file for the log data
#define LOGFILE "linelead.dat"
#define MENUITEMS 5
// function prototypes
void doMainMenu();
void doMenuItem(int activeOption);
bool checkTimer(TTimers timer);
void doLineLead();
void writeParams();
string menuHeader;
string menuFooter;
const TTimers rightButtonTimer = T1;
const TTimers leftButtonTimer = T2;
int activeOption = 0;
int keep_running = 0;
byte sensor = 0;
// array to hold user entered values
int params[4];
string optionMainMenu[] = {
"Kp",
"Ki",
"Kd",
"Sp",
"Run" };
string optionMainMenuFooter[] = {
//123456789012345678
"*=Exit #=Edit",
"*=Exit #=Edit",
"*=Exit #=Edit",
"*=Exit #=Edit",
"*=Exit #=Run " };
tByteArray signalstr;
int getNumber(tSensors link, int &_number, int numdigits=3, int timeout=0) {
long starttime = nPgmTime;
int tmpnum, prevnum, counter = 0;
//_number = 0;
while (true) {
if ((timeout != 0) && nPgmTime > (starttime + timeout))
return -1;
// Read the keypad
tmpnum = MSNPscanKeys(NUMPAD);
// Reset the timer if a key has been pressed
if (tmpnum != -255) {
starttime = nPgmTime;
}
// Make sure we're not going beyond the requested length
if ((counter >= numdigits) && (tmpnum >= 0))
continue;
if ((tmpnum >= 0) && (prevnum != tmpnum)) {
PlaySound(soundBlip);
while(bSoundActive) EndTimeSlice();
if ((counter == 0) && (tmpnum >= 0)) {
_number = tmpnum;
counter++;
} else {
_number = _number*10 + tmpnum;
counter++;
}
} else if (tmpnum == -1) {
return (counter > 0) ? 0 : -1;
} else if (tmpnum == -2) {
return -2;
}
prevnum = tmpnum;
}
return 0;
}
//task batteryMonitor() {
// while (true) {
// if (nAvgBatteryLevel < 6200) {
// PlaySound(soundBeepBeep);
// while(bSoundActive) EndTimeSlice();
// PlaySound(soundBeepBeep);
// while(bSoundActive) EndTimeSlice();
// StopAllTasks();
// }
// wait1Msec(500);
// }
//}
// This function draws the current values of the sensor and other data on the screen
// in a visually pleasing way
task drawSensors() {
while (keep_running == 1) {
// This clears the entire area occupied by the small rectangles
nxtEraseRect(6,62, 91, 43);
for (int i = 0; i < 8; i++) {
// Draw the rectangles for the signal strength first
nxtDrawRect(6+(i*11),62, 14+(i*11), 50);
nxtFillRect(6+(i*11),51+signalstr[i]/10, 14+(i*11), 50);
// Draw a black rectangle if the sensor has detected the line,
// a hollow one when nothing has been detected.
if ((sensor >> i) & 1) {
nxtFillRect(6+(i*11),48, 14+(i*11), 43);
} else {
nxtDrawRect(6+(i*11),48, 14+(i*11), 43);
}
}
wait1Msec(100);
}
}
// Keep the robot on the line!
task followTheYellowBrickRoad () {
int powerA = 0;
int powerC = 0;
byte steering = 0;
eraseDisplay();
nxtDisplayCenteredTextLine(3, "Running...");
nxtDisplayCenteredTextLine(5, "Press exit or *");
nxtDisplayCenteredTextLine(6, "to stop");
time1[T4] = 0;
while (keep_running == 1) {
if (MSNPscanKeys(NUMPAD) == -2)
keep_running = 0;
steering = LLreadSteering(LLEADER);
sensor = LLreadResult(LLEADER);
//average = LLreadAverage(LLEADER);
LLreadSensorRaw(LLEADER, signalstr);
steering /= 2;
powerA = (params[3] - steering);
powerC = (params[3] + steering);
// If your robot is going in the wrong direction, comment out the
// lines above and uncomment the lines below.
//powerA = (params[3] - steering);
//powerC = (params[3] + steering);
// this clips the values
powerA = clip(powerA, -100, 100);
powerC = clip(powerC, -100, 100);
//if(powerA>100) powerA=100;
//if(powerA<-100) powerA=-100;
//if(powerC<-100) powerC=-100;
//if(powerC>100) powerC=100;
motor[motorA] = -(byte)powerA;
motor[motorC] = -(byte)powerC;
wait1Msec(1);
if (sensor != 0xFF) {
time1[T4] = 0;
} else if (time1[T4] > 500) {
keep_running = 0;
}
}
motor[motorA] = 0;
motor[motorC] = 0;
}
task redrawMenu() {
while(true) {
eraseDisplay();
nxtDisplayTextLine(0, menuHeader);
for (int i = 0; i < MENUITEMS; i++) {
nxtDisplayClearTextLine(i + 1);
if (i == activeOption) {
if (i < 4)
nxtDisplayTextLine(i + 1, "> %s [%3d] <", optionMainMenu[i], params[i]);
else
nxtDisplayTextLine(i + 1, "> %s <", optionMainMenu[i]);
nxtDisplayTextLine(7, menuFooter);
} else {
if (i < 4)
nxtDisplayTextLine(i + 1, " %s [%3d]", optionMainMenu[i], params[i]);
else
nxtDisplayTextLine(i + 1, " %s", optionMainMenu[i]);
}
}
wait1Msec(100);
}
}
// Main task
task main () {
// memcpy(params, default_params, sizeof(default_params));
params[0] = LLreadKp(LLEADER);
params[1] = LLreadKi(LLEADER);
params[2] = LLreadKd(LLEADER);
params[3] = DEFAULT_SP;
// Set the default parameters and write them to the sensor
//writeParams();
nNxtButtonTask = -2;
nNxtExitClicks = 3;
StartTask(redrawMenu);
doMainMenu();
while(true)
wait1Msec(100);
}
// Draw the main menu
void doMainMenu () {
while (true) {
menuHeader = " 2/8 => up/down";
//StringFormat(menuFooter, "%s", optionMainMenuFooter[activeOption]);
menuFooter = optionMainMenuFooter[activeOption];
switch(MSNPscanKeys(NUMPAD)) {
case 8:
if (!checkTimer(rightButtonTimer)) {
break;
}
if (activeOption == (MENUITEMS - 1))
activeOption = 0;
else
activeOption++;
//StringFormat(menuFooter, "%s", optionMainMenuFooter[activeOption]);
menuFooter = optionMainMenuFooter[activeOption];
wait1Msec(300);
break;
case 2:
if (!checkTimer(leftButtonTimer)) {
break;
}
if (activeOption == 0)
activeOption = (MENUITEMS - 1);
else
activeOption--;
//StringFormat(menuFooter, "%s", optionMainMenuFooter[activeOption]);
menuFooter = optionMainMenuFooter[activeOption];
wait1Msec(300);
break;
case -1:
if (!checkTimer(rightButtonTimer)) {
break;
}
wait1Msec(600);
doMenuItem(activeOption);
break;
case -2:
wait1Msec(500);
StopAllTasks();
}
}
}
// When enter is pressed in the doMainMenu() we come here.
// Here we handle the actual actions for each menu item.
void doMenuItem(int activeOption) {
long oldnumber = 0;
int retval = 0;
PlaySound(soundBlip);
while(bSoundActive) EndTimeSlice();
if (activeOption == 4) {
doLineLead();
return;
}
if (activeOption == 3)
menuHeader = "Enter nr (0-100)";
else
menuHeader = "Enter nr (0-127)";
//123456789012345678
menuFooter = "*=No Save #=Save";
oldnumber = params[activeOption];
retval = getNumber(NUMPAD, params[activeOption], 3, 5000);
switch (retval) {
case -2: // aborted
params[activeOption] = oldnumber;
writeParams();
wait1Msec(1000);
break;
case -1: // Nothing entered
params[activeOption] = oldnumber;
writeParams();
wait1Msec(1000);
return;
break;
case 0: // A number was entered
if (activeOption == 3 && params[activeOption] > 100 )
params[activeOption] = oldnumber;
else if (params[activeOption] > 127)
params[activeOption] = oldnumber;
writeParams();
wait1Msec(1000);
return;
break;
}
}
// See if more than 300ms has elapsed
bool checkTimer(TTimers timer) {
if (time1[timer] < 300) {
return false;
} else {
time1[timer] = 0;
return true;
}
}
// Start and stop the line following task (followTheYellowBrickRoad)
void doLineLead() {
sensor = 0;
StopTask(redrawMenu);
keep_running = 1;
eraseDisplay();
// Count down and
for (int i = 0; i < 5; i++) {
nxtDisplayCenteredBigTextLine(3, "%d", 5-i);
PlaySound(soundBlip);
wait1Msec(600);
}
PlaySound(soundFastUpwardTones);
while(bSoundActive) EndTimeSlice();
StartTask(drawSensors);
StartTask(followTheYellowBrickRoad);
while(nNxtButtonPressed != kExitButton && keep_running != 0) {
wait1Msec(10);
}
// this will kill off the followTheYellowBrickRoad task
keep_running = 0;
wait1Msec(1000);
StartTask(redrawMenu);
}
// Write the PID values to the LineLeader sensor
void writeParams() {
LLsetKp(LLEADER, params[0], 32);
LLsetKi(LLEADER, params[1], 32);
LLsetKd(LLEADER, params[2], 32);
}
/*
* $Id: mindsensors-lineleader-test5.c 133 2013-03-10 15:15:38Z xander $
*/