Skip to content

Commit 1a780fc

Browse files
committed
added pid_controller class
1 parent eef8b46 commit 1a780fc

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed

examples/motor/motor.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void setup(){
1414
t=millis();
1515
t_change=millis();
1616
robot.setRpmRight(reference);
17-
robot.setKPidRight(150, 0.1, 0);
17+
robot.setKPidRight(30.0, 0.1, 0.4);
1818
Serial.print("reference");
1919
Serial.print(" ");
2020
Serial.println("measure");

src/motor_control.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "dcmotor.h"
66
#include "encoder.h"
77
#include "robot_definitions.h"
8+
#include "pid_controller.h"
89

910
#define CONTROL_LIMIT 4095
1011
#define MEM_SIZE 5
@@ -41,6 +42,7 @@ class MotorControl{
4142

4243
DCmotor * motor;
4344
Encoder * encoder;
45+
PidController * vel_pid;
4446
public:
4547
MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp, const float _ki, const float _kd, const float _controller_period){
4648
motor = _motor;
@@ -72,6 +74,7 @@ class MotorControl{
7274
mean=0.0;
7375

7476
conversion_factor = (1/MOTOR_RATIO)/(controller_period);
77+
vel_pid = new PidController(kp,ki,kd,controller_period,CONTROL_LIMIT);
7578
}
7679

7780
void begin(){
@@ -84,6 +87,7 @@ class MotorControl{
8487
bool setRPM(const float ref){
8588
if ((ref<MOTOR_LIMIT)&&(ref>-MOTOR_LIMIT)){
8689
reference = ref;
90+
vel_pid->setReference(reference);
8791
return true;
8892
}
8993
return false;
@@ -159,7 +163,9 @@ class MotorControl{
159163
}
160164
161165
*/
166+
162167

168+
/*
163169
void update(){
164170
measure = encoder->getCount();
165171
encoder->reset();
@@ -175,6 +181,16 @@ class MotorControl{
175181
motor->setSpeed(-actuation);
176182
}
177183
184+
*/
185+
186+
void update(){
187+
measure = encoder->getCount();
188+
encoder->reset();
189+
measure = measure*conversion_factor;
190+
vel_pid->update(measure);
191+
motor->setSpeed(-vel_pid->getControlOutput());
192+
}
193+
178194
void setKP(const float _kp){
179195
kp=_kp;
180196
}

src/pid_controller.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#ifndef __PID_CONTROLLER_H__
2+
#define __PID_CONTROLLER_H__
3+
4+
#include "Arduino.h"
5+
6+
class PidController{
7+
private:
8+
float kp;
9+
float ki;
10+
float kd;
11+
12+
float reference;
13+
float error;
14+
float error_rate;
15+
float previous_error;
16+
float error_sum;
17+
float last_measure;
18+
19+
float ctrl_p;
20+
float ctrl_i;
21+
float ctrl_d;
22+
float ctrl_output;
23+
24+
float ctrl_period;
25+
float ctrl_limit;
26+
27+
28+
public:
29+
PidController(const float _kp, const float _ki, const float _kd, const float _ctrl_period, const int _ctrl_limit = 4095){
30+
kp=_kp;
31+
ki=_ki;
32+
kd=_kd;
33+
ctrl_period=_ctrl_period;
34+
ctrl_limit=_ctrl_limit;
35+
36+
reference=0.0;
37+
error=0.0;
38+
error_rate=0.0;
39+
previous_error=0.0;
40+
error_sum=0.0;
41+
last_measure=0.0;
42+
43+
ctrl_p=0.0;
44+
ctrl_i=0.0;
45+
ctrl_d=0.0;
46+
ctrl_output=0.0;
47+
}
48+
49+
void setReference(const float _reference){
50+
reference=_reference;
51+
}
52+
53+
float getControlOutput(){
54+
return ctrl_output;
55+
}
56+
57+
void setKPid(const float _kp, const float _ki, const float _kd){
58+
kp=_kp;
59+
ki=_ki;
60+
kd=_kd;
61+
}
62+
63+
void setKp(const float _k){
64+
kp=_k;
65+
}
66+
67+
void setKi(const float _k){
68+
ki=_k;
69+
}
70+
71+
void setKd(const float _k){
72+
kd=_k;
73+
}
74+
75+
void update(const float measure){
76+
error = reference - measure;
77+
error_sum += error*ctrl_period;
78+
error_rate = (measure-last_measure)/ctrl_period;
79+
last_measure=measure;
80+
ctrl_p = error*kp;
81+
ctrl_i = checkLimits(error_sum*ki);
82+
ctrl_d = error_rate*kd;
83+
ctrl_output = checkLimits(ctrl_p+ctrl_i-ctrl_d);
84+
}
85+
86+
float checkLimits(float value){
87+
if (value>ctrl_limit){
88+
return ctrl_limit;
89+
}
90+
if (value<-ctrl_limit){
91+
return -ctrl_limit;
92+
}
93+
return value;
94+
}
95+
96+
void reset(){
97+
ctrl_output=0.0;
98+
error=0.0;
99+
error_sum=0.0;
100+
previous_error=0.0;
101+
reference=0.0;
102+
last_measure=0.0;
103+
}
104+
};
105+
106+
107+
108+
#endif

src/sensor_tof_matrix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SensorTofMatrix{
1717
}
1818

1919
int begin(){
20-
wire->begin();
20+
//wire->begin();
2121
sensor->begin();
2222
sensor->init_sensor();
2323
sensor->vl53l7cx_set_resolution(VL53L7CX_RESOLUTION_4X4);

0 commit comments

Comments
 (0)