-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunDex.c
3014 lines (2682 loc) · 76.1 KB
/
RunDex.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
#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 <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define INPUT_OFFSET 11
// Motor position index
#define BASE_POSITION 0
#define END_POSITON 1
#define PIVOT_POSITON 2
#define ANGLE_POSITON 3
#define ROT_POSITON 4
#define ACCELERATION_MAXSPEED 5
// DC bias paramaters
#define BASE_SIN_CENTER 6
#define BASE_COS_CENTER 7
#define END_SIN_CENTER 8
#define END_COS_CENTER 9
#define PIVOT_SIN_CENTER 10
#define PIVOT_COS_CENTER 11
#define ANGLE_SIN_CENTER 12
#define ANGLE_COS_CENTER 13
#define ROT_SIN_CENTER 14
#define ROT_COS_CENTER 15
//PID VALUES
#define PID_DELTATNOT 16
#define PID_DELTAT 17
#define PID_D 18
#define PID_I 19
#define PID_P 20
#define PID_ADDRESS 21
//FORCE PARAMATERS
#define BOUNDRY_BASE 22
#define BOUNDRY_END 23
#define BOUNDRY_PIVOT 24
#define BOUNDRY_ANGLE 25
#define BOUNDRY_ROT 26
#define SPEED_FACTORA 27
#define SPEED_FACTORB 28
#define FRICTION_BASE 29
#define FRICTION_END 30
#define FRICTION_PIVOT 31
#define FRICTION_ANGLE 32
#define FRICTION_ROT 33
#define MOVE_TRHESHOLD 34
#define F_FACTOR 35
#define MAX_ERROR 36
#define FORCE_BIAS_BASE 37
#define FORCE_BIAS_END 38
#define FORCE_BIAS_PIVOT 39
#define FORCE_BIAS_ANGLE 40
#define FORCE_BIAS_ROT 41
// control state register
#define COMMAND_REG 42
#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
//DMA
#define DMA_READ_ADDRESS 48
#define DMA_READ_PARAMS 47
#define DMA_WRITE_ADDRESS 46
#define DMA_WRITE_PARAMS 45
#define DMA_WRITE_DATA 44
#define DMA_CONTROL 43
// 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
#define REC_PLAY_CMD 49
#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 50
#define DIFF_FORCE_TIMEBASE 51
#define DIFF_FORCE_BETA 52
#define DIFF_FORCE_MOVE_THRESHOLD 53
#define DIFF_FORCE_MAX_SPEED 54
#define DIFF_FORCE_SPEED_FACTOR_ANGLE 55
#define DIFF_FORCE_SPEED_FACTOR_ROT 56
#define DIFF_FORCE_ANGLE_COMPENSATE 57
#define FINE_ADJUST_BASE 58
#define FINE_ADJUST_END 59
#define FINE_ADJUST_PIVOT 60
#define FINE_ADJUST_ANGLE 61
#define FINE_ADJUST_ROT 62
#define RECORD_LENGTH 63
#define END_EFFECTOR_IO 64
#define SERVO_SETPOINT_A 65
#define SERVO_SETPOINT_B 66
#define BASE_FORCE_DECAY 67
#define END_FORCE_DECAY 68
#define PIVOT_FORCE_DECAY 69
#define ANGLE_FORCE_DECAY 70
#define ROTATE_FORCE_DECAY 71
#define PID_SCHEDULE_INDEX 72
#define GRIPPER_MOTOR_CONTROL 73
#define GRIPPER_MOTOR_ON_WIDTH 75
#define GRIPPER_MOTOR_OFF_WIDTH 74
#define START_SPEED 76
#define ANGLE_END_RATIO 77
// 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
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
// 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 bool int
#define TRUE 1
#define FALSE 0
int XLowBound[5]={BASE_COS_LOW,END_COS_LOW,PIVOT_COS_LOW,ANGLE_COS_LOW,ROT_COS_LOW};
int XHighBound[5]={BASE_COS_HIGH,END_COS_HIGH,PIVOT_COS_HIGH,ANGLE_COS_HIGH,ROT_COS_HIGH};
int YLowBound[5]={BASE_SIN_LOW,END_SIN_LOW,PIVOT_SIN_LOW,ANGLE_SIN_LOW,ROT_SIN_LOW};
int YHighBound[5]={BASE_SIN_HIGH,END_SIN_HIGH,PIVOT_SIN_HIGH,ANGLE_SIN_HIGH,ROT_SIN_HIGH};
int ForcePossition[5]={0,0,0,0,0};
int ForceDestination[5]={0,0,0,0,0};
int ThreadsExit=1;
int StatusReportIndirection[60]={DMA_READ_DATA,DMA_READ_DATA,RECORD_BLOCK_SIZE,END_EFFECTOR_IO_IN,BASE_POSITION_AT,BASE_POSITION_DELTA,
BASE_POSITION_PID_DELTA,BASE_POSITION_FORCE_DELTA,BASE_SIN,BASE_COS,PLAYBACK_BASE_POSITION,SENT_BASE_POSITION,
SLOPE_BASE_POSITION,0,PIVOT_POSITION_AT,PIVOT_POSITION_DELTA,PIVOT_POSITION_PID_DELTA,PIVOT_POSITION_FORCE_DELTA,
PIVOT_SIN,PIVOT_COS,PLAYBACK_PIVOT_POSITION,SENT_PIVOT_POSITION,SLOPE_PIVOT_POSITION,0,END_POSITION_AT,
END_POSITION_DELTA,END_POSITION_PID_DELTA,END_POSITION_FORCE_DELTA,END_SIN,END_COS,PLAYBACK_END_POSITION,
SENT_END_POSITION,SLOPE_END_POSITION,0,ANGLE_POSITION_AT,ANGLE_POSITION_DELTA,ANGLE_POSITION_PID_DELTA,
ANGLE_POSITION_FORCE_DELTA,ANGLE_SIN,ANGLE_COS,PLAYBACK_ANGLE_POSITION,SENT_ANGLE_POSITION,SLOPE_ANGLE_POSITION,
0,ROT_POSITION_AT,ROT_POSITION_DELTA,ROT_POSITION_PID_DELTA,ROT_POSITION_FORCE_DELTA,ROT_SIN,ROT_COS,
PLAYBACK_ROT_POSITION,SENT_ROT_POSITION,SLOPE_ROT_POSITION,0};
void *map_addr,*map_addrCt;
volatile unsigned int *mapped;
volatile unsigned int *CalTables;
volatile unsigned int *RecordTable;
int CmdVal=0,maxSpeed=0,startSpeed=0,coupledAcceleration=0,forceMode=0,fa0=0,fa1=0,fa2=0,fa3=0,fa4=0,RunMode=0,ServerMode=3;
int BaseBoundryHigh,BaseBoundryLow,EndBoundryHigh,EndBoundryLow,PivotBoundryHigh,PivotBoundryLow,AngleBoundryHigh=DEFAULT_ANGLE_BOUNDRY_HI,AngleBoundryLow=DEFAULT_ANGLE_BOUNDRY_LOW,RotateBoundryHigh,RotateBoundryLow;
int Cycle = 0;
struct BotPossition {
int base;
int end;
int pivot;
int angle;
int rotate;
int baseForce;
int endForce;
int pivotForce;
int angleForce;
int rotateForce;
int Controlled;
int ForceMoveState;
};
struct BotSetHomePossition {
int base;
int end;
int pivot;
int angle;
int rotate;
};
char RemoteRobotAdd[255];
struct MasterSlaveMoveRatio{
int base;
int end;
int pivot;
int angle;
int rotate;
};
struct MasterSlaveMoveDelta{
int base;
int end;
int pivot;
int angle;
int rotate;
};
struct SlaveBotPossiton{
int base;
int end;
int pivot;
int angle;
int rotate;
};
float DiffCorrectionFactor;
int AngleHIBoundry;
int AngleLOWBoundry;
int controlled=0;
int DexError=0;
struct MasterSlaveMoveRatio MSMoveRatio = {1,1,1,1,1};
struct MasterSlaveMoveDelta MSMoveDelta = {0,0,0,0,0};
struct BotSetHomePossition SetHome = {0,0,0,0,0};
struct SlaveBotPossiton SlaveBotPos = {0,0,0,0,0};
pthread_t tid[10];
struct CaptureArgs {
char *FileName;
FILE *fp;
};
struct CaptureArgs CptA,CptMove;
#define MAX_PARAMS 26
#define PARAM_LENGTH 20
char Params[MAX_PARAMS][PARAM_LENGTH+1] = {"MaxSpeed", "Acceleration", "J1Force","J3Force","J2Force","J4Force","J5Force","J1Friction","J3Friction","J2Friction","J4Friction","J5Friction","J1BoundryHigh","J1BoundryLow","J3BoundryHigh","J3BoundryLow","J2BoundryHigh","J2BoundryLow","J4BoundryHigh","J4BoundryLow","J5BoundryHigh","J5BoundryLow","GripperMotor","EERoll","StartSpeed","EndSpeed","End"};
static struct termios old, new;
int PositionAdjust[5]={0,0,0,0,0};
int ForceLimit[5]={4400,4400,4400,800,800};
int MyBotForce[5]={0,0,0,0,0};
float ForceAdjustPossition[5]={-.01,-.01,-.01,-.01,-.01};
int FroceMoveMode=0;
int LastGoal[5]={0,0,0,0,0};
float JointsCal[5];
int SoftBoundries[5];
void printPosition()
{
int a1,a2,a3,a4,a5;
a1=getNormalizedInput(BASE_POSITION_AT)+getNormalizedInput(BASE_POSITION_FORCE_DELTA);
a2=getNormalizedInput(END_POSITION_AT)+getNormalizedInput(END_POSITION_FORCE_DELTA);
a3=getNormalizedInput(PIVOT_POSITION_AT)+getNormalizedInput(PIVOT_POSITION_FORCE_DELTA);
a4=getNormalizedInput(ANGLE_POSITION_AT)+getNormalizedInput(ANGLE_POSITION_FORCE_DELTA);
a5=getNormalizedInput(ROT_POSITION_AT)+getNormalizedInput(ROT_POSITION_FORCE_DELTA);
//printf(" %d,%d,%d,%d,%d\n",a1,a2,a3,a4,a5);
}
int sign(int i)
{
if(i<0)
return -1;
if(i>0)
return 1;
return 0;
}
void RealtimeMonitor(void *arg)
{
int* ExitState = arg;
int i,j,ForceDelta,disTime=0;
while(*ExitState)
{
if(FroceMoveMode==1)
{
// do force based movement
for(i=0;i<5;i++)
{
ForceDelta=ForcePossition[i]-getNormalizedInput(BASE_POSITION_FORCE_DELTA+i);
/*if(abs(ForceDelta)>500)
{
ForceDelta=(abs(ForceDelta)-500)*sign(ForceDelta);
}*/
//ForceDelta=ForceDelta;//+MyBotForce[i];
if(disTime==90)
{
//for(j=0;j<5;j++)
//printf(" Force %d ",ForceDelta);
}
if(abs(ForceDelta)<ForceLimit[i])
{
mapped[FORCE_BIAS_BASE+i]=ForceDelta;
}
else
{
mapped[FORCE_BIAS_BASE+i]=sign(ForceDelta)*ForceLimit[i];
}
//ForcePossition[i]=ForcePossition[i]+(int)((float)ForceDelta*ForceAdjustPossition[i]);
/*if(ForceDestination[i]>ForcePossition[i])
{
ForcePossition[i]=ForcePossition[i]+10;
}
else
{
ForcePossition[i]=ForcePossition[i]-10;
}*/
}
}
disTime++;
if(disTime>100)
{
//printPosition();
disTime = 0;
}
usleep(1000);
}
//printf("\nMonitor Thread Exiting\n");
}
void SetGripperRoll(int Possition)
{
int ServoSpan=(SERVO_HI_BOUND-SERVO_LOW_BOUND)/360;
mapped[END_EFFECTOR_IO]=64;
mapped[SERVO_SETPOINT_B]=(ServoSpan*Possition)+SERVO_LOW_BOUND;
// mapped[SERVO_SETPOINT_B]=Possition;
}
void SetGripperMotor(int state)
{
mapped[GRIPPER_MOTOR_ON_WIDTH]=12000;
mapped[GRIPPER_MOTOR_OFF_WIDTH]=200000;
mapped[GRIPPER_MOTOR_CONTROL]=state;
}
void StartServerSocketDDE(void *arg)
{
struct timeval tv;
tv.tv_sec = 30; /* 30 Secs Timeout */
tv.tv_usec = 0; // Not init'ing this can cause strange errors
int* ExitState = arg;
int listenfd = 0, connfd = 0,RLength = 0,SLength = 0;
struct sockaddr_in serv_addr;
char sendBuff[256];
char recBuff[256];
time_t ticks;
bool SocketLive=0;
//printf("\n Start DDE Socket Server \n");
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(50000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
while(*ExitState)
{
// connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
while ( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) < 0)
{
//printf("error on accept()! %s\n", strerror(errno));
////printf("\n Timeout no connect \n");
}
//printf("\n new connect \n");
//ticks = time(NULL);
//strcpy(sendBuff,"Dexter Connect Service Connected\n");
//sn//printf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
//write(connfd, sendBuff, strlen(sendBuff));
SocketLive=TRUE;
// while(SocketLive==TRUE)
{
while((errno = 0, (RLength = recv(connfd, recBuff, /*sizeof(recBuff)*/128, 0))>0) ||
errno == EINTR)
{
if(RLength>0)
{
////printf("Receive size %i ",RLength);
//output.append(recBuff, RLength);
}
if(RLength < 0)
{
//printf("%s \n",strerror(errno));
/* handle error - for example throw an exception*/
SocketLive = FALSE;
}
// while ( (RLength = recv (connfd,recBuff,sizeof(recBuff),0 )) > 0)
{
//recBuff[RLength]=0;
ProcessServerReceiveDataDDE(recBuff);
ProcessServerSendDataDDE(sendBuff,recBuff);/*==TRUE)*/
write (connfd,sendBuff,60*4/*sizeof(sendBuff)*/);
}
}
//printf("error code %s \n",strerror(errno));
}
//printf("\n Socket Read Zero closing socket\n");
close(connfd);
//sleep(1);
}
}
void StartServerSocket(void *arg)
{
int* ExitState = arg;
int listenfd = 0, connfd = 0,RLength = 0,SLength = 0;
struct sockaddr_in serv_addr;
char sendBuff[64];
char recBuff[64];
time_t ticks;
bool SocketLive=0;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(40000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(*ExitState)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
//printf("\n new connect \n");
//ticks = time(NULL);
strcpy(sendBuff,"Dexter Connect Service Connected\n");
//sn//printf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
SocketLive=TRUE;
while(SocketLive==TRUE)
{
while ( (RLength = recv (connfd,recBuff,sizeof(recBuff),0 )) > 0)
{
//recBuff[RLength]=0;
ProcessServerReceiveData(recBuff);
SocketLive = ProcessServerSendData(sendBuff);
write (connfd,sendBuff,sizeof(sendBuff));
}
}
//printf("\n Socket Read Zero closing socket\n");
close(connfd);
}
}
int MaxForce(int Max,int Val)
{
if(abs(Max) > abs(Val))
{
return Val;
}
else
{
return abs(Max)*sign(Val);
}
}
bool ProcessServerReceiveData(char *recBuff)
{
struct BotPossition MyBot;
int MxForce=9800;
float fScale=1;
memcpy(&MyBot,recBuff,sizeof(MyBot));
////printf("Server %d %d %d %d %d \n",MyBot.baseForce,MyBot.endForce,MyBot.pivotForce,MyBot.angleForce,MyBot.rotateForce);
if(MyBot.Controlled!=0)
{
/*MyBotForce[0]=MaxForce(MxForce,MyBot.baseForce);
MyBotForce[1]=MaxForce(MxForce,MyBot.endForce);
MyBotForce[2]=MaxForce(MxForce,MyBot.pivotForce);
MyBotForce[3]=MaxForce(MxForce,MyBot.angleForce);
MyBotForce[4]=MaxForce(MxForce,MyBot.rotateForce);*/
if(FroceMoveMode==0)
{
mapped[FORCE_BIAS_BASE]=MaxForce(MxForce,(int)((float)MyBot.baseForce)*fScale);
mapped[FORCE_BIAS_END]=MaxForce(MxForce,(int)((float)MyBot.endForce)*fScale);
mapped[FORCE_BIAS_PIVOT]=MaxForce(MxForce,(int)((float)MyBot.pivotForce)*fScale);
mapped[FORCE_BIAS_ANGLE]=MaxForce(MxForce,(int)((float)MyBot.angleForce)*fScale);
mapped[FORCE_BIAS_ROT]=MaxForce(MxForce,(int)((float)MyBot.rotateForce)*fScale);
}
else
{
ForcePossition[0]=MyBot.base;
ForcePossition[1]=MyBot.end;
ForcePossition[2]=MyBot.pivot;
ForcePossition[3]=MyBot.angle;
ForcePossition[4]=MyBot.rotate;
}
FroceMoveMode=MyBot.ForceMoveState;
}
//MoveRobot(MyBot.base,MyBot.end,MyBot.pivot,MyBot.angle,MyBot.rotate,BLOCKING_MOVE);
/* Cycle++;
if(Cycle>100)
return FALSE;
else
return TRUE;*/
}
bool ProcessServerSendData(char *sendBuff)
{
struct BotPossition MyBot={0,0,0,0,0,0,0,0,0,0,0,0};
static int Cycle = 0;
MyBot.baseForce=getNormalizedInput(BASE_POSITION_DELTA);
MyBot.endForce=getNormalizedInput(END_POSITION_DELTA);
MyBot.pivotForce=getNormalizedInput(PIVOT_POSITION_DELTA);
MyBot.angleForce=getNormalizedInput(ANGLE_POSITION_DELTA);
MyBot.rotateForce=getNormalizedInput(ROT_POSITION_DELTA);
MyBot.Controlled=controlled;
MyBot.base=getNormalizedInput(BASE_POSITION_FORCE_DELTA);
MyBot.end=getNormalizedInput(END_POSITION_FORCE_DELTA);
MyBot.pivot=getNormalizedInput(PIVOT_POSITION_FORCE_DELTA);
MyBot.angle=getNormalizedInput(ANGLE_POSITION_FORCE_DELTA);
MyBot.rotate=getNormalizedInput(ROT_POSITION_FORCE_DELTA);
MyBot.ForceMoveState=FroceMoveMode;
memcpy(sendBuff,&MyBot,sizeof(MyBot));
//
// s//printf(sendBuff, "%d \n", Cycle);
// Cycle++;
return TRUE;
}
bool ProcessServerSendDataDDE(char *sendBuff,char *recBuff)
{
int i;
int *sendBuffReTyped;
const char delimiters[] = " ,";
char *token;
/* for(i=0;i<25;i++)
{
//printf("%c",recBuff[i]);
}*/
// if(recBuff[0]=="g")
token = strtok (recBuff, delimiters);
{
////printf("returning heartbeat\n");
sendBuffReTyped=(int *)sendBuff;
sendBuffReTyped[0]=atoi(token);
token = strtok (NULL, delimiters);
////printf("\n %s \n",token);
sendBuffReTyped[1]=atoi(token);
token = strtok (NULL, delimiters);
token = strtok (NULL, delimiters);
token = strtok (NULL, delimiters);
sendBuffReTyped[2]=0;// this shoud be start_time_internal broken into 2 words atoi(token);
sendBuffReTyped[3]=0;// this shoud be start_time_internal broken into 2 words atoi(token);
sendBuffReTyped[4]=token[0];
sendBuffReTyped[5]=DexError;
//sendBuffReTyped[1]=token[0];
//sendBuffReTyped[2]=DexError;
for(i=0;i<59;i++)
{
sendBuffReTyped[i+6]=getNormalizedInput(StatusReportIndirection[i]);
/*if(StatusReportIndirection[i] == BASE_POSITION_PID_DELTA)
{
//printf(" PID Delta for L1 = %d", sendBuffReTyped[i+6]);
}
if(StatusReportIndirection[i] == BASE_POSITION_DELTA)
{
//printf(" TABLE OFFSET Delta for L1 = %d", sendBuffReTyped[i+6]);
}*/
}
return TRUE;
}
return FALSE;
}
bool ProcessServerReceiveDataDDE(char *recBuff)
{
struct BotPossition MyBot;
char CmdString[255];
int i,FoundStart=0,j=0;
bool GotDelim=FALSE;
int *sendBuffReTyped;
const char delimiters[] = " ,";
char *token;
//memcpy(&MyBot,recBuff,sizeof(MyBot));
////printf("Server %d %d %d %d %d \n",MyBot.base,MyBot.end,MyBot.pivot,MyBot.angle,MyBot.rotate);
//mapped[FINE_ADJUST_BASE]=MyBot.base;
//mapped[FINE_ADJUST_END]=MyBot.end;
//mapped[FINE_ADJUST_PIVOT]=MyBot.pivot;
//mapped[FINE_ADJUST_ANGLE]=MyBot.angle;
//mapped[FINE_ADJUST_ROT]=MyBot.rotate;
//MoveRobot(MyBot.base,MyBot.end,MyBot.pivot,MyBot.angle,MyBot.rotate,BLOCKING_MOVE);
FoundStart=0;
for(i=0;i<255;i++)
{
if(FoundStart >= 4)
{
CmdString[j]=recBuff[i];
j++;
}
if(recBuff[i]==' ')
{
FoundStart++;
}
if(recBuff[i]==0x3b)
{
recBuff[i]=0;
GotDelim=TRUE;
break;
}
}
if(GotDelim==FALSE)
{
DexError=2;
////printf("%s",recBuff);
return FALSE;
}
CmdString[j-1]=0;
if(CmdString[0] != 'g')
{
//printf("\n%s \n",recBuff);
//printf("\n%s \n",CmdString);
}
////printf("\n%s \n",recBuff);
////printf("\n%s \n",CmdString);
DexError=ParseInput(CmdString);
////printf("\nsent parse\n");
return TRUE;
}
void StartClientSocket()
{
int sockfd = 0, n = 0,j;
char recvBuff[64];
char sendBuff[64];
struct sockaddr_in serv_addr;
int Cycle = 100;
struct BotPossition MyBot;
float DiffDelta;
float ForceScale=0;
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
//printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(40000);
RemoteRobotAdd[strcspn(RemoteRobotAdd,"\n")]= 0;
if(inet_pton(AF_INET, RemoteRobotAdd/*"192.168.1.145"*/, &serv_addr.sin_addr)<=0)
{
//printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
//printf("\n Error : Connect Failed \n");
return 1;
}
// //printf("\n Socket connect\n");
// while(1)//for(j=0;j<1000;j++)
{
////printf("\n next\n");
while( (n = recv(sockfd, recvBuff, sizeof(recvBuff),0 ) ) > 0)
{
//recvBuff[n] = 0;
////printf("Socket Read\n");
////printf("%s ",recvBuff);
if(n < 0)
{
//printf("\n Read error \n");
}
/*mapped[FORCE_BIAS_BASE]=(int)(((float)MyBot.base)*ForceScale);
mapped[FORCE_BIAS_END]=(int)(((float)MyBot.end)*ForceScale);
mapped[FORCE_BIAS_PIVOT]=(int)(((float)MyBot.pivot)*ForceScale);
mapped[FORCE_BIAS_ANGLE]=(int)(((float)MyBot.angle)*ForceScale);
mapped[FORCE_BIAS_ROT]=(int)(((float)MyBot.rotate)*ForceScale);*/
////printf("%d %d %d %d %d \n",MyBot.base,MyBot.end,MyBot.pivot,MyBot.angle,MyBot.rotate);
#ifdef FOLLOW_POSSITION
memcpy(&MyBot,recvBuff,sizeof(MyBot));
DiffDelta=1.5;
MyBot.base=SlaveBotPos.base + ((GetAxisCurrent(0) - SetHome.base)/MSMoveRatio.base);
MyBot.end=SlaveBotPos.end + ((GetAxisCurrent(1) - SetHome.end)/MSMoveRatio.end);
MyBot.pivot=SlaveBotPos.pivot + ((GetAxisCurrent(2) - SetHome.pivot)/MSMoveRatio.pivot);
MyBot.angle=SlaveBotPos.angle + (((GetAxisCurrent(3) - SetHome.angle)/MSMoveRatio.angle)*DiffDelta);
MyBot.rotate=SlaveBotPos.rotate + (((GetAxisCurrent(4) - SetHome.rotate)/MSMoveRatio.rotate)*DiffDelta);
/* MyBot.base=0;
MyBot.end=0;
MyBot.pivot=0;
MyBot.angle=0;
MyBot.rotate=0;*/
/*
SlaveBotPos.base=MyBot.base;
SlaveBotPos.end=MyBot.base;
SlaveBotPos.pivot=MyBot.base;
SlaveBotPos.angle=MyBot.base;
SlaveBotPos.rotate=MyBot.base;
*/
//usleep(25000);
/*MyBot.base=BASE_POSITION_FORCE_DELTA;
MyBot.end=12300;
MyBot.pivot=5123;
MyBot.angle=10;
MyBot.rotate=15;*/
memcpy(sendBuff,&MyBot,sizeof(MyBot));
Cycle++;
#else
ProcessServerReceiveData(recvBuff);
ProcessServerSendData(sendBuff);
#endif
write(sockfd,sendBuff,sizeof(sendBuff));
}
}
}
int GetAxisCurrent(int Axis)
{
return getNormalizedInput(BASE_POSITION_AT+Axis)+getNormalizedInput(BASE_POSITION_FORCE_DELTA+Axis);
}
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
new = old; /* make new settings same as old settings */
new.c_lflag &= ~ICANON; /* disable buffered i/o */
new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);