Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions include/hskylib/subsystems/odometry/odometry_drivebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DrivebaseOdometry : public Odometry {
pros::IMU *imu = nullptr, bool async = true);

void reset() override;
void setPose(const pose_t &newPose) override;

protected:
void updatePose() override;
Expand All @@ -38,6 +39,8 @@ class DrivebaseOdometry : public Odometry {
// Getters
double getLeftEncoderPosition() const;
double getRightEncoderPosition() const;

double headingOffset_ = 0.0;
};

#endif
2 changes: 1 addition & 1 deletion include/hskylib/utils/commands/base_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Command {
virtual void initialize() {}

/**
* Executes the command. This should be a blocking implementation.
* Executes the command. This should be a non-blocking implementation.
*/
virtual void execute() {}

Expand Down
6 changes: 6 additions & 0 deletions include/hskylib/utils/commands/command_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ class CommandRunner {
*/
void run();

/**
* Stops the CommandRunner and any currently running command.
*/
void stop();

~CommandRunner();

private:
std::queue<Command *> commands_;
Command *currentCommand_;
bool isRunning_;
};

#endif
3 changes: 2 additions & 1 deletion src/subsystems/odometry/odometry.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hskylib/subsystems/odometry/odometry.h"
#include <hskylib/utils/pose.h>

Odometry::Odometry(bool async) : async_(async) {}

Expand Down Expand Up @@ -32,7 +33,7 @@ pose_t Odometry::update() {

void Odometry::setPose(const pose_t &newPose) {
poseMutex_.take();
currentPose_ = newPose;
currentPose_ = pose_t(newPose.x, newPose.y, newPose.theta);
poseMutex_.give();
}

Expand Down
22 changes: 18 additions & 4 deletions src/subsystems/odometry/odometry_drivebase.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hskylib/subsystems/odometry/odometry_drivebase.h"
#include "robot_config.hpp"

#include <cmath>

Expand All @@ -15,8 +16,6 @@ DrivebaseOdometry::DrivebaseOdometry(pros::MotorGroup *left,
rightMotorGroup_->set_encoder_units_all(pros::E_MOTOR_ENCODER_DEGREES);
leftMotorGroup_->tare_position_all();
rightMotorGroup_->tare_position_all();

init();
}

void DrivebaseOdometry::reset() {
Expand All @@ -28,6 +27,13 @@ void DrivebaseOdometry::reset() {
rightMotorGroupLastPosDeg_ = 0;
}

void DrivebaseOdometry::setPose(const pose_t &newPose) {
poseMutex_.take();
headingOffset_ = newPose.theta - currentPose_.theta;
currentPose_ = pose_t(newPose.x, newPose.y, newPose.theta);
poseMutex_.give();
}

void DrivebaseOdometry::updatePose() {
double currLeftPosDeg = getLeftEncoderPosition();
double currRightPosDeg = getRightEncoderPosition();
Expand All @@ -47,9 +53,9 @@ void DrivebaseOdometry::updatePose() {
double newTheta = currentPose_.theta;

if (imu_) {
newTheta = -imu_->get_rotation() * DEG_TO_RAD;
newTheta = -imu_->get_rotation() * DEG_TO_RAD + headingOffset_;
} else {
newTheta += (dR - dL) / specs_.trackWidth;
newTheta += (dR - dL) / specs_.trackWidth + headingOffset_;
}

// Normalize to [-pi, pi]
Expand All @@ -59,6 +65,14 @@ void DrivebaseOdometry::updatePose() {

double avgTheta = 0.5 * (currentPose_.theta + newTheta);

double deltaX = dCenter * std::cos(avgTheta);
double deltaY = dCenter * std::sin(avgTheta);

if (std::isnan(deltaX) || std::isnan(deltaY)) {
printf("Warning: NaN detected in odometry update. Skipping pose update.\n");
return;
}

currentPose_.x += dCenter * std::cos(avgTheta);
currentPose_.y += dCenter * std::sin(avgTheta);
currentPose_.theta = newTheta;
Expand Down
17 changes: 14 additions & 3 deletions src/utils/commands/command_runner.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include "hskylib/utils/commands/command_runner.h"
#include <hskylib/utils/commands/base_commands.h>

CommandRunner::CommandRunner(std::queue<Command *> commands)
: commands_(std::move(commands)), currentCommand_(nullptr) {}
: commands_(std::move(commands)), currentCommand_(nullptr) {
isRunning_ = false;
}

void CommandRunner::run() {
while (!commands_.empty()) {
isRunning_ = true;
while (!commands_.empty() && isRunning_) {
currentCommand_ = commands_.front();
currentCommand_->initialize();

while (!currentCommand_->isFinished()) {
while (!currentCommand_->isFinished() && isRunning_) {
currentCommand_->execute();
pros::delay(10);
}
Expand All @@ -20,13 +24,20 @@ void CommandRunner::run() {
}
}

void CommandRunner::stop() {
isRunning_ = false;
}

CommandRunner::~CommandRunner() {
stop();
if (currentCommand_) {
currentCommand_->end();
delete currentCommand_;
currentCommand_ = nullptr;
}
while (!commands_.empty()) {
delete commands_.front();
commands_.pop();
}
}