Skip to content

Commit 5d77178

Browse files
committed
update: version 4.6.2 fix the motor bug
1 parent 110e798 commit 5d77178

File tree

8 files changed

+101
-149
lines changed

8 files changed

+101
-149
lines changed

Basic_Development/New_Car_Control/.~lock.Test_Engineering.ods#

Lines changed: 0 additions & 1 deletion
This file was deleted.

Basic_Development/New_Car_Control/Encoder_Base/DC_MotoTest.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/**
1+
/**
22
* @author FENN MAI
3-
* @date 06/05/2024
4-
* @version Basic 4.6
5-
* @brief pos-vol-pid control test
3+
* @date 26/06/2024
4+
* @version Basic 4.6.2
5+
* @brief 左右电机控制测试
6+
* pos-vol-pid control test
67
*/
78

89
#include <iostream>
@@ -51,71 +52,77 @@ int main() {
5152
PID position_pid2(0.1, 0.01, 0.05); // 位置PID motor2
5253
PID speed_pid2(0.1, 0.01, 0.05); // 速度PID motor2
5354

54-
int target_pulses = calculatePulsesForDistance(1.0); // 计算前进1米所需的脉冲数
55+
// 计算前进1米所需的脉冲数
56+
int target_pulses = calculatePulsesForDistance(1.0);
57+
58+
// 必须步骤,重置编码器位置,都设定为0
5559
motor1->resetEncoder();
5660
motor2->resetEncoder();
5761

58-
int previous_pulses1 = 0;
59-
int previous_pulses2 = 0;
60-
double delta_time = 0.1; // 假设每0.1秒进行一次速度计算
61-
62-
motor1->setSpeed(128);
63-
motor2->setSpeed(128);
62+
gpioDelay(2500000);
63+
// 控制左轮(motor1)前进
64+
motor1->setSpeed(50);
6465
motor1->run(FORWARD);
66+
std::cout << "左轮-FORWARD" << std::endl;
67+
68+
// 获取编码器的方向
69+
int dir1 = motor1->getDirection();
70+
std::cout << "左轮方向:" << dir1 << std::endl;
71+
72+
gpioDelay(2500000);
73+
74+
// 获取编码器的方向
75+
dir1 = motor1->getDirection();
76+
std::cout << "左轮方向:" << dir1 << std::endl;
77+
gpioDelay(2500000);
78+
// 停止电机1
79+
motor1->run(BRAKE);
80+
std::cout << "左轮-BRAKE" << std::endl;
81+
gpioDelay(5000000);
82+
83+
// 控制右轮(motor2)前进
84+
motor2->setSpeed(50);
6585
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) ? "逆时针" : "静止";
81-
82-
std::cout << "电机编号:1;理想前进值:" << target_pulses
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;
112-
}
113-
114-
gpioDelay(static_cast<unsigned int>(delta_time * 1000 * 1000)); // 延时delta_time秒
115-
}
116-
117-
motor1->run(RELEASE);
118-
motor2->run(RELEASE);
86+
std::cout << "右轮-FORWARD" << std::endl;
87+
// 获取编码器的方向
88+
int dir2 = motor2->getDirection();
89+
std::cout << "右轮方向:" << dir2 << std::endl;
90+
// 持续5s
91+
gpioDelay(2500000);
92+
// 获取编码器的方向
93+
dir2 = motor2->getDirection();
94+
std::cout << "右轮方向:" << dir2 << std::endl;
95+
gpioDelay(2500000);
96+
// 停止电机2
97+
motor2->run(BRAKE);
98+
std::cout << "右轮-RELEASE" << std::endl;
99+
100+
// // 控制左轮(motor1)后退
101+
// motor1->setSpeed(50);
102+
// motor1->run(BACKWARD);
103+
// std::cout << "Left motor (motor1) running backward at speed 50" << std::endl;
104+
// gpioDelay(5000000);
105+
// motor1->run(RELEASE);
106+
// std::cout << "Left motor (motor1) stopped" << std::endl;
107+
108+
// // 控制右轮(motor2)后退
109+
// motor2->setSpeed(50);
110+
// motor2->run(BACKWARD);
111+
// std::cout << "Right motor (motor2) running backward at speed 50" << std::endl;
112+
// gpioDelay(5000000);
113+
// motor2->run(RELEASE);
114+
// std::cout << "Right motor (motor2) stopped" << std::endl;
115+
116+
// // 控制左右轮同时前进
117+
// motor1->setSpeed(50);
118+
// motor2->setSpeed(50);
119+
// motor1->run(FORWARD);
120+
// motor2->run(FORWARD);
121+
// std::cout << "Both motors running forward at speed 50" << std::endl;
122+
// gpioDelay(5000000);
123+
// motor1->run(RELEASE);
124+
// motor2->run(RELEASE);
125+
// std::cout << "Both motors stopped" << std::endl;
119126
gpioTerminate();
120127
return 0;
121128
}

