Skip to content

Commit d51412b

Browse files
committed
isPositionReached in motor_control class
1 parent 905768e commit d51412b

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/definitions/robot_definitions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const float MOTOR_RATIO = MOTOR_CPR*MOTOR_GEAR_RATIO;
3737
#define POSITION_KD_DEFAULT 0.0001
3838
#define POSITION_CONTROL_PERIOD 0.02
3939
#define POSITION_MAX_SPEED 30.0
40+
#define POSITION_THRESHOLD 2.0
4041

4142

4243

src/motor_control/motor_control.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ MotorControl::MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp
3535
pos_controller_period = _pos_controller_period;
3636
pos_max_velocity = _pos_max_velocity;
3737
position_control_enabled = false;
38+
is_position_reached = false;
3839

3940

4041

@@ -168,6 +169,9 @@ void MotorControl::update(){
168169
if (position_control_enabled){
169170
pos_pid->update(position);
170171
setRPM(round(pos_pid->getControlOutput()/10.0)*10);
172+
if (abs(pos_pid->getError())<POSITION_THRESHOLD){
173+
is_position_reached = true;
174+
}
171175
}
172176
}
173177

@@ -210,6 +214,7 @@ void MotorControl::enablePositionControl(){
210214

211215
void MotorControl::disablePositionControl(){
212216
position_control_enabled = false;
217+
is_position_reached = false;
213218
}
214219

215220
bool MotorControl::isPositionControlEnabled(){
@@ -230,5 +235,10 @@ float MotorControl::getPosition(){
230235
void MotorControl::setPosition(const float degree){
231236
pos_pid->setReference(degree);
232237
enablePositionControl();
238+
is_position_reached = false;
239+
}
240+
241+
bool MotorControl::isPositionReached(){
242+
return is_position_reached;
233243
}
234244

src/motor_control/motor_control.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class MotorControl{
4343
float pos_controller_period;
4444
float pos_max_velocity;
4545
bool position_control_enabled;
46+
bool is_position_reached;
4647

4748
float position;
4849
float angle;
@@ -112,6 +113,7 @@ class MotorControl{
112113
void setPosition(const float degree); // set the reference for position control
113114
float getPosition(); // get the actual angle in degrees of motor
114115
void resetPosition(const float p0=0.0); // reset/set the position value
116+
bool isPositionReached();
115117

116118
float getError();
117119

0 commit comments

Comments
 (0)