Skip to content

Commit

Permalink
Pass structs by const ref
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnmp committed May 15, 2024
1 parent 19be6cc commit f32e508
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
18 changes: 4 additions & 14 deletions src/control/SwerveController.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "SwerveController.h"

#include "../Constants.h"
#include "../Globals.h"

using namespace control;
using robot::types::motorid_t;
Expand All @@ -21,11 +20,6 @@ SwerveController::setTurnInPlaceCmdVel(double dtheta, const swerve_rots_t& wheel
swerve_commands_t SwerveController::setTurnInPlaceCmdVel(double dtheta,
const swerve_rots_t& wheel_rots,
double& scaleFactor) const {
if (dtheta == 0) {
scaleFactor = 0;
return {0.0, 0.0, 0.0, 0.0};
}

if (!checkWheelRotation(DriveMode::TurnInPlace, wheel_rots)) {
scaleFactor = 0;
return {0.0, 0.0, 0.0, 0.0};
Expand Down Expand Up @@ -63,11 +57,6 @@ swerve_commands_t SwerveController::setCrabCmdVel(double dtheta, double dy,
swerve_commands_t SwerveController::setCrabCmdVel(double dtheta, double dy,
const swerve_rots_t& wheel_rots,
double& scaleFactor) const {
if (Globals::E_STOP && (dtheta != 0 || dy != 0)) {
scaleFactor = 0;
return {0.0, 0.0, 0.0, 0.0};
}

if (!checkWheelRotation(DriveMode::Crab, wheel_rots)) {
scaleFactor = 0;
return {0.0, 0.0, 0.0, 0.0};
Expand All @@ -91,7 +80,8 @@ swerve_commands_t SwerveController::setCrabCmdVel(double dtheta, double dy,
return {rPWM, rPWM, lPWM, lPWM};
}

bool SwerveController::checkWheelRotation(DriveMode mode, swerve_rots_t wheel_rots) const {
bool SwerveController::checkWheelRotation(DriveMode mode,
const swerve_rots_t& wheel_rots) const {
std::array<int, 4> rots = {wheel_rots.lfRot, wheel_rots.rfRot, wheel_rots.lbRot,
wheel_rots.rbRot};
swerve_rots_t target = getSteerRots(mode);
Expand Down Expand Up @@ -135,12 +125,12 @@ DriveMode SwerveController::getDriveMode() const {
return driveMode;
}

swerve_rots_t SwerveController::setDriveMode(DriveMode mode) const {
swerve_rots_t SwerveController::setDriveMode(DriveMode mode) {
driveMode = mode;
return getSteerRots(mode);
}

void SwerveController::setOverride(bool override) const {
void SwerveController::setOverride(bool override) {
override_steer_check = override;
}

Expand Down
10 changes: 5 additions & 5 deletions src/control/SwerveController.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class SwerveController {
*
* @param override Override the wheel rotation check.
*/
void setOverride(bool override) const;
void setOverride(bool override);

/**
* @brief Get the current drive mode of the controller.
Expand All @@ -147,7 +147,7 @@ class SwerveController {
* @param mode The new drive mode to set.
* @return swerve_rots_t The wheel steer rotations associated with this drive mode.
*/
swerve_rots_t setDriveMode(DriveMode mode) const;
swerve_rots_t setDriveMode(DriveMode mode);

/**
* @brief Check to see if all wheels are at their target position.
Expand All @@ -157,7 +157,7 @@ class SwerveController {
* @return bool if the wheels are within `STEER_EPSILON` of their
* target position OR if `overide_steer_check` is true.
*/
bool checkWheelRotation(DriveMode mode, swerve_rots_t wheel_rots) const;
bool checkWheelRotation(DriveMode mode, const swerve_rots_t& wheel_rots) const;

/**
* @brief Get the wheel steer rotations for a given drive mode in millidegrees.
Expand All @@ -172,13 +172,13 @@ class SwerveController {
/**
* @brief The current drive mode.
*/
mutable DriveMode driveMode;
DriveMode driveMode;

/**
* @brief Whether or not wheel rotation check should be ignored when
* doing swerve-based driving.
*/
mutable bool override_steer_check;
bool override_steer_check;
};
} // namespace control

Expand Down
4 changes: 2 additions & 2 deletions src/network/MissionControlTasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static control::swerve_rots_t getAllSteerRotations();
/**
* Set the steer power of all wheels with a swerve_commands_t.
*/
static void setAllSteerPowers(control::swerve_commands_t commands);
static void setAllSteerPowers(const control::swerve_commands_t& commands);

PowerRepeatTask::PowerRepeatTask()
: util::PeriodicTask<>(Constants::JOINT_POWER_REPEAT_PERIOD,
Expand Down Expand Up @@ -233,7 +233,7 @@ static control::swerve_rots_t getAllSteerRotations() {
robot::getMotorPos(motorid_t::rearRightWheel).getData()};
}

static void setAllSteerPowers(control::swerve_commands_t commands) {
static void setAllSteerPowers(const control::swerve_commands_t& commands) {
robot::setMotorPower(motorid_t::frontLeftWheel, commands.lfPower);
robot::setMotorPower(motorid_t::frontRightWheel, commands.rfPower);
robot::setMotorPower(motorid_t::rearLeftWheel, commands.lbPower);
Expand Down

0 comments on commit f32e508

Please sign in to comment.