Skip to content

Commit

Permalink
pass controllers using shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLuGit committed Feb 13, 2024
1 parent 6fbacfd commit d5851eb
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 30 deletions.
18 changes: 10 additions & 8 deletions include/VOSS/chassis/AbstractChassis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,45 @@

namespace voss::chassis {

using controller_ptr = std::shared_ptr<controller::AbstractController>;

class AbstractChassis {

protected:
pros::Mutex m;
controller::AbstractController* default_controller;
controller_ptr default_controller;

void move_task(controller::AbstractController* controller, double max,
void move_task(controller_ptr controller, double max,
voss::Flags flags, double exitTime);

void turn_task(controller::AbstractController* controller, double max,
void turn_task(controller_ptr controller, double max,
voss::Flags flags, double exitTime);

public:
AbstractChassis(controller::AbstractController& default_controller);
AbstractChassis(controller_ptr default_controller);

virtual void tank(double left_speed, double right_speed) = 0;
virtual void arcade(double forward_speed, double turn_speed) = 0;

virtual bool execute(ChassisCommand cmd, double max) = 0;

void move(Point target, controller::AbstractController* controller,
void move(Point target, controller_ptr controller,
double max = 100.0, voss::Flags flags = voss::Flags::NONE,
double exitTime = 22500);
void move(Pose target, controller::AbstractController* controller,
void move(Pose target, controller_ptr controller,
double max = 100.0, voss::Flags flags = voss::Flags::NONE,
double exitTime = 22500);
void move(Point target, double max = 100.0,
voss::Flags flags = voss::Flags::NONE, double exitTime = 22500);
void move(Pose target, double max = 100.0,
voss::Flags flags = voss::Flags::NONE, double exitTime = 22500);

void turn(double target, controller::AbstractController* controller,
void turn(double target, controller_ptr controller,
double max = 100.0, voss::Flags flags = voss::Flags::NONE,
double exitTime = 22500);
void turn(double target, double max = 100.0,
voss::Flags flags = voss::Flags::NONE, double exitTime = 22500);
void turn_to(Point target, controller::AbstractController* controller,
void turn_to(Point target, controller_ptr controller,
double max = 100.0, voss::Flags flags = voss::Flags::NONE,
double exitTime = 22500);
void turn_to(Point target, double max = 100.0,
Expand Down
2 changes: 1 addition & 1 deletion include/VOSS/chassis/DiffChassis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DiffChassis : public AbstractChassis {
public:
DiffChassis(std::initializer_list<int8_t> left_motors,
std::initializer_list<int8_t> right_motors,
controller::AbstractController& default_controller,
controller_ptr default_controller,
double slew_step = 8);

void tank(double left_speed, double right_speed);
Expand Down
2 changes: 1 addition & 1 deletion include/VOSS/controller/ArcPIDControllerBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ArcPIDControllerBuilder {
ArcPIDControllerBuilder& with_settle_time(double time);
ArcPIDControllerBuilder& with_slew(double slew);

ArcPIDController build();
std::shared_ptr<ArcPIDController> build();
};

} // namespace voss::controller
2 changes: 1 addition & 1 deletion include/VOSS/controller/BoomerangControllerBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BoomerangControllerBuilder {
BoomerangControllerBuilder& with_lead_pct(double lead_pct);
BoomerangControllerBuilder& with_settle_time(double time);

BoomerangController build();
std::shared_ptr<BoomerangController> build();
};

} // namespace voss::controller
2 changes: 1 addition & 1 deletion include/VOSS/controller/PIDControllerBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PIDControllerBuilder {
PIDControllerBuilder& with_min_error(double error);
PIDControllerBuilder& with_settle_time(double time);

PIDController build();
std::shared_ptr<PIDController> build();
};

} // namespace voss::controller
2 changes: 1 addition & 1 deletion include/VOSS/controller/SwingControllerBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SwingControllerBuilder {
SwingControllerBuilder& with_settle_time(double time);
// SwingControllerBuilder& with_slew(double slew);

SwingController build();
std::shared_ptr<SwingController> build();
};

} // namespace voss::controller
16 changes: 8 additions & 8 deletions src/VOSS/chassis/AbstractChassis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace voss::chassis {

AbstractChassis::AbstractChassis(
controller::AbstractController& default_controller) {
this->default_controller = &default_controller;
controller_ptr default_controller) {
this->default_controller = default_controller;
}

void AbstractChassis::move_task(controller::AbstractController* controller,
void AbstractChassis::move_task(controller_ptr controller,
double max, voss::Flags flags,
double exitTime) {
int t = 0;
Expand Down Expand Up @@ -42,7 +42,7 @@ void AbstractChassis::move_task(controller::AbstractController* controller,
// this->m.give();
}

void AbstractChassis::turn_task(controller::AbstractController* controller,
void AbstractChassis::turn_task(controller_ptr controller,
double max, voss::Flags flags,
double exitTime) {
int t = 0;
Expand Down Expand Up @@ -83,14 +83,14 @@ void AbstractChassis::move(Pose target, double max, voss::Flags flags,
}

void AbstractChassis::move(Point target,
controller::AbstractController* controller,
controller_ptr controller,
double max, voss::Flags flags, double exitTime) {
Pose pose_target = Pose{target.x, target.y, 361};
this->move(pose_target, controller, max, flags, exitTime);
}

void AbstractChassis::move(Pose target,
controller::AbstractController* controller,
controller_ptr controller,
double max, voss::Flags flags, double exitTime) {
// this->m.take();

Expand All @@ -105,7 +105,7 @@ void AbstractChassis::turn(double target, double max, voss::Flags flags,
}

void AbstractChassis::turn(double target,
controller::AbstractController* controller,
controller_ptr controller,
double max, voss::Flags flags, double exitTime) {
// this->m.take();

Expand All @@ -121,7 +121,7 @@ void AbstractChassis::turn_to(Point target, double max, voss::Flags flags,
}

void AbstractChassis::turn_to(Point target,
controller::AbstractController* controller,
controller_ptr controller,
double max, voss::Flags flags, double exitTime) {
// this->m.take();

Expand Down
2 changes: 1 addition & 1 deletion src/VOSS/chassis/DiffChassis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ double DiffChassis::slew(double target, bool is_left) {

DiffChassis::DiffChassis(std::initializer_list<int8_t> left_motors,
std::initializer_list<int8_t> right_motors,
controller::AbstractController& default_controller,
controller_ptr default_controller,
double slew_step)
: AbstractChassis(default_controller) {
this->left_motors = std::make_unique<pros::MotorGroup>(left_motors);
Expand Down
4 changes: 2 additions & 2 deletions src/VOSS/controller/ArcPIDControllerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ ArcPIDControllerBuilder& ArcPIDControllerBuilder::with_slew(double slew) {
return *this;
}

ArcPIDController ArcPIDControllerBuilder::build() {
return this->ctrl;
std::shared_ptr<ArcPIDController> ArcPIDControllerBuilder::build() {
return std::make_shared<ArcPIDController>(this->ctrl);
}

} // namespace voss::controller
4 changes: 2 additions & 2 deletions src/VOSS/controller/BoomerangControllerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ BoomerangControllerBuilder::with_settle_time(double time) {
return *this;
}

BoomerangController BoomerangControllerBuilder::build() {
return this->ctrl;
std::shared_ptr<BoomerangController> BoomerangControllerBuilder::build() {
return std::make_shared<BoomerangController>(this->ctrl);
}

} // namespace voss::controller
4 changes: 2 additions & 2 deletions src/VOSS/controller/PIDControllerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ PIDControllerBuilder& PIDControllerBuilder::with_settle_time(double time) {
return *this;
}

PIDController PIDControllerBuilder::build() {
return this->ctrl;
std::shared_ptr<PIDController> PIDControllerBuilder::build() {
return std::make_shared<PIDController>(this->ctrl);
}

} // namespace voss::controller
4 changes: 2 additions & 2 deletions src/VOSS/controller/SwingControllerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ SwingControllerBuilder& SwingControllerBuilder::with_settle_time(double time) {
return *this;
}

SwingController SwingControllerBuilder::build() {
return this->ctrl;
std::shared_ptr<SwingController> SwingControllerBuilder::build() {
return std::make_shared<SwingController>(this->ctrl);
}

} // namespace voss::controller
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void opcontrol() {
if (master.get_digital_new_press(DIGITAL_Y)) {
odom->set_pose(voss::Pose{0.0, 0.0, 0});
chassis.turn(180, 100, voss::Flags::NONE, 10000);
chassis.turn(0, swing);
}

pros::lcd::clear_line(1);
Expand Down

0 comments on commit d5851eb

Please sign in to comment.