-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnxtcomms.cc
More file actions
355 lines (277 loc) · 11.2 KB
/
nxtcomms.cc
File metadata and controls
355 lines (277 loc) · 11.2 KB
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
//#include <iostream>
#include <usb.h>
#include <unistd.h>
#include <string.h>
#include <libplayercore/playercore.h>
static const int NXT_VENDOR_ID = 0x0694;
static const int NXT_PRODUCT_ID = 0x0002;
static usb_dev_handle *pUSBHandle = 0;
static unsigned char ucEpOut = 0;
static unsigned char ucEpIn = 0;
static const int TIMEOUT = 0x0BB8;
static struct usb_device *DevInit(void)
{
struct usb_bus *usb_bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next)
{
for (dev = usb_bus->devices; dev; dev = dev->next)
{
if ((dev->descriptor.idVendor == NXT_VENDOR_ID) && (dev->descriptor.idProduct == NXT_PRODUCT_ID))
return dev;
}
}
return 0;
}
static bool DevOpen()
{
struct usb_device *usb_dev;
int nEp = 0;
usb_dev = DevInit();
if (usb_dev == 0)
{
//std::cerr << "Device not found!" << std::endl;
puts("Device not found!");
return 0;
}
pUSBHandle = usb_open(usb_dev);
if (pUSBHandle == 0)
{
//std::cerr << "Not able to claim the USB device" << std::endl;
puts("Not able to claim the USB device");
return 0;
}
else
{
if (usb_dev->config)
{
if (usb_dev->config->interface)
{
if (usb_dev->config->interface->altsetting)
{
usb_claim_interface(pUSBHandle, usb_dev->config->interface->altsetting->bInterfaceNumber);
if ((nEp = usb_dev->config->interface->altsetting->bNumEndpoints))
{
if (usb_dev->config->interface->altsetting->endpoint)
{
ucEpIn = (unsigned char) (usb_dev->config->interface->altsetting->endpoint[0].bEndpointAddress);
if (nEp > 1)
ucEpOut = (unsigned char) (usb_dev->config->interface->altsetting->endpoint[1].bEndpointAddress);
}
}
}
}
}
}
return 1;
}
//////////////////////
//Generic Functions//
////////////////////
static void closeSensorOnPort(unsigned char port){
//std::cout << "set sonar ports" << std::endl;
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no return
directCommandBuffer[1] = 0x05;//command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x00;//sensor type -- no sensor
directCommandBuffer[4] = 0x00;//sensor mode
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
}
////////////////////
//Drive Functions//
//////////////////
static void nxt_set_motor(int speed, int motor)
{
if(motor > 2){
return;
}
char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;// no return
directCommandBuffer[1] = 0x04;//set ouput state command
directCommandBuffer[2] = motor;//port
directCommandBuffer[3] = speed;//power
directCommandBuffer[4] = 0x01;//mode - motor on
directCommandBuffer[5] = 0x00;//regulation mode - none
directCommandBuffer[6] = 0x00;//turn ratio
directCommandBuffer[7] = 0x20;//run state - running
directCommandBuffer[8] = 0x00;//tach state - run forever
directCommandBuffer[9] = 0x00;//tach state - run forever
directCommandBuffer[10] = 0x00;//tach state - run forever
directCommandBuffer[11] = 0x00;//tach state - run forever
directCommandBuffer[12] = 0x00;//tach state - run forever
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 13, 0x0BB8);
return;
}
static void nxt_set_motor_with_limit(int speed, int limit, int motor)
{
if(motor > 2){
return;
}
char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;// no return
directCommandBuffer[1] = 0x04;//set ouput state command
directCommandBuffer[2] = motor;//port
directCommandBuffer[3] = speed;//power
directCommandBuffer[4] = 0x01;//mode - motor on
directCommandBuffer[5] = 0x00;//regulation mode - none
directCommandBuffer[6] = 0x00;//turn ratio
directCommandBuffer[7] = 0x20;//run state - running
directCommandBuffer[8] = limit;//tach state - run forever
directCommandBuffer[9] = 0x00;//tach state - run forever
directCommandBuffer[10] = 0x00;//tach state - run forever
directCommandBuffer[11] = 0x00;//tach state - run forever
directCommandBuffer[12] = 0x00;//tach state - run forever
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 13, 0x0BB8);
return;
}
////////////////////
//Sonar Functions//
//////////////////
static void openSonarOnPort(int port){
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no return
directCommandBuffer[1] = 0x05;//set input mode command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x0B;//sensor type - LOWSPEED_V9
directCommandBuffer[4] = 0x00;//sensor mode - raw mode
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
//unsigned char directReadBuffer[256];
//usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer), 3, 0x0BB8);
return;
}
//this function is allready set on sonar by defualt so it isnt needed, is just here in case of "whatever"
static void setSonorContinuousReadMode(unsigned char port){
unsigned char directCommandBuffer2[256];
directCommandBuffer2[0] = 0x80;//no return
directCommandBuffer2[1] = 0x0f;//command - lswrite
directCommandBuffer2[2] = port;//port
directCommandBuffer2[3] = 0x03;//outgoing package size
directCommandBuffer2[4] = 0x00;//returning package size (from sonar to nxt, not from nxt to me)
directCommandBuffer2[5] = 0x02;//package 1 - from sonar docs - set continus mode
directCommandBuffer2[6] = 0x41;//package 2
directCommandBuffer2[7] = 0x02;//package 3
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer2), 8, 0x0BB8);
//unsigned char directReadBuffer2[256];
//usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer2), 3, 0x0BB8);
}
static int getSonarValue(int port){
int bytesRead = 0;
do{
unsigned char directCommandBuffer21[256];
directCommandBuffer21[0] = 0x80;//no return
directCommandBuffer21[1] = 0x0F;//command - lsread
directCommandBuffer21[2] = port;//port
directCommandBuffer21[3] = 0x02;//send size
directCommandBuffer21[4] = 0x01;//return size
directCommandBuffer21[5] = 0x02;//send 1
directCommandBuffer21[6] = 0x42;//send 2
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer21), 7, 0x0BB8);
//unsigned char directReadBuffer21[256];
//usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer21), 3, 0x0BB8);
unsigned char directCommandBuffer211[256];
directCommandBuffer211[0] = 0x00;//return
directCommandBuffer211[1] = 0x0E;//command - lsget status
directCommandBuffer211[2] = port;//port
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer211), 3, 0x0BB8);
unsigned char directReadBuffer211[256];
usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer211), 4, 0x0BB8);
bytesRead = (int)directReadBuffer211[3];
}while(bytesRead < 1);
unsigned char directCommandBuffer22[256];
directCommandBuffer22[0] = 0x00;//return
directCommandBuffer22[1] = 0x10;//command - lsread
directCommandBuffer22[2] = port;//port
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer22), 3, 0x0BB8);
unsigned char directReadBuffer22[256];
usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer22), 20, 0x0BB8);
//std::cout << "bit #" << 4 << " contents = " << std::hex << (int)directReadBuffer22[4] << std::endl;
return (int)directReadBuffer22[4];
}
///////////////////////////
//Light Sensor Functions//
/////////////////////////
static void openLightActiveOnPort(unsigned char port){
//std::cout << "set sonar ports" << std::endl;
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no return
directCommandBuffer[1] = 0x05;//command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x05;//sensor type
directCommandBuffer[4] = 0x80;//sensor mode
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
}
static void openLightPassiveOnPort(unsigned char port){
//std::cout << "set sonar ports" << std::endl;
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no return
directCommandBuffer[1] = 0x05;//command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x06;//sensor type
directCommandBuffer[4] = 0x80;//sensor mode
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
}
static int getLightValue(unsigned char port){
unsigned char directCommandBuffer22[256];
directCommandBuffer22[0] = 0x00;//return
directCommandBuffer22[1] = 0x07;//command - lsread
directCommandBuffer22[2] = port;//port
unsigned char directReadBuffer22[256];
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer22), 3, 0x0BB8);
usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer22), 16, 0x0BB8);
return directReadBuffer22[12];
}
///////////////////////////
//Sound Sensor Functions//
/////////////////////////
static void openSoundOnPort(unsigned char port){
//std::cout << "set sonar ports" << std::endl;
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no return
directCommandBuffer[1] = 0x05;//command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x05;//sensor type
directCommandBuffer[4] = 0x80;//sensor mode
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
}
//this is exactly the same as get light value
static int getSoundValue(unsigned char port){
unsigned char directCommandBuffer22[256];
directCommandBuffer22[0] = 0x00;//return
directCommandBuffer22[1] = 0x07;//command - lsread
directCommandBuffer22[2] = port;//port
unsigned char directReadBuffer22[256];
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer22), 3, 0x0BB8);
usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer22), 16, 0x0BB8);
return directReadBuffer22[12];
}
/////////////////////
//Bumper Functions//
///////////////////
static void openBumperOnPort(int port)
{
//ViUInt8 directCommandBuffer[] = { 0x05, port, 0x01, 0x20 };
//Comm::SendDirectCommand( false, reinterpret_cast< ViByte* >( directCommandBuffer ), sizeof( directCommandBuffer ), NULL, 0);
unsigned char directCommandBuffer[256];
directCommandBuffer[0] = 0x80;//no nreturn
directCommandBuffer[1] = 0x05;//set sensor command
directCommandBuffer[2] = port;//port
directCommandBuffer[3] = 0x01;//sensor type (switch)
directCommandBuffer[4] = 0x20;//sensor mode (boolean)
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer), 5, 0x0BB8);
return;
}
//same exact function as get light and sound functions
static int getBumperValue(unsigned char port){
unsigned char directCommandBuffer22[256];
directCommandBuffer22[0] = 0x00;//return
directCommandBuffer22[1] = 0x07;//command - lsread
directCommandBuffer22[2] = port;//port
unsigned char directReadBuffer22[256];
usb_bulk_write(pUSBHandle, ucEpIn, reinterpret_cast < char *>(directCommandBuffer22), 3, 0x0BB8);
usb_bulk_read(pUSBHandle, ucEpOut, reinterpret_cast < char *>(directReadBuffer22), 16, 0x0BB8);
return directReadBuffer22[12];
}