Basic_Development/New_Car_Control/Encoder_Base/EncoderShield.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,28 @@ void EncoderMotor::run(uint8_t cmd) {
5353
MDIR = cmd;
5454
uint16_t pwm_val = _speed * 16; // Adjusted for the PCA9685 PWM range
5555
switch (cmd) {
56-
// case FORWARD:
57-
// MC->setPin(IN2pin, 0);
58-
// MC->setPWM(IN1pin, pwm_val);
59-
// break;
60-
// case BACKWARD:
61-
// MC->setPin(IN1pin, 0);
62-
// MC->setPWM(IN2pin, pwm_val);
63-
// break;
64-
// case RELEASE:
65-
// MC->setPin(IN1pin, 0);
66-
// MC->setPin(IN2pin, 0);
67-
// break;
68-
// case BRAKE:
69-
// MC->setPin(IN1pin, 1);
70-
// MC->setPin(IN2pin, 1);
71-
// break;
72-
case FORWARD:
56+
// BACKWARD:设置IN2pin为低电平(0),并为IN1pin设置PWM值。
57+
case BACKWARD:
7358
std::cout << "Setting IN2pin: " << static_cast<int>(IN2pin) << " to 0" << std::endl;
7459
std::cout << "Setting PWM IN1pin: " << static_cast<int>(IN1pin) << " to " << pwm_val << std::endl;
7560
MC->setPin(IN2pin, 0);
7661
MC->setPWM(IN1pin, pwm_val);
7762
break;
78-
case BACKWARD:
63+
// FORWARD:设置IN1pin为低电平(0),并为IN2pin设置PWM值。
64+
case FORWARD:
7965
std::cout << "Setting IN1pin: " << static_cast<int>(IN1pin) << " to 0" << std::endl;
8066
std::cout << "Setting PWM IN2pin: " << static_cast<int>(IN2pin) << " to " << pwm_val << std::endl;
8167
MC->setPin(IN1pin, 0);
8268
MC->setPWM(IN2pin, pwm_val);
69+
8370
break;
71+
// RELEASE:将IN1pin和IN2pin都设置为低电平(0),释放电机。
8472
case RELEASE:
8573
std::cout << "Releasing motor" << std::endl;
8674
MC->setPin(IN1pin, 0);
8775
MC->setPin(IN2pin, 0);
8876
break;
77+
// BRAKE:将IN1pin和IN2pin都设置为高电平(1),刹车。
8978
case BRAKE:
9079
std::cout << "Braking motor" << std::endl;
9180
MC->setPin(IN1pin, 1);

Basic_Development/New_Car_Control/Encoder_Base/encoder.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,15 @@ B | | | | | |
1616
*/
1717

1818

19-
2019
#include "encoder.h"
2120

2221
// Encoder 构造函数
23-
Encoder::Encoder(int gpioA, int gpioB, encoderCallback_t callback) {
24-
this->gpioA = gpioA;
25-
this->gpioB = gpioB;
26-
this->callback = callback;
27-
position = 0;
28-
levA = 0;
29-
levB = 0;
30-
lastGpio = -1;
31-
direction = 0;
32-
33-
// 将 gpioA 和 gpioB 引脚设置为输入模式
22+
Encoder::Encoder(int gpioA, int gpioB, encoderCallback_t callback)
23+
: gpioA(gpioA), gpioB(gpioB), position(0), direction(0), levA(0), levB(0), lastGpio(-1), callback(callback) {
3424
gpioSetMode(gpioA, PI_INPUT);
3525
gpioSetMode(gpioB, PI_INPUT);
36-
// 启用了 gpioA 和 gpioB 引脚的上拉电阻。上拉电阻用于确保引脚在未连接或未驱动时具有确定的电平(在这里是高电平)。
3726
gpioSetPullUpDown(gpioA, PI_PUD_UP);
3827
gpioSetPullUpDown(gpioB, PI_PUD_UP);
39-
// gpioA 和 gpioB 引脚设置了一个回调函数 _pulseEx。
40-
// 当引脚的电平发生变化(从高到低或从低到高)时,将调用 _pulseEx 函数。
41-
// 通过这种方式,我们可以检测编码器的脉冲信号
4228
gpioSetAlertFuncEx(gpioA, _pulseEx, this);
4329
gpioSetAlertFuncEx(gpioB, _pulseEx, this);
4430
}
@@ -51,13 +37,14 @@ Encoder::~Encoder() {
5137

5238
// 静态脉冲回调函数
5339
void Encoder::_pulseEx(int gpio, int level, uint32_t tick, void *user) {
54-
Encoder *mySelf = (Encoder *) user;
40+
Encoder *mySelf = static_cast<Encoder *>(user);
5541
mySelf->_pulse(gpio, level, tick);
5642
}
5743

5844
// 实例脉冲回调函数
5945
void Encoder::_pulse(int gpio, int level, uint32_t tick) {
60-
if (gpio == gpioA) levA = level; else levB = level;
46+
if (gpio == gpioA) levA = level;
47+
else levB = level;
6148

6249
if (gpio != lastGpio) {
6350
lastGpio = gpio;
@@ -79,7 +66,7 @@ void Encoder::_pulse(int gpio, int level, uint32_t tick) {
7966
}
8067

8168
// 获取编码器位置
82-
int32_t Encoder::getPosition() {
69+
int Encoder::getPosition() {
8370
return position;
8471
}
8572

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

124
#include <pigpio.h>
135

14-
typedef void (*encoderCallback_t)(int);
6+
typedef void (*encoderCallback_t)(int direction);
157

168
class Encoder {
179
public:
1810
Encoder(int gpioA, int gpioB, encoderCallback_t callback = nullptr);
1911
~Encoder();
20-
int32_t getPosition();
12+
int getPosition();
2113
void reset();
22-
int getDirection(); // 新增方法,获取方向
14+
int getDirection();
2315

2416
private:
2517
int gpioA, gpioB;
26-
int32_t position;
27-
int lastGpio;
18+
int position;
19+
int direction;
2820
int levA, levB;
21+
int lastGpio;
2922
encoderCallback_t callback;
30-
int direction; // 记录方向
3123

32-
static void _pulseEx(int gpio, int level, uint32_t tick, void *user);
3324
void _pulse(int gpio, int level, uint32_t tick);
25+
static void _pulseEx(int gpio, int level, uint32_t tick, void *user);
3426
};
3527

36-
#endif
28+
#endif // ENCODER_H
-995 Bytes
Binary file not shown.

Basic_Development/New_Car_Control/System document.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ Raspi_i2c --> Emakefun_MotorDriver -->
4242
20240607:
4343
1. pos-vol-pid control test
4444
2. motor2 still have bug
45-
3. the pos and pulse have bug
45+
3. the pos and pulse have bug
46+
47+
20240626:
48+
1. 缠了黑色带子的电机--以我的视觉方向平行车头朝向,视觉上是是右边
49+
2. 现在的代码,可以测试可以运行
50+
3. 但是!一个很重要的问题,有个编码器没有变化
51+
4.

0 commit comments

Comments
 (0)