Skip to content

Commit 110e798

Browse files
committed
feat: version 4.6 pos-vol-pid control test( still have 2 bugs)
1 parent 8875be8 commit 110e798

File tree

8 files changed

+85
-56
lines changed

8 files changed

+85
-56
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,zmai,macc,06.06.2024 23:44,file:///home/zmai/.config/libreoffice/4;

Basic_Development/New_Car_Control/Encoder_Base/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 4.5)
1+
cmake_minimum_required(VERSION 3.0)
22
project(tt)
33

44
set(CMAKE_CXX_STANDARD 11)
Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @author FENN MAI
33
* @date 06/05/2024
4-
* @version Basic 4.5
5-
* @brief 编码电机初次测试
4+
* @version Basic 4.6
5+
* @brief pos-vol-pid control test
66
*/
77

88
#include <iostream>
@@ -11,7 +11,7 @@
1111
#include "PID.h"
1212

1313
#ifndef M_PI
14-
#define M_PI 3.14159265358979323846
14+
#define M_PI 3.1415927
1515
#endif
1616

1717
// 轮子的直径(单位:米)
@@ -26,6 +26,11 @@ int calculatePulsesForDistance(double distance) {
2626
return static_cast<int>((distance / circumference) * PULSES_PER_REVOLUTION);
2727
}
2828

29+
// 计算速度(假设每秒钟调用一次)
30+
double calculateSpeed(int current_pulses, int previous_pulses, double delta_time) {
31+
return (current_pulses - previous_pulses) / delta_time;
32+
}
33+
2934
int main() {
3035
if (gpioInitialise() < 0) {
3136
std::cerr << "pigpio initialization failed" << std::endl;
@@ -36,45 +41,81 @@ int main() {
3641
shield.begin();
3742

3843
EncoderMotor *motor1 = shield.getEncoderMotor(1);
44+
EncoderMotor *motor2 = shield.getEncoderMotor(2);
3945
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
4050

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
4253

4354
int target_pulses = calculatePulsesForDistance(1.0); // 计算前进1米所需的脉冲数
4455
motor1->resetEncoder();
56+
motor2->resetEncoder();
57+
58+
int previous_pulses1 = 0;
59+
int previous_pulses2 = 0;
60+
double delta_time = 0.1; // 假设每0.1秒进行一次速度计算
4561

4662
motor1->setSpeed(128);
63+
motor2->setSpeed(128);
4764
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) ? "逆时针" : "静止";
6481

6582
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;
72112
}
73113

74-
gpioDelay(100 * 1000); // 延时100毫秒
114+
gpioDelay(static_cast<unsigned int>(delta_time * 1000 * 1000)); // 延时delta_time秒
75115
}
76116

77117
motor1->run(RELEASE);
118+
motor2->run(RELEASE);
78119
gpioTerminate();
79120
return 0;
80121
}
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
/**
2-
*
3-
* @author FENN MAI
4-
* @date 06/05/2024
5-
* @version Basic 4.0
6-
* @brief 编码电机初次测试
7-
*/
8-
9-
101
#include "PID.h"
112

12-
// PID 构造函数
13-
PID::PID(double kp, double ki, double kd) : kp(kp), ki(ki), kd(kd), prev_error(0), integral(0) {}
3+
PID::PID(double kp, double ki, double kd)
4+
: kp(kp), ki(ki), kd(kd), prev_error(0), integral(0) {
5+
}
146

15-
// 计算控制输出
16-
double PID::calculate(double setpoint, double measured_value) {
17-
double error = setpoint - measured_value;
7+
double PID::calculate(double setpoint, double pv) {
8+
double error = setpoint - pv;
189
integral += error;
1910
double derivative = error - prev_error;
2011
prev_error = error;
21-
return kp * error + ki * integral + kd * derivative;
12+
13+
double output = kp * error + ki * integral + kd * derivative;
14+
return output;
2215
}

Basic_Development/New_Car_Control/Encoder_Base/PID.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
/**
2-
*
3-
* @author FENN MAI
4-
* @date 06/05/2024
5-
* @version Basic 4.0
6-
* @brief 编码电机初次测试
7-
*/
8-
9-
#ifndef _PID_H_
10-
#define _PID_H_
1+
#ifndef PID_H
2+
#define PID_H
113

124
class PID {
135
public:
146
PID(double kp, double ki, double kd);
15-
double calculate(double setpoint, double measured_value);
7+
double calculate(double setpoint, double pv);
168

179
private:
1810
double kp;
58 Bytes
Binary file not shown.

Basic_Development/New_Car_Control/System document.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ Raspi_i2c --> Emakefun_MotorDriver -->
3939
20240529:
4040
1. goal: writing code
4141

42-
20240606:
43-
1.
42+
20240607:
43+
1. pos-vol-pid control test
44+
2. motor2 still have bug
45+
3. the pos and pulse have bug
Binary file not shown.

0 commit comments

Comments
 (0)