-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgb_serial.c
More file actions
974 lines (845 loc) · 23.9 KB
/
gb_serial.c
File metadata and controls
974 lines (845 loc) · 23.9 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
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
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <stdlib.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX 80
#define PORT 10001
#define LANTRONIX "10.130.133.24"
#define SA struct sockaddr
#include "gb_serial.h"
#define error_message printf
#define NMOTORS 7
int moog_lock=0;
const char * STATUS_CODES[4][16] = {
{
"Drive Ready",
"Bo: Motor is off (indicator)",
"Bt: Trajectory in progress (indicator)",
"Servo Bus Voltage Fault",
"Peak Over Current occurred",
"Excessive Temperature fault latch",
"Excessive Position Error Fault",
"Velocity Limit Fault",
"Real-time temperature limit",
"Derivative Error Limit (dE/dt) Fault",
"Hardware Limit Positive Enabled",
"Hardware Limit Negative Enabled",
"Historical Right Limit (+ or Positive)",
"Historical Left Limit (- or Negative)",
"Right ( + or Positive) Limit Asserted",
"Left Limit ( - or Negative) Asserted",
},
{
"Rise Capture Encoder(0) Armed",
"Fall Capture Encoder(0) Armed",
"Rising edge captured ENC(0) (historical bit)",
"Falling edge captured ENC(0) (historical bit)",
"Rise Capture Encoder(1) Armed",
"Fall Capture Encoder(1) Armed",
"Rising edge captured ENC(1) (historical bit)",
"Falling edge captured ENC(1) (historical bit)",
"Capture input state 0 (indicator)",
"Capture input state 1 (indicator)",
"Software Travel Limits Enabled",
"Soft limit mode (indicator): 0-Don’t Stop. 1-Cause Fault. Default is 1",
"Historical positive software over travel limit",
"Historical negative software over travel limit",
"Real time positive soft limit (indicator)",
"Real time negative soft limit (indicator)",
},
{
"Error on Communications Channel 0",
"Error on Communications Channel 1",
"USB Error (Class 6 only)",
"Reserved 3",
"CAN Port Error",
"Reserved 5",
"Ethernet Error (Class 6 only)",
"I2C Running",
"Watchdog Event",
"ADB (Animatics Data Block) Bad Checksum",
"Program Running",
"Trace in Progress",
"EE Write Buffer Overflow",
"EE Busy",
"Command Syntax Error",
"Program Checksum Error",
},
{
"Reserved 0",
"Torque Saturation",
"Voltage Saturation",
"Wraparound Occurred",
"KG Enabled",
"Velocity Direction",
"Torque Direction",
"I/O Fault Latch",
"Relative Position Mode",
"Reserved 9",
"Peak Current Saturation",
"Modulo Rollover",
"Brake Asserted",
"Brake OK",
"External Go Enabled",
"Velocity Target Reached",
}
};
/*############################################################################
# Title: lantronix_reset()
# Author: C.Johnson
# Date: 10/2/19
# Args: char *host -> address of lantronix
int port -> port that telnet server runs on(usually 9999 on lantronix)
# Returns: N/A
# Description: Resets the Lantronix UDS1100 RS485/Network adapter. This
# is done in a hacky way by talking to the telnet server with
# sleeps to delay between communications.
#
#############################################################################*/
void gb_lantronix_reset(char *host, short port)
{
int sockfd, connfd, x;
char serialfix=128;
char buff[500];
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(host);
servaddr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
//first wait for the connection message
sleep(2);
memset(buff, 0, sizeof(buff));
x=recv(sockfd, buff, 500, MSG_DONTWAIT);
//printf("x=%i chars\n", x);
//printf("buff=%s\n", buff);
//send a carriage return to enter the menu
//then wait for the menu items
write(sockfd, "\r", 1);
sleep(2);
memset(buff, 0, sizeof(buff));
x=recv(sockfd, buff, sizeof(buff), MSG_DONTWAIT);
//printf("x=%i chars\n", x);
//printf("buff=%s\n", buff);
//select option 9 "save and exit", and
//send another carriage return
write(sockfd, "9\r", 2);
close(sockfd);
sleep(5);
}
/*############################################################################
# Title: open_port_net()
# Author: C.Johnson
# Date: 9/9/19
# Args:
# Returns:
#
#############################################################################*/
int open_port_net(char *host, short port)
{
int sockfd, connfd;
char serialfix=128;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(host);
servaddr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
int status = fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
if (status == -1)
{
perror("calling fcntl");
// handle the error. By the way, I've never seen fcntl fail in this way
}
moog_write(sockfd, &serialfix);//
return sockfd;
}
/*############################################################################
# Title: set_interface_attribs
# Author: C.Johnson
# Date: 9/9/19
# Args: int fd -> tty port file descriptor
# int speed -> serial speed
# int polarity -> serial polarity
# Returns: 0 on success, non zero on failure
# Description: setup tty port serial attibutes for port referred to by fd
#
#############################################################################*/
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
error_message ("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
/*############################################################################
# Title: set_blocking
# Author: C.Johnson
# Date: 9/9/19
# Args: int fd -> tty port file descriptor
# int should_block -> 1 for blocking, 0 for non blocking
# Returns: N/A
# Description: setup tty port blocking attribute for port referred to by fd
#
#############################################################################*/
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error_message ("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
error_message ("error %d setting term attributes", errno);
}
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port( char usbport[] )
{
int fd; /* File descriptor for the port */
char serialfix = 128;
fd = open (usbport, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
error_message ("error %d opening %s: %s", errno, usbport, strerror (errno));
return (fd);
}
//set_interface_attribs (fd, B9600, 0); // set speed to 9600 bps, 8n1 (no parity)
set_interface_attribs (fd, B115200, 0); // set speed to 115200 bps, 8n1 (no parity)
set_blocking (fd, 0); // set no blocking
// this is necessary, not sure why.
moog_write(fd, &serialfix);
return (fd);
}
/*
* 'close_port()' - close serial port 1.
*
*
*/
int close_port( int fd )
{
close(fd);
}
int moog_write( int rs485_fd, const char *msg )
{
char send_buffer[SENDSIZE];
if( strlen(msg) > SENDSIZE-1 )
return -1; //Buffer oversize.
while(moog_lock){
fprintf(stderr, "damn mutex!\n");
usleep(100000);
}
snprintf(send_buffer, strlen(msg)+2, "%s ", msg );
fprintf(stderr, "sending <%s>\n", send_buffer);
write( rs485_fd, send_buffer, strlen(send_buffer) );
}
/********************************************************
* Name: moog_read
* args: rs485_fd-> filedescriptor for serial port
* resp -> pointer to char array to be filled
* with data from the serial port
* Descr: Read one line of data from (ending in \n or \r)
* and put that data in the resp char array.
* The serial line is non blocking
* this function should probably be called
* moog_readline, but alas it is not.
*
*
* -Scott Swindell September 2019
*
*
* ****************************************************/
int moog_read( int rs485_fd, char resp[] )
{
int rn=1;
int ii=0;
fd_set set;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
FD_ZERO(&set);
FD_SET(rs485_fd, &set);
moog_lock=1;
while(rn > 0)
{
if(select( rs485_fd+1, &set, NULL, NULL, &timeout ) == 1)
{
rn = read(rs485_fd, resp+ii, 1);
//fprintf(stderr, "%i %c\n", ii, resp[ii]);
if(resp[ii] == '\r' || resp[ii] == '\n')
{
resp[ii+1] = '\0';
moog_lock = 0;
return 0;// finished line
}
ii++;
}
else
{
break;
}
}
moog_lock=0;
return -1;// there is no data on the line
}
int moog_callsub( int rs485_fd, int subnum, int can_addr )
{
char msg[20];
if( can_addr == -1 )//head node
snprintf( msg, 20, "GOSUB(%i)", subnum );
else
snprintf( msg, 20, "GOSUB(%i):%i", subnum, can_addr );
moog_write( rs485_fd, msg );
return 0;
}
/****************************************************
* Name: moog_init
* Args: rs485_fd -> file descript of serial port
* Description:
* Sends a reset, 'Z', and calls the head node
* subroutine on the head node. This starts
* the head node and then triggers all
* nodes to ready themselves.
*
*
* -Scott Swindell 8/2019
*********************************************/
int moog_init(int rs485_fd )
{
char resp[10000];
moog_write(rs485_fd, "Z:0"); //reset all nodes
sleep(1);
moog_write(rs485_fd, "ddd=0" ); // Turn debug off
moog_write(rs485_fd, "GOSUB(0)" ); // start head node
usleep(200000);
usleep(200000);
//moog_write(rs485_fd, "aa:2=100000"); // lower the y velocity
//moog_write(rs485_fd, "VT:2=100000"); // lower the y velocity
moog_write(rs485_fd, "aa:2=50000"); // lower the y velocity
moog_write(rs485_fd, "VT:2=50000"); // lower the y velocity
while ( moog_read(rs485_fd, resp) !=-1 )
{
printf("%s\n", resp);// starting head node print a bunch of stuff
}
return 0;
}
/****************************************************
* Name: moog_home
* args: rs485_fd-> serial file descriptor
* can_addr-> motor number
*
*
* Description:
* Send a home command to a motor. For the
* linear stages this is the subroutine
* 100 for the filter wheels it is
* 101.
*
*
* **************************************************/
int moog_home( int rs485_fd, int can_addr )
{
switch(can_addr)
{
case -1: //Home all in series
moog_callsub( rs485_fd, 100, can_addr );
break;
case OFFSET_X:
case OFFSET_Y:
case OFFSET_FOCUS:
case OFFSET_MIRRORS:
moog_callsub( rs485_fd, 103, can_addr );
break;
case OFFSET_FWHEEL:
case FWHEEL_UPPER:
case FWHEEL_LOWER:
moog_callsub( rs485_fd, 103, can_addr );
break;
default:
printf("Motor %i can not be homed yet", can_addr);
return -1;
}
return 0;
}
/****************************************************
* Name: moog_lgoto
* args: rs485_fd-> serial file descriptor
* can_addr-> motor number
* pos-> integer position to send to the motor
*
*
* Description:
* Sends one of the linear stage motors
* to a new position. This works by setting
* the position target 'PT' on the motor
* and then sending go command 'GOSUB(500)'
*
*
* **************************************************/
int moog_lgoto(int rs485_fd, int can_addr, int pos )
{
char msg[40];
snprintf( msg, 40, "PT:%i=%i", can_addr, pos );
moog_write(rs485_fd, (const char *) msg);
usleep(100); // this is probably not necessary
//snprintf(msg, 40, "G:%i", can_addr );
//moog_write(rs485_fd, (const char *) msg ); //GO!
moog_callsub(rs485_fd, 500, can_addr);
return 0;
}
/****************************************************
* Name: moog_fgoto
* args: rs485_fd-> serial file descriptor
* can_addr-> motor number
* fnum-> integer filter number.
*
*
* Description:
* Moves one of the given filer wheel to the
* fitler number. This is done by
* setting the f variable on the motor using
* f:can_addr=fnum and movign the motor
* by calling the subroutine gosub(400).
*
*
* **************************************************/
int moog_fgoto( int rs485_fd, int can_addr, int fnum )
{
char msg[10];
snprintf( msg, 10, "f:%i=%i", can_addr, fnum );
moog_write(rs485_fd, (const char *) msg);
moog_callsub(rs485_fd, 400, can_addr);//call the subroutine to move fwheel
usleep(100); // this is probably not necessary
//moog_callsub(rs485_fd, )
}
/*******************************
* Name: moog_getstatus
* args: rs485_fd-> serial file descriptor
* stat -> status struct to be populated
*
* Description:
* Populates the given MSTATUS structure
* by querying the drive for various
* status words with the RW(WORDNUM):motor_num
* command. Each status word is a 16 bit number
* and the status codes are contained in the
* STATUS_CODES array.
*
* TODO: add other important statuses to the
* struct like a motion bit and current speed
* and acceleration.
*
*
*
* ***************************/
int moog_getstatus(int rs485_fd, MSTATUS* stat)
{
char msg[20];
char resp[READSIZE];
int error;
moog_read(rs485_fd, resp);//flush--probably unecessary
snprintf(msg, 20, "RPA:%i", stat->motor_num);
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
stat->pos = atoi(resp);
}
else
return -1;
snprintf(msg, 20, "Rf:%i", stat->motor_num);
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
stat->fnum = atoi(resp);
}
else
return -1;
for(int ii=0; ii<4; ii++)
{
snprintf( msg, 20, "RW(%i):%i", ii, stat->motor_num );
moog_write( rs485_fd, (const char *) msg );
error = moog_read(rs485_fd, resp);
stat->words[ii] = atoi(resp);
}
snprintf( msg, 20, "RW(16):%i", stat->motor_num );
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
stat->iobits = atoi(resp);
}
else
{
fprintf(stderr, "Could not determine homed status of %s check msg %s\n", stat->name, msg);
return -1;
}
snprintf( msg, 20, "RW(12):%i", stat->motor_num );
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
stat->userbits = atoi(resp);
stat->isHomed = stat->userbits & 1;
}
else
{
fprintf(stderr, "Could not determine homed status of %s check msg %s\n", stat->name, msg);
return -1;
}
switch(stat->motor_num)
{
case OFFSET_FWHEEL:
stat->isFilter = 1;
break;
case FWHEEL_LOWER:
stat->isFilter = 1;
break;
case FWHEEL_UPPER:
stat->isFilter = 1;
break;
default:
stat->isFilter = 0;
}
return 0;
}
void moog_serialfix(int moogfd)
{
char serialfix=128;
moog_write(moogfd, &serialfix);
}
/***************************************
*Name: moog_getallstatus
*Args: rs485_fd stat-> array of MSTATUS structs
*Description: get the status of all the drives
* By querying individual motors one at a
* time. This is a laborious process and
* should be replaced.
*
*
*
*
*
* ***************************************/
int moog_getallstatus(int rs485_fd, MSTATUS stat[])
{
int active;
char msg[10];
char resp[READSIZE];
snprintf(msg, 10, "RW(12)");
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
active = atoi(resp);
}
else
{
fprintf(stderr, "WE could not get active status\n");
return -1;
}
for(MSTATUS *motor=stat; motor!=stat+NMOTORS; motor++)
{
if((1<<motor->motor_num) & active)
{
//TODO error check this response.
moog_getstatus(rs485_fd, motor);
motor->isActive = 1;
}
else
motor->isActive = 0;
}
return active;
}
/*****************************************************8
*Name: moog_getallstatus_quick
*Args: rs485_fd, motors->allmotors array
*
*Description: This function replaces the moog_getallstatus
* with a quicker version. Here we call the 998 subroutine
* on the head node and it spits back the necessary info
* about each motor all at once rather that querying
* each piece of data one at a time.
*
*
*
* *****************************************************/
int moog_getallstatus_quick(int rs485_fd, MSTATUS motors[])
{
char resp[5000];
int read_status=0;
int motor_num=0, pos, f, w0, w1, w2, w3, userbits, iobits, word6, temp, current;
int wc, active;
static int foo = 0;
int motor_count=0;
char msg[10];
moog_read(rs485_fd, resp);//Flush the line
snprintf(msg, 10, "RW(12)");
moog_write(rs485_fd, (const char *) msg );
if( moog_read(rs485_fd, resp) == 0)
{
active = atoi(resp);
}
else
{
fprintf(stderr, "WE could not get active status\n");
return -1;
}
moog_callsub( rs485_fd, 998, -1);
int retries = 0;
usleep(500000);
while(motor_num <7 )
{
moog_read(rs485_fd, resp);
wc = sscanf(resp, "%i %i %i %i %i %i %i %i %i %i %i %i", &motor_num, &pos, &f, &w0, &w1, &w2, &w3, &userbits, &iobits, &word6, &temp, ¤t );
if (wc != 12)
{
retries++;
if (retries >= 5)
return -1;
fprintf(stderr, "bad, %i\n", retries );
//fprintf(stderr, "[%s]\n", resp );
//moog_serialfix(rs485_fd);
continue;
}
else
{
//fprintf(stderr, "{%s}\n", resp );
retries = 0;
}
//f seems to come back as ridiculous numbers sometimes... lets fix it
if(f>4){
fprintf(stderr, "found the bug\n");
f=0;
}
//fprintf(stderr, " %i %i %i %i %i %i %i %i %i\n", motor_num, pos, f, w0, w1, w2, w3, userbits, iobits );
for(MSTATUS *motor=motors; motor!=motors+NMOTORS; motor++)
{
if(motor_num == motor->motor_num)
{
motor->pos = pos;
motor->fnum = f;
motor->words[0] = w0;
motor->words[1] = w1;
motor->words[2] = w2;
motor->words[3] = w3;
motor->userbits = userbits;
motor->iobits = iobits;
motor->isActive = (1<<motor->motor_num) & active;
motor->inPosLimit = w0 & (1<<16);
motor->inNegLimit = w0 & (1<<15);
motor->isMoving = w0 & (1<<2);
motor->isHomed = userbits & 1;
motor->word6 = word6;
motor->current = current;
motor->temp = temp;
motor_count++;
}
}
}
if(motor_count != 7)
{
//fprintf(stderr, "We are missing motors\n");
}
return active;
}
/**********************************
* Name: print_status
* Arg: stat -> status struct to be printed
*
* Description:
* Pretty prints the status struct
* using the STATUS_CODES array.
*
* ********************************/
void print_status(MSTATUS stat)
{
printf("Motor %i %s:\n", stat.motor_num, stat.name);
printf(" Pos == %i\n", stat.pos);
printf(" fnum == %i\n", stat.fnum);
//report the first 4 status words
for( int word_ii=0; word_ii<4; word_ii++ )
{
printf(" Word%i == %i\n", word_ii, stat.words[word_ii]);
for(int bit=0; bit<16; bit++)
{
if( (1<<bit) & stat.words[word_ii] )
{//Print the error code descript of each status word bit
printf(" %s\n", STATUS_CODES[word_ii][bit]);
}
}
}
printf("\n\n");
}
/*******************************
* Name: build_stat_structs
* args: rs485_fd-> serial file descriptor
* motors - > arrary of status structs to be built
*
* Description:
* Uses the GOSUB(999) subroutine call
* to get a list of all the motor names
* and their numbers from from the serial
* line. This information is then stored
* in the array of status structs. This
* is how the GUI maps the can address
* (motor_num) to the name of the motor.
*
* TODO: The number of motors should
* probably be a variable and not a
* hard coded thing. Perhaps a macro
* is called for.
*
*
*
* ***************************/
int build_stat_structs( int rs485_fd, MSTATUS motors[] )
{
char resp[READSIZE];
char msg[20];
moog_read(rs485_fd, resp); //flush line
fprintf(stderr, "\nflush gives %s\n", resp);
moog_read(rs485_fd, resp); //flush line
fprintf(stderr, "flush gives %s\n", resp);
int wc;
int head_node;
//Grab the head node address.
moog_write(rs485_fd, "RCADDR");
moog_read(rs485_fd, resp);
head_node = atoi(resp);
moog_callsub(rs485_fd, 999, -1 );
for(int ii=0; ii<7; ii++ )
{
//TODO: We need to come up with a way to
//determine if the response back from the motor
//is reliable. If a motor is not communicating
//on the can bus (as long as its not the head),
//the response will be normal. This will require
//a change to the smartmotor program.
moog_read( rs485_fd, resp );
fprintf(stderr, "%i %s\n", ii, resp);
wc = sscanf(resp, "MOTOR #%i %s", &motors[ii].motor_num, motors[ii].name );
motors[ii].head_node = head_node;
if( strcmp(motors[ii].name, "OFFSET_FWHEEL") == 0 )
{
motors[ii].isFilter = 1;
}
else if( strcmp(motors[ii].name, "OFFSET_FWHEEL") == 0 )
{
motors[ii].isFilter = 1;
}
else if( strcmp(motors[ii].name, "OFFSET_FWHEEL") == 0 )
{
motors[ii].isFilter = 1;
}
else
{
motors[ii].isFilter = 0;
}
}
for( MSTATUS *motorii=motors; motorii!=motors+7; motorii++ )
{
if(motorii->motor_num < 1 || motorii->motor_num > 7 )
{
continue;// Erroneous motor num
}
//positive software limit
snprintf( msg, 20, "RSLP:%i", motorii->motor_num );
moog_write( rs485_fd, msg );
moog_read( rs485_fd, resp );
motorii->pos_slimit = atoi(resp);
//negative software limit
snprintf( msg, 20, "RSLN:%i", motorii->motor_num );
moog_write( rs485_fd, msg );
moog_read( rs485_fd, resp );
motorii->neg_slimit = atoi(resp);
//filter_dist is stored in hh variable.
snprintf( msg, 20, "Rhh:%i", motorii->motor_num );
moog_write( rs485_fd, msg );
moog_read( rs485_fd, resp );
motorii->fdist = atoi(resp);
}
}
MSTATUS * motor_by_name(char name[], MSTATUS motors[] )
{
MSTATUS * motor;
for(motor=motors; motor<motors+7; motors++)
if(strcmp( motor->name, name ) == 0)
return motor;
}