Skip to content

Commit

Permalink
isPositionReached in motor_control class
Browse files Browse the repository at this point in the history
  • Loading branch information
gbr1 committed Aug 12, 2024
1 parent 905768e commit d51412b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/definitions/robot_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const float MOTOR_RATIO = MOTOR_CPR*MOTOR_GEAR_RATIO;
#define POSITION_KD_DEFAULT 0.0001
#define POSITION_CONTROL_PERIOD 0.02
#define POSITION_MAX_SPEED 30.0
#define POSITION_THRESHOLD 2.0



Expand Down
10 changes: 10 additions & 0 deletions src/motor_control/motor_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ MotorControl::MotorControl(DCmotor * _motor, Encoder * _encoder, const float _kp
pos_controller_period = _pos_controller_period;
pos_max_velocity = _pos_max_velocity;
position_control_enabled = false;
is_position_reached = false;



Expand Down Expand Up @@ -168,6 +169,9 @@ void MotorControl::update(){
if (position_control_enabled){
pos_pid->update(position);
setRPM(round(pos_pid->getControlOutput()/10.0)*10);
if (abs(pos_pid->getError())<POSITION_THRESHOLD){
is_position_reached = true;
}
}
}

Expand Down Expand Up @@ -210,6 +214,7 @@ void MotorControl::enablePositionControl(){

void MotorControl::disablePositionControl(){
position_control_enabled = false;
is_position_reached = false;
}

bool MotorControl::isPositionControlEnabled(){
Expand All @@ -230,5 +235,10 @@ float MotorControl::getPosition(){
void MotorControl::setPosition(const float degree){
pos_pid->setReference(degree);
enablePositionControl();
is_position_reached = false;
}

bool MotorControl::isPositionReached(){
return is_position_reached;
}

2 changes: 2 additions & 0 deletions src/motor_control/motor_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MotorControl{
float pos_controller_period;
float pos_max_velocity;
bool position_control_enabled;
bool is_position_reached;

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

float getError();

Expand Down

0 comments on commit d51412b

Please sign in to comment.