Skip to content

Commit

Permalink
Added util to convert encoders and millimeters per second
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Aug 16, 2024
1 parent 8aa973f commit 436d392
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/APPCalib/src/MotorSpeedCalibrationState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ void MotorSpeedCalibrationState::finishCalibration(StateMachine& sm)
}
else
{
int32_t maxSpeed32 =
static_cast<int32_t>(maxSpeed) * 1000 / static_cast<int32_t>(RobotConstants::ENCODER_STEPS_PER_M);
int32_t maxSpeed32 = Util::stepsPerSecondToMillimetersPerSecond(maxSpeed);

LOG_INFO_VAL("Calibrated max. speed (steps/s): ", maxSpeed);
LOG_INFO_VAL("Calibrated max. speed (mm/s): ", maxSpeed32);
Expand Down
2 changes: 1 addition & 1 deletion lib/APPLineFollower/src/MotorSpeedCalibrationState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void MotorSpeedCalibrationState::finishCalibration(StateMachine& sm)
}
else
{
int32_t maxSpeed32 = static_cast<int32_t>(maxSpeed) * 1000 / static_cast<int32_t>(RobotConstants::ENCODER_STEPS_PER_M);
int32_t maxSpeed32 = Util::stepsPerSecondToMillimetersPerSecond(maxSpeed);

LOG_INFO_VAL("Calibrated max. speed (steps/s): ", maxSpeed);
LOG_INFO_VAL("Calibrated max. speed (mm/s): ", maxSpeed32);
Expand Down
12 changes: 12 additions & 0 deletions lib/Service/src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Includes
*****************************************************************************/
#include <Util.h>
#include <RobotConstants.h>

/******************************************************************************
* Compiler Switches
Expand Down Expand Up @@ -210,6 +211,17 @@ bool Util::isButtonTriggered(IButton& button, bool& lastState)
return isTriggered;
}

int32_t Util::stepsPerSecondToMillimetersPerSecond(int16_t speedStepsPerSec)
{
return (static_cast<int32_t>(speedStepsPerSec) * 1000 / static_cast<int32_t>(RobotConstants::ENCODER_STEPS_PER_M));
}

int16_t Util::millimetersPerSecondToStepsPerSecond(int32_t speedMmPerSec)
{
int32_t speedStepsPerSec = speedMmPerSec * static_cast<int32_t>(RobotConstants::ENCODER_STEPS_PER_M);
return (static_cast<int16_t>(speedStepsPerSec / 1000));
}

/******************************************************************************
* Local Functions
*****************************************************************************/
30 changes: 24 additions & 6 deletions lib/Service/src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,53 @@ namespace Util

/**
* Divide and round.
*
*
* @param[in] numerator The numerator.
* @param[in] denominator The denominator.
*
*
* @return Result
*/
uint32_t divRoundUp(uint32_t numerator, uint32_t denominator);

/**
* Divide and round.
*
*
* @param[in] numerator The numerator.
* @param[in] denominator The denominator.
*
*
* @return Result
*/
int32_t divRoundUp(int32_t numerator, int32_t denominator);

/**
* Is button triggered?
* Triggered means a pressed/released change.
*
*
* @param[in] button The button.
* @param[inout] lastState The last button state.
*
*
* @return If button is triggered, it will return true otherwise false.
*/
bool isButtonTriggered(IButton& button, bool& lastState);

/**
* Convert a speed in encoder steps per second to a speed in mm/s.
*
* @param[in] speedStepsPerSec Speed in encoder steps per second
*
* @return Speed in mm/s
*/
int32_t stepsPerSecondToMillimetersPerSecond(int16_t speedStepsPerSec);

/**
* Convert a speed in mm/s to a speed in encoder steps per second.
*
* @param[in] speedMmPerSec Speed in mm/s
*
* @return Speed in encoder steps per second
*/
int16_t millimetersPerSecondToStepsPerSecond(int32_t speedMmPerSec);

} /* namespace Util */

#endif /* UTIL_H */
Expand Down

0 comments on commit 436d392

Please sign in to comment.