-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmindsensors-lineleader-test3.c
340 lines (293 loc) · 8.65 KB
/
mindsensors-lineleader-test3.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
#pragma config(Sensor, S1, LLEADER, sensorI2CCustom9V)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: mindsensors-lineleader-test3.c 133 2013-03-10 15:15:38Z xander $
*/
/** \file mindsensors-lineleader-test3.c
* \brief Mindsensors LineLeader Sensor demo program
*
* mindsensors-lineleader-test3.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: Updated to use new array types that don't use structs\n
* Removed common.h from includes
*
* 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"
#define printXY nxtDisplayStringAt
#define println nxtDisplayTextLine
#define clearln nxtDisplayClearTextLine
// The PID constants and base speed
#define DEFAULT_KP 5
#define DEFAULT_KI 0
#define DEFAULT_KD 30
#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;
const TTimers enterButtonTimer = T3;
int activeOption = 0;
int keep_running = 0;
byte sensor = 0;
// array holding default values for PID constants and base speed
const int default_params[4] = {
DEFAULT_KP,
DEFAULT_KI,
DEFAULT_KD,
DEFAULT_SP };
// array to hold user entered values
int params[4];
const string optionMainMenu[5] = {
"Kp",
"Ki",
"Kd",
"Sp",
"Run" };
const string optionMainMenuFooter[5] = {
"Ent=Edit",
"Ent=Edit",
"Ent=Edit",
"Ent=Edit",
"Ent=Run" };
tByteArray signalstr;
// 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");
nxtDisplayCenteredTextLine(6, "to stop");
time1[T4] = 0;
while (keep_running == 1) {
steering = LLreadSteering(LLEADER);
sensor = LLreadResult(LLEADER);
LLreadSensorRaw(LLEADER, signalstr);
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();
println(0, menuHeader);
for (int i = 0; i < MENUITEMS; i++) {
clearln(i + 1);
if (i == activeOption) {
if (i < 4)
println(i + 1, "> %s [%3d] <", optionMainMenu[i], params[i]);
else
println(i + 1, "> %s <", optionMainMenu[i]);
println(7, menuFooter);
} else {
if (i < 4)
println(i + 1, " %s [%3d]", optionMainMenu[i], params[i]);
else
println(i + 1, " %s", optionMainMenu[i]);
}
}
wait1Msec(100);
}
}
// Main task
task main () {
// Set the default parameters and write them to the sensor
memcpy(params, default_params, sizeof(default_params));
writeParams();
nNxtButtonTask = -2;
nNxtExitClicks = 3;
StartTask(redrawMenu);
doMainMenu();
while(true)
wait1Msec(100);
}
// Draw the main menu
void doMainMenu () {
while (true) {
menuHeader = "L/R to select";
menuFooter = optionMainMenuFooter[activeOption];
switch(nNxtButtonPressed) {
case kRightButton:
if (!checkTimer(rightButtonTimer)) {
break;
}
if (activeOption == (MENUITEMS - 1))
activeOption = 0;
else
activeOption++;
menuFooter = optionMainMenuFooter[activeOption];
wait1Msec(300);
break;
case kLeftButton:
if (!checkTimer(leftButtonTimer)) {
break;
}
if (activeOption == 0)
activeOption = (MENUITEMS - 1);
else
activeOption--;
menuFooter = optionMainMenuFooter[activeOption];
wait1Msec(300);
break;
case kEnterButton:
if (!checkTimer(rightButtonTimer)) {
break;
}
wait1Msec(600);
doMenuItem(activeOption);
break;
case kExitButton:
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) {
PlaySound(soundBlip);
while(bSoundActive) EndTimeSlice();
if (activeOption == 4) {
doLineLead();
return;
}
while (true) {
menuHeader = "L/R to edit val";
menuFooter = "Ent=Save/Exit=Def";
switch(nNxtButtonPressed) {
case kRightButton:
if (!checkTimer(rightButtonTimer)) {
break;
}
if (params[activeOption] < 128)
params[activeOption]++;
break;
case kLeftButton:
if (!checkTimer(leftButtonTimer)) {
break;
}
if (params[activeOption] > 0)
params[activeOption]--;
break;
case kEnterButton:
if (!checkTimer(enterButtonTimer)) {
break;
}
writeParams();
wait1Msec(600);
return;
break;
case kExitButton:
params[activeOption] = default_params[activeOption];
writeParams();
wait1Msec(600);
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;
for (int i = 0; i < 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-test3.c 133 2013-03-10 15:15:38Z xander $
*/