-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·1674 lines (1360 loc) · 55.9 KB
/
main.cpp
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
/*****************************************************************************
*
* $Id: main.c,v 6a6dec6fc806 2012/09/19 17:46:58 fp $
*
* Copyright (C) 2007-2009 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* The IgH EtherCAT Master is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* The license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
****************************************************************************/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <iostream>
#include <memory>
// #include<array>
/****************************************************************************/
#include "ecrt.h"
#include "pid_controller.h"
#include "interface/cmdpanel.h"
#include "fuzzy_pid.h"
// #include "datalogger.h"
#include "Eigen/Dense"
#include "biped.h"
#include "math/MathUtilities.h"
// add
#include "../interface/CheatIO.h"
#include "../include/checkjoint.h"
#include "../include/OrientationEstimator.h"
#include "../include/PositionVelocityEstimator.h"
#include "../include/utility/cppTypes.h"
#include "../include/ControlFSMData.h"
#include "../include/DesiredCommand.h"
#include "../include/FSM/FSM.h"
#include "../include/N100WP.h"
/****************************************************************************/
// Application parameters
#define FREQUENCY 1000
#define PRIORITY 1
// Optional features
#define CONFIGURE_PDOS 1
#define BIGNUM 100000000.0
PIDControl *PID_ptr_M; //
fuzzypid *FPID_ptr;
int sock;
struct sockaddr_in server;
char message[1000];
/****************************************************************************/
// EtherCAT
static ec_master_t *master = NULL;
static ec_master_state_t master_state = {};
static ec_domain_t *domain = NULL;
static ec_domain_state_t domain_state = {};
static ec_slave_config_t *sc1;
static ec_slave_config_state_t sc1_ana_in_state = {};
static ec_slave_config_t *sc2;
static ec_slave_config_state_t sc2_ana_in_state = {};
static ec_slave_config_t *sc3;
static ec_slave_config_state_t sc3_ana_in_state = {};
static ec_slave_config_t *sc4;
static ec_slave_config_state_t sc4_ana_in_state = {};
static ec_slave_config_t *sc5;
static ec_slave_config_state_t sc5_ana_in_state = {};
static ec_slave_config_t *sc6;
static ec_slave_config_state_t sc6_ana_in_state = {};
static ec_slave_config_t *sc7;
static ec_slave_config_state_t sc7_ana_in_state = {};
static CmdPanel *cmdptr = NULL;
// Timer
static unsigned int sig_alarms = 0;
static unsigned int user_alarms = 0;
int fd1, count_r3,fd2;
struct termios options1,options2;
// u8 Fd_data[64];
// u8 Fd_rsimu[64];
// u8 Fd_rsahrs[56];
// int rs_imutype = 0;
// int rs_ahrstype = 0;
// IMUData_Packet_t IMUData_Packet;
// AHRSData_Packet_t AHRSData_Packet;
/****************************************************************************/
// process data
// static uint8_t *domain_pd = NULL;
// #define BusCouplerPos 0, 0
// #define TI_AM3359ICE 0x00000009, 0x26483052
// // offsets for PDO entries
static unsigned int off_bytes_0x7030;
static unsigned int off_bits_0x7030;
static unsigned int off_bytes_0x7030_1;
static unsigned int off_bits_0x7030_1;
static unsigned int off_bytes_0x6000[2] = {1};
static unsigned int off_bits_0x6000[2] = {1};
static unsigned int off_bytes_0x6000_1[2] = {1};
static unsigned int off_bits_0x6000_1[2] = {1};
static unsigned int off_bytes_0x6010[8] = {1};
static unsigned int off_bits_0x6010[8] = {1};
static unsigned int off_bytes_0x6010_1[2] = {1};
static unsigned int off_bits_0x6010_1[2] = {1};
static unsigned int off_bytes_0x6000_2[2] = {1};
static unsigned int off_bits_0x6000_2[2] = {1};
static unsigned int off_bytes_0x6000_3[2] = {1};
static unsigned int off_bits_0x6000_3[2] = {1};
static unsigned int off_bytes_0x6000_4[2] = {1};
static unsigned int off_bits_0x6000_4[2] = {1};
static unsigned int off_bytes_0x6000_5[2] = {1};
static unsigned int off_bits_0x6000_5[2] = {1};
static unsigned int off_bytes_0x7010[6] = {1};
static unsigned int off_bits_0x7010[6] = {1};
static unsigned int off_bytes_0x7011[6] = {1};
static unsigned int off_bits_0x7011[6] = {1};
static unsigned int counter = 0;
static unsigned int blink = 0x00;
static uint8_t *domain_pd = NULL;
#define BusCouplerPos1 0, 0
#define BusCouplerPos2 256, 1
#define BusCouplerPos3 256, 0
#define BusCouplerPos4 256, 2
#define BusCouplerPos5 256, 3
#define BusCouplerPos6 256, 4
#define BusCouplerPos7 256, 5
#define TI_AM3359ICE 0x01222222, 0x00020310
#define TI_AM3359ICE1 0x00000b95, 0x00020310
#define Valve 0x00000b95, 0x00020130
#include "cstruct.h"
int fd = -1;
struct sockaddr_in saddr;
// DataLogger &logger = DataLogger::GET();
// LowPassFilter filter;
float ifswing = 0;
float Deg[3];
float KuanDeg[2];
float rj4angle = 0, rj5angle = 0, rj3angle = 0, rj2angle = 0, rj1angle = 0, rj0angle = 0;
float LDeg[3];
float RDeg[3];
float LKuanDeg[2];
float lj4angle = 0, lj5angle = 0, lj3angle = 0, lj2angle = 0, lj1angle = 0, lj0angle = 0;
float Ps = 0;
float P0 = 0;
float swingtime = 0;
float swingtime1 = 0;
float zmpscope[4] = {0, 0, 0, 0};
float cphasescope[2] = {0, 0};
float walkphase;
float dyx, dyy;
float uxx, uyy;
float rlegswingphase, rlegcontactphase;
float llegswingphase, llegcontactphase;
float zmpInitialq[12];
int error1 = 0;
int correct1 = 0;
int error2=0;
int correct2=0;
IMUDataprocess SX;
IMUDataprocess SY;
Vec3<float> Base_RPY_RAW;
Vec3<float> Base_ACC_RAW;
bool firstrun = true;
/*
legpre2:rad
*/
float legpre2[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/*
legpre2l:mm
*/
float legpre2L[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
float legpre1_5[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
float *TR_data[12];
/*
legpre1:rad
*/
float legpre1[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/*
legpre1L:油缸长度,mm
*/
float legpre1L[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned long SSI[12];
float PressureA[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
float PressureB[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void copy_arr_part(u8 src[], u8 dest[], int start, int end)
{
memcpy(dest, src + start, (end - start) * sizeof(u8));
}
void check_domain_state(void)
{
ec_domain_state_t ds;
ecrt_domain_state(domain, &ds);
if (ds.working_counter != domain_state.working_counter)
printf("Domain1: WC %u.\n", ds.working_counter);
if (ds.wc_state != domain_state.wc_state)
printf("Domain1: State %u.\n", ds.wc_state);
domain_state = ds;
}
/*****************************************************************************/
/*****************************************************************************/
void check_master_state(void)
{
// printf("check_master_state\n");
ec_master_state_t ms;
ecrt_master_state(master, &ms);
if (ms.slaves_responding != master_state.slaves_responding)
printf("%u slave(s).\n", ms.slaves_responding);
if (ms.al_states != master_state.al_states)
printf("AL states: 0x%02X.\n", ms.al_states);
if (ms.link_up != master_state.link_up)
printf("Link is %s.\n", ms.link_up ? "up" : "down");
master_state = ms;
}
/*****************************************************************************/
void check_slave1_config_states(void)
{
// printf("check_slave_config_states\n");
ec_slave_config_state_t s;
ecrt_slave_config_state(sc1, &s);
if (s.al_state != sc1_ana_in_state.al_state)
printf("AnaIn1: State 0x%02X.\n", s.al_state);
if (s.online != sc1_ana_in_state.online)
printf("AnaIn1: %s.\n", s.online ? "online" : "offline");
if (s.operational != sc1_ana_in_state.operational)
printf("AnaIn1: %soperational.\n",
s.operational ? "" : "Not ");
sc1_ana_in_state = s;
}
void check_slave2_config_states(void)
{
// printf("check_slave_config_states\n");
ec_slave_config_state_t s;
ecrt_slave_config_state(sc2, &s);
if (s.al_state != sc2_ana_in_state.al_state)
printf("AnaIn2: State 0x%02X.\n", s.al_state);
if (s.online != sc2_ana_in_state.online)
printf("AnaIn2: %s.\n", s.online ? "online" : "offline");
if (s.operational != sc2_ana_in_state.operational)
printf("AnaIn2: %soperational.\n",
s.operational ? "" : "Not ");
sc2_ana_in_state = s;
}
void check_slave3_config_states(void)
{
// printf("check_slave_config_states\n");
ec_slave_config_state_t s;
ecrt_slave_config_state(sc3, &s);
if (s.al_state != sc3_ana_in_state.al_state)
printf("AnaIn3: State 0x%02X.\n", s.al_state);
if (s.online != sc3_ana_in_state.online)
printf("AnaIn3: %s.\n", s.online ? "online" : "offline");
if (s.operational != sc3_ana_in_state.operational)
printf("AnaIn3: %soperational.\n",
s.operational ? "" : "Not ");
sc3_ana_in_state = s;
}
void GetIMURPYData()
{
unsigned char buff3[21];
if ((count_r3 = read(fd2, (void *)buff3, 21)) < 0)
perror("ERR:No data is ready to be read\n");
else if (count_r3 == 0)
printf("ERR:No data is ready to be read\n");
else
{
buff3[count_r3] = 0;
for (int i = 0; i < count_r3 - 11; i++)
{
if ((buff3[i] == 0x55) && (buff3[i + 1] == 0x55) &&
(buff3[i + 2] == 0x01))
{
unsigned char sum = buff3[i] + buff3[i + 1] + buff3[i + 2] +
buff3[i + 3] + buff3[i + 4] + buff3[i + 5] +
buff3[i + 6] + buff3[i + 7] + buff3[i + 8] +
buff3[i + 9];
if (sum == buff3[i + 10])
{
correct1++;
Base_RPY_RAW[0]=(float)((int)buff3[i + 5] * 256 + (int)buff3[i + 4]) /
32768.0 * 180.0;
Base_RPY_RAW[1]=(float)((int)buff3[i + 7] * 256 + (int)buff3[i + 6]) /
32768.0 * 180.0;
Base_RPY_RAW[2]=(float)((int)buff3[i + 9] * 256 + (int)buff3[i + 8]) /
32768.0 * 180.0;
break;
}
else
error1++;
}
}
}
}
void GetIMUACCData()
{
unsigned char buff3[34];
if ((count_r3 = read(fd1, (void *)buff3, 34)) < 0)
perror("ERR:No data is ready to be read\n");
else if (count_r3 == 0)
printf("ERR:No data is ready to be read\n");
else
{
buff3[count_r3] = 0;
for (int i = 0; i < count_r3 - 17; i++)
{
if((buff3[i] == 0x55) && (buff3[i + 1] == 0x55) &&
(buff3[i + 2] == 0x03))
{
unsigned char sum ;
for(int j=0;j<16;j++)
{
sum+=buff3[i+j];
}
if(sum==buff3[i+16])
{
correct2++;
unsigned char AxL=buff3[i+4];
unsigned char AxH=buff3[i+5];
unsigned char AyL=buff3[i+6];
unsigned char AyH=buff3[i+7];
unsigned char AzL=buff3[i+8];
unsigned char AzH=buff3[i+9];
int16_t Axint=(int16_t) (AxH<<8) | AxL;
int16_t Ayint=(int16_t) (AyH<<8) | AyL;
int16_t Azint=(int16_t) (AzH<<8) | AzL;
Base_ACC_RAW[0]=(float)Axint/32768*4;
Base_ACC_RAW[1]=(float)Ayint/32768*4;
Base_ACC_RAW[2]=(float)Azint/32768*4;
break;
}
else
error2++;
}
}
}
}
void cyclic_task(Biped &bipins, float time, FSM *_FSMController)
{
// receive process data
ecrt_master_receive(master);
ecrt_domain_process(domain);
// check process data state (optional)
check_domain_state();
if (counter)
{
counter--;
}
else
{ // do this at 1 Hz
// double LEGlength = bipins.calfLinkLength + bipins.thighLinkLength + bipins.toeLinkLength;
// Vec6<double> RjQ;
// Vec3<double> P;
// float Deg[3];
// float KuanDeg[2];
// float rj4angle=0,rj5angle=0;
// P.setZero();
// P(2) = -1 * LEGlength - 0.07 * LEGlength * (cos(0.2 * M_PI * time + 1.5 * M_PI) - 1);
// Deg[0] = 1*(14 * cos(0.2 * M_PI * time) - 14);
// Deg[0] = 1*(14 * sin(0.2 * M_PI * time));
rj0angle = 0;
rj1angle = 0;
rj2angle = 0;
rj3angle = 0;
rj4angle = 0;
rj5angle = 0;
lj0angle = 0;
lj1angle = 0;
lj2angle = 0;
lj3angle = 0;
lj4angle = 0;
lj5angle = 0;
int press = 0;
for (int i = 0; i < 12; i++)
SSI[i] = 0;
uint16_t output1 = 0;
SSI[0] = EC_READ_U32(domain_pd + off_bytes_0x6000[0]); // rj0
SSI[1] = EC_READ_U32(domain_pd + off_bytes_0x6000[1]);
SSI[2] = EC_READ_U32(domain_pd + off_bytes_0x6000_1[0]);
SSI[3] = EC_READ_U32(domain_pd + off_bytes_0x6000_1[1]);
SSI[4] = EC_READ_U32(domain_pd + off_bytes_0x6000_2[0]);
SSI[5] = EC_READ_U32(domain_pd + off_bytes_0x6000_2[1]);
SSI[6] = EC_READ_U32(domain_pd + off_bytes_0x6000_3[0]); // lj0
SSI[7] = EC_READ_U32(domain_pd + off_bytes_0x6000_3[1]);
SSI[8] = EC_READ_U32(domain_pd + off_bytes_0x6000_4[0]);
SSI[9] = EC_READ_U32(domain_pd + off_bytes_0x6000_4[1]);
SSI[10] = EC_READ_U32(domain_pd + off_bytes_0x6000_5[0]);
SSI[11] = EC_READ_U32(domain_pd + off_bytes_0x6000_5[1]);
for (int i = 0; i < 12; i++)
{
TR_data[i] = (float *)&SSI[i];
}
PressureA[2] = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010[0])) / 65535 * 10 * 3;
PressureB[2] = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010[1])) / 65535 * 10 * 3;
PressureA[8] = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010[2])) / 65535 * 10 * 3;
PressureB[8] = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010[3])) / 65535 * 10 * 3;
Ps = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010_1[0]));
P0 = (float)(EC_READ_U16(domain_pd + off_bytes_0x6010_1[1]));
Ps = Ps / 100;
cout << "A,B口压力: " << PressureA[2] << '\n';
cout << PressureB[2] << '\n';
cout << "LA,B口压力: " << PressureA[8] << '\n';
cout << PressureB[8] << '\n';
cout << "PS压力:\n"
<< Ps << '\n';
// cout<<"Base 姿态ZYX:\n"<<Base_RPY_RAW<<'\n';
// cout<<"Base 加速度:\n"<<Base_ACC_RAW<<'\n';
// for (int i = 0; i < 12; i++)
// std::cout<<"编码器"<<i<< SSI[i] <<'\n';zz
// bool firstrun=true;
if (time)
{
// Vec6<double> Qt;
// Vec3<double> pt(0,0,-0.897);
// bipins.ComputeIK(Qt,pt);
// cout<<"关节角度"<<Qt<<'\n';
// IKinbodyframe(statectrl->_biped, QDes[foot], &Pdes[foot], foot);
if (bipins.zmpAngleInit(rj1angle, rj2angle, rj3angle, rj4angle, rj5angle, lj1angle, lj2angle, lj3angle, lj4angle, lj5angle))
{
if (cmdptr->uservalue.Settime == true)
{
_FSMController->run();
}
}
legpre1[0]=rj0angle ;
legpre1[1]=rj1angle ;
legpre1[2]=rj2angle ;
legpre1[3]=rj3angle ;
legpre1[4]=rj4angle ;
legpre1[5]=rj5angle ;
legpre1[6]=lj0angle ;
legpre1[7]=lj1angle ;
legpre1[8]=lj2angle ;
legpre1[9]=lj3angle ;
legpre1[10]=lj4angle ;
legpre1[11]=lj5angle ;
/** 右腿流量前馈调试
rj3angle=20*sin(1*M_PI*time+1.5*M_PI)+20;
legpre2[3]=20*sin(1*M_PI*(time+0.5+bipins.sampletime)+1.5*M_PI)+20;
legpre1[3]=20*sin(1*M_PI*(time+0.5-bipins.sampletime)+1.5*M_PI)+20;
press=0;
rj3angle=0;
legpre1[3]=0;
legpre2[3]=0;
rj2angle=15*sin(1*M_PI*time);
legpre2[2]=15*sin(1*M_PI*(time+0.1+bipins.sampletime));
legpre1[2]=15*sin(1*M_PI*(time+0.1-bipins.sampletime));
rj0angle=0*15*sin(0.5*M_PI*time);
rj1angle=0*15*sin(0.5*M_PI*time);
*/
// rj1angle=4*sin(0.5*M_PI*time);
// legpre1[1]=4*sin(0.5*M_PI*(time -bipins.sampletime));
// legpre2[1]=4*sin(0.5*M_PI*(time+bipins.sampletime));
// lj1angle=4*sin(0.5*M_PI*time);
// legpre1[7]=4*sin(0.5*M_PI*(time -bipins.sampletime));
// legpre2[7]=4*sin(0.5*M_PI*(time+bipins.sampletime));
// rj3angle=20*sin(1*M_PI*time+1.5*M_PI)+20;
// legpre2[3]=20*sin(1*M_PI*(time+0.5+bipins.sampletime)+1.5*M_PI)+20;
// legpre1[3]=20*sin(1*M_PI*(time+0.5-bipins.sampletime)+1.5*M_PI)+20;
// lj3angle=20*sin(1*M_PI*time+1.5*M_PI)+20;
// legpre2[9]=20*sin(1*M_PI*(time+0.5+bipins.sampletime)+1.5*M_PI)+20;
// legpre1[9]=20*sin(1*M_PI*(time+0.5-bipins.sampletime)+1.5*M_PI)+20;
// rj2angle=10*sin(0.5*M_PI*time);
// legpre1[2]=10*sin(0.5*M_PI*(time -bipins.sampletime));
// legpre2[2]=10*sin(0.5*M_PI*(time+bipins.sampletime));
// lj2angle=10*sin(0.5*M_PI*time);
// legpre1[8]=10*sin(0.5*M_PI*(time -bipins.sampletime));
// legpre2[8]=10*sin(0.5*M_PI*(time+bipins.sampletime));
// legpre1L[2]=bipins.RJ2convert(legpre1[2])*1e-3;
// legpre2L[2]=bipins.RJ2convert(legpre2[2])*1e-3;
// rj4angle=16.5*cos(0.5*M_PI*time)-16.5;
// legpre1[4]=16.5*cos(0.5*M_PI*(time-bipins.sampletime))-16.5;
// lj4angle=16.5*cos(0.5*M_PI*time)-16.5;
// legpre1[10]=16.5*cos(0.5*M_PI*(time-bipins.sampletime))-16.5;
// legpre2[4]=16.5*cos(0.5*M_PI*(time+bipins.sampletime))-16.5;
// rj3angle=-1*(rj2angle+rj4angle);
// legpre1[3]=-1*(legpre1[2]+legpre1[4]);
// legpre2[3]=-1*(legpre2[2]+legpre2[4]);
// rj2angle=15*sin(0.5*M_PI*time);
// legpre1[2]=15*sin(0.5*M_PI*(time-bipins.sampletime));
// legpre2[2]=15*sin(0.5*M_PI*(time+bipins.sampletime));
// lj2angle=10;
// lj3angle=5;
// lj4angle=10;
// lj5angle=5;
// lj2angle=-15*sin(0.5*M_PI*time);
// legpre1[8]=-15*sin(0.5*M_PI*(time-bipins.sampletime));
// legpre1[8]=-15*sin(0.5*M_PI*(time+bipins.sampletime));
// float lj2vec=-1*cos(0.5*M_PI*time);
// float rj2vec=cos(0.5*M_PI*time);
// if(firstrun)
// {
// rj3angle=0;
// lj3angle=0;
// if(lj2angle<=-29)
// {
// firstrun=false;
// }
// swingtime1=time;
// swingtime=time;
// }
// else
// {
// if(lj2vec<0)
// {
// float time1;
// time1=time-swingtime;
// lj3angle=15*sin(M_PI*(time1)+1.5*M_PI)+15;
// legpre1[9]=15*sin(M_PI*(time1-bipins.sampletime)+1.5*M_PI)+15;
// legpre2[9]=15*sin(M_PI*(time1+bipins.sampletime)+1.5*M_PI)+15;
// }
// else
// {
// swingtime=time;
// }
// if(rj2vec<0)
// {
// float time1;
// time1=time-swingtime1;
// rj3angle=15*sin(M_PI*(time1)+1.5*M_PI)+15;
// legpre1[3]=15*sin(M_PI*(time1-bipins.sampletime)+1.5*M_PI)+15;
// legpre2[3]=15*sin(M_PI*(time1+bipins.sampletime)+1.5*M_PI)+15;
// }
// else
// {
// swingtime1=time;
// }
// }
// cout<<"LJDES[3]:"<<lj3angle<<'\n';
// rj0angle=0;
// bipins.RJ0RJ1convert(legpre1[0],legpre1[1],legpre1L[0],legpre1L[1]);
// bipins.RJ0RJ1convert(legpre2[0],legpre2[1],legpre2L[0],legpre2L[1]);
// rj3angle=20*sin(0.5*M_PI*time+1.5*M_PI)+20;
// legpre1[3]=20*sin(0.5*M_PI*(time+0.1-bipins.sampletime)+1.5*M_PI)+20;
// legpre2[3]=20*sin(0.5*M_PI*(time+0.1+bipins.sampletime)+1.5*M_PI)+20;
// rj5angle=8*sin(0.5*M_PI*time);
// legpre1[5]=8*sin(0.5*M_PI*(time-bipins.sampletime));
// lj5angle=8*sin(0.5*M_PI*time);
// legpre1[11]=8*sin(0.5*M_PI*(time-bipins.sampletime));
// legpre1L[3]=bipins.RJ2convert(legpre1[2])*1e-3;
// legpre2L[3]=bipins.RJ2convert(legpre2[2])*1e-3;
// rj4angle=18*cos(0.2*M_PI*time)-18;
// legpre1[4]=18*cos(0.2*M_PI*(time+0.5+bipins.sampletime))-18;
// legpre2[4]=18*cos(0.2*M_PI*(time+0.5-bipins.sampletime))-18;
// rj3angle=-1*(rj2angle+rj4angle);
// legpre1[3]=-1*(legpre1[4]+legpre1[2]);
// legpre2[3]=-1*(legpre2[4]+legpre2[2]);
// rj3angle=20*sin(0.5*M_PI*time+1.5*M_PI)+20;
// legpre2[3]=20*sin(0.5*M_PI*(time+0.5+bipins.sampletime)+1.5*M_PI)+20;
// legpre1[3]=20*sin(0.5*M_PI*(time+0.5-bipins.sampletime)+1.5*M_PI)+20;
// rj2angle=15*sin(0.5*M_PI*time);
// legpre2[2]=15*sin(0.5*M_PI*(time+0.1+bipins.sampletime));
// legpre1[2]=15*sin(0.5*M_PI*(time+0.1-bipins.sampletime));
// LDeg[2]=8*sin(0.5*M_PI*time);
// LDeg[2]=-20;
// KuanDeg[0] = 2.5 * sin(M_PI * time);
// rj4angle=-12*sin(M_PI*time);
// _FSMController->run();
// rj5angle=-10*sin(M_PI*time);
}
bipins.computepumpvel(legpre1);
// bipins.computepumpvel(legpre1,legpre2,legpre1_5);
cout << "泵前馈:\n"
<< bipins.pumpvelFF << '\n';
float pumpfff=bipins.pumpvelFF;
bipins.pumpvelFF = bipins.pumpvelFF << 16;
EC_WRITE_U32(domain_pd + off_bytes_0x7030_1, bipins.pumpvelFF);
float RJDES[6];
float LJDES[6];
RJDES[0] = rj0angle;
RJDES[1] = rj1angle;
RJDES[2] = rj2angle; // RJ0
RJDES[3] = rj3angle; // RJ1
RJDES[4] = rj4angle; // Rj4
RJDES[5] = rj5angle; // Rj5
LJDES[0] = lj0angle;
LJDES[1] = lj1angle;
LJDES[2] = lj2angle; // LJ3
LJDES[3] = lj3angle;
LJDES[4] = lj4angle; // Lj4
LJDES[5] = lj5angle; // Lj5
// unsigned long ssi[4];
// std::vector<unsigned long> ssi;
// for (int i = 0; i < 12; i++)
// std::cout << "编码器" << i <<" "<< *TR_data[i] << '\n';
// float Ldes = bipins.RJ2convert(RJDES[0]);
// float Lreal = bipins.RJ2convert(*TR_data[0]);
float L4real, L5real, L4des, L5des;
float RL4real, RL5real, RL4des, RL5des;
float LL4real, LL5real, LL4des, LL5des;
float gang0des, gang1des, lgang0des, lgang1des;
float gang0real, gang1real, lgang0real, lgang1real;
bipins.RJ0RJ1convert(RJDES[0], RJDES[1], gang0des, gang1des);
bipins.RJ0RJ1convert(*TR_data[0], *TR_data[1], gang0real, gang1real);
PIDSetpointSet(&PID_ptr_M[0], gang0des);
PIDInputSet(&PID_ptr_M[0], gang0real); // gang0
PIDCompute(&PID_ptr_M[0]);
FeedforwardAdd(&PID_ptr_M[0], gang0des, true);
PIDSetpointSet(&PID_ptr_M[1], gang1des);
PIDInputSet(&PID_ptr_M[1], gang1real); // gang1
PIDCompute(&PID_ptr_M[1]);
FeedforwardAdd(&PID_ptr_M[1], gang1des, true);
bipins.LJ0LJ1convert(LJDES[0], LJDES[1], lgang0des, lgang1des);
bipins.LJ0LJ1convert(*TR_data[6], *TR_data[7], lgang0real, lgang1real);
PIDSetpointSet(&PID_ptr_M[6], lgang0des);
PIDInputSet(&PID_ptr_M[6], lgang0real); // gang0
PIDCompute(&PID_ptr_M[6]);
FeedforwardAdd(&PID_ptr_M[6], lgang0des, true);
PIDSetpointSet(&PID_ptr_M[7], lgang1des);
PIDInputSet(&PID_ptr_M[7], lgang1real); // gang1
PIDCompute(&PID_ptr_M[7]);
FeedforwardAdd(&PID_ptr_M[7], lgang1des, true);
float rj2desl = bipins.RJ2convert(RJDES[2]);
float rj2reall = bipins.RJ2convert(*TR_data[2]);
PIDSetpointSet(&PID_ptr_M[2], rj2desl); // RJ2
PIDInputSet(&PID_ptr_M[2], rj2reall);
PIDCompute(&PID_ptr_M[2]);
FeedforwardAdd(&PID_ptr_M[2], rj2desl, false);
float u = numderivative(PID_ptr_M[2].setpoint * 1e-3, PID_ptr_M[2].lastsetpoint * 1e-3, bipins.sampletime);
float ps = Ps;
float p0 = 0;
FeedforwardAdd_P(&PID_ptr_M[2], bipins.J2gangW, bipins.J2gangY, ps * 1e6, p0, PressureA[2] * 1e6, PressureB[2] * 1e6, u, 10, 1);
// cout<<"RJ2 FFp"<<PID_ptr_M[2].FFP<<'\n';
// cout<<"RJ2_FF"<<PID_ptr_M[2].FF<<'\n';
// FPID_ptr[2].setpointset(rj2desl);
// FPID_ptr[2].inputset(rj2reall);
// FPID_ptr[2].PIDcompute();
// FPID_ptr[2].FeedforwardAdd(true);
// PID_ptr_M[2].output=FPID_ptr[2].output;
float lj2desl = bipins.LJ2convert(LJDES[2]);
float lj2reall = bipins.LJ2convert(*TR_data[8]);
PIDSetpointSet(&PID_ptr_M[8], lj2desl); // RJ2
PIDInputSet(&PID_ptr_M[8], lj2reall);
PIDCompute(&PID_ptr_M[8]);
FeedforwardAdd(&PID_ptr_M[8], lj2desl, false);
FeedforwardAdd_P(&PID_ptr_M[8], bipins.J2gangW, bipins.J2gangY, ps * 1e6, p0, PressureA[8] * 1e6, PressureB[8] * 1e6, u, 10, -1);
float RJ3Ldes = bipins.RJ3Convert(RJDES[3]);
float RJ3L = bipins.RJ3Convert(*TR_data[3]);
PIDSetpointSet(&PID_ptr_M[3], RJ3Ldes);
PIDInputSet(&PID_ptr_M[3], RJ3L);
PIDCompute(&PID_ptr_M[3]);
FeedforwardAdd(&PID_ptr_M[3], RJ3Ldes, true);
float LJ3des = bipins.LJ3Convert(LJDES[3]);
float LJ3L = bipins.LJ3Convert(*TR_data[9]);
PIDSetpointSet(&PID_ptr_M[9], LJ3des);
PIDInputSet(&PID_ptr_M[9], LJ3L);
PIDCompute(&PID_ptr_M[9]);
FeedforwardAdd(&PID_ptr_M[9], LJ3des, true);
// std::cout<<"RJ3PID输出"<<PID_ptr_M[3].output<<'\n';
// std::cout<<"RJ3实际:"<<*TR_data[3]<<'\n';
// std::cout<<"RJ3期望"<<RJDES[3]<<'\n';
// std::cout<<"RJ3期望长度"<<RJ3Ldes<<'\n';
// std::cout<<"RJ3实际长度"<<RJ3L<<'\n';
bipins.RJ4RJ5convert(RJDES[4], RJDES[5], RL4des, RL5des);
bipins.RJ4RJ5convert(*TR_data[4], *TR_data[5], RL4real, RL5real);
PIDSetpointSet(&PID_ptr_M[4], RL4des);
PIDInputSet(&PID_ptr_M[4], RL4real);
PIDCompute(&PID_ptr_M[4]);
FeedforwardAdd(&PID_ptr_M[4], RL4des, true);
PIDSetpointSet(&PID_ptr_M[5], RL5des);
PIDInputSet(&PID_ptr_M[5], RL5real);
PIDCompute(&PID_ptr_M[5]);
FeedforwardAdd(&PID_ptr_M[5], RL5des, true);
bipins.LJ4LJ5convert(LJDES[4], LJDES[5], LL4des, LL5des);
bipins.LJ4LJ5convert(*TR_data[10], *TR_data[11], LL4real, LL5real);
PIDSetpointSet(&PID_ptr_M[10], LL4des);
PIDInputSet(&PID_ptr_M[10], LL4real);
PIDCompute(&PID_ptr_M[10]);
FeedforwardAdd(&PID_ptr_M[10], LL4des, true);
PIDSetpointSet(&PID_ptr_M[11], LL5des);
PIDInputSet(&PID_ptr_M[11], LL5real);
PIDCompute(&PID_ptr_M[11]);
FeedforwardAdd(&PID_ptr_M[11], LL5des, true);
// PID_ptr_M[11].output = 0;
int OutIndex[6] = {0, 1, 5, 6, 7, 11};
// 创建一个布尔数组来标记
bool is_out_index[12] = {false};
for (int i = 0; i < 6; ++i)
{
is_out_index[OutIndex[i]] = true;
}
int c_idx = 0;
int v_idx = 0;
// PID_ptr_M[3].output = 1;
// PID_ptr_M[4].output = 1;
// PID_ptr_M[5].output = 1;
// PID_ptr_M[0].output=5*sin(M_PI*time);
// PID_ptr_M[8].output = 1+5*sin(M_PI*time);
// PID_ptr_M[9].output = 1+5*sin(M_PI*time);
// PID_ptr_M[10].output = 1+5*sin(M_PI*time);
// PID_ptr_M[11].output = 1+5*sin(M_PI*time);
for (int i = 0; i < 12; ++i)
{
output1 = (uint16_t)((PID_ptr_M[i].output + 10) / 20 * 65535);
if (is_out_index[i])
{
EC_WRITE_U16(domain_pd + off_bytes_0x7010[c_idx++], output1);
}
else
{
EC_WRITE_U16(domain_pd + off_bytes_0x7011[v_idx++], output1);
}
}
// output1 = (uint16_t)((PID_ptr_M[4].output+ 10) / 20 * 65536);
// EC_WRITE_U16(domain_pd + off_bytes_0x7011[2], output1);
// output1 = (uint16_t)((PID_ptr_M[5].output + 10) / 20 * 65536);
// EC_WRITE_U16(domain_pd + off_bytes_0x7010[2], output1);
counter = 0;
// std::cout << "-------------------\n";
// logger.Savedata();
char sendBuf[128];
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f\n", RJDES[0],*TR_data[0],*TR_data[2],*TR_data[3],
// PID_ptr_M[2].alteredKp,PID_ptr_M[2].dispKi,rj2desl,PID_ptr_M[2].FF,PID_ptr_M[2].output);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", *TR_data[0], *TR_data[1], *TR_data[2],
// *TR_data[3],*TR_data[4], *TR_data[5],*TR_data[6],*TR_data[7],*TR_data[8],*TR_data[9],*TR_data[10]
// ,*TR_data[11],RJDES[1],LJDES[1],gang0des,gang0real,gang1des,gang1real,lgang0des,lgang0real,lgang1des,lgang1real);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", PID_ptr_M[1].Error1,PID_ptr_M[2].Error1,PID_ptr_M[3].Error1,PID_ptr_M[4].Error1,PID_ptr_M[5].Error1, PID_ptr_M[7].Error1,PID_ptr_M[8].Error1,PID_ptr_M[9].Error1,PID_ptr_M[10].Error1,PID_ptr_M[11].Error1,PID_ptr_M[4].input,PID_ptr_M[10].input);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",gang1des,rj2desl,RJ3Ldes,RL4des,RL5des,
// lgang1des,lj2desl,LJ3des,LL4des,LL5des);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", *TR_data[0], *TR_data[1], *TR_data[2],
// *TR_data[3], *TR_data[4], *TR_data[5], RJDES[0], RJDES[1], RJDES[2], RJDES[3], RJDES[4], RJDES[5], Deg[2], LDeg[2], (float)PressureA[2], (float)PressureB[2], PID_ptr_M[2].FF, PID_ptr_M[2].FFP, u, Ps);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", Deg[0], Deg[1],Deg[2], LDeg[0], LDeg[1], LDeg[2],RJDES[0],RJDES[1],RJDES[2],RJDES[3],RJDES[4],RJDES[5],ifswing);
sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", RJDES[0], RJDES[1], RJDES[2],
RJDES[3], RJDES[4], RJDES[5], LJDES[0], LJDES[1], LJDES[2], LJDES[3], LJDES[4], LJDES[5], *TR_data[0], *TR_data[1], *TR_data[2],
*TR_data[3], *TR_data[4], *TR_data[5], *TR_data[6], *TR_data[7], *TR_data[8], *TR_data[9], *TR_data[10], *TR_data[11], PressureA[2], PressureB[2], PressureA[8], PressureB[8],Ps,pumpfff);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f\n",Deg[0],Deg[1],Deg[2],LDeg[0],LDeg[1],LDeg[2]);
// ZMP
// sprintf(sendBuf,"d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",zmpscope[0],zmpscope[1],zmpscope[2],zmpscope[3],
// walkphase,dyx,dyy,rlegswingphase,rlegcontactphase,llegswingphase,llegcontactphase,RDeg[0],RDeg[1],RDeg[2],LDeg[0],LDeg[1],LDeg[2]
// ,LJDES[0], LJDES[1], LJDES[2], LJDES[3], LJDES[4], LJDES[5]);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", RJDES[0], RJDES[1], RJDES[2],
// RJDES[3], RJDES[4], RJDES[5], LJDES[0], LJDES[1], LJDES[2], LJDES[3], LJDES[4], LJDES[5]);
// float sx=SX.computeS(Base_ACC_RAW[0]);
// float sy=SY.computeS(Base_ACC_RAW[1]);
// sprintf(sendBuf, "d:%f,%f,%f,%f,%f,%f,%f,%f\n", Base_RPY_RAW[0],Base_RPY_RAW[1],Base_RPY_RAW[2],Base_ACC_RAW[0],
// Base_ACC_RAW[1],Base_ACC_RAW[2],sx,sy);
// sprintf(sendBuf,"d:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", zmpInitialq[0], zmpInitialq[1],zmpInitialq[2],
// zmpInitialq[3], zmpInitialq[4],zmpInitialq[5], zmpInitialq[0],zmpInitialq[1],zmpInitialq[2], zmpInitialq[3], zmpInitialq[4],
// zmpInitialq[5]);
sendto(fd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *)&saddr, sizeof(saddr));
}
blink++;
check_master_state();
ecrt_domain_queue(domain);
ecrt_domain_queue(domain);
ecrt_master_send(master);
}
/****************************************************************************/
void signal_handler(int signum)
{
switch (signum)
{
case SIGALRM:
sig_alarms++;
break;
}
}
/****************************************************************************/
int main(int argc, char **argv)
{
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("socket");
exit(-1);
}