Skip to content

Commit e31cc31

Browse files
committed
pid cleanup
1 parent 7a2b411 commit e31cc31

File tree

3 files changed

+124
-83
lines changed

3 files changed

+124
-83
lines changed

src/motor_control/pid_controller.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2023 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "pid_controller.h"
13+
14+
PidController::PidController(const float _kp, const float _ki, const float _kd, const float _ctrl_period, const int _ctrl_limit){
15+
kp=_kp;
16+
ki=_ki;
17+
kd=_kd;
18+
ctrl_period=_ctrl_period;
19+
ctrl_limit=_ctrl_limit;
20+
21+
reference=0.0;
22+
error=0.0;
23+
error_rate=0.0;
24+
previous_error=0.0;
25+
error_sum=0.0;
26+
last_measure=0.0;
27+
28+
ctrl_p=0.0;
29+
ctrl_i=0.0;
30+
ctrl_d=0.0;
31+
ctrl_output=0.0;
32+
}
33+
34+
void PidController::setReference(const float _reference){
35+
reference=_reference;
36+
}
37+
38+
float PidController::getControlOutput(){
39+
return ctrl_output;
40+
}
41+
42+
float PidController::getError(){
43+
return error;
44+
}
45+
46+
void PidController::setKPid(const float _kp, const float _ki, const float _kd){
47+
kp=_kp;
48+
ki=_ki;
49+
kd=_kd;
50+
}
51+
52+
void PidController::setKp(const float _k){
53+
kp=_k;
54+
}
55+
56+
void PidController::setKi(const float _k){
57+
ki=_k;
58+
}
59+
60+
void PidController::setKd(const float _k){
61+
kd=_k;
62+
}
63+
64+
void PidController::update(const float measure){
65+
error = reference - measure;
66+
error_sum += error*ctrl_period;
67+
error_rate = (measure-last_measure)/ctrl_period;
68+
last_measure=measure;
69+
ctrl_p = error*kp;
70+
ctrl_i = checkLimits(error_sum*ki);
71+
ctrl_d = error_rate*kd;
72+
ctrl_output = checkLimits(ctrl_p+ctrl_i-ctrl_d);
73+
}
74+
75+
float PidController::checkLimits(float value){
76+
if (value>ctrl_limit){
77+
return ctrl_limit;
78+
}
79+
if (value<-ctrl_limit){
80+
return -ctrl_limit;
81+
}
82+
return value;
83+
}
84+
85+
void PidController::reset(){
86+
error=0.0;
87+
error_sum=0.0;
88+
previous_error=0.0;
89+
error_rate=0.0;
90+
reference=0.0;
91+
last_measure=0.0;
92+
ctrl_p=0.0;
93+
ctrl_i=0.0;
94+
ctrl_d=0.0;
95+
ctrl_output=0.0;
96+
}

src/motor_control/pid_controller.h

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -38,89 +38,23 @@ class PidController{
3838

3939

4040
public:
41-
PidController(const float _kp, const float _ki, const float _kd, const float _ctrl_period, const int _ctrl_limit = 4095){
42-
kp=_kp;
43-
ki=_ki;
44-
kd=_kd;
45-
ctrl_period=_ctrl_period;
46-
ctrl_limit=_ctrl_limit;
47-
48-
reference=0.0;
49-
error=0.0;
50-
error_rate=0.0;
51-
previous_error=0.0;
52-
error_sum=0.0;
53-
last_measure=0.0;
54-
55-
ctrl_p=0.0;
56-
ctrl_i=0.0;
57-
ctrl_d=0.0;
58-
ctrl_output=0.0;
59-
}
60-
61-
void setReference(const float _reference){
62-
reference=_reference;
63-
}
64-
65-
float getControlOutput(){
66-
return ctrl_output;
67-
}
68-
69-
float getError(){
70-
return error;
71-
}
72-
73-
void setKPid(const float _kp, const float _ki, const float _kd){
74-
kp=_kp;
75-
ki=_ki;
76-
kd=_kd;
77-
}
78-
79-
void setKp(const float _k){
80-
kp=_k;
81-
}
82-
83-
void setKi(const float _k){
84-
ki=_k;
85-
}
86-
87-
void setKd(const float _k){
88-
kd=_k;
89-
}
90-
91-
void update(const float measure){
92-
error = reference - measure;
93-
error_sum += error*ctrl_period;
94-
error_rate = (measure-last_measure)/ctrl_period;
95-
last_measure=measure;
96-
ctrl_p = error*kp;
97-
ctrl_i = checkLimits(error_sum*ki);
98-
ctrl_d = error_rate*kd;
99-
ctrl_output = checkLimits(ctrl_p+ctrl_i-ctrl_d);
100-
}
101-
102-
float checkLimits(float value){
103-
if (value>ctrl_limit){
104-
return ctrl_limit;
105-
}
106-
if (value<-ctrl_limit){
107-
return -ctrl_limit;
108-
}
109-
return value;
110-
}
111-
112-
void reset(){
113-
error=0.0;
114-
error_sum=0.0;
115-
previous_error=0.0;
116-
error_rate=0.0;
117-
reference=0.0;
118-
last_measure=0.0;
119-
ctrl_p=0.0;
120-
ctrl_i=0.0;
121-
ctrl_d=0.0;
122-
ctrl_output=0.0;
123-
}
41+
PidController(const float _kp, const float _ki, const float _kd, const float _ctrl_period, const int _ctrl_limit = 4095);
42+
43+
void setReference(const float _reference);
44+
45+
float getControlOutput();
46+
float getError();
47+
48+
void setKPid(const float _kp, const float _ki, const float _kd);
49+
void setKp(const float _k);
50+
void setKi(const float _k);
51+
void setKd(const float _k);
52+
53+
void update(const float measure);
54+
55+
float checkLimits(float value);
56+
57+
void reset();
12458
};
12559

12660

src/utilities/rgb_led.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2023 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
112
#include "rgb_led.h"
213

314
RGBled::RGBled(const uint8_t _Red, const uint8_t _Green, const uint8_t _Blue){

0 commit comments

Comments
 (0)