-
Notifications
You must be signed in to change notification settings - Fork 0
/
mecanumrover_commander.c
1571 lines (1372 loc) · 67.4 KB
/
mecanumrover_commander.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
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
NLAB-MecanumCommander for Linux, a simple bridge to control VStone MecanumRover 2.1 / VStone MegaRover 3
by David Vincze, vincze.david@webcode.hu
at Human-System Laboratory, Chuo University, Tokyo, Japan, 2021-2022
version 0.60
https://github.com/szaguldo-kamaz/
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ncurses.h>
#include <locale.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include "mecanumrover_commlib.h"
#include "crc16/crc16.h"
#define COMMANDER_VERSION "0.60"
#define COMMANDER_PASSWORD "PASSWORD"
#define REPEAT_TIME_SEC_CMDSENT 0.4
#define REPEAT_TIME_SEC_KCMDSENT 0.4
#define REPEAT_TIME_SEC_REMOTECMDRECV 0.5
//#define REPEAT_TIME_SEC_MEMMAPREAD 0.5
#define REPEAT_TIME_SEC_MEMMAPREAD 0.4
int got_sigpipe = 0;
void errormsg(unsigned char *errmsg, unsigned char xpos) {
attron(COLOR_PAIR(5));
mvprintw(1, xpos, errmsg);
attroff(COLOR_PAIR(5));
beep();
timeout(-1);
getch();
}
void sigpipe_handler(int signum) {
got_sigpipe = 1;
}
void stoprobot(struct roverstruct *rover, char usekcommands, char *answer) {
if (usekcommands == 1) {
rover_kset_STOP(answer);
} else {
rover_set_XYrotation_speed_to_zero(rover, answer);
}
}
void commandsend_lamp_on() {
attron(COLOR_PAIR(7));
// mvprintw(2 + 5, 2 + 9, "⚞ ⚟");
mvprintw(2, 2 + 23, "⚟");
attroff(COLOR_PAIR(7));
refresh();
}
void commandsend_lamp_off() {
attron(COLOR_PAIR(1));
// mvprintw(2 + 5, 2 + 9, " ");
mvprintw(2, 2 + 23, " ");
attroff(COLOR_PAIR(1));
refresh();
}
void read_memmap_files(struct roverstruct *rover) {
int fd;
fd = open("memmap_0x10.dat", O_RDONLY);
if (fd != -1) {
read(fd, rover->memmap_main, 512);
}
close(fd);
fd = open("memmap_0x1F.dat", O_RDONLY);
if (fd != -1) {
read(fd, rover->memmap_second, 512);
}
close(fd);
}
void logmsg(int logfd, double time_start, const char *msg) {
double time_curr;
char logmsg[1024];
struct timeval timestruct;
gettimeofday(×truct, NULL);
time_curr = timestruct.tv_sec + timestruct.tv_usec / 1000000.0;
sprintf(logmsg, "%9.3f %s\n", time_curr - time_start, msg);
write(logfd, logmsg, strlen(logmsg));
}
int main() {
int ret;
unsigned char answer[BUFFER_SIZE];
struct roverstruct rover;
struct timeval timestruct;
double time_start, time_current, time_last_memmapread, time_last_cmdsent, time_last_kcmdsent, time_last_remotecmd_recv;
unsigned char remotecmd_timed_out=1;
unsigned char repeatcommand_timeisup=0;
int logfd;
char logstring[BUFFER_SIZE+BUFFER_SIZE/4];
int speedX=0, speedY=0, rotate=0;
int prevspeedX=0, prevspeedY=0, prevrotate=0;
char quit=0, c, c2, c3, c4, c5, c6;
int roverdrawx=5, roverdrawy=10;
int commanddrawx=2, commanddrawy=2;
int statusdrawx=30, statusdrawy=2;
int aboutdrawx=2, aboutdrawy=16;
fd_set commfdset;
struct timeval tv;
int listenfd=0, clientfd=0, sockread=0, socketcommbuff_offset=0;
struct sockaddr_in serv_addr;
unsigned int udp_lastpacketno=0;
unsigned char socketcommbuff[BUFFER_SIZE+1];
unsigned char receivedcommand[16];
unsigned char set_new_spx_value_from_remote=0;
unsigned char set_new_spy_value_from_remote=0;
unsigned char set_new_rot_value_from_remote=0;
unsigned char main_motor_status, second_motor_status;
unsigned char remotecontrol=0; // set to 1 to listen on tcp/3475 for easy remote control
unsigned char dummymode=0; // for testing, do not send real commands to the robot
unsigned char keyboardmode=1; // for future use...
unsigned char repeatcommands=1; // repeat commands every REPEAT_TIME_SEC_CMDSENT, so "commandtimeout" on the robot's controller won't trigger
unsigned char usekcommands=0; // use the "triple command set"
unsigned char readmemmapfromfile=0; // do not get the memmap from the robot, instead read it from a file
unsigned char remotecontrolproto=0; // 0 - TCP, 1 - UDP
unsigned char refreshmemmap=0; // re-read memmap periodically (on/off - 1/0)
unsigned char nolamp_when_setcmd=1; // do not blink the "lamps" on the UI when sending set speed commands
// create logfile
logfd = open("mecanumcommander.log", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP);
if (logfd == -1) {
printf("Cannot create logfile: mecanumcommander.log\n");
exit(1);
}
gettimeofday(×truct, NULL);
time_start = timestruct.tv_sec + timestruct.tv_usec / 1000000.0;
logmsg(logfd, time_start, "Initializing");
if (dummymode == 1) {
int fd;
fd = open("memmap_sample.dat", O_RDONLY);
if (fd == -1) {
perror("open(memmap_sample.dat)");
exit(1);
}
if (read(fd, rover.memmap_main, 386) == -1) {
perror("read() memmap first part");
exit(1);
}
if (read(fd, rover.memmap_second, 386) == -1) {
perror("read() memmap second part");
exit(1);
}
close(fd);
if (rover_identify_from_main_memmap(&rover) == 1 ) {
printf("Unknown rover type: 0x%X!\n", rover.sysname);
exit(1);
}
} else {
if (check_serial_dev() == -1) {
printf("Cannot open serial port: %s!\n", DEVFILE);
exit(1);
}
if (readmemmapfromfile == 1) {
if (access("memmap_0x10.dat", R_OK) != 0) {
printf("Cannot access memmap_0x10.dat!\n");
exit(1);
}
read_memmap_files(&rover); // when using memmapupdate_via_wifi.sh
if (rover_identify_from_main_memmap(&rover) == 1 ) {
printf("Unknown rover type: 0x%X!\n", rover.sysname);
exit(1);
}
} else {
if (rover_identify(&rover) == 0) {
printf("Rover found: 0x%x:%s FWRev: 0x%x\n", rover.sysname, rover.fullname, rover.firmrev);
} else {
printf("Unknown rover type: 0x%X!\n", rover.sysname);
exit(1);
}
logmsg(logfd, time_start, "Reading main memmap from robot - initial");
ret = rover_read_full_memmap(rover.memmap_main, rover.regs->controller_addr_main, &rover);
if (ret == -2) {
logmsg(logfd, time_start, "Err: Fatal error, while reading main memmap! (initial)");
printf("Fatal error, while reading main memmap - initial!");
exit(1);
}
if (ret != 384) {
logmsg(logfd, time_start, "Err: Failed to read main memmap correctly (invalid length) (initial)");
printf("Failed to read main memmap correctly (invalid length: %d) - initial.", ret);
exit(1);
}
if (rover.config->has_second_controller == 1) {
logmsg(logfd, time_start, "Reading second memmap from robot - initial");
ret = rover_read_full_memmap(rover.memmap_second, rover.regs->controller_addr_second, &rover);
if (ret == -2) {
logmsg(logfd, time_start, "Err: Fatal error, while reading second memmap! (initial)");
printf("Fatal error, while reading second memmap - initial!");
exit(1);
}
if (ret != 384) {
logmsg(logfd, time_start, "Err: Failed to read second memmap correctly (invalid length) (initial)");
printf("Failed to read second memmap correctly (invalid length: %d) - initial", ret);
exit(1);
}
}
}
}
sprintf(logstring, "Rover type: 0x%x:%s FWRev: 0x%x", rover.sysname, rover.fullname, rover.firmrev);
logmsg(logfd, time_start, logstring);
// bind to tcp/3475 or udp/3475
if (remotecontrol == 1) {
typedef void (*sighandler_t)(int);
sighandler_t sigret;
unsigned char replymsg[128];
int replylen, wret;
int enable=1;
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(3475);
if (remotecontrolproto == 0 ) { // TCP
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
perror("socket(TCP)");
exit(3);
}
sigret = signal(SIGPIPE, sigpipe_handler);
if (sigret == SIG_ERR) {
perror("signal(SIGPIPE)");
exit(3);
}
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt(SO_REUSEADDR)");
exit(3);
}
ret = bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (ret == -1) {
perror("bind(INADDR_ANY/tcp/3475)");
exit(3);
}
listen(listenfd, 1);
if (ret == -1) {
perror("listen()");
exit(3);
}
printf("Waiting for connection on tcp/3475...\n");
logmsg(logfd, time_start, "Waiting for connection on tcp/3475");
clientfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
if (clientfd == -1) {
perror("accept()");
exit(3);
}
replylen = sprintf(replymsg, "I'm NLAB-MecanumCommander. Please authenticate yourself.\r\n");
wret = write(clientfd, replymsg, replylen);
logmsg(logfd, time_start, "Welcome string sent to client");
if (wret == -1) {
printf("Cannot send reply to client! Connection lost?\n");
perror("write()");
exit(6);
}
FD_ZERO(&commfdset);
FD_SET(clientfd, &commfdset);
tv.tv_sec = 10;
tv.tv_usec = 0;
ret = select(clientfd + 1, &commfdset, NULL, NULL, &tv);
if (ret == -1) {
perror("select()");
exit(7);
}
if (got_sigpipe == 1) {
printf("Broken pipe.\n");
exit(8);
}
if (ret) {
sockread = read(clientfd, &socketcommbuff[socketcommbuff_offset], BUFFER_SIZE);
if ((sockread > 10) ||
((strncmp(socketcommbuff, COMMANDER_PASSWORD, 8) != 0) &&
(strncmp(socketcommbuff, COMMANDER_PASSWORD"\n", 9) != 0) &&
(strncmp(socketcommbuff, COMMANDER_PASSWORD"\r\n", 10) != 0) ) ) {
replylen = sprintf(replymsg, "!BADPWD!\r\n");
wret = write(clientfd, replymsg, replylen);
logmsg(logfd, time_start, "Bad password!");
if (wret == -1) {
printf("Cannot send reply to client! Connection lost?\n");
perror("write()");
exit(6);
}
shutdown(clientfd, SHUT_RDWR);
close(clientfd);
usleep(1000000);
shutdown(listenfd, SHUT_RDWR);
close(listenfd);
exit(0);
}
} else { // timeout
replylen = sprintf(replymsg, "Timeout. Goodbye!\r\n");
logmsg(logfd, time_start, "Authentication timed out");
wret = write(clientfd, replymsg, replylen);
if (wret == -1) {
printf("Cannot send reply to client! Connection lost?\n");
perror("write()");
exit(6);
}
shutdown(clientfd, SHUT_RDWR);
shutdown(listenfd, SHUT_RDWR);
close(clientfd);
close(listenfd);
exit(0);
}
replylen = sprintf(replymsg, "NLAB-MecanumCommander v" COMMANDER_VERSION " - Rover type: 0x%02x firmware: 0x%02x. Ready.\r\n", rover.sysname, rover.firmrev);
wret = write(clientfd, replymsg, replylen);
logmsg(logfd, time_start, "Welcome message sent to client");
if (wret == -1) {
printf("Cannot send reply to client! Connection lost?\n");
perror("write()");
exit(6);
}
sockread = 0;
} else { // UDP
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
if (listenfd == -1) {
perror("socket(UDP)");
exit(3);
}
ret = bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (ret == -1) {
perror("bind(INADDR_ANY/udp/3475)");
exit(3);
}
}
}
// it seems like this doesn't really do anything on this robot... but anyway
if (dummymode == 0) {
printf("Enabling motors on main controller.\n");
rover_enable_motors(&rover, rover.regs->controller_addr_main, answer);
logmsg(logfd, time_start, "Enabled motors on main controller");
if (rover.config->has_second_controller == 1) {
printf("Enabling motors on second controller.\n");
rover_enable_motors(&rover, rover.regs->controller_addr_second, answer);
logmsg(logfd, time_start, "Enabled motors on second controller");
}
}
// ncurses init
setlocale(LC_CTYPE, "");
initscr();
noecho();
cbreak();
timeout(500);
start_color();
init_pair(1, 15, 0); // feher + fekete
init_pair(2 , 9, 7); // piros + szurke
init_pair(3, 10, 0); // zold + fekete
init_pair(4, 10, 15); // zold + feher
init_pair(5, 9, 0); // piros + fekete
init_pair(6, 12, 0); // kek + fekete
init_pair(7, 11, 0); // sarga + fekete
init_pair(8, 12, 7); // kek + szurke
init_pair(9, 7, 0); // szurke + fekete
init_pair(10, 0, 7); // fekete + szurke
curs_set(0);
attron(COLOR_PAIR(1));
mvprintw(0, 1, "Rover type 0x%x (%s) Firmware revision: 0x%x", rover.sysname, rover.fullname, rover.firmrev);
attroff(COLOR_PAIR(1));
switch (rover.sysname) {
case SYSNAME_MECANUMROVER21:
attron(COLOR_PAIR(9));
mvprintw(roverdrawy , roverdrawx + 5, "☵▇█▇☵");
mvprintw(roverdrawy + 1, roverdrawx + 5, "▐███▌");
mvprintw(roverdrawy + 2, roverdrawx + 5, "☵");
attroff(COLOR_PAIR(9));
attron(COLOR_PAIR(10));
mvprintw(roverdrawy + 2, roverdrawx + 6, "▁▁▁");
attroff(COLOR_PAIR(10));
attron(COLOR_PAIR(2));
mvprintw(roverdrawy + 2, roverdrawx + 7, "⬤");
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(9));
mvprintw(roverdrawy + 2, roverdrawx + 9, "☵");
attroff(COLOR_PAIR(9));
break;
case SYSNAME_MEGAROVER3:
attron(COLOR_PAIR(5));
mvprintw(roverdrawy - 1, roverdrawx + 6, "▂");
attroff(COLOR_PAIR(5));
attron(COLOR_PAIR(9));
mvprintw(roverdrawy , roverdrawx + 5, "≣███≣");
mvprintw(roverdrawy + 1, roverdrawx + 7, "█▊");
// mvprintw(roverdrawy + 1, roverdrawx + 7, "█▉");
attroff(COLOR_PAIR(9));
attron(COLOR_PAIR(10));
mvprintw(roverdrawy + 1, roverdrawx + 6, "▎");
mvprintw(roverdrawy + 2, roverdrawx + 7, "▇");
attroff(COLOR_PAIR(10));
break;
default:
attron(COLOR_PAIR(9));
mvprintw(roverdrawy , roverdrawx + 5, "☵███☵");
mvprintw(roverdrawy + 2, roverdrawx + 6, "███");
attroff(COLOR_PAIR(9));
attron(COLOR_PAIR(2));
mvprintw(roverdrawy + 1, roverdrawx + 7, "⬤");
attroff(COLOR_PAIR(2));
}
attron(A_UNDERLINE | A_BOLD);
mvprintw(commanddrawy, commanddrawx, "Command ");
mvprintw(statusdrawy, statusdrawx , "Rover status ");
mvprintw(aboutdrawy, aboutdrawx , "About ");
attroff(A_UNDERLINE | A_BOLD);
attron(COLOR_PAIR(1));
mvprintw(aboutdrawy + 1, aboutdrawx, "NLAB-MecanumCommander");
mvprintw(aboutdrawy + 2, aboutdrawx + 7, "for Linux v" COMMANDER_VERSION);
mvprintw(aboutdrawy + 4, aboutdrawx, "See source for more info.");
mvprintw(commanddrawy + 1, commanddrawx, "X Speed: 0 mm/s");
if (rover.config->has_Y_speed == 1) {
mvprintw(commanddrawy + 2, commanddrawx, "Y Speed: 0 mm/s");
}
mvprintw(commanddrawy + 3, commanddrawx, "Rotation: 0 mrad/s");
mvprintw(statusdrawy + 1, statusdrawx, "Uptime :");
mvprintw(statusdrawy + 2, statusdrawx, "Battery :");
mvprintw(statusdrawy + 3, statusdrawx, "Motors 🏍️ :");
switch (rover.sysname) {
case SYSNAME_MECANUMROVER21:
mvprintw(statusdrawy + 4, statusdrawx + 1, "Speed M3,M4 : ");
mvprintw(statusdrawy + 5, statusdrawx + 1, "Speed M1,M2 : ");
mvprintw(statusdrawy + 6, statusdrawx + 1, "Position M3,M4 : ");
mvprintw(statusdrawy + 7, statusdrawx + 1, "Position M1,M2 : ");
mvprintw(statusdrawy + 8, statusdrawx + 1, "Encoder M3,M4 : ");
mvprintw(statusdrawy + 9, statusdrawx + 1, "Encoder M1,M2 : ");
mvprintw(statusdrawy + 10, statusdrawx + 1, "OutputOffset M3,M4 : ");
mvprintw(statusdrawy + 11, statusdrawx + 1, "OutputOffset M1,M2 : ");
mvprintw(statusdrawy + 12, statusdrawx + 1, "MotOutCalc M3,M4 : %%");
mvprintw(statusdrawy + 13, statusdrawx + 1, "MotOutCalc M1,M2 : %%");
mvprintw(statusdrawy + 14, statusdrawx + 1, "MeasuredCurr M3,M4 : A");
mvprintw(statusdrawy + 15, statusdrawx + 1, "MeasuredCurr M1,M2 : A");
mvprintw(statusdrawy + 16, statusdrawx + 1, "MaxCurrent M3,M4 : A");
mvprintw(statusdrawy + 17, statusdrawx + 1, "MaxCurrent M1,M2 : A");
mvprintw(statusdrawy + 18, statusdrawx + 1, "CurrentLimit M3,M4 : A");
mvprintw(statusdrawy + 19, statusdrawx + 1, "CurrentLimit M1,M2 : A");
break;
case SYSNAME_MEGAROVER3:
mvprintw(statusdrawy + 5, statusdrawx + 1, "MotorSpeed M1,M2 : ");
mvprintw(statusdrawy + 9, statusdrawx + 1, "Encoder M1,M2 : ");
// mvprintw(statusdrawy + 15, statusdrawx + 1, "MeasuredCurr M1,M2 : A");
break;
}
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(7));
mvprintw(statusdrawy + 2, statusdrawx + 16, "⚡");
attroff(COLOR_PAIR(7));
// the fun begins here
time_last_memmapread = 0;
time_last_cmdsent = 0;
time_last_kcmdsent = 0;
time_last_remotecmd_recv = 0;
logmsg(logfd, time_start, "Start");
while ((quit == 0) && (got_sigpipe == 0)) {
int setret = 0;
gettimeofday(×truct, NULL);
time_current = timestruct.tv_sec + timestruct.tv_usec / 1000000.0;
if ( (refreshmemmap == 1) && (dummymode == 0) ) {
if ((time_current - time_last_memmapread) > REPEAT_TIME_SEC_MEMMAPREAD) {
// heart on
attron(COLOR_PAIR(5) | A_BOLD);
mvprintw(statusdrawy + 1, statusdrawx + 16, "♥");
attroff(COLOR_PAIR(5) | A_BOLD);
refresh();
if (readmemmapfromfile == 1) {
logmsg(logfd, time_start, "Reading memmap from files");
read_memmap_files(&rover); // when using memmapupdate_via_wifi.sh
} else { // read memmap from rover
logmsg(logfd, time_start, "Reading main memmap from robot");
ret = rover_read_full_memmap(rover.memmap_main, rover.regs->controller_addr_main, &rover);
if (ret == -2) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Err: Fatal error, while reading main memmap! (2)");
errormsg("Fatal error, while reading main memmap! Press a key to quit!", 5);
quit = 2;
break;
}
if (ret != 384) {
unsigned char errmsg[256];
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Err: Failed to read main memmap correctly (invalid length) (3)");
sprintf(errmsg, "Failed to read main memmap correctly (invalid length: %d). Press a key to quit!", ret);
errormsg(errmsg, 1);
quit = 3;
break;
}
if (rover.config->has_second_controller == 1) {
logmsg(logfd, time_start, "Reading second memmap from robot");
ret = rover_read_full_memmap(rover.memmap_second, rover.regs->controller_addr_second, &rover);
if (ret == -2) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Err: Fatal error, while reading second memmap! (2)");
errormsg("Fatal error, while reading second memmap! Press a key to quit!", 5);
quit = 2;
break;
}
if (ret != 384) {
unsigned char errmsg[256];
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Err: Failed to read second memmap correctly (invalid length) (3)");
sprintf(errmsg, "Failed to read second memmap correctly (invalid length: %d). Press a key to quit!", ret);
errormsg(errmsg, 1);
quit = 3;
break;
}
}
}
gettimeofday(×truct, NULL);
time_last_memmapread = timestruct.tv_sec + timestruct.tv_usec / 1000000.0;
}
}
if ((rover.rs485_err_0x10 > 0) || (rover.rs485_err_0x1F > 0)) {
int alertcolor = 7;
if ((rover.rs485_err_0x10 >= 5) || (rover.rs485_err_0x1F >= 5)) {
alertcolor = 5;
}
attron(COLOR_PAIR(alertcolor));
mvprintw(1, 30, "⚠ RS485 CommErr! Main:%03d Front:%03d ⚠", rover.rs485_err_0x10, rover.rs485_err_0x1F);
attroff(COLOR_PAIR(alertcolor));
}
attron(COLOR_PAIR(1));
mvprintw(statusdrawy + 1, statusdrawx + 22, " % 6.2lf sec", rover_get_uptime(&rover));
mvprintw(statusdrawy + 2, statusdrawx + 22, " %2.2lf V", rover_get_battery_voltage(&rover));
attroff(COLOR_PAIR(1));
main_motor_status = rover_get_motor_status(&rover, rover.memmap_main);
if ((main_motor_status & 1) == 1) {
attron(COLOR_PAIR(3));
mvprintw(statusdrawy + 3, statusdrawx + 22, "On ");
if (rover.sysname == SYSNAME_MECANUMROVER21) {
mvprintw(roverdrawy + 3, roverdrawx + 2, "M1");
} else {
mvprintw(roverdrawy - 1, roverdrawx + 2, "M1");
}
attroff(COLOR_PAIR(3));
} else {
attron(COLOR_PAIR(5));
mvprintw(statusdrawy + 3, statusdrawx + 22, "Off");
if (rover.sysname == SYSNAME_MECANUMROVER21) {
mvprintw(roverdrawy + 3, roverdrawx + 2, "M1");
} else {
mvprintw(roverdrawy - 1, roverdrawx + 2, "M1");
}
attroff(COLOR_PAIR(5));
}
if ((main_motor_status >> 1) == 1) {
attron(COLOR_PAIR(3));
mvprintw(statusdrawy + 3, statusdrawx + 26, "On ");
if (rover.sysname == SYSNAME_MECANUMROVER21) {
mvprintw(roverdrawy + 3, roverdrawx + 11, "M2");
} else {
mvprintw(roverdrawy - 1, roverdrawx + 11, "M2");
}
attroff(COLOR_PAIR(3));
} else {
attron(COLOR_PAIR(5));
mvprintw(statusdrawy + 3, statusdrawx + 26, "Off");
if (rover.sysname == SYSNAME_MECANUMROVER21) {
mvprintw(roverdrawy + 3, roverdrawx + 11, "M2");
} else {
mvprintw(roverdrawy - 1, roverdrawx + 11, "M2");
}
attroff(COLOR_PAIR(5));
}
if (rover.config->motor_count == 4) {
second_motor_status = rover_get_motor_status(&rover, rover.memmap_second);
if ((second_motor_status & 1) == 1) {
attron(COLOR_PAIR(3));
mvprintw(statusdrawy + 3, statusdrawx + 30, "On ");
mvprintw(roverdrawy - 1, roverdrawx + 2, "M3");
attroff(COLOR_PAIR(3));
} else {
attron(COLOR_PAIR(5));
mvprintw(statusdrawy + 3, statusdrawx + 30, "Off");
mvprintw(roverdrawy - 1, roverdrawx + 2, "M3");
attroff(COLOR_PAIR(5));
}
if ((second_motor_status >> 1) == 1) {
attron(COLOR_PAIR(3));
mvprintw(statusdrawy + 3, statusdrawx + 34, "On ");
mvprintw(roverdrawy - 1, roverdrawx + 11, "M4");
attroff(COLOR_PAIR(3));
} else {
attron(COLOR_PAIR(5));
mvprintw(statusdrawy + 3, statusdrawx + 34, "Off");
mvprintw(roverdrawy - 1, roverdrawx + 11, "M4");
attroff(COLOR_PAIR(5));
}
}
attron(COLOR_PAIR(1));
switch (rover.sysname) {
case SYSNAME_MECANUMROVER21:
mvprintw(statusdrawy + 4, statusdrawx + 22, "% 6d,% 6d", rover_get_speed0(&rover, rover.memmap_second), rover_get_speed1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 5, statusdrawx + 22, "% 6d,% 6d", rover_get_speed0(&rover, rover.memmap_main), rover_get_speed1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 6, statusdrawx + 22, "% 6d,% 6d", rover_get_measured_position0(&rover, rover.memmap_second), rover_get_measured_position1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 7, statusdrawx + 22, "% 6d,% 6d", rover_get_measured_position0(&rover, rover.memmap_main), rover_get_measured_position1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 8, statusdrawx + 22, "% 6d,% 6d", rover_get_encoder_value0(&rover, rover.memmap_second), rover_get_encoder_value1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 9, statusdrawx + 22, "% 6d,% 6d", rover_get_encoder_value0(&rover, rover.memmap_main), rover_get_encoder_value1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 10, statusdrawx + 22, "% 6d,% 6d", rover_get_outputoffset0(&rover, rover.memmap_second), rover_get_outputoffset1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 11, statusdrawx + 22, "% 6d,% 6d", rover_get_outputoffset0(&rover, rover.memmap_main), rover_get_outputoffset1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 12, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_motoroutput_calc0(&rover, rover.memmap_second), rover_get_motoroutput_calc1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 13, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_motoroutput_calc0(&rover, rover.memmap_main), rover_get_motoroutput_calc1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 14, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_measured_current_value0(&rover, rover.memmap_second), rover_get_measured_current_value1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 15, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_measured_current_value0(&rover, rover.memmap_main), rover_get_measured_current_value1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 16, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_max_current0(&rover, rover.memmap_second), rover_get_max_current1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 17, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_max_current0(&rover, rover.memmap_main), rover_get_max_current1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 18, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_current_limit0(&rover, rover.memmap_second), rover_get_current_limit1(&rover, rover.memmap_second));
mvprintw(statusdrawy + 19, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_current_limit0(&rover, rover.memmap_main), rover_get_current_limit1(&rover, rover.memmap_main));
break;
case SYSNAME_MEGAROVER3:
mvprintw(statusdrawy + 5, statusdrawx + 22, "% 8d,% 8d", rover_get_motorspeed0(&rover, rover.memmap_main), rover_get_motorspeed1(&rover, rover.memmap_main));
mvprintw(statusdrawy + 9, statusdrawx + 22, "% 8d,% 8d", rover_get_encoder_value0(&rover, rover.memmap_main), rover_get_encoder_value1(&rover, rover.memmap_main));
// mvprintw(statusdrawy + 15, statusdrawx + 22, "% 6.2lf,% 6.2lf", rover_get_measured_current_value0(&rover, rover.memmap_main), rover_get_measured_current_value1(&rover, rover.memmap_main));
break;
}
attroff(COLOR_PAIR(1));
// heart off
if ( (refreshmemmap == 1) && (dummymode == 0) ) {
attron(COLOR_PAIR(1));
mvprintw(statusdrawy + 1, statusdrawx + 16, " ");
attroff(COLOR_PAIR(1));
}
refresh();
c = -1;
FD_ZERO(&commfdset);
FD_SET(0, &commfdset);
if (remotecontrol == 1) {
if (remotecontrolproto == 0) { // TCP
FD_SET(clientfd, &commfdset);
} else { // UDP
FD_SET(listenfd, &commfdset);
}
}
tv.tv_sec = 0;
tv.tv_usec = REPLYWAIT_TIMEOUT_USEC;
if (remotecontrol == 1) {
if (remotecontrolproto == 0) { // TCP
ret = select(clientfd + 1, &commfdset, NULL, NULL, &tv);
} else { // UDP
ret = select(listenfd + 1, &commfdset, NULL, NULL, &tv);
}
} else {
ret = select(1, &commfdset, NULL, NULL, &tv);
}
if (ret == -1) {
endwin();
perror("select()");
quit = 1;
break;
} else {
if (got_sigpipe == 1) { break; }
if (ret) {
if (FD_ISSET(0, &commfdset)) {
c = getch();
sprintf(logstring, "Keypress: %c", c);
logmsg(logfd, time_start, logstring);
}
// don't read any new data, while the previous buffer has not been emptied yet
if ( (remotecontrol == 1) && (sockread == 0) ) {
if (remotecontrolproto == 0) { // TCP
if (FD_ISSET(clientfd, &commfdset)) {
sockread = read(clientfd, &socketcommbuff[socketcommbuff_offset], BUFFER_SIZE);
socketcommbuff[sockread] = 0;
if (sockread == 0) {
logmsg(logfd, time_start, "Client disconnected. (7)");
errormsg("Client disconnected! Press a key to quit!", 1);
quit = 7;
break;
}
sprintf(logstring, "Read from socket %d:-%s-", sockread, socketcommbuff);
logmsg(logfd, time_start, logstring);
if (sockread == -1) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Socket read error! (4)");
errormsg("Socket read error! Press a key to quit!", 1);
quit = 4;
break;
}
}
} else { // UDP
unsigned int udprecvpacketno = 0;
unsigned char udp_payload[32];
int udp_sockread;
sockread = 0;
while (1) {
FD_ZERO(&commfdset);
FD_SET(listenfd, &commfdset);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(listenfd + 1, &commfdset, NULL, NULL, &tv);
if (ret == -1) {
endwin();
perror("select()");
quit = 1;
break;
}
if (sockread >= (BUFFER_SIZE - 32)) {
sprintf(logstring, "UDP buffer full: %d/%d", sockread, BUFFER_SIZE - 32);
logmsg(logfd, time_start, logstring);
break;
}
if (FD_ISSET(listenfd, &commfdset)) {
udp_sockread = recvfrom(listenfd, udp_payload, 32, 0, (struct sockaddr *)NULL, NULL);
udprecvpacketno++;
sprintf(logstring, "Read UDP packet %d. (%d):%x %x %x %x %x %x %x %x %x %x %x %x", udprecvpacketno, udp_sockread,
udp_payload[0], udp_payload[1], udp_payload[2], udp_payload[3], udp_payload[4], udp_payload[5], udp_payload[6], udp_payload[7], udp_payload[8], udp_payload[9], udp_payload[10], udp_payload[11] );
logmsg(logfd, time_start, logstring);
if (udp_sockread == 12) {
unsigned int udp_packetno = (udp_payload[0] << 8) + udp_payload[1];
if ( (udp_packetno > udp_lastpacketno) ||
( (udp_lastpacketno > 0xFF00) && (udp_packetno < 0x00FF) ) ) { // allow a little tolerance for possible packet loss
unsigned int recvcksum = (udp_payload[10] << 8) + udp_payload[11];
unsigned int calccksum = crc16_ccitt(udp_payload, 10);
if (recvcksum != calccksum) {
sprintf(logstring, "Checksum error! Dropped UDP packet %d:%s:recv/calccrc:%x/%x", udp_packetno, &udp_payload[2], recvcksum, calccksum);
logmsg(logfd, time_start, logstring);
continue;
}
udp_payload[10] = 0;
sprintf(logstring, "Command in UDP packet:-%s-", &udp_payload[2]);
logmsg(logfd, time_start, logstring);
strncpy(&socketcommbuff[sockread], &udp_payload[2], 8);
sockread += 9;
socketcommbuff[sockread-1] = '\n';
socketcommbuff[sockread] = 0;
//sprintf(logstring, "Full socketcommbuff(%d):-%s-", sockread, socketcommbuff);
//logmsg(logfd, time_start, logstring);
udp_lastpacketno = udp_packetno;
} else {
sprintf(logstring, "Dropped old UDP packet: %d vs. %d", udp_packetno, udp_lastpacketno);
logmsg(logfd, time_start, logstring);
continue;
}
} else {
logmsg(logfd, time_start, "UDP payload is not 12 bytes!");
}
} else { // nothing can be read (select)
break; // while(1)
}
} // while(1)
if (quit == 1) {
break;
}
} // UDP
} // remotecontrol true + sockbuff is empty
} else {
sockread = 0;
}
}
while (sockread > 0) {
unsigned char replymsg[16];
unsigned int wret;
unsigned int sockcommi, copyi, commlen, difflen;
for (sockcommi = 0; ((socketcommbuff[sockcommi] != '\n') && (socketcommbuff[sockcommi] != '\r') && (sockcommi < BUFFER_SIZE) && (sockcommi < sockread)); sockcommi++) {}
if (sockcommi == BUFFER_SIZE) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_on();
}
logmsg(logfd, time_start, "Bad message through socket - Too long (no newline found) (5)!");
errormsg("Bad message through socket - Too long (no newline found)! Press a key to quit!", 1);
quit = 5;
break;
}
// first char was a newline
if (sockcommi == 1) {
for (sockcommi = 0; sockcommi < (sockread-1); sockcommi++) {
socketcommbuff[sockcommi] = socketcommbuff[sockcommi+1];
}
}
commlen = sockcommi;
// if not just an empty line (only /r/n) - commlen/sockcommi == 0
if ((commlen > 0) && (socketcommbuff[sockcommi-1] == '\r')) {
commlen--;
}
strncpy(receivedcommand, socketcommbuff, commlen);
receivedcommand[commlen] = 0;
difflen = sockread - sockcommi - 1;
if (difflen != 0) {
for (copyi = 0; copyi < difflen; copyi++) {
socketcommbuff[copyi] = socketcommbuff[copyi+sockcommi+1];
}
socketcommbuff[copyi] = 0;
}
sockread -= sockcommi + 1;
socketcommbuff[sockread] = 0;
if (commlen == 0) { continue; }
if (commlen != 8) {
logmsg(logfd, time_start, "Bad command length (!=8)");
if (remotecontrolproto == 0) { // TCP
logmsg(logfd, time_start, "Sent: !BADCMD!");
strncpy(replymsg, "!BADCMD!\r\n", 10);
wret = write(clientfd, replymsg, 10);
if (wret == -1) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
logmsg(logfd, time_start, "Err: Cannot send reply to client (6).");
errormsg("Cannot send reply to client! Connection lost? Press a key to quit!", 1);
quit = 6;
break;
}
}
continue;
}
{
int badcommand = 1;
int cmd_stopzero = 0;
int cmd_resetall = 0;
logmsg(logfd, time_start, "Processing command: ");
logmsg(logfd, time_start, receivedcommand);
if (strncmp(receivedcommand, "STOPZERO", 8) == 0) {
cmd_stopzero = 1;
} else if (strncmp(receivedcommand, "RESETALL", 8) == 0) {
cmd_resetall = 1;
}
if ((cmd_stopzero == 1) || (cmd_resetall == 1)) {
if (dummymode == 0) {
commandsend_lamp_on();
logmsg(logfd, time_start, "Stoprobot");
stoprobot(&rover, usekcommands, answer);
commandsend_lamp_off();
}
rotate = 0;
speedX = 0;
speedY = 0;
badcommand = 0;
if (cmd_resetall == 1) {