-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcx.cpp
More file actions
796 lines (701 loc) · 24.1 KB
/
rcx.cpp
File metadata and controls
796 lines (701 loc) · 24.1 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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//rcx21.cpp
//direct serial LEGO Mindstorms RCX control
//V 2.1 (c) 23.8.2005 by Daniel Berger (daniel.berger@tuebingen.mpg.de),
// under usage of docs of Kekoa Proudfood and the Win32 SDK.
// With kind help from Sudhanshu Gaur (Virginia Institute of Technology) for USB support.
#include <windows.h>
volatile int RCX_stop=-1; //-1: not running, 0:running, 1:about to be stopped
volatile int RCX_finished=0;
volatile int RCX_error=0; //error code 1: tower init failed, 2: robot communication failed
//////////////////////////////////
//input/output variables
// alive, sensor 1, sensor 2, sensor 3, battery
// motor A, motor B, motor C
volatile int RCX_sensor_on[5]={0,0,0,0,0}; //1 indicates a sensor that should be polled.
volatile int RCX_motor_on[3]={0,0,0}; //1 indicates a motor that should be updated (switched on)
volatile int RCX_sensor_val[5]={0,0,0,0,0}; //values of sensors; 0 .. 1024
volatile int RCX_motor_val[3]={8,8,8}; //values of motors; -8 .. 8, 0 is float
volatile int RCX_sensor_type[5]={0,0,0,0,0}; //types for sensors. Only sensors 1,2,3 are used.
//sensor types are: 0: raw, 1: touch, 2: temperature, 3: light, 4: rotation
volatile int RCX_sensor_mode[5]={0,0,0,0,0}; //modes for sensors. Only sensors 1,2,3 are used.
/*sensor modes are:
0: Raw - value in 0..1023
1: Boolean - Either 0 or 1
2: Edge count - Number of boolean transitions
3: Pulse count - Number of boolean transitions divided by two
4: Percentage - Raw value scaled to 0..100
5: Temperature in °C - 1/10ths of a degree, -19.8 .. 69.5
6: Temperature in °F - 1/10ths of a degree, -3.6 .. 157.1
7: Angle - 1/16ths of a rotation, represented as a signed short
*/
volatile int RCX_sound=0; //0: no sound, 1: blip, 2: beep beep, 3: downward tones, 4: upward tones, 5: low buzz, 6: fast upward tones
int init_wait_ms=1;
int wait_ms=1; //wait interval between message send and receive. Increase this value in case of communication problems
//////////////////////////////////
//internal variables
int work_sensor_type[5]={0,0,0,0,0};
int last_sensor_type[5]={0,0,0,0,0}; //sensor types are only used for sensors 1,2,3.
int work_sensor_mode[5]={0,0,0,0,0};
int last_sensor_mode[5]={0,0,0,0,0}; //sensor modes are only used for sensors 1,2,3.
int work_motor_on[3]={0,0,0};
int work_motor_val[3]={8,8,8};
int last_motor_on[3]={0,0,0};
int last_motor_val[3]={8,8,8};
int motor_on_ok[3]={0,0,0};
int motor_val_ok[3]={0,0,0};
int work_error=0;
DCB dcb;
COMMTIMEOUTS tout;
HANDLE RCX_port=0;
DWORD RCX_thread_id;
char portname[256];
int porttype=0;
int RCX_close(){
return(CloseHandle(RCX_port));
};
int RCX_open(int type, char *portname){
//Opens the connections to the RCX via tower on serial or usb port.
//type 1: serial port, type 2: USB port
//portname for serial port is "COM1" for example
//portname for USB port is "\\\\.\\LEGOTOWER1" for example
//returns 0 if failed, 1 if successful
if (type==1){//open serial port
RCX_port = CreateFile(portname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0);
if (RCX_port == INVALID_HANDLE_VALUE) return(0);
//set data protocol format
GetCommState(RCX_port,&dcb);
FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate=CBR_2400;
dcb.fBinary=1;
dcb.fParity=1;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.ByteSize=8;
dcb.Parity=ODDPARITY;
dcb.StopBits=ONESTOPBIT;
if (!SetCommState(RCX_port, &dcb)){
RCX_close();
return(0);
}
GetCommTimeouts(RCX_port,&tout);
tout.ReadIntervalTimeout=250;
tout.ReadTotalTimeoutConstant=10;
tout.ReadTotalTimeoutMultiplier=10;
tout.WriteTotalTimeoutConstant=10;
tout.WriteTotalTimeoutMultiplier=10;
SetCommTimeouts(RCX_port,&tout);
SetupComm(RCX_port,65536,65536);
} else { //type 2: open USB port
RCX_port = CreateFile(portname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, 0);
if (RCX_port == INVALID_HANDLE_VALUE) return(0);
GetCommTimeouts(RCX_port,&tout);
tout.ReadIntervalTimeout=250;
tout.ReadTotalTimeoutConstant=10;
tout.ReadTotalTimeoutMultiplier=10;
tout.WriteTotalTimeoutConstant=10;
tout.WriteTotalTimeoutMultiplier=10;
SetCommTimeouts(RCX_port,&tout);
}
return(1);
}
unsigned char sendbuf[256];
unsigned char recbuf[256];
OVERLAPPED ovl;
int RCX_send(unsigned char *message, int length){
//sends a message to the RCX
//returns 1 if write succeeded, 0 if failed
//maximal length of message is 125 bytes
int i;
unsigned long res,ptr;
int s=0;
//create message to send
sendbuf[0]=0x55;
sendbuf[1]=0xff;
sendbuf[2]=0x00;
ptr=3;
for (i=0; i<length; i++){
sendbuf[ptr++]=message[i];
sendbuf[ptr++]=message[i]^0xff;
s+=message[i];
}
sendbuf[ptr++]=(unsigned char)s;
sendbuf[ptr++]=((unsigned char)s)^0xff;
res=0;
ovl.Offset=0;
ovl.OffsetHigh=0;
ovl.hEvent=NULL;
if(!WriteFile(RCX_port,sendbuf,ptr,&res,&ovl)){
if (GetLastError()==ERROR_IO_PENDING){
i=0;
do{
Sleep(10);
i++;
} while ((!HasOverlappedIoCompleted(&ovl))&&(i<50));
if (i<50){
return(1); //completed.
} else {
CancelIo(RCX_port); //cancel transmission.
return(0); //not completed.
}
} else { //some error occured
CancelIo(RCX_port); //cancel transmission.
return(0);
}
}
return(1);
}
int RCX_receive(unsigned char *rcbuf, unsigned long length){
//attempts to read a message of specified length from the RCX
unsigned long res=0;
int i;
ovl.Offset=0;
ovl.OffsetHigh=0;
ovl.hEvent=NULL;
if (!ReadFile(RCX_port,rcbuf,length,&res,&ovl)){
//return(0);
if (GetLastError()==ERROR_IO_PENDING){
i=0;
do{
Sleep(10);
i++;
} while ((!HasOverlappedIoCompleted(&ovl))&&(i<50));
if (i<50){
GetOverlappedResult(RCX_port,&ovl,&res,FALSE);
return(res); //completed.
} else {
CancelIo(RCX_port); //cancel transmission.
return(0); //not completed.
}
} else { //some error occured
CancelIo(RCX_port); //cancel transmission.
return(0);
}
}
return(res);
}
int lastdecodedlength=0;
int RCX_decode(unsigned char *recbuf, int len){
//tries to decode bytestream in recbuf up to length len.
//returns -1 if failed, else decoded value
int pos=0;
int val=0;
if (porttype==1){ //decode message from serial port
if (len<4){lastdecodedlength=0; return(-1);}
if (recbuf[0]!=0x55){lastdecodedlength=0; return(-1);}
if (recbuf[1]!=0xff){lastdecodedlength=1; return(-1);}
if (recbuf[2]!=0x00){lastdecodedlength=2; return(-1);}
switch(recbuf[3]){
case 0x10:
case 0x18: //alive
pos=7;
if (len!=14){
lastdecodedlength=4;
val=0;
}
break;
case 0x12: //get value
case 0x1a:
pos=11;
break;
case 0x30:
case 0x38: //get battery power
pos=7;
break;
}
if (len<(pos+3)){
lastdecodedlength=3;
return(-1);
}
if (recbuf[pos]!=0x55){lastdecodedlength=pos; return(-1);}
if (recbuf[pos+1]!=0xff){lastdecodedlength=pos+1; return(-1);}
if (recbuf[pos+2]!=0x00){lastdecodedlength=pos+2; return(-1);}
switch(recbuf[pos+4]){
case 0x10:
case 0x18: //alive
val=1;
lastdecodedlength=pos+4;
break;
case 0x12: //get sensor value
case 0x1a:
val=(((int)recbuf[pos+7])<<8)+recbuf[pos+5];
lastdecodedlength=pos+8;
break;
case 0x30:
case 0x38: //get battery power
val=(((int)recbuf[pos+7])<<8)+recbuf[pos+5];
lastdecodedlength=pos+8;
break;
}
} else { //decode message from USB port
if (len<5){lastdecodedlength=0; return(-1);} //only messages longer than 3 bytes will be processed correctly furtheron
if (recbuf[0]!=0x55){lastdecodedlength=0; return(-1);}
if (recbuf[1]!=0xff){lastdecodedlength=1; return(-1);}
if (recbuf[2]!=0x00){lastdecodedlength=2; return(-1);}
switch(recbuf[4]){
case 0x10:
case 0x18: //alive
val=1;
lastdecodedlength=5;
break;
case 0x12: //get sensor value
case 0x1a:
val=(((int)recbuf[7])<<8)+recbuf[5];
lastdecodedlength=8;
break;
case 0x30:
case 0x38: //get battery power
val=(((int)recbuf[7])<<8)+recbuf[5];
lastdecodedlength=8;
break;
default: //unknown message!
lastdecodedlength=3;
return(-1);
break;
}
}
return(val);
}
DWORD WINAPI RCX_loop(LPVOID lpParameter){
//void RCX_loop(){
//main routine for serial/usb I/O thread
int i,len,val,chunksize;
unsigned char switchbit=0;
unsigned char mess[256];
if (porttype==1){chunksize=16;} else {chunksize=7;} //serial and usb send different amounts of data
RCX_finished=0;
RCX_error=0;
//open port to lego infrared tower
if (!RCX_open(porttype,portname)){
RCX_stop=-1;
RCX_error=1;
return(0);
}
//initialize: set transmitter range to 'large'
mess[0]=0x31 | switchbit;
switchbit^=0x08;
mess[1]=1; //long range
RCX_send(mess,2);
Sleep(init_wait_ms);
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
//initialize: switch motors off
mess[0]=0x21 | switchbit;
switchbit^=0x08;
mess[1]=0x47; //switch off all motors
RCX_send(mess,2);
Sleep(init_wait_ms);
if (RCX_receive(recbuf,chunksize)!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
//initialize: set to forward
mess[1]=0x87; //set to forward
mess[0]=0xe1 | switchbit;
switchbit^=0x08;
RCX_send(mess,2);
Sleep(init_wait_ms);
if (RCX_receive(recbuf,chunksize)!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
//initialize: set motors power level to 7
mess[0]=0x13 | switchbit; //set motor power level
switchbit^=0x08;
mess[1]=7; //motors A,B,C
mess[2]=2; //set by immediately following value
mess[3]=7; //set all motors to initialized value (8)
RCX_send(mess,4);
Sleep(init_wait_ms);
if (porttype==1) chunksize=20; //serial port sends 20 bytes on this message, usb sends 7 as above
if (RCX_receive(recbuf,chunksize)!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
//initialize: set sensor types to raw
if (porttype==1) chunksize=18;
for (i=0; i<3; i++){
mess[0]=0x32 | switchbit; //set sensor type
switchbit^=0x08;
mess[1]=i; //sensor 0,1,2
mess[2]=0; //set to raw mode
RCX_send(mess,3);
Sleep(init_wait_ms);
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
}
//initialize: set sensor modes to raw
if (porttype==1) chunksize=18;
for (i=0; i<3; i++){
mess[0]=0x42 | switchbit; //set sensor mode
switchbit^=0x08;
mess[1]=i; //sensor 0,1,2
mess[2]=0; //set to raw mode, bool switch detection slope 0
RCX_send(mess,3);
Sleep(init_wait_ms);
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize){
RCX_close();
RCX_stop=-1;
RCX_error=2;
return(0);
}
}
RCX_motor_val[0]=8;
RCX_motor_val[1]=8;
RCX_motor_val[2]=8;
last_motor_on[0]=0;
last_motor_on[1]=0;
last_motor_on[2]=0;
last_motor_val[0]=8;
last_motor_val[1]=8;
last_motor_val[2]=8;
RCX_stop=0;
while(!RCX_stop){
//this loop handles the interface-RCX communication
//(in parallel with the main program, since it runs in an extra task)
work_error=0;
if (RCX_sound!=0){ //transmit signals for sounds
mess[0]=0x51 | switchbit;
mess[1]=RCX_sound-1;
switchbit^=0x08;
RCX_send(mess,2);
Sleep(wait_ms);
if (porttype==1){chunksize=16;} else {chunksize=7;}
if (RCX_receive(recbuf,chunksize)==chunksize){ //check if success
RCX_sound=0;
} else {
work_error=1;
}
}
if (RCX_sensor_on[0]){ //read alive
mess[0]=0x10 | switchbit;
switchbit^=0x08;
RCX_send(mess,1);
Sleep(wait_ms);
if (porttype==1){chunksize=14;} else {chunksize=7;}
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize) work_error=2;
val=RCX_decode(recbuf,len);
if (val>=0) RCX_sensor_val[0]=val; //use data only if decoding worked
}
if (RCX_sensor_on[1]){ //read sensor 1
mess[0]=0x12 | switchbit; //get value
switchbit^=0x08;
mess[1]=9; //get value //12; //raw sensor value
mess[2]=0; //sensor 1
RCX_send(mess,3);
Sleep(wait_ms);
if (porttype==1){chunksize=22;} else {chunksize=11;}
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize) work_error=2;
val=RCX_decode(recbuf,len);
if (val>=0) RCX_sensor_val[1]=val; //use data only if decoding worked
}
if (RCX_sensor_on[2]){ //read sensor 2
mess[0]=0x12 | switchbit; //get value
switchbit^=0x08;
mess[1]=9; //get value //12; //raw sensor value
mess[2]=1; //sensor 2
RCX_send(mess,3);
Sleep(wait_ms);
if (porttype==1){chunksize=22;} else {chunksize=11;}
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize) work_error=2;
val=RCX_decode(recbuf,len);
if (val>=0) RCX_sensor_val[2]=val; //use data only if decoding worked
}
if (RCX_sensor_on[3]){ //read sensor 3
mess[0]=0x12 | switchbit; //get value
switchbit^=0x08;
mess[1]=9; //get value //12; //raw sensor value
mess[2]=2; //sensor 3
RCX_send(mess,3);
Sleep(wait_ms);
if (porttype==1){chunksize=22;} else {chunksize=11;}
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize) work_error=2;
val=RCX_decode(recbuf,len);
if (val>=0) RCX_sensor_val[3]=val; //use data only if decoding worked
}
if (RCX_sensor_on[4]){ //read battery
mess[0]=0x30 | switchbit;
switchbit^=0x08;
RCX_send(mess,1);
Sleep(wait_ms);
if (porttype==1){chunksize=18;} else {chunksize=11;}
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize) work_error=2;
val=RCX_decode(recbuf,len);
if (val>=0) RCX_sensor_val[4]=val; //use data only if decoding worked
}
//sensor type handling
if (porttype==1){chunksize=18;} else {chunksize=7;}
for (i=1; i<=3; i++){
work_sensor_type[i]=RCX_sensor_type[i];
if ((work_sensor_type[i]<0)||(work_sensor_type[i]>4)){ //only sensor type values 0..4 allowed.
work_sensor_type[i]=0;
RCX_sensor_type[i]=0; //in this case, force 0
}
if (work_sensor_type[i]!=last_sensor_type[i]){
mess[0]=0x32 | switchbit; //get value
switchbit^=0x08;
mess[1]=i-1; //sensor 0,1,2
mess[2]=work_sensor_type[i]; //set type
RCX_send(mess,3);
Sleep(wait_ms);
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize){
work_error=2;
} else {
last_sensor_type[i]=work_sensor_type[i];
}
}
}
//sensor mode handling
if (porttype==1){chunksize=18;} else {chunksize=7;}
for (i=1; i<=3; i++){
work_sensor_mode[i]=RCX_sensor_mode[i];
if ((work_sensor_mode[i]<0)||(work_sensor_mode[i]>7)){ //only sensor mode values 0..7 allowed.
work_sensor_mode[i]=0;
RCX_sensor_mode[i]=0; //in this case, force 0
}
if (work_sensor_mode[i]!=last_sensor_mode[i]){
mess[0]=0x42 | switchbit; //get value
switchbit^=0x08;
mess[1]=i-1; //sensor 0,1,2
mess[2]=(work_sensor_mode[i])<<5; //bits 5-7 code the mode
RCX_send(mess,3);
Sleep(wait_ms);
len=RCX_receive(recbuf,chunksize);
if (len!=chunksize){
work_error=2;
} else {
last_sensor_mode[i]=work_sensor_mode[i];
}
}
}
//motor_val handling
//motor value changes (direction changes, may come from float but not go to float)
//set motor direction
motor_on_ok[0]=1;
motor_on_ok[1]=1;
motor_on_ok[2]=1;
motor_val_ok[0]=1;
motor_val_ok[1]=1;
motor_val_ok[2]=1;
work_motor_val[0]=RCX_motor_val[0]; //fixate values in work_motor_val (prevents problems when task switches happen and the main task changes the value)
work_motor_val[1]=RCX_motor_val[1];
work_motor_val[2]=RCX_motor_val[2];
work_motor_on[0]=RCX_motor_on[0]; //fixate values in work_motor_val
work_motor_on[1]=RCX_motor_on[1];
work_motor_on[2]=RCX_motor_on[2];
if (porttype==1){chunksize=16;} else {chunksize=7;}
mess[1]=0;
//set directions to forward in case some motors were in floating mode or backward mode and are now set to forward
if ((work_motor_val[0]>0)&&(last_motor_val[0]<=0)) mess[1]+=1;
if ((work_motor_val[1]>0)&&(last_motor_val[1]<=0)) mess[1]+=2;
if ((work_motor_val[2]>0)&&(last_motor_val[2]<=0)) mess[1]+=4;
if (mess[1]>0){
mess[1]+=0x80; //set to forward
mess[0]=0xe1 | switchbit; //get value
switchbit^=0x08;
RCX_send(mess,2);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){ //check if failed
if((mess[1]&1)!=0) motor_val_ok[0]=0;
if((mess[1]&2)!=0) motor_val_ok[1]=0;
if((mess[1]&4)!=0) motor_val_ok[2]=0;
work_error=2;
}
}
mess[1]=0;
//set directions to backward in case some motors were in floating mode or forward mode and are now set to backward
if ((work_motor_val[0]<0)&&(last_motor_val[0]>=0)) mess[1]+=1;
if ((work_motor_val[1]<0)&&(last_motor_val[1]>=0)) mess[1]+=2;
if ((work_motor_val[2]<0)&&(last_motor_val[2]>=0)) mess[1]+=4;
if (mess[1]>0){
mess[1]+=0x00; //set to backward
mess[0]=0xe1 | switchbit; //get value
switchbit^=0x08;
RCX_send(mess,2);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){ //check if failed
if((mess[1]&1)!=0) motor_val_ok[0]=0;
if((mess[1]&2)!=0) motor_val_ok[1]=0;
if((mess[1]&4)!=0) motor_val_ok[2]=0;
work_error=2;
}
}
//motor value changes (power changes, may come from floating but not go to floating)
//set motor power. Set several motors with one command if they are set to equal values to minimize number of messages
if (porttype==1){chunksize=20;} else {chunksize=7;}
mess[1]=0;
if ((abs(work_motor_val[0])!=abs(last_motor_val[0])) && (work_motor_val[0]!=0)){
mess[1]+=1;
if (abs(work_motor_val[1])==abs(work_motor_val[0])) mess[1]+=2;
if (abs(work_motor_val[2])==abs(work_motor_val[0])) mess[1]+=4;
mess[0]=0x13 | switchbit; //get value
switchbit^=0x08;
mess[2]=2;
mess[3]=abs(work_motor_val[0]);
mess[3]--;
RCX_send(mess,4);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_val_ok[0]=0;
if((mess[1]&2)!=0) motor_val_ok[1]=0;
if((mess[1]&4)!=0) motor_val_ok[2]=0;
work_error=2;
}
}
mess[1]=0;
if ((abs(work_motor_val[1])!=abs(last_motor_val[1])) && (abs(work_motor_val[1])!=abs(work_motor_val[0])) && (work_motor_val[1]!=0)){
mess[1]+=2;
if (abs(work_motor_val[2])==abs(work_motor_val[1])) mess[1]+=4;
mess[0]=0x13 | switchbit; //get value
switchbit^=0x08;
mess[2]=2;
mess[3]=abs(work_motor_val[1]);
mess[3]--;
RCX_send(mess,4);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_val_ok[0]=0;
if((mess[1]&2)!=0) motor_val_ok[1]=0;
if((mess[1]&4)!=0) motor_val_ok[2]=0;
work_error=2;
}
}
mess[1]=0;
if ((abs(work_motor_val[2])!=abs(last_motor_val[2]))
&& ((abs(work_motor_val[2])!=abs(work_motor_val[1])) || (abs(work_motor_val[1])==abs(last_motor_val[1])))
&& ((abs(work_motor_val[2])!=abs(work_motor_val[0])) || (abs(work_motor_val[0])==abs(last_motor_val[0])))
&& (work_motor_val[2]!=0)){
mess[1]+=4;
mess[0]=0x13 | switchbit; //get value
switchbit^=0x08;
mess[2]=2;
mess[3]=abs(work_motor_val[2]);
mess[3]--;
RCX_send(mess,4);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_val_ok[0]=0;
if((mess[1]&2)!=0) motor_val_ok[1]=0;
if((mess[1]&4)!=0) motor_val_ok[2]=0;
work_error=2;
}
}
//motor_on handling
if (porttype==1){chunksize=16;} else {chunksize=7;}
//switch motors from off to on or motor leaves float, motor is on
mess[1]=0;
if (((work_motor_on[0]!=0) && (last_motor_on[0]==0) && (work_motor_val[0]!=0))
|| ((work_motor_val[0]!=0) && (last_motor_val[0]==0) && (work_motor_on[0]!=0))) mess[1]+=1;
if (((work_motor_on[1]!=0) && (last_motor_on[1]==0) && (work_motor_val[1]!=0))
|| ((work_motor_val[1]!=0) && (last_motor_val[1]==0) && (work_motor_on[1]!=0))) mess[1]+=2;
if (((work_motor_on[2]!=0) && (last_motor_on[2]==0) && (work_motor_val[2]!=0))
|| ((work_motor_val[2]!=0) && (last_motor_val[2]==0) && (work_motor_on[2]!=0))) mess[1]+=4;
if (mess[1]>0){
mess[1]+=0x80; //switch on motors
mess[0]=0x21 | switchbit; //get value
switchbit^=0x08;
RCX_send(mess,2);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_on_ok[0]=0;
if((mess[1]&2)!=0) motor_on_ok[1]=0;
if((mess[1]&4)!=0) motor_on_ok[2]=0;
work_error=2;
}
}
//switch motors from off to float mode or motor is on and val goes to 0 (float)
mess[1]=0;
if (((work_motor_on[0]!=0) && (last_motor_on[0]==0) && (work_motor_val[0]==0))
|| ((work_motor_val[0]==0) && (last_motor_val[0]!=0) && (work_motor_on[0]!=0))) mess[1]+=1;
if (((work_motor_on[1]!=0) && (last_motor_on[1]==0) && (work_motor_val[1]==0))
|| ((work_motor_val[1]==0) && (last_motor_val[1]!=0) && (work_motor_on[1]!=0))) mess[1]+=2;
if (((work_motor_on[2]!=0) && (last_motor_on[2]==0) && (work_motor_val[2]==0))
|| ((work_motor_val[2]==0) && (last_motor_val[2]!=0) && (work_motor_on[2]!=0))) mess[1]+=4;
if (mess[1]>0){
mess[1]+=0x00; //switch on in float mode
mess[0]=0x21 | switchbit;
switchbit^=0x08;
RCX_send(mess,2);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_on_ok[0]=0;
if((mess[1]&2)!=0) motor_on_ok[1]=0;
if((mess[1]&4)!=0) motor_on_ok[2]=0;
work_error=2;
}
}
//switch motors off?
if (((work_motor_on[0]==0)&&(last_motor_on[0]!=0))||((work_motor_on[1]==0)&&(last_motor_on[1]!=0))||((work_motor_on[2]==0)&&(last_motor_on[2]!=0))){
//some motors have to be switched off
mess[0]=0x21 | switchbit;
switchbit^=0x08;
mess[1]=0;
if (work_motor_on[0]==0) mess[1]+=1;
if (work_motor_on[1]==0) mess[1]+=2;
if (work_motor_on[2]==0) mess[1]+=4;
mess[1]+=0x40; //switch off motors
RCX_send(mess,2);
Sleep(wait_ms);
if(RCX_receive(recbuf,chunksize)!=chunksize){
if((mess[1]&1)!=0) motor_on_ok[0]=0;
if((mess[1]&2)!=0) motor_on_ok[1]=0;
if((mess[1]&4)!=0) motor_on_ok[2]=0;
work_error=2;
}
}
//store current state for use in the next loop
if (motor_on_ok[0]) last_motor_on[0]=work_motor_on[0];
if (motor_on_ok[1]) last_motor_on[1]=work_motor_on[1];
if (motor_on_ok[2]) last_motor_on[2]=work_motor_on[2];
if (motor_val_ok[0]) last_motor_val[0]=work_motor_val[0];
if (motor_val_ok[1]) last_motor_val[1]=work_motor_val[1];
if (motor_val_ok[2]) last_motor_val[2]=work_motor_val[2];
RCX_error=work_error;
}
len=RCX_close();
RCX_stop=0;
RCX_finished=1;
return(1);
}
int RCX_start(int type, char *in_portname){
//forks task for serial/usb port handling and returns 1 if success, 0 if failed
//type 1: serial port, type 2: USB port
//portname for serial port is "COM1" for example
//portname for USB port is "\\\\.\\LEGOTOWER1" for example
//to stop thread, set global variable RCX_stop to 1
int i=0;
while(in_portname[i]!=0){
portname[i]=in_portname[i];
i++;
}
portname[i]=0;
porttype=type;
RCX_stop=1;
CreateThread(NULL,0,&RCX_loop,0,0,&RCX_thread_id); //fork thread
while(RCX_stop==1) Sleep(10); //wait until thread has been initialized
if (RCX_stop==-1) return(0);
return(1);
}