Skip to content

Commit 4dcd9d8

Browse files
committed
control mode
1 parent cbb0160 commit 4dcd9d8

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

src/motor_control.h

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,30 @@
2828

2929
#define CONTROL_LIMIT 4095
3030
#define MEM_SIZE 5
31+
#define CONTROL_MODE_NORMAL 0
32+
#define CONTROL_MODE_LINEAR 1
3133

3234
class MotorControl{
3335
private:
36+
37+
uint8_t control_mode;
38+
3439
float kp;
3540
float ki;
3641
float kd;
3742

3843
float travel;
3944
float reference;
4045

46+
float trip;
47+
float iterations;
48+
float start_value;
49+
float end_value;
50+
float step_size;
51+
float step;
52+
int step_index;
53+
float interpolation;
54+
4155
float controller_period;
4256
float conversion_factor;
4357
float measure;
@@ -56,10 +70,15 @@ class MotorControl{
5670

5771
PidController * vel_pid;
5872

59-
MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp, const float _ki, const float _kd, const float _controller_period){
73+
MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp, const float _ki, const float _kd,
74+
const float _controller_period,
75+
const uint8_t _control_mode = CONTROL_MODE_LINEAR, const float _step_size=5.0){
6076
motor = _motor;
6177
encoder = _encoder;
6278

79+
control_mode = _control_mode;
80+
81+
6382
kp = _kp;
6483
ki = _ki;
6584
kd = _kd;
@@ -69,6 +88,15 @@ class MotorControl{
6988
travel=0.0;
7089
reference = 0.0;
7190

91+
trip=0.0;
92+
iterations=0.0;
93+
start_value=0.0;
94+
end_value=0.0;
95+
step_size=_step_size;
96+
step=0.0;
97+
step_index=0;
98+
interpolation=0.0;
99+
72100
measure = 0.0;
73101
last_measure = 0.0;
74102

@@ -86,20 +114,16 @@ class MotorControl{
86114
vel_pid->reset();
87115
clearMemory();
88116
}
89-
117+
/*
90118
bool setRPM(const float ref){
91119
if ((ref<MOTOR_LIMIT)&&(ref>-MOTOR_LIMIT)){
92120
reference = ref;
93121
vel_pid->setReference(reference);
94-
/*
95-
if ((reference)<0.1&&(reference>-0.1)){
96-
vel_pid->reset();
97-
}
98-
*/
99122
return true;
100123
}
101124
return false;
102125
}
126+
*/
103127

104128
float checkLimits(float value){
105129
if (value>CONTROL_LIMIT){
@@ -191,6 +215,26 @@ class MotorControl{
191215
192216
*/
193217

218+
219+
bool setRPM(const float ref){
220+
if ((ref<MOTOR_LIMIT)&&(ref>-MOTOR_LIMIT)){
221+
reference = ref;
222+
if (control_mode==CONTROL_MODE_LINEAR){
223+
start_value=interpolation;
224+
end_value=reference;
225+
trip=0.0;
226+
iterations=abs(end_value-start_value)/step_size;
227+
step=1.0/iterations;
228+
step_index=0;
229+
}
230+
else if(control_mode==CONTROL_MODE_NORMAL){
231+
vel_pid->setReference(reference);
232+
}
233+
return true;
234+
}
235+
return false;
236+
}
237+
194238
void update(){
195239

196240
measure = encoder->getCount();
@@ -224,7 +268,25 @@ class MotorControl{
224268
measure = measure*conversion_factor;
225269
*/
226270

227-
271+
272+
//vel_pid->update(measure);
273+
//motor->setSpeed(vel_pid->getControlOutput());
274+
275+
276+
if (control_mode==CONTROL_MODE_LINEAR){
277+
if (step_index<iterations){
278+
step_index++;
279+
trip+=step;
280+
interpolation=trip*(end_value-start_value)+start_value;
281+
if (abs(interpolation)>abs(reference)){
282+
interpolation=reference;
283+
}
284+
}
285+
vel_pid->setReference(interpolation);
286+
}
287+
else if(control_mode==CONTROL_MODE_NORMAL){
288+
vel_pid->setReference(reference);
289+
}
228290

229291

230292
vel_pid->update(measure);
@@ -247,6 +309,22 @@ class MotorControl{
247309
return vel_pid->getError();
248310
}
249311

312+
void brake(){
313+
reference=0.0;
314+
trip=0.0;
315+
iterations=0.0;
316+
start_value=0.0;
317+
end_value=0.0;
318+
step=0.0;
319+
step_index=0;
320+
interpolation=0.0;
321+
clearMemory();
322+
323+
motor->setSpeed(0);
324+
vel_pid->setReference(0.0);
325+
vel_pid->reset();
326+
}
327+
250328

251329
};
252330

src/robot_definitions.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
const float MOTOR_RATIO = MOTOR_CPR*MOTOR_GEAR_RATIO;
3131

32-
#define MOTOR_KP_RIGHT 120.0
33-
#define MOTOR_KI_RIGHT 300.0
34-
#define MOTOR_KD_RIGHT 1.0
32+
#define MOTOR_KP_RIGHT 32.0
33+
#define MOTOR_KI_RIGHT 450.0
34+
#define MOTOR_KD_RIGHT 0.0
35+
36+
//30, 450, 0.0
37+
//120,300,1,0
3538
#define MOTOR_CONTROL_PERIOD 0.02
3639

3740

0 commit comments

Comments
 (0)