motor.c
25.5 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
#include "motor.h"
#include "FOC.h"
#include "math_utils.h"
#include "MGE_Hal.h"
// #include "PID.h"
#include "string.h"
#include "MOT_Dev_Config.h"
#include "Motor_Manage.h"
#include "BMCL_ParaLoadF1.h"
#include "BMCL_Config.h"
#include "buzzer.h"
#if MOT_DEV_TARGET == STM32_TARGET_A12_F4
#include "T_SpeedShape.h"
#include "OBS_Gen.h"
#endif
// KFP kfp_vel = {0.02, 0, 0, 0, 0.005, 20.0};
KFP kfp_vel = {0};
// KFP kfp_ang = {0.02, 0, 0, 0, 0.0001, 0.01};
KFP kfp_ang = {0};
float offset_Temp;
float mge_direction_Temp;
float zeroElectricAngleOffset_Temp;
/*================== Motor Setting ===================*/
static int8_t motor_PP = 7; // 极对数
static float mot_LimitAngle_B= 90;//上限位
static float mot_LimitAngle_S = -30;//下限位
// MOTOR_IMU_DIRECTION mot_Imu_dir = MOTOR_IMU_DIRECTION_CW;
// CALIBRATION_STATE calibration_State = 0; //校准状态
/*=============== Motor Runtime Setting ==============*/
float Set_Velocity = 0.0f; // Target Vel - available in Close Vel Control
float actual_Velocity;
float Target_Angle = 0.0f; // Target Angle - available in Close Angle Control
float actual_Angle;
/*==================== PID Config ===================*/
// PID_Typedef velocity_PID; // vel PID - Simple Ver
// PID_Typedef angle_PID; // angle PID - Simgle Ver
static GenPID_s velGenPID = {0};
static GenPID_s angGenPID = {0};
/********************************* Motor Init ***************************************************/
void motor_InitALL(void)
{
}
// CUT## 电机属性初始化
int8_t motor_SetPP(uint8_t PP)
{
if (PP <= 0)
{
return -1;
}
else
{
motor_PP = PP;
return 0;
}
}
int8_t motor_GetPP()
{
return motor_PP;
}
int8_t motor_SetUpLimit(float AngLimit)
{
if (AngLimit < 0)
{
return -1;
}
else
{
mot_LimitAngle_B = AngLimit;
return 0;
}
}
float motor_GetUpLimit()
{
return mot_LimitAngle_B;
}
int8_t motor_SetDownLimit(float AngLimit)
{
if (AngLimit > 0)
{
return -1;
}
else
{
mot_LimitAngle_S = AngLimit;
return 0;
}
}
float motor_GetDownLimit()
{
return mot_LimitAngle_S;
}
// CUT## PID初始化相关
// GenPID_CONFIG motor_angGenPID_Get()
// {
// return angGenPID.config;
// }
// int motor_angGenPID_Get_Needle(GenPID_CONFIG *config)
// {
// *config = velGenPID.config;
// return 0;
// }
// int motor_angGenPID_Set(GenPID_CONFIG config)
// {
// return GenPID_Set(&angGenPID, config);
// }
// int motor_angGenPID_Set_Needle(GenPID_CONFIG *config)
// {
// return GenPID_Set_Needle(&angGenPID, config);
// }
// GenPID_CONFIG motor_velGenPID_Get()
// {
// return velGenPID.config;
// }
// int motor_velGenPID_Get_Needle(GenPID_CONFIG *config)
// {
// *config = velGenPID.config;
// return 0;
// }
// int motor_velGenPID_Set(GenPID_CONFIG config)
// {
// return GenPID_Set(&velGenPID, config);
// }
// int motor_velGenPID_Set_Needle(GenPID_CONFIG *config)
// {
// return GenPID_Set_Needle(&velGenPID, config);
// }
int motor_GenPID_Get_Needle(__MOTOR_GENPID_INDEX index, GenPID_CONFIG *config)
{
int status = 0;
switch (index)
{
case __MOTOR_GENPID_INDEX_VEL:
*config = velGenPID.config;
break;
case __MOTOR_GENPID_INDEX_ANG:
*config = angGenPID.config;
break;
default:
status = -1;
break;
}
return status;
}
int motor_GenPID_Set_Needle(__MOTOR_GENPID_INDEX index, GenPID_CONFIG *config)
{
int status = 0;
switch (index)
{
case __MOTOR_GENPID_INDEX_VEL:
GenPID_Set_Needle(&velGenPID, config);
break;
case __MOTOR_GENPID_INDEX_ANG:
GenPID_Set_Needle(&angGenPID, config);
break;
default:
status = -1;
break;
}
return status;
}
// CUT## 速度角度滤波初始化相关
KFP_CONFIG motor_velKFP_Get(void)
{
return kfp_vel.config;
}
KFP_CONFIG motor_angKFP_Get(void)
{
return kfp_ang.config;
}
int motor_velKFP_Set(KFP_CONFIG config)
{
return kalmanFilter_Init(&kfp_vel, config);
}
int motor_angKFP_Set(KFP_CONFIG config)
{
return kalmanFilter_Init(&kfp_ang, config);
}
/********************************* PID Config ***************************************************/
/********************************* EAngle *******************************************************/
// one of the zeroElectAngle : 5.12464619
static float zeroElectricAngleOffset = 0.0f;
static float zeroElectricLoopAngleOffset = 0.0f;
float motor_getElectricalAngleLoop(void)
{
return _normalizeAngle(motor_PP * mge_hal_GetAngle() - zeroElectricLoopAngleOffset);
}
float motor_getElectricalAngleABS(void)
{
return _normalizeAngle(motor_PP * mge_hal_GetAbsAngle() - zeroElectricAngleOffset);
}
// float motor_getElectricalAngleABS_LP(void)
// {
// return _normalizeAngle(motor_PP * mge_hal_GetAbsAngle_withFilter() - zeroElectricAngleOffset);
// }
float motor_getElectricalAngleABS_LP_AngIn(float angle)
{
return _normalizeAngle(motor_PP * _normalizeAngle(angle + mge_Get_abOffset()) - zeroElectricAngleOffset);
}
void motor_Set_ZeroEAngleOffset(float offset)
{
zeroElectricAngleOffset = offset;
}
float motor_Get_ZeroEAngleOffset()
{
return zeroElectricAngleOffset;
}
float motor_Get_ZeroEAngleLoopOffset()
{
return zeroElectricLoopAngleOffset;
}
void motor_Set_ZeroEAngleLoopOffset(float offset)
{
zeroElectricLoopAngleOffset = offset;
}
/********************************* Motor Align ***************************************************/
#if MOT_DEV_TARGET == STM32_TARGET_A12_F4
// Motor Align
void motor_AutoCalibration()
{
if (workMode == WORK_MODE_0||workMode == WORK_MODE_1||workMode == WORK_MODE_2)
{
do
{
mode_Switch(0xBB, WORK_MODE_3);
osDelay(1);
} while (operationResult == 0);
operationResult = 0;
do
{
mode_Switch(0xCC, WORK_MODE_3);
osDelay(1);
} while (operationResult == 0);
operationResult = 0;
workModePer = workMode;
workMode = WORK_MODE_3;
// motor_Align_Sensor(&imu_AHRS_result);
// do
// {
// motor_Calibration_Start(0xBB, PROTOCOL_F_SROUTE_0X51_SR_0X1A); // 开始roll电机校准
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// do
// {
// motor_Calibration_Status_Checks(0xBB, PROTOCOL_F_SROUTE_0X52_SR_0X2A);
// osDelay(2);
// } while (CalibrationCmplete == 0);
// // 判断通用回复正常并且是否校准完成
// generalResult = 0;
// CalibrationCmplete = 0;
// do
// {
// motor_Calibration_Start(0xCC, PROTOCOL_F_SROUTE_0X51_SR_0X1A); // 开始yaw电机校准
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
}
else
{
do
{
mode_Switch(0xBB, WORK_MODE_1);
osDelay(1);
} while (operationResult == 0);
operationResult = 0;
do
{
mode_Switch(0xCC, WORK_MODE_1);
osDelay(1);
} while (operationResult == 0);
operationResult = 0;
workMode = workModePer;
}
// do
// {
// motor_Calibration_Status_Checks(0XBB, PROTOCOL_F_SROUTE_0X02_SR_0X11);
// HAL_Delay(500);
// } while (generalResult == 0 || Gen_dataResult.rawdata_8t[2] != __PARAMGE_DEF_MOT_CALI_STA_S1); // 判断通用回复正常并且是否校准完成
// generalResult = 0;
// Gen_dataResult.rawdata_8t[2] = __PARAMGE_DEF_MOT_CALI_STA_BAD;
// do
// {
// motor_Calibration_Status_Checks(0xCC, PROTOCOL_F_SROUTE_0X02_SR_0X11);
// osDelay(1);
// } while (generalResult == 0 || Gen_dataResult.rawdata_8t[2] != __PARAMGE_DEF_MOT_CALI_STA_S1); // 判断通用回复正常并且是否校准完成
// generalResult = 0;
// Gen_dataResult.rawdata_8t[2] = __PARAMGE_DEF_MOT_CALI_STA_BAD;
// if (workMode == WORK_MODE_1)
// {
// do
// {
// mode_Switch(0xBB, WORK_MODE_3);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// do
// {
// mode_Switch(0xCC, WORK_MODE_3);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// workMode = WORK_MODE_3;
// }
// else
// {
// do
// {
// mode_Switch(0xBB, WORK_MODE_1);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// do
// {
// mode_Switch(0xCC, WORK_MODE_1);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// workMode = WORK_MODE_1;
// }
}
void gimbal_Axisalign(void)
{
if (workMode != WORK_MODE_4)
{
workModePer = workMode;
workMode = WORK_MODE_4;
}
else
{
pitchCenterOffset = gen_OBS.vaule[7] / 57.2957795130f;
rollCenterOffset += gen_OBS.vaule[5] / 57.2957795130f;
yawCenterOffset += gen_OBS.vaule[6] / 57.2957795130f;
BMCL_mot_Calibration = BMCL_CONFIG_MOT_CAL_STA_CALS2;
mge_Set_abOffset(mge_Get_abOffset()+pitchCenterOffset);
BMCL_PL_WriteAll2ParaMge();
paraMge_SaveALL();
workMode = workModePer;
}
}
void gimbal_Horizontalalign(void)//云台水平对齐
{
//进入自稳模式,可手动调整外框各电机角度至中间位置,同时如果imu位置不准时,\
可手动将imu调整到水平位置(改变目标姿态即可)
// if (workMode == WORK_MODE_1)
// {
// do
// {
// mode_Switch(0xBB, WORK_MODE_3);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// do
// {
// mode_Switch(0xCC, WORK_MODE_3);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// workMode = WORK_MODE_3;
// osDelay(500);
pitchCenterOffset += gen_OBS.vaule[7] / 57.2957795130f;
rollCenterOffset += gen_OBS.vaule[5] / 57.2957795130f;
// 手动将imu调整到水平位置(改变目标姿态即可)
osDelay(1);
yawCenterOffset += gen_OBS.vaule[6] / 57.2957795130f;
// }
// else
// {
// do
// {
// mode_Switch(0xBB, WORK_MODE_1);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// do
// {
// mode_Switch(0xCC, WORK_MODE_1);
// osDelay(1);
// } while (operationResult == 0);
// operationResult = 0;
// workMode = WORK_MODE_1;
// }
}
#endif
uint8_t motor_start_SelfTest(void)
{
int i;
float Uq;
float angle;
float mid_angle, end_angle;
float moved;
int stop_Count = 0;
float pre_Angle, cur_Angle;
float Up_Angle, Down_Angle;
// float imu_Up_Angle, imu_Down_Angle;
// HAL_Delay(500);
for (i = 0; i <= 1000; i++)//缓存作用
{
Uq = 4*i / 1000.0;
setPhaseVoltage(Uq, 0, _3PI_2);
HAL_Delay(1);
}
/*============= Sensor Zero Position Move =========*/
/* Obtain the operating range of the motor and offset the zero position of the sensor to an inaccessible place */
int checkTime = 0;
// Up moving
pre_Angle = mge_hal_GetAngle();
for (i = 0; i <= 2000; i++)
{
angle = _3PI_2 + _2PI * motor_PP * i / 2000.0;
setPhaseVoltage(3.0f, 0, angle);
cur_Angle = mge_hal_GetAngle();
if (checkTime > 5)
{
if (fabsf(cur_Angle - pre_Angle) < 0.005f)
{
stop_Count++;
}
else
{
stop_Count = 0;
}
if (stop_Count > 10)
{
break;
}
pre_Angle = cur_Angle;
checkTime = 0;
}
checkTime++;
delay_us(700);
}
HAL_Delay(100);//延时以保证读取准确
Up_Angle = mge_hal_GetAngle();
// imu_Up_Angle = imu_Angle_Process(¤t_axis_data);
stop_Count = 0;
checkTime = 0;
// Pre Moving
for (i = 1000; i >= 0; i--)
{
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(3.0f, 0, angle);
delay_us(700);
}
// Down moving
pre_Angle = mge_hal_GetAngle();
// Down moving
pre_Angle = mge_hal_GetAngle();
for (i = 2000; i >= 0; i--)
{
angle = _3PI_2 + _2PI * motor_PP * i / 2000.0;
setPhaseVoltage(3.0f, 0, angle);
cur_Angle = mge_hal_GetAngle();
if (checkTime > 5)
{
if (fabsf(cur_Angle - pre_Angle) < 0.005f)
{
stop_Count++;
}
else
{
stop_Count = 0;
}
if (stop_Count > 10)
{
break;
}
pre_Angle = cur_Angle;
checkTime = 0;
}
checkTime++;
delay_us(700);
}
HAL_Delay(100);//延时以保证读取准确
Down_Angle = mge_hal_GetAngle();
// imu_Down_Angle = imu_Angle_Process(¤t_axis_data);
moved = Up_Angle - Down_Angle;
// imu_moved = imu_Up_Angle - imu_Down_Angle;
for (i = 0; i <= 2000; i++)
{
// Uq = 5*i / 1000.0;
angle = _3PI_2 + _2PI * 2* i / 2000.0;
setPhaseVoltage(3.0f, 0, angle);
delay_us(700);
}
if (fabs(moved) < _PI) // 未达到正常限位范围
{
return 1; // 开机自检异常
}
return 0;
}
/********************************* Motor Align ***************************************************/
__PARAMGE_DEF_MOT_CALI_STA motor_Align_Sensor(void)
{
#if MOTOR_DEBUG_SENSOR_ALIGN == PROJECT_CONFIG_ENABLE
int i;
float Uq;
float angle;
float mid_angle, end_angle;
float moved;
int stop_Count = 0;
float pre_Angle, cur_Angle;
float Up_Angle, Down_Angle;
// float imu_Up_Angle, imu_Down_Angle;
HAL_Delay(500);
motor_Calibration_Status = __PARAMGE_DEF_MOT_CALI_STA_CALIBRATING;//校准中
for (i = 0; i <= 1000; i++)//缓存作用
{
Uq = 4*i / 1000.0;
setPhaseVoltage(Uq, 0, _3PI_2);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
/*============= Sensor Zero Position Move =========*/
/* Obtain the operating range of the motor and offset the zero position of the sensor to an inaccessible place */
int checkTime = 0;
// Up moving
pre_Angle = mge_hal_GetAngle();
for (i = 0; i <= 2000; i++)
{
angle = _3PI_2 + _2PI * motor_PP * i / 2000.0;
setPhaseVoltage(3.0f, 0, angle);
cur_Angle = mge_hal_GetAngle();
if (checkTime > 5)
{
if (fabsf(cur_Angle - pre_Angle) < 0.005f)
{
stop_Count++;
}
else
{
stop_Count = 0;
}
if (stop_Count > 10)
{
break;
}
pre_Angle = cur_Angle;
checkTime = 0;
}
checkTime++;
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
HAL_Delay(100);//延时以保证读取准确
Up_Angle = mge_hal_GetAngle();
// imu_Up_Angle = imu_Angle_Process(¤t_axis_data);
stop_Count = 0;
checkTime = 0;
// Pre Moving
for (i = 1000; i >= 0; i--)
{
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(3.0f, 0, angle);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
// Down moving
pre_Angle = mge_hal_GetAngle();
for (i = 2000; i >= 0; i--)
{
angle = _3PI_2 + _2PI * motor_PP * i / 2000.0;
setPhaseVoltage(3.0f, 0, angle);
cur_Angle = mge_hal_GetAngle();
if (checkTime > 5)
{
if (fabsf(cur_Angle - pre_Angle) < 0.005f)
{
stop_Count++;
}
else
{
stop_Count = 0;
}
if (stop_Count > 10)
{
break;
}
pre_Angle = cur_Angle;
checkTime = 0;
}
checkTime++;
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
HAL_Delay(100);//延时以保证读取准确
Down_Angle = mge_hal_GetAngle();
// imu_Down_Angle = imu_Angle_Process(¤t_axis_data);
moved = Up_Angle - Down_Angle;
// imu_moved = imu_Up_Angle - imu_Down_Angle;
if (fabs(moved) < _PI) // 未达到正常限位范围
{
motor_Calibration_Status = __PARAMGE_DEF_MOT_CALI_STA_ODD; // 基本校准异常
return motor_Calibration_Status;
}
// //电机旋转方向与imu方向判断
// if(moved*imu_moved>0){
// mot_Imu_dir = MOTOR_IMU_DIRECTION_CW;
// } else{
// mot_Imu_dir = MOTOR_IMU_DIRECTION_CCW;
// }
offset_Temp = mge_hal_calculateArcMidpointAngleBTP(Up_Angle, Down_Angle);
/*============= Motor electrical and direction calibration =========*/
// Pre Moving
for (i = 0; i <= 1000; i++)
{
// Uq = 5*i / 1000.0;
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(5.0f, 0, angle);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
for (i = 0; i <= 1000; i++)
{
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(5.0f, 0, angle);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
for (i = 0; i <= 1000; i++)
{
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(3.0f, 0, angle);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
HAL_Delay(100);//延时以保证读取准确
mid_angle = mge_hal_GetAngle();
for (i = 1000; i >= 0; i--)
{
angle = _3PI_2 + _2PI * i / 1000.0;
setPhaseVoltage(3.0f, 0, angle);
if (motor_Calibration_Status == 0)
{
goto motor_Calibration_Jump;
}
HAL_Delay(1);
}
HAL_Delay(100);//延时以保证读取准确
end_angle = mge_hal_GetAngle();
moved = fabs(mid_angle - end_angle);
if ((mid_angle == end_angle) || (moved < 0.5)) // 相等或者小于一个电角度周期角度
{
motor_Calibration_Status = __PARAMGE_DEF_MOT_CALI_STA_ODD; // 基本校准异常
return motor_Calibration_Status;
}
else if (mid_angle < end_angle)
{
mge_direction_Temp = MGE_DIRECTION_CCW;
}
else
{
mge_direction_Temp = MGE_DIRECTION_CW;
}
// 电角度零位标定
setPhaseVoltage(5.0f, 0.0f, _3PI_2);
HAL_Delay(1000);
zeroElectricAngleOffset_Temp = _normalizeAngle(mge_direction_Temp * motor_PP * mge_hal_GetAngle());
if (mge_direction_Temp == MGE_DIRECTION_CCW)
{
offset_Temp = -offset_Temp;
}
motor_Calibration_Status = __PARAMGE_DEF_MOT_CALI_STA_GOOD; // 基本校准已完成
setPhaseVoltage(0.0f, 0.0f, _3PI_2);
return motor_Calibration_Status;
motor_Calibration_Jump:
motor_Calibration_Stop = 1;
return motor_Calibration_Status;
#else
// #if MOTOR_BOARD_SELECT_AXIS == MOTOR_HOLDER_AXIS_ROLL
// mge_direction = CW;
// mge_set_abOffset(0.787124157f);
// zeroElectricAngleOffset = 4.4036727f;
// #endif
// #if MOTOR_BOARD_SELECT_AXIS == MOTOR_HOLDER_AXIS_YAW
// mge_direction = CCW;
// mge_set_abOffset(5.60535765f);
// zeroElectricAngleOffset = 2.32839298f;
// #endif
#endif
}
/********************************* Openloop Control ********************************************/
// 开环运行测试1,模拟电角度
void motor_OpenVelocity1(float target)
{
const float voltageLimit = 1.0f; // 开环电压限制
static float _estimateAngle = 0.0f; // 开环虚拟机械角度
const float deltaT = 0.002f; // 开环运行时间间隔
_estimateAngle = _normalizeAngle((_estimateAngle + target * deltaT) * 7);
setPhaseVoltage(voltageLimit, 0.0f, _estimateAngle);
}
// 开环速度控制测试,使用MT6701反馈的电角度
void motor_OpenVelocity2(float Uq)
{
float el_Angle = motor_getElectricalAngleLoop();
setPhaseVoltage(Uq, 0.0f, el_Angle);
}
/********************************** Closeloop Control SimpleVer *********************************/
/*======================================== Close Control With GenPID ===========================*/
float CACG_CurAngle = 0;
static float CACG_CurVelocity = 0;
// LowPassFSTD_s velLP_Config = {0};
// void velLP_Configuration(void)
// {
// LowPassFilterSTD_Init(&velLP_Config, 0.0001f);
// }
#if MOT_DEV_TARGET == STM32_TARGET_A12_F4
extern TRAP_CURVE_s pitchANGPlan;
extern float pitchANGPlan_Time;
extern TimeRec_s pitchANGPlan_TimeS;
void MOT_Pitch_AngleControl_WithTCurve_GenPID(state_t *imu_AHRS_result)
{
static float set_Velocity = 0;
CACG_CurAngle = mge_hal_GetAbsAngle_withFilter();
CACG_CurVelocity = mge_hal_GetVel_ExtAngle(CACG_CurAngle);
// Vel Filter
// CACG_CurVelocity = LowPassFilterSTD_Process(&velLP_Config , CACG_CurVelocity );
float CACG_CurVelocity_KFP = kalmanFilter(&kfp_vel, CACG_CurVelocity);
// Angle PID Process
if (imu_AHRS_result != NULL)
{
pitchANGPlan_Time += TimeFlash(&pitchANGPlan_TimeS);
float angle_traget = trap_Curve_Update(&pitchANGPlan, pitchANGPlan_Time);
set_Velocity = GenPID_ProcessSTD(&angGenPID, angle_traget / 57.2957795130f, imu_AHRS_result->attitude.pitch / 57.2957795130f, &angGenPID_obs); // 角度换PID控制,输出转速
// set_Velocity = GenPID_Process(&angGenPID, angleIMU_traget, imu_AHRS_result->attitude.pitch, &angGenPID_obs);
}
// Vel PID Process
// float Uq = GenPID_ProcessSTD_LPIn(&velGenPID,set_Velocity,CACG_CurVelocity,CACG_CurVelocity_KFP,&velGenPID_obs);
float Uq = mot_Imu_dir*GenPID_ProcessSTD(&velGenPID, set_Velocity, CACG_CurVelocity_KFP, &velGenPID_obs);
float el_Angle = motor_getElectricalAngleABS_LP();
setPhaseVoltage(Uq, 0.0f, el_Angle);
/*############## Observation Add ############*/
motor_obs.actual_Angle = CACG_CurAngle;
motor_obs.actual_Velocity = CACG_CurVelocity;
motor_obs.actual_Velocity_KPF = CACG_CurVelocity_KFP;
motor_obs.Set_Angle = Set_Angle;
motor_obs.Set_Velocity = set_Velocity;
motor_obs.el_Angle = el_Angle;
motor_obs.Uq = Uq;
}
#endif
#if MOT_DEV_TARGET == STM32_TARGET_A2_F1
void MOT_YandR_VelControl_Direct_GenPID_ErrIn(float err)
{
static float set_Velocity = 0;
// #if MOTOR_BOARD_DEBUG_OR_TEST == PROJECT_CONFIG_ENABLE
// #endif
set_Velocity = err;
CACG_CurAngle = mge_hal_GetAbsAngle_withFilter();
CACG_CurVelocity = mge_hal_GetVel_ExtAngle(CACG_CurAngle);
// Vel Filter
// CACG_CurVelocity = LowPassFilterSTD_Process(&velLP_Config , CACG_CurVelocity );
float CACG_CurVelocity_KFP = kalmanFilter(&kfp_vel, CACG_CurVelocity);
// Vel PID Process
// float Uq = GenPID_ProcessSTD_LPIn(&velGenPID,set_Velocity,CACG_CurVelocity,CACG_CurVelocity_KFP,&velGenPID_obs);
float Uq = GenPID_ProcessSTD(&velGenPID, set_Velocity, CACG_CurVelocity_KFP, &velGenPID_obs);
float el_Angle = motor_getElectricalAngleABS_LP_AngIn(CACG_CurAngle);
setPhaseVoltage(Uq, 0.0f, el_Angle);
/*############## Observation Add ############*/
motor_obs.actual_Angle = CACG_CurAngle;
motor_obs.actual_Velocity = CACG_CurVelocity;
motor_obs.actual_Velocity_KPF = CACG_CurVelocity_KFP;
motor_obs.Set_Angle = Target_Angle;
motor_obs.Set_Velocity = set_Velocity;
motor_obs.el_Angle = el_Angle;
motor_obs.Uq = Uq;
// motor_obs.errPIDOut = err;
// motor_obs.errPIDOut1 = err1;
motor_obs.Set_Velocity = set_Velocity;
}
#endif
//! 注意,该函数使用了电机所在轴的宏判断,如需使用,则需要修改
void close_Angle_Control_GenPID(float Target_Angle)//目标角度闭环控制
{
float CACG_CurAngle_KFP;
CACG_CurAngle = mge_hal_GetAbsAngle_withFilter();
CACG_CurAngle_KFP = kalmanFilter(&kfp_ang, CACG_CurAngle);
CACG_CurVelocity = mge_hal_GetVel_ExtAngle(CACG_CurAngle_KFP); // get vel
// Vel Filter
// CACG_CurVelocity = LowPassFilterSTD_Process(&velLP_Config, CACG_CurVelocity);
float CACG_CurVelocity_KFP = kalmanFilter(&kfp_vel, CACG_CurVelocity);
// Angle PID Process
float set_Velocity = GenPID_ProcessSTD(&angGenPID, Target_Angle, CACG_CurAngle_KFP, &angGenPID_obs); // 角度换PID控制,输出转速
// Vel PID Process
float Uq = GenPID_ProcessSTD_LPIn(&velGenPID, set_Velocity, CACG_CurVelocity, CACG_CurVelocity_KFP, &velGenPID_obs);
float el_Angle = motor_getElectricalAngleABS_LP_AngIn(CACG_CurAngle_KFP);
setPhaseVoltage(Uq, Ud, el_Angle);
/*############## Observation Add ############*/
motor_obs.actual_Angle = CACG_CurAngle_KFP;
motor_obs.actual_Velocity = CACG_CurVelocity;
motor_obs.actual_Velocity_KPF = CACG_CurVelocity_KFP;
motor_obs.Set_Angle = Target_Angle;
motor_obs.Set_Velocity = set_Velocity;
motor_obs.el_Angle = el_Angle;
motor_obs.Uq = Uq;
}