-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenedettelli-nxt2wifi-test4.c
275 lines (236 loc) · 7.05 KB
/
benedettelli-nxt2wifi-test4.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
#pragma config(Sensor, S1, TOUCH, sensorTouch)
#pragma config(Sensor, S2, COLOUR, sensorCOLORFULL)
#pragma config(Sensor, S3, SONAR, sensorSONAR)
#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
#pragma config(Motor, motorA, MOT_ACTION, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorB, MOT_LEFT, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorC, MOT_RIGHT, tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "drivers/common.h"
#include "drivers/benedettelli-nxt2wifi.h"
#define DRIVESPEED 50
#define TURNSPEED 20
#define SHOOTSPEED 30
string IPaddress = "0.0.0.0";
string connStatus = "disconnected";
string dataStrings[5];
tHugeByteArray data;
int prevDetectedColour = 0;
int currDetectedColour = 0;
int prevBatteryLevel = 0;
int currBatteryLevel = 0;
int prevTouchState = 0;
int currTouchState = 0;
int prevSonarDistance = 0;
int currSonarDistance = 0;
long prevEncMotorA = 0;
long currEncMotorA = 0;
long prevEncMotorB = 0;
long currEncMotorB = 0;
long prevEncMotorC = 0;
long currEncMotorC = 0;
task updateScreen()
{
while(true)
{
nxtDisplayTextLine(0, "Stat: %s", connStatus);
nxtDisplayTextLine(1, "%s",IPaddress);
nxtDisplayTextLine(2, "-------------------");
nxtDisplayTextLine(3, "%s", dataStrings[0]);
nxtDisplayTextLine(4, "%s", dataStrings[1]);
nxtDisplayTextLine(5, "%s", dataStrings[2]);
nxtDisplayTextLine(6, "%s", dataStrings[3]);
nxtDisplayTextLine(7, "%s", dataStrings[4]);
wait1Msec(100);
}
}
void handleColour(ubyte state)
{
switch (state)
{
case 3: SensorType[COLOUR] = sensorCOLORFULL;
break;
case 4: SensorType[COLOUR] = sensorCOLORRED;
break;
case 5: SensorType[COLOUR] = sensorCOLORGREEN;
break;
case 6: SensorType[COLOUR] = sensorCOLORBLUE;
break;
case 7: SensorType[COLOUR] = sensorCOLORNONE;
break;
}
}
void doStraight(ubyte state)
{
motor[MOT_LEFT] = state * DRIVESPEED;
motor[MOT_RIGHT] = state * DRIVESPEED;
}
void doRight(ubyte state)
{
motor[MOT_LEFT] = state * TURNSPEED;
motor[MOT_RIGHT] = state * -TURNSPEED;
}
void doStop(ubyte state)
{
motor[MOT_LEFT] = 0;
motor[MOT_RIGHT] = 0;
}
void doLeft(ubyte state)
{
motor[MOT_LEFT] = state * -TURNSPEED;
motor[MOT_RIGHT] = state * TURNSPEED;
}
void doReverse(ubyte state)
{
motor[MOT_LEFT] = state * -DRIVESPEED;
motor[MOT_RIGHT] = state * -DRIVESPEED;
}
void doAction(ubyte state)
{
motor[MOT_ACTION] = state * SHOOTSPEED;
}
task main ()
{
ubyte type;
ubyte ID;
ubyte state;
ubyte value;
string dataString;
string tmpString;
// initialise the port, etc
RS485initLib();
StartTask(updateScreen);
// Disconnect if already connected
N2WDisconnect();
N2WchillOut();
wait1Msec(1000);
if (!N2WCustomExist())
{
StopTask(updateScreen);
wait1Msec(50);
eraseDisplay();
PlaySound(soundException);
nxtDisplayCenteredBigTextLine(1, "ERROR");
nxtDisplayTextLine(3, "No custom profile");
nxtDisplayTextLine(4, "configured!!");
while(true) EndTimeSlice();
}
N2WLoad();
wait1Msec(100);
N2WConnect(true);
connStatus = "connecting";
while (!N2WConnected()) wait1Msec(100);
wait1Msec(1000);
connStatus = "connected";
PlaySound(soundBeepBeep);
wait1Msec(3000);
N2WgetIP(IPaddress);
wait1Msec(1000);
// 123456789012345
dataStrings[0] = "Tch | Snr | Clr";
// on | 011 | 1"
while (true)
{
if (N2WreadWS(type, ID, state, value))
{
writeDebugStreamLine("btn: %d, state: %d", ID, state);
switch (ID)
{
case 1: doStraight(state); break;
case 3: doRight(state); break;
case 4: doStop(state); break;
case 5: doLeft(state); break;
case 7: doReverse(state); break;
case 9: doAction(state); break;
case 11: handleColour(state); break;
default: break;
}
}
// All values are only updated when they've changed.
// This cuts backs drastically on the number of messages
// that have to be sent to the NXT2WIFI
// Fetch the state of the Touch Sensor
// This value is displayed by field 0 (in0) on the page
currTouchState = SensorBoolean[TOUCH];
if (currTouchState != prevTouchState)
{
memset(data, 0, sizeof(data));
data[0] = (currTouchState) ? '1' : '0';
N2WwriteWS(1, 0, data, 2);
prevTouchState = currTouchState;
N2WchillOut();
}
// Fetch the currently detected colour.
// This value is displayed by field 1 (in1) on the page
currDetectedColour = SensorValue[COLOUR];
if (currDetectedColour != prevDetectedColour)
{
sprintf(dataString, "%d", currDetectedColour);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 1, data, strlen(dataString));
prevDetectedColour = currDetectedColour;
N2WchillOut();
}
// Fetch the distance detected by the sonar sensor
// This value is displayed by field 2 (in2) on the page
currSonarDistance = SensorValue[SONAR];
if (currSonarDistance != prevSonarDistance)
{
sprintf(dataString, "%d", currSonarDistance);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 2, data, strlen(dataString));
prevSonarDistance = currSonarDistance;
N2WchillOut();
}
// Fetch the tacho count for motor A
// This value is displayed by field 3 (in3) on the page
currEncMotorA = nMotorEncoder[MOT_ACTION];
if (currEncMotorA != prevEncMotorA)
{
sprintf(dataString, "%d", currEncMotorA);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 3, data, strlen(dataString));
prevEncMotorA = currEncMotorA;
N2WchillOut();
}
// Fetch the tacho count for motor B
// This value is displayed by field 4 (in4) on the page
//currEncMotorB = nMotorEncoder[MOT_LEFT];
if (currEncMotorB != prevEncMotorB)
{
sprintf(dataString, "%d", currEncMotorB);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 4, data, strlen(dataString));
prevEncMotorB = currEncMotorB;
N2WchillOut();
}
// Fetch the tacho count for motor C
// This value is displayed by field 5 (in5) on the page
currEncMotorC = nMotorEncoder[MOT_RIGHT];
if (currEncMotorC != prevEncMotorC)
{
sprintf(dataString, "%d", currEncMotorC);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 5, data, strlen(dataString));
prevEncMotorC = currEncMotorC;
N2WchillOut();
}
// Fetch the current voltage level. The average one
// works best, the other one jumps around too much.
// This value is displayed by field 6 (in6) on the page
currBatteryLevel = nAvgBatteryLevel;
if (currBatteryLevel != prevBatteryLevel)
{
sprintf(dataString, "%d", currBatteryLevel);
memcpy(data, dataString, strlen(dataString));
N2WwriteWS(1, 6, data, strlen(dataString));
prevBatteryLevel = currBatteryLevel;
N2WchillOut();
}
sprintf(dataStrings[2], "A: %d", currEncMotorA);
sprintf(dataStrings[3], "B: %d", currEncMotorB);
sprintf(dataStrings[4], "C: %d", currEncMotorC);
sprintf(tmpString, "%s | %3d", (currTouchState == 0) ? "off" : "on ", currSonarDistance);
sprintf(dataStrings[1], "%s | %3d", tmpString, currDetectedColour);
}
}