-
Notifications
You must be signed in to change notification settings - Fork 85
/
DexRun.c
5833 lines (4975 loc) · 175 KB
/
DexRun.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
//#define NO_BOOT_DANCE
//#define DEBUG_API
//#define DEBUG_XL320_UART
#define DEBUG_MONITOR
//
#define DEG_ARCSEC 3600
#define MONITOR_MAX_VELOCITY (100*DEG_ARCSEC)
//actual max angular velocity should never be more than 50 degress per second. JW
#define MONITOR_MAX_ERROR 180
//multiplier for the number of degrees of error the raw encoder can be off.
//180 basically disables it
// this is the SpeedsUpdate code
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>
#include <termios.h>
#include <inttypes.h>
#include <ctype.h>
//#include "test.h" //TODO: Make that work
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>
//prototypes
int getNormalizedInput(int);
int RestoreCalTables(char *filename);
void ProcessServerReceiveData(char *recBuff);
bool ProcessServerReceiveDataDDE(char *recBuff);
bool ProcessServerSendData(char *recBuff);
bool ProcessServerSendDataDDE(char *sendBuff,char *recBuff);
int ParseInput(char *iString);
int MoveRobot(int a1,int a2,int a3,int a4,int a5, int mode);
int ReadDMA(int p1,int p2,char *p3);
int CheckBoundry(int* j1, int* j2, int* j3, int* j4, int* j5);
void SendPacket(unsigned char *TxPkt, int length, int TxRxTimeDelay, unsigned char *RxPkt, int ReadLength);
void KeyholeSend(int *DataArray, int controlOffset, int size, int entryOffset );
int CalcUartTimeout(int size);
#define MAX_CONTENT_CHARS 62
//maybe could be 112, 4 * 32 bit integers and then 128 bytes in the socket
#define NUM_JOINTS 5
//how many standard joints are there? e.g. with encoders / drives / etc.. not including tool interface.
#define L1_TABLE 0x600000
#define L1_TABLE_LENGTH 0xa8000
#define L2_TABLE 0xa00000
#define L2_TABLE_LENGTH 0x52be0
#define L3_TABLE 0x800000
#define L3_TABLE_LENGTH 0x93f00
#define L5_TABLE 0xe00000
#define L5_TABLE_LENGTH 0xc4b60
#define L4_TABLE 0xc00000
#define L4_TABLE_LENGTH 0x6b0a0
#define INPUT_OFFSET 14
// Motor position index
#define CMD_POSITION_KEYHOLE 0
#define CMD_POSITION_KEYHOLE_CMD 19 // s19 datatype
#define CMD_POSITION_KEYHOLE_SIZE 5
#define ACCELERATION_MAXSPEED 1 // made acceleration 12 bits
#define ADC_CENTERS_KEYHOLE 2
#define ADC_CENTERS_KEYHOLE_CMD 29 // fixedpoint 13.16 datatype
#define ADC_CENTERS_KEYHOLE_SIZE 10 // sin/cos 5 axis
#define PID_P 3
#define PID_ADDRESS 4
//FORCE PARAMATERS
#define BOUNDRY_KEYHOLE 5
#define BOUNDRY_KEYHOLE_CMD 19 // S19 DATATYPE
#define BOUNDRY_KEYHOLE_SIZE 10
#define SPEED_FACTORA 6
#define BETA_XYZ 7 // FIXED 16
#define FRICTION_KEYHOLE 8
#define FRICTION_KEYHOLE_CMD 16 // FIXED 16 DATATYPE
#define FRICTION_KEYHOLE_SIZE 5
#define MOVE_TRHESHOLD 9
#define F_FACTOR 10
#define MAX_ERROR 11
#define FORCE_BIAS_KEYHOLE 12
#define FORCE_BIAS_KEYHOLE_CMD 19 // S19 DATATYPE
#define FORCE_BIAS_KEYHOLE_SIZE 5
// control state register
#define COMMAND_REG 13
#define CMD_CAPCAL_BASE 1
#define CMD_CAPCAL_END 2
#define CMD_CAPCAL_PIVOT 4
#define CMD_MOVEEN 8
#define CMD_MOVEGO 16
#define CMD_ENABLE_LOOP 32
#define CMD_CLEAR_LOOP 64
#define CMD_CALIBRATE_RUN 128
#define CMD_RESET_POSITION 256
#define CMD_RESET_FORCE 512
#define CMD_CAPCAL_ANGLE 1024
#define CMD_CAPCAL_ROT 2048
#define CMD_ANGLE_ENABLE 4096
#define CMD_ROT_ENABLE 8196
#define ISTRING_LEN 255 //should be less or equal to socket buffer size.
char iString[ISTRING_LEN]; //make global so we can re-use (main, getInput, etc...)
//DMA
#define DMA_CONTROL 14
#define DMA_WRITE_DATA 15
#define DMA_WRITE_PARAMS 16
#define DMA_WRITE_ADDRESS 17
#define DMA_READ_PARAMS 18
#define DMA_READ_ADDRESS 19
// DMA control bits breakdown
#define DMA_WRITE_ENQUEUE 1
#define DMA_WRITE_INITIATE 2
#define DMA_READ_DEQUEUE 4
#define DMA_READ_BLOCK 8
#define DMA_RESET_ALL 16
// RECORD AND PLAYPACK
#define REC_PLAY_CMD 20
#define CMD_RESET_RECORD 1
#define CMD_RECORD 2
#define CMD_RESET_PLAY 4
#define CMD_PLAYBACK 8
#define CMD_RESET_PLAY_POSITION 16
#define REC_PLAY_TIMEBASE 21
#define MAXSPEED_XYZ 22
#define DIFF_FORCE_BETA 23
#define DIFF_FORCE_MOVE_THRESHOLD 24
#define DIFF_FORCE_MAX_SPEED 25
#define DIFF_FORCE_SPEED_FACTOR_ANGLE 26
#define DIFF_FORCE_SPEED_FACTOR_ROT 27
#define EXTRUDER_CONTROL 28
#define FINE_ADJUST_KEYHOLE 29 // PID MOVE OFFSET
#define FINE_ADJUST_KEYHOLE_CMD 19 // S19 DATATYPE
#define FINE_ADJUST_KEYHOLE_SIZE 5
#define RECORD_LENGTH 30
#define END_EFFECTOR_IO 31
#define SERVO_SETPOINT_A 32
#define SERVO_SETPOINT_B 33
#define UART1_XMIT_CNT 34 // SELT,LOADQ,FORCE TRANSMIT,RESET
#define UART1_XMIT_DATA 35 // 8BIT
#define UART1_XMIT_TIMEBASE 36
#define BASE_FORCE_DECAY 37
#define END_FORCE_DECAY 38
#define PIVOT_FORCE_DECAY 39
#define ANGLE_FORCE_DECAY 40
#define ROTATE_FORCE_DECAY 41
#define PID_SCHEDULE_INDEX 42
#define GRIPPER_MOTOR_CONTROL 43
#define GRIPPER_MOTOR_ON_WIDTH 44
#define GRIPPER_MOTOR_OFF_WIDTH 45
#define START_SPEED 46
#define ANGLE_END_RATIO 47
#define RESET_PID_AND_FLUSH_QUEUE 48 // BIT ZERO = RESET PID ACCUMULATOR, BIT 1 = FLUSH QUEUE
#define XYZ_FORCE_TIMEBASE 49
#define DIFFERENTIAL_FORCE_TIMEBASE 50
#define PID_TIMEBASE 51
#define RAW_ECONDER_ANGLE_KEYHOLE 52
#define RAW_ECONDER_ANGLE_KEYHOLE_CMD 10 // s19 datatype
#define RAW_ECONDER_ANGLE_KEYHOLE_SIZE 5
// DMA DATA IN
#define DMA_READ_DATA 30 + INPUT_OFFSET
// READ REGISTER DEFINITIONS
// POSITION REPORT
#define BASE_POSITION_AT 0 + INPUT_OFFSET
#define END_POSITION_AT 1 + INPUT_OFFSET
#define PIVOT_POSITION_AT 2 + INPUT_OFFSET
#define ANGLE_POSITION_AT 3 + INPUT_OFFSET
#define ROT_POSITION_AT 4 + INPUT_OFFSET
//TABLE CALCULATED DELTA
#define BASE_POSITION_DELTA 5 + INPUT_OFFSET
#define END_POSITION_DELTA 6 + INPUT_OFFSET
#define PIVOT_POSITION_DELTA 7 + INPUT_OFFSET
#define ANGLE_POSITION_DELTA 8 + INPUT_OFFSET
#define ROT_POSITION_DELTA 9 + INPUT_OFFSET
//PID CALCULATED DELTA
#define BASE_POSITION_PID_DELTA 10 + INPUT_OFFSET
#define END_POSITION_PID_DELTA 11 + INPUT_OFFSET
#define PIVOT_POSITION_PID_DELTA 12 + INPUT_OFFSET
#define ANGLE_POSITION_PID_DELTA 13 + INPUT_OFFSET
#define ROT_POSITION_PID_DELTA 14 + INPUT_OFFSET
// FORCE CALCULATED POSITION MODIFICATION
#define BASE_POSITION_FORCE_DELTA 15 + INPUT_OFFSET
#define END_POSITION_FORCE_DELTA 16 + INPUT_OFFSET
#define PIVOT_POSITION_FORCE_DELTA 17 + INPUT_OFFSET
#define ANGLE_POSITION_FORCE_DELTA 18 + INPUT_OFFSET
#define ROT_POSITION_FORCE_DELTA 19 + INPUT_OFFSET
// RAW ANALOG TO DIGITAL VALUES
#define BASE_SIN 20 + INPUT_OFFSET
#define BASE_COS 21 + INPUT_OFFSET
#define END_SIN 22 + INPUT_OFFSET
#define END_COS 23 + INPUT_OFFSET
#define PIVOT_SIN 24 + INPUT_OFFSET
#define PIVOT_COS 25 + INPUT_OFFSET
#define ANGLE_SIN 26 + INPUT_OFFSET
#define ANGLE_COS 27 + INPUT_OFFSET
#define ROT_SIN 28 + INPUT_OFFSET
#define ROT_COS 29 + INPUT_OFFSET
// RECORD AND PLAYBACK
#define RECORD_BLOCK_SIZE 31 + INPUT_OFFSET
#define READ_BLOCK_COUNT 32 + INPUT_OFFSET
#define PLAYBACK_BASE_POSITION 33 + INPUT_OFFSET
#define PLAYBACK_END_POSITION 34 + INPUT_OFFSET
#define PLAYBACK_PIVOT_POSITION 35 + INPUT_OFFSET
#define PLAYBACK_ANGLE_POSITION 36 + INPUT_OFFSET
#define PLAYBACK_ROT_POSITION 37 + INPUT_OFFSET
#define END_EFFECTOR_IO_IN 38 + INPUT_OFFSET
#define SENT_BASE_POSITION 39 + INPUT_OFFSET
#define SENT_END_POSITION 40 + INPUT_OFFSET
#define SENT_PIVOT_POSITION 41 + INPUT_OFFSET
#define SENT_ANGLE_POSITION 42 + INPUT_OFFSET
#define SENT_ROT_POSITION 43 + INPUT_OFFSET
#define SLOPE_BASE_POSITION 44 + INPUT_OFFSET
#define SLOPE_END_POSITION 45 + INPUT_OFFSET
#define SLOPE_PIVOT_POSITION 46 + INPUT_OFFSET
#define SLOPE_ANGLE_POSITION 47 + INPUT_OFFSET
#define SLOPE_ROT_POSITION 48 + INPUT_OFFSET
#define CMD_FIFO_STATE 49 + INPUT_OFFSET
#define UART_DATA_IN 50 + INPUT_OFFSET
//Wiggles code:
#define BASE_MEASURED_ANGLE 51 + INPUT_OFFSET
#define END_MEASURED_ANGLE 52 + INPUT_OFFSET
#define PIVOT_MEASURED_ANGLE 53 + INPUT_OFFSET
#define ANGLE_MEASURED_ANGLE 54 + INPUT_OFFSET
#define ROT_MEASURED_ANGLE 55 + INPUT_OFFSET
// Encoder Angles (Integer portion only??)
#define BASE_EYE_NUMBER 56 + INPUT_OFFSET //
#define PIVOT_EYE_NUMBER 58 + INPUT_OFFSET// (not 57)
#define END_EYE_NUMBER 57 + INPUT_OFFSET // (not 58)
#define ANGLE_EYE_NUMBER 59 + INPUT_OFFSET//
#define ROT_EYE_NUMBER 60 + INPUT_OFFSET //
#define BASE_RAW_ENCODER_ANGLE_FXP 61 + INPUT_OFFSET // was 64 //was 61
#define PIVOT_RAW_ENCODER_ANGLE_FXP 63 + INPUT_OFFSET// was 61 //was 63 (not 62)
#define END_RAW_ENCODER_ANGLE_FXP 62 + INPUT_OFFSET // was 65 //was 62 (not 63)
#define ANGLE_RAW_ENCODER_ANGLE_FXP 64 + INPUT_OFFSET// was 62 //was 64
#define ROT_RAW_ENCODER_ANGLE_FXP 65 + INPUT_OFFSET // was 63 //was 65
int OldMemMapInderection[90]={0,0,0,0,0,ACCELERATION_MAXSPEED,0,0,0,0,0,0,0,0,0,0,0,0,0,0,PID_P,PID_ADDRESS,
0,0,0,0,0,SPEED_FACTORA,BETA_XYZ,0,0,0,0,0,MOVE_TRHESHOLD,F_FACTOR,MAX_ERROR,0,0,0,0,0,COMMAND_REG,
DMA_CONTROL,DMA_WRITE_DATA,DMA_WRITE_PARAMS,DMA_WRITE_ADDRESS,DMA_READ_PARAMS,DMA_READ_ADDRESS,
REC_PLAY_CMD,REC_PLAY_TIMEBASE,MAXSPEED_XYZ,DIFF_FORCE_BETA,DIFF_FORCE_MOVE_THRESHOLD,
DIFF_FORCE_MAX_SPEED,DIFF_FORCE_SPEED_FACTOR_ANGLE,DIFF_FORCE_SPEED_FACTOR_ROT, EXTRUDER_CONTROL,
0,0,0,0,0,0,0,0,0,BASE_FORCE_DECAY,END_FORCE_DECAY,PIVOT_FORCE_DECAY,ANGLE_FORCE_DECAY,ROTATE_FORCE_DECAY,
0,0,0,0,0,0,RESET_PID_AND_FLUSH_QUEUE,XYZ_FORCE_TIMEBASE,DIFFERENTIAL_FORCE_TIMEBASE,PID_TIMEBASE,0,0,0,0};
int ADLookUp[5] = {BASE_SIN,END_SIN,PIVOT_SIN,ANGLE_SIN,ROT_SIN};
//commands
#define MOVE_CMD 1
#define READ_CMD 2
#define WRITE_CMD 3
#define EXIT_CMD 4
#define SLOWMOVE_CMD 5
#define MOVEALL_CMD 6
#define DMAREAD_CMD 7
#define DMAWRITE_CMD 8
#define CAPTURE_AD_CMD 9
#define FIND_HOME_CMD 10
#define FIND_HOME_REP_CMD 11
#define LOAD_TABLES 12
#define CAPTURE_POINTS_CMD 14
#define FIND_INDEX_CMD 15
#define SLEEP_CMD 16
#define RECORD_MOVEMENT 17
#define REPLAY_MOVEMENT 18
#define MOVEALL_RELATIVE 19
#define SET_PARAM 20
#define SEND_HEARTBEAT 21
#define SET_FORCE_MOVE_POINT 22
#define HEART_BEAT 23
#define PID_FINEMOVE 24
#define SET_ALL_BOUNDRY 25
//////////////////////////////////////////////////////////////////////////
/* Start Wigglesworth Code*/
//////////////////////////////////////////////////////////////////////////
#define MOVETO_CMD 26
#define MOVETOSTRAIGHT_CMD 27
#define WRITE_TO_ROBOT 28
#define DEFAULT_MAXSPEED = 232642; // 30 (deg/s)
#define DEFAULT_STARTSPEED = 512; // .066 (deg/s) This is the smallest number allowed
//////////////////////////////////////////////////////////////////////////
/* End Wigglesworth Code*/
//////////////////////////////////////////////////////////////////////////
// modes
#define BLOCKING_MOVE 1
#define NON_BLOCKING_MOVE 2
#define DEFAULT_MOVE_TIMEOUT 0
#define TRUE 1
#define FALSE 0
// defaults
#define DEFAULT_PID_SETTING_XYZ 0x3dcCCCCC
//#define DEFAULT_PID_SETTING_XYZ 0x3f000000
//#define DEFAULT_PID_SETTING_XYZ 0x3f800000
//#define DEFAULT_PID_SETTING_PY 0x3f000000
//#define DEFAULT_PID_SETTING_PY 0x3D4CCCCC
//#define DEFAULT_PID_SETTING_PY 0x3dcCCCCC
#define DEFAULT_PID_SETTING_PY 0x3cf5c28f
#define DEF_SPEED_FACTOR_A 30
#define DEF_SPEED_FACTOR_DIFF 8
#define ACCELERATION_MAXSPEED_DEF 250000+(3<<20)
#define BASE_SIN_LOW 0X760
#define BASE_COS_LOW 0X1
#define BASE_SIN_HIGH 0x83A
#define BASE_COS_HIGH 0X40
#define END_SIN_LOW 0Xc01
#define END_COS_LOW 0X0
#define END_SIN_HIGH 0xd5c
#define END_COS_HIGH 0X70
#define PIVOT_SIN_LOW 0X50
#define PIVOT_COS_LOW 0X880
#define PIVOT_SIN_HIGH 0x100
#define PIVOT_COS_HIGH 0X8d1
#define ANGLE_SIN_LOW 0X900
#define ANGLE_COS_LOW 0X1
#define ANGLE_SIN_HIGH 0x9A0
#define ANGLE_COS_HIGH 0X50
#define ROT_SIN_LOW 0XA90
#define ROT_COS_LOW 0X1
#define ROT_SIN_HIGH 0xcc2
#define ROT_COS_HIGH 0X50
#define DEFAULT_ANGLE_BOUNDRY_HI 3000
#define DEFAULT_ANGLE_BOUNDRY_LOW -3000
#define SERVO_LOW_BOUND 1142000
#define SERVO_HI_BOUND 1355000
#define MONITOR_ERROR_CODE 666
#define BOUNDARY_ERROR_CODE 665
//////////////////////////////////////////////////////////////////////////
/* Start Wigglesworth Code*/
//double micro_step_per_s_to_MaxSpeed = 8.388535999997595;
double arcsec_per_nbits = 0.4642324678861586;
double bit_sec_per_microstep_clockcycle = 8.192;
int startSpeed_arcsec_per_sec = 3600;
int maxSpeed_arcsec_per_sec = 30*3600;
//int AngularSpeed = 30*3600; // (arcsec/s)
//int AngularSpeedStartAndEnd = 360; // (arcsec/s)
int AngularAcceleration = 1; // (arcsec/s^2)
int CartesianSpeed = 300000; // (micron/s)
int CartesianSpeedStart = 0; // (micron/s)
int CartesianSpeedEnd = 0; // (micron/s)
int CartesianAcceleration = 1000000; // (micron/s^2)
int CartesianStepSize = 10; // (micron)
int CartesianPivotSpeed = 108000; // (arcsec/s)
int CartesianPivotSpeedStart = 0; // (arcsec/s)
int CartesianPivotSpeedEnd = 0; // (arcsec/s)
int CartesianPivotAcceleration = 10800000; // (arcsec/s^2)
int CartesianPivotStepSize = 36; // (arcsec)
int LastGoal[5]={0,0,0,0,0};
//#include <cstdlib> //C++ header
//#include <math.h> // for trig and floor functions
//#include <iostream> //doesn't exist on Dexter
//#include <limits> // for intentionaly returning nan
//#include <time.h> /* c//lock_t, clock, CLOCKS_PER_SEC */
//#include <stdlib.h> //defined above
//#include <stdbool.h>
// Vector Library:
#include <math.h>
#define PI (3.141592653589793)
//LinkLengths
//double L[5] = { 0.1651, 0.320675, 0.3302, 0.0508, 0.08255 }; // (meters)
//double L[5] = { 165100, 320675, 330200, 50800, 82550 }; // (microns)
//double L[5] = { 165100, 340320, 321032, 50800, 140000 }; // (microns) JW 20190312 for HD version with gripper
double L[5] = { 228600, 320676, 330201, 50801, 82551 }; // JW / JF 20190523 better for HD from Monty
//double SP[5] = { 0, 0, 0, 0, 0 }; // (arcseconds)
int SP_CommandedAngles[5] = { 0, 0, 0, 0, 0 }; // Starting Position Commanded (arcseconds)
int SP_EyeNumbers[5] = { 0, 0, 0, 0, 0 }; // Starting Position EyeNumber (arcseconds)
struct Vector {
double x, y, z;
};
double max(double a, double b){
if(a > b){
return a;
}else{
return b;
}
}
double min(double a, double b){
if(a < b){
return a;
}else{
return b;
}
}
struct Vector new_vector(double x, double y, double z) {
struct Vector a;
a.x = x;
a.y = y;
a.z = z;
return a;
}
void print_vector(struct Vector a) {
printf("[%f, %f, %f]", a.x, a.y, a.z);
}
struct Vector add(struct Vector v1, struct Vector v2) {
struct Vector result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
};
struct Vector subtract(struct Vector v1, struct Vector v2) {
struct Vector result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
struct Vector mult(struct Vector v1, struct Vector v2) {
struct Vector result;
result.x = v1.x * v2.x;
result.y = v1.y * v2.y;
result.z = v1.z * v2.z;
return result;
}
struct Vector vector_div(struct Vector v1, struct Vector v2) {
struct Vector result;
result.x = v1.x / v2.x;
result.y = v1.y / v2.y;
result.z = v1.z / v2.z;
return result;
}
struct Vector scalar_mult(double a, struct Vector v) {
struct Vector result;
result.x = a * v.x;
result.y = a * v.y;
result.z = a * v.z;
return result;
}
struct Vector scalar_div(struct Vector v, double a) {
struct Vector result;
result.x = v.x / a;
result.y = v.y / a;
result.z = v.z / a;
return result;
}
struct Vector negate(struct Vector v) {
struct Vector result;
result.x = -v.x;
result.y = -v.y;
result.z = -v.z;
return result;
}
double magnitude(struct Vector v) {
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
struct Vector normalize(struct Vector v) {
double mag = magnitude(v);
return scalar_div(v, mag);
}
struct Vector cross(struct Vector v1, struct Vector v2) {
struct Vector result = new_vector(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
return result;
}
double dot(struct Vector v1, struct Vector v2) {
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
struct Vector v_abs(struct Vector v1){
struct Vector v_result = new_vector(fabs(v1.x), fabs(v1.y), fabs(v1.z));
return v_result;
}
double v_max(struct Vector v1){
double cur_max = 0;
if(v1.x > cur_max)
cur_max = v1.x;
if(v1.y > cur_max)
cur_max = v1.y;
if(v1.z > cur_max)
cur_max = v1.z;
return cur_max;
}
struct Vector v_round(struct Vector v1, int decimals) {
return new_vector(floor(v1.x * pow(10, decimals)) / pow(10, decimals), floor(v1.y * pow(10, decimals)) / pow(10, decimals), floor(v1.z * pow(10, decimals)) / pow(10, decimals));
}
bool is_equal(struct Vector v1, struct Vector v2, int decimals) {
bool result = pow(10, -decimals-1) > v_max(v_abs(subtract(v2, v1)));
/*
struct Vector r_v1 = v_round(v1, decimals);
struct Vector r_v2 = v_round(v2, decimals);
*/
/*
printf("\n\n is_equal:");
printf("\nv1: ");
print_vector(v1);
printf("\nv2: ");
print_vector(v2);
printf("\ndecimals: %d", decimals);
*/
/*
printf("\nr_v1: ");
print_vector(r_v1);
printf("\nr_v1: ");
print_vector(r_v1);
*/
//bool result = (r_v1.x == r_v2.x && r_v1.y == r_v2.y && r_v1.z == r_v2.z);
//printf("\nresult: %d", result);
return result;
}
struct Vector project_onto_plane(struct Vector vector, struct Vector plane) {
//double term1 = dot(vector, plane);
double plane_mag = magnitude(plane);
return subtract(vector, scalar_mult(dot(vector, plane) / (plane_mag*plane_mag), plane));
}
#define arcsec_to_rad PI/(180*3600); // Floating point error may be a problem
#define rad_to_arcsec (180*3600)/PI;
double sin_arcsec(double theta) {
//return sin(theta * arcsec_to_rad); //this was erroring for some reason
return sin(theta * PI / (180 * 3600));
}
double cos_arcsec(double theta) {
return cos(theta * PI / (180 * 3600));
}
double tan_arcsec(double theta) {
return tan(theta * PI / (180 * 3600));
}
double asin_arcsec(double ratio) {
return asin(ratio) * rad_to_arcsec;
}
double acos_arcsec(double ratio) {
return acos(ratio) * rad_to_arcsec;
}
double atan_arcsec(double ratio) {
return atan(ratio) * rad_to_arcsec;
}
double atan2_arcsec(double num1, double num2) {
return atan2(num1, num2) * rad_to_arcsec;
}
struct Vector rotate(struct Vector vector, struct Vector plane, double theta) {
if (is_equal(vector, plane, 14)) {
return vector;
}
return scalar_mult(magnitude(vector), normalize(add(scalar_mult(cos_arcsec(theta), vector), scalar_mult(sin_arcsec(theta), cross(normalize(plane), vector)))));
}
struct Vector three_points_to_plane(struct Vector u1, struct Vector u2, struct Vector u3) {
return normalize(cross(subtract(u2, u1), subtract(u3, u1)));
}
double angle(struct Vector v1, struct Vector v2) {
if (is_equal(v1, v2, 14)) {
return 0.0;
}
if (magnitude(add(v1, v2)) == 0) {
return 180*3600;
}
else {
return atan2_arcsec(magnitude(cross(v1, v2)), dot(v1, v2));
}
}
double signed_angle(struct Vector v1, struct Vector v2, struct Vector plane) {
double guess_angle = angle(v1, v2);
long double epsilon = 0.0000000001; // 1e-10
if (abs(guess_angle) < epsilon || abs(abs(guess_angle) - 648000) < epsilon) {
return round(guess_angle); // Will rounding increase or decrease error?
}
struct Vector c_prod = normalize(cross(v1, v2));
if (is_equal(c_prod, normalize(plane), 10)) {
return guess_angle;
}
else if (is_equal(negate(c_prod), normalize(plane), 10)){
return -guess_angle;
}
else {
printf("\nError: signed_angle wants to return NaN:");
printf("\nguessangle: %f", guess_angle);
printf("\nc_prod: ");
print_vector(c_prod);
printf("\nnegate(c_prod): ");
print_vector(negate(c_prod));
printf("\nv1: ");
print_vector(v1);
printf("\nv2: ");
print_vector(v2);
printf("\nplane: ");
print_vector(plane);
printf("\n\n");
}
return 0;
}
double dist_point_to_point(struct Vector point_a, struct Vector point_b) {
/*
printf("point_a: ");
print_vector(point_a);
printf(", point_b: ");
print_vector(point_b);
printf("\n");
*/
return magnitude(subtract(point_a, point_b));
}
double dist_point_to_line(struct Vector point, struct Vector line_point_a, struct Vector line_point_b) {
struct Vector term1 = subtract(point, line_point_a);
struct Vector term2 = subtract(point, line_point_b);
struct Vector term3 = subtract(line_point_b, line_point_a);
return magnitude(cross(term1, term2)) / magnitude(term3);
}
bool is_NaN(struct Vector v) {
return isnan(v.x) || isnan(v.y) || isnan(v.z);
}
//////////////////////////////////////////////////////////////////
// Kinematics:
//Config structure
struct Config{
bool right_arm, elbow_up, wrist_out;
};
struct Config new_config(bool right_arm, bool elbow_up, bool wrist_out) {
struct Config a;
a.right_arm = right_arm;
a.elbow_up = elbow_up;
a.wrist_out = wrist_out;
return a;
}
void print_config(struct Config a) {
printf("[");
if (a.right_arm){
printf("Right, ");
}else{
printf("Left, ");
}
if (a.elbow_up){
printf("Up, ");
}else {
printf("Down, ");
}
if (a.wrist_out) {
printf("Out]");
}
else {
printf("In]");
}
//return
}
//J_angles structure
struct J_angles{
double J1, J2, J3, J4, J5;
};
struct J_angles new_J_angles(double J1, double J2, double J3, double J4, double J5) {
struct J_angles a;
a.J1 = J1;
a.J2 = J2;
a.J3 = J3;
a.J4 = J4;
a.J5 = J5;
return a;
}
void print_J_angles(struct J_angles a) {
printf("[%f, %f, %f, %f, %f]", a.J1, a.J2, a.J3, a.J4, a.J5);
}
double J_angles_max_diff(struct J_angles J_angles_1, struct J_angles J_angles_2){
double cur_max = 0.0;
double delta;
delta = abs(J_angles_1.J1 - J_angles_2.J1);
if(delta > cur_max)
cur_max = delta;
delta = abs(J_angles_1.J2 - J_angles_2.J2);
if(delta > cur_max)
cur_max = delta;
delta = abs(J_angles_1.J3 - J_angles_2.J3);
if(delta > cur_max)
cur_max = delta;
delta = abs(J_angles_1.J4 - J_angles_2.J4);
if(delta > cur_max)
cur_max = delta;
delta = abs(J_angles_1.J5 - J_angles_2.J5);
if(delta > cur_max)
cur_max = delta;
return cur_max;
}
//XYZ structure
struct XYZ{
struct Vector position;
struct Vector direction;
struct Config config;
};
struct XYZ new_XYZ(struct Vector position, struct Vector direction, struct Config config) {
struct XYZ a;
a.position = position;
a.direction = direction;
a.config = config;
return a;
}
void print_XYZ(struct XYZ a) {
printf("{");
print_vector(a.position);
printf(", ");
print_vector(a.direction);
printf(", ");
print_config(a.config);
printf("}");
}
// Forward Kinematics:
struct XYZ J_angles_to_XYZ(struct J_angles angles) {
//Pre-allocation:
struct Vector U0 = {0, 0, 0};
struct Vector U1 = {0, 0, 0};
struct Vector U2 = {0, 0, 0};
struct Vector U3 = {0, 0, 0};
struct Vector U4 = {0, 0, 0};
struct Vector U5 = {0, 0, 0};
struct Vector V0 = {0, 0, 1};
struct Vector V1 = {0, 0, 0};
struct Vector V2 = {0, 0, 0};
struct Vector V3 = {0, 0, 0};
struct Vector V4 = {0, 0, 0};
struct Vector P0 = {1, 0, 0};
struct Vector P1 = {0, 0, 0};
struct Vector P2 = {0, 0, 0};
//FK:
P1 = rotate(P0, V0, -(angles.J1 - 180*3600)); // Links 2, 3 and 4 lie in P1
V1 = rotate(V0, P1, angles.J2); // Vector for Link 2
V2 = rotate(V1, P1, angles.J3); // Vector for Link 3
V3 = rotate(V2, P1, angles.J4); // Vector for Link 4
P2 = rotate(P1, V3, -(angles.J5 - 180*3600)); // Link 4 and 5 lie in P2
V4 = rotate(V3, P2, -90*3600); // Vector for Link 5 (90 degree bend)
U1 = add(U0, scalar_mult(L[0], V0));
U2 = add(U1, scalar_mult(L[1], V1));
U3 = add(U2, scalar_mult(L[2], V2));
U4 = add(U3, scalar_mult(L[3], V3));
U5 = add(U4, scalar_mult(L[4], V4));
// Forward Kinematic solved, now to determine configuration:
// Inverse Kinematics for just U3 to determine if wrist is in or out
struct Vector U54_proj = project_onto_plane(V4, P1);
struct Vector U3_a = add(U4, scalar_mult(L[3], rotate(normalize(U54_proj), P1, 90)));
struct Vector U3_b = add(U4, scalar_mult(L[3], rotate(normalize(U54_proj), P1, -90)));
double dist_a = dist_point_to_line(U3_a, U1, U0);
double dist_b = dist_point_to_line(U3_b, U1, U0);
struct Config my_config = new_config(1, 1, 1); // Initialize my_config
// Determine wrist state, in or out
if (is_equal(U3, U3_a, 10)) { // Negative or positive rotation of Link 4?
if (dist_a > dist_b) {
my_config.wrist_out = 1; // Wrist out
}
else {
my_config.wrist_out = 0; // Wrist in
}
}
else {
if (dist_a > dist_b) {
my_config.wrist_out = 0; // Wrist in
}
else {
my_config.wrist_out = 1; // Wrist out
}
}
// Determine arm state, right or left
struct Vector U50 = subtract(U5, U0);
if (dot(cross(U50, P1), V0) < 0) { // Is end effector point on right or left half of plane 1?
my_config.right_arm = 0; // Left arm
my_config.wrist_out = !my_config.wrist_out; // switches wrist state if left arm
}
else {
my_config.right_arm = 1; // Right arm
}
// Determine elbow state, up or down
if (my_config.right_arm) { // If right arm
if (dot(cross(V1, V2), P1) > 0) { // Is Joint 3 angle positive or negative?
my_config.elbow_up = 1; // Elbow down
}
else {
my_config.elbow_up = 0; // Elbow up
}
}
else { // If left arm
if (dot(cross(V1, V2), P1) > 0) { // Is Joint 3 angle positive or negative?
my_config.elbow_up = 0; // Elbow down
}
else {
my_config.elbow_up = 1; // Elbow down
}
}
// Calculating the end effector direction
struct Vector my_dir = normalize(subtract(U5, U4));
struct XYZ solution = new_XYZ(U5, my_dir, my_config);