1
1
/* *
2
2
* @author FENN MAI
3
3
* @date 06/05/2024
4
- * @version Basic 4.5
5
- * @brief 编码电机初次测试
4
+ * @version Basic 4.6
5
+ * @brief pos-vol-pid control test
6
6
*/
7
7
8
8
#include < iostream>
11
11
#include " PID.h"
12
12
13
13
#ifndef M_PI
14
- #define M_PI 3.14159265358979323846
14
+ #define M_PI 3.1415927
15
15
#endif
16
16
17
17
// 轮子的直径(单位:米)
@@ -26,6 +26,11 @@ int calculatePulsesForDistance(double distance) {
26
26
return static_cast <int >((distance / circumference) * PULSES_PER_REVOLUTION);
27
27
}
28
28
29
+ // 计算速度(假设每秒钟调用一次)
30
+ double calculateSpeed (int current_pulses, int previous_pulses, double delta_time) {
31
+ return (current_pulses - previous_pulses) / delta_time;
32
+ }
33
+
29
34
int main () {
30
35
if (gpioInitialise () < 0 ) {
31
36
std::cerr << " pigpio initialization failed" << std::endl;
@@ -36,45 +41,81 @@ int main() {
36
41
shield.begin ();
37
42
38
43
EncoderMotor *motor1 = shield.getEncoderMotor (1 );
44
+ EncoderMotor *motor2 = shield.getEncoderMotor (2 );
39
45
motor1->begin (1 , 5 , 6 ); // 设置电机1,编码器引脚为 GPIO 5 和 GPIO 6
46
+ motor2->begin (2 , 13 , 19 ); // 设置电机2,编码器引脚为 GPIO 13 和 GPIO 19
47
+
48
+ PID position_pid1 (0.1 , 0.01 , 0.05 ); // 位置PID motor1
49
+ PID speed_pid1 (0.1 , 0.01 , 0.05 ); // 速度PID motor1
40
50
41
- PID pid (0.1 , 0.01 , 0.05 );
51
+ PID position_pid2 (0.1 , 0.01 , 0.05 ); // 位置PID motor2
52
+ PID speed_pid2 (0.1 , 0.01 , 0.05 ); // 速度PID motor2
42
53
43
54
int target_pulses = calculatePulsesForDistance (1.0 ); // 计算前进1米所需的脉冲数
44
55
motor1->resetEncoder ();
56
+ motor2->resetEncoder ();
57
+
58
+ int previous_pulses1 = 0 ;
59
+ int previous_pulses2 = 0 ;
60
+ double delta_time = 0.1 ; // 假设每0.1秒进行一次速度计算
45
61
46
62
motor1->setSpeed (128 );
63
+ motor2->setSpeed (128 );
47
64
motor1->run (FORWARD);
48
-
49
- bool reached_target = false ;
50
- while (!reached_target) {
51
- int actual_pulses = motor1-> readEncoder () ;
52
- double error = target_pulses - actual_pulses;
53
- double speed = pid. calculate (target_pulses, actual_pulses);
54
-
55
- // 调整速度以更精确地接近目标
56
- if ( abs (error) < 2000 ) { // 如果误差小于2000,减速
57
- speed = std::max ( 30.0 , speed); // 保持一个最低速度以防止突然停止
58
- }
59
-
60
- motor1->setSpeed (static_cast <uint8_t >(std::min (std::max (speed , 0.0 ), 255.0 )));
61
-
62
- int direction = motor1->getDirection ();
63
- std::string direction_str = (direction == 1 ) ? " 顺时针" : (direction == -1 ) ? " 逆时针" : " 静止" ;
65
+ motor2-> run (FORWARD);
66
+
67
+ bool reached_target1 = false ;
68
+ bool reached_target2 = false ;
69
+ while (!reached_target1 || !reached_target2) {
70
+ // Motor 1
71
+ int actual_pulses1 = motor1-> readEncoder ();
72
+ double position_error1 = target_pulses - actual_pulses1;
73
+ double desired_speed1 = position_pid1. calculate (target_pulses, actual_pulses1);
74
+ double actual_speed1 = calculateSpeed (actual_pulses1, previous_pulses1, delta_time);
75
+ previous_pulses1 = actual_pulses1;
76
+ double pwm_value1 = speed_pid1. calculate (desired_speed1, actual_speed1);
77
+ motor1->setSpeed (static_cast <uint8_t >(std::min (std::max (pwm_value1 , 0.0 ), 255.0 )));
78
+
79
+ int direction1 = motor1->getDirection ();
80
+ std::string direction_str1 = (direction1 == 1 ) ? " 顺时针" : (direction1 == -1 ) ? " 逆时针" : " 静止" ;
64
81
65
82
std::cout << " 电机编号:1;理想前进值:" << target_pulses
66
- << " ;实际前进值:" << actual_pulses
67
- << " ;误差:" << error
68
- << " ;方向:" << direction_str << std::endl;
69
-
70
- if (abs (error) <50 && error < 0 ) { // 允许的误差范围
71
- reached_target = true ;
83
+ << " ;实际前进值:" << actual_pulses1
84
+ << " ;误差:" << position_error1
85
+ << " ;方向:" << direction_str1
86
+ << " ;PWM值:" << pwm_value1 << std::endl;
87
+
88
+ // Motor 2
89
+ int actual_pulses2 = motor2->readEncoder ();
90
+ double position_error2 = target_pulses - actual_pulses2;
91
+ double desired_speed2 = position_pid2.calculate (target_pulses, actual_pulses2);
92
+ double actual_speed2 = calculateSpeed (actual_pulses2, previous_pulses2, delta_time);
93
+ previous_pulses2 = actual_pulses2;
94
+ double pwm_value2 = speed_pid2.calculate (desired_speed2, actual_speed2);
95
+ motor2->setSpeed (static_cast <uint8_t >(std::min (std::max (pwm_value2, 0.0 ), 255.0 )));
96
+
97
+ int direction2 = motor2->getDirection ();
98
+ std::string direction_str2 = (direction2 == 1 ) ? " 顺时针" : (direction2 == -1 ) ? " 逆时针" : " 静止" ;
99
+
100
+ std::cout << " 电机编号:2;理想前进值:" << target_pulses
101
+ << " ;实际前进值:" << actual_pulses2
102
+ << " ;误差:" << position_error2
103
+ << " ;方向:" << direction_str2
104
+ << " ;PWM值:" << pwm_value2 << std::endl;
105
+
106
+ // 判断是否达到目标位置
107
+ if (abs (position_error1) < 618 ) { // 允许的误差范围
108
+ reached_target1 = true ;
109
+ }
110
+ if (abs (position_error2) < 618 ) { // 允许的误差范围
111
+ reached_target2 = true ;
72
112
}
73
113
74
- gpioDelay (100 * 1000 ) ; // 延时100毫秒
114
+ gpioDelay (static_cast < unsigned int >(delta_time * 1000 * 1000 )) ; // 延时delta_time秒
75
115
}
76
116
77
117
motor1->run (RELEASE);
118
+ motor2->run (RELEASE);
78
119
gpioTerminate ();
79
120
return 0 ;
80
121
}
0 commit comments