3686 views|3 replies

654

Posts

26

Resources
The OP
 

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) [Copy link]

 

Earlier we introduced how to obtain the motor speed of the Raspberry Pi and how to use the Raspberry Pi timer. By combining the two and using the control algorithm, we can also control the motor speed.

PID Control Algorithm

Speed control uses the PID algorithm, which is introduced a lot on the Internet. The schematic diagram is shown below.

In practice, PI control can be used, that is, the speed error is multiplied by the Kp coefficient, and then the accumulated speed error is multiplied by the Ki coefficient, and then the two are added together to give the motor control value. The selection of the values of these two coefficients is the difficulty of control.

Coding

#include <stdio.h>
#include <wiringPi.h>
#include <iostream>
#include "Emakefun_MotorShield.h"
#define SPEED_INTEGRAL_MAX 1
float Speed_Kp=255,Speed_Ki=150;
float Set_Speet=0.5;
int A_pin=21;
int B_pin=22;
float Encoder_Integral=0;
long Cnt;
long Cha;
long now,pre_time;
float Speed=0;
float Set_PWM_float=0;
uint8_t Set_PWM=0;
Emakefun_DCMotor *DCmotor1 ;
//电机速度PI控制函数
float Speed_PI(float Now_Speed,float Set_Speet)
{
static float fP;
fP=Set_Speet-Now_Speed;
Encoder_Integral+=fP;
if(Encoder_Integral>SPEED_INTEGRAL_MAX)  Encoder_Integral=SPEED_INTEGRAL_MAX; 
return fP*Speed_Kp+Encoder_Integral*Speed_Ki; 
}
//外部中断回调函数,用来计数电机速度
void Interrupt (void) {
Cnt++;
}
//定时器线程函数
PI_THREAD (timer)
{
for (;;)
{
   now=micros();
 Cha=(now-pre_time);
   Speed=(float)Cnt/Cha*50;
   Cnt=0;
   pre_time= now;
   Set_PWM_float=Speed_PI(Speed, Set_Speet);
   if(Set_PWM_float>255)Set_PWM_float=255;
   if(Set_PWM_float<0)Set_PWM_float=0;
   Set_PWM=(uint8_t)Set_PWM_float;
   DCmotor1->setSpeed(Set_PWM);
  printf ("%ld,%f,%f,%f\n",Cha,Speed,Set_PWM_float,Encoder_Integral);
   delay(100);
}
}
int main () {
  Emakefun_MotorShield Pwm = Emakefun_MotorShield();
  Pwm.begin(50);
 DCmotor1 = Pwm.getMotor(1);
 DCmotor1->setSpeed(0);
 DCmotor1->run(FORWARD);
 wiringPiSetup () ;
 wiringPiISR (A_pin, INT_EDGE_BOTH, &Interrupt) ;
 wiringPiISR (B_pin, INT_EDGE_BOTH, &Interrupt) ;
 piThreadCreate (timer) ;
   while(1) {
   delay(1000);
}
}

Parameter debugging

The parameters in the code are obtained through calculation and debugging, which are introduced below.

Normalize the maximum speed: first let the motor run at full speed, and calculate the maximum speed of the motor at full speed to be about 0.02 (this value has no actual speed meaning), multiply it by 50 to make the maximum speed become 1.

Kp parameter determination: the motor control value is 0 to 255, and the maximum speed is 1, so Kp is directly determined to be 255.

Ki parameter determination: The Ki parameter should not be adjusted continuously. It should respond quickly but not be too large to cause oscillation. After continuous adjustment, 150 is selected.

Determination of integral limit parameters: If the integral is not limited to the maximum value, the value of the integral will be infinitely large, which is obviously not acceptable. The output after the code is run is as shown in the figure below.

The first column of the output is time, the second column is speed, the third column is control amount, and the fourth column is integral value. The ideal situation is that the integral control output is zero after running. Of course, it is also possible to have a value. By giving the wheel some resistance, this value will not exceed 1, so the integral limit parameter is set to 1.

Operation effect

After running, the Raspberry Pi will control the wheels to rotate at the set speed. From the speed printed out, the speed has been maintained well, but the control amount changes dramatically, and the sound can also tell that the wheel speed is fast and slow. However, I can't measure the actual speed of the wheel. I can only know the effect after all the codes are written and actually run on the ground.

question

In the project, the driver library file and the application file are placed in one folder, which wastes space and is not conducive to reuse. If you want to put the driver library in another file, how do you set the header file include path?

Source code

链接已隐藏,如需查看请登录或者注册

链接已隐藏,如需查看请登录或者注册

This post is from Innovation Lab

Latest reply

Summary of "Wanli" Raspberry Pi car: lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn) Table of contents: "Wanli" Raspberry Pi car launched 1. “Wanli” Raspberry Pi car - Establishing a project warehouse 2. "Wanli" Raspberry Pi car - Python learning (using Thonny) 3. "Wanli" Raspberry Pi car - Python learning (timing task) 4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany) 5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer) 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer) 7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer) 8. "Wanli" Raspberry Pi car - socket learning (local communication) 9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication) 11. "Wanli" Raspberry Pi car - socket learning (sent from Android) 12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) 13. "Wanli" Raspberry Pi Car - Accessories Preparation 14 "Wanli" Raspberry Pi car - motor drive learning 15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment) 16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed) 17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging) 18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning 19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging) 20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) 21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control) 22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation 23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen 24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System 25 "Million Miles" Raspberry Pi Car - Nam Wheel Control 26 "Wanli" Raspberry Pi car - program startup 27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address 28 "Wanli" Raspberry Pi car - car assembly 29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode 30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)   Details Published on 2022-3-21 13:38
Personal signatureQQ:252669569
 
 

1w

Posts

204

Resources
From 4
 

Summary of "Wanli" Raspberry Pi car:

lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn)

Table of contents:

"Wanli" Raspberry Pi car launched

1. “Wanli” Raspberry Pi car - Establishing a project warehouse

2. "Wanli" Raspberry Pi car - Python learning (using Thonny)

3. "Wanli" Raspberry Pi car - Python learning (timing task)

4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany)

5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer)

6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)

7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer)

8. "Wanli" Raspberry Pi car - socket learning (local communication)

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication)

10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)

11. "Wanli" Raspberry Pi car - socket learning (sent from Android)

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving)

13. "Wanli" Raspberry Pi Car - Accessories Preparation

14 "Wanli" Raspberry Pi car - motor drive learning

15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment)

16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed)

17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging)

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning

19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging)

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed)

21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control)
22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation

23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen

24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System

25 "Million Miles" Raspberry Pi Car - Nam Wheel Control

26 "Wanli" Raspberry Pi car - program startup

27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address

28 "Wanli" Raspberry Pi car - car assembly

29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode

30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

1w

Posts

25

Resources
2
 

Looking at the title of 20 "Wanli", it is very touching

This post is from Innovation Lab
 
 
 

2w

Posts

74

Resources
3
 

come on:)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list