Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the SwerveUtils header #13

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions lib/cpp/subzero/drivetrain/SwerveUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "subzero/drivetrain/SwerveUtils.h"

#include <cmath>
#include <numbers>

using namespace subzero;

double SwerveUtils::StepTowards(double _current, double _target,
having11 marked this conversation as resolved.
Show resolved Hide resolved
double _stepsize) {
if (abs(_current - _target) <= _stepsize) {
return _target;
} else if (_target < _current) {
return _current - _stepsize;
} else {
having11 marked this conversation as resolved.
Show resolved Hide resolved
return _current + _stepsize;
}
}

double SwerveUtils::StepTowardsCircular(double _current, double _target,
double _stepsize) {
_current = WrapAngle(_current);
_target = WrapAngle(_target);

double temp = _target - _current;
double stepDirection = temp > 0 ? 1 : temp < 0 ? -1 : 0;
double difference = abs(_current - _target);

if (difference <= _stepsize) {
return _target;
} else if (difference > std::numbers::pi) {
// Handles the case where the difference between the two angles is shorter
// is less than _stepsize when being measured in the other direction
if (_current + 2 * std::numbers::pi - _target < _stepsize ||
_target + 2 * std::numbers::pi - _current < _stepsize) {
return _target;
} else {
having11 marked this conversation as resolved.
Show resolved Hide resolved
return WrapAngle(_current - stepDirection * _stepsize);
}
} else {
having11 marked this conversation as resolved.
Show resolved Hide resolved
// If the difference is less than pi yet still greater than the step size,
// we just add stepsize to current
return _current + stepDirection * _stepsize;
}
}

double SwerveUtils::AngleDifference(double _angleA, double _angleB) {
double difference = abs(_angleA - _angleB);
return difference > std::numbers::pi ? (2 * std::numbers::pi) - difference
: difference;
}

double SwerveUtils::WrapAngle(double _angle) {
double twoPi = 2 * std::numbers::pi;

if (_angle == twoPi) {
// This case must be handled seperately to avoid floating point errors with
// the floor after division
return 0.0;
} else if (_angle > twoPi) {
double rotations = floor(_angle / twoPi);
// Gets the rotations back into radians and then subtracts angle by the
// amount of full rotations
return _angle - twoPi * rotations;
} else if (_angle < 0.0) {
// Must negate the angle which turns it into a positive value. One more
// rotation is added just in case the result of the floor is zero. Since the
// _angle is negative, adding it has the effect of subtracting it from those
// rotations, giving us a valid positive rotation
double rotations = floor((-_angle) / twoPi) + 1;
return _angle + twoPi * rotations;
} else {
having11 marked this conversation as resolved.
Show resolved Hide resolved
return _angle;
}
}