libstp
Robotics library for Botball
Loading...
Searching...
No Matches
Motor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4
6#include "hal/IMotor.hpp"
8
9namespace libstp::hal::motor
10{
19 class Motor : public IMotor
20 {
21#ifdef SAFETY_CHECKS_ENABLED
22 static inline detail::PortRegistry registry_{};
23
24 static void registerMotorPort(int port);
25
26 static void unregisterMotorPort(int port);
27#endif
28
29 public:
36 explicit Motor(int port, bool inverted, const foundation::MotorCalibration& calibration = {});
37
38 ~Motor() override;
39
41 void setSpeed(int percent) override;
43 void setVelocity(int velocity) override;
45 void moveToPosition(int velocity, int goalPosition) override;
47 void moveRelative(int velocity, int deltaPosition) override;
49 [[nodiscard]] int getPosition() const override;
51 [[nodiscard]] int getBemf() const override;
53 [[nodiscard]] bool isDone() const override;
54
56 void brake() override;
58 void off() override;
60 void resetPositionCounter() override;
61
62 [[nodiscard]] const foundation::MotorCalibration& getCalibration() const override;
63 void setCalibration(const foundation::MotorCalibration& calibration) override;
64 [[nodiscard]] int getPort() const override { return port_; }
65 [[nodiscard]] bool isInverted() const override { return inverted_; }
66
71 void setFirmwarePidGains(float kp, float ki, float kd) override;
72
74 static void disableAll();
76 static void enableAll();
77
78 // --------------------------------------------------------------------
79 // Test/simulation hooks (active on mock platform; no-op on wombat).
80 //
81 // The members are declared in the shared class so test code that holds
82 // a `Motor*` (and the autotune tests) can interrogate / control the
83 // simulated motor without an upcast. On the wombat platform these are
84 // never written and `getBemf()` ignores them.
85 // --------------------------------------------------------------------
86
91 void setSimulatedBemfResponse(int steady_state_bemf, double time_constant_s);
92
95
98 void getLastFirmwarePidGains(float& kp, float& ki, float& kd) const override;
99
100 private:
101 int port_;
102 bool inverted_;
103 foundation::MotorCalibration calibration_;
104
105 // --- Simulation / test state (mock platform writes; wombat ignores) --
106 bool sim_active_{false};
107 int sim_steady_state_{0};
108 double sim_time_constant_s_{0.1};
109 std::chrono::steady_clock::time_point sim_t_ref_{};
110
111 float last_fw_kp_{0.0f};
112 float last_fw_ki_{0.0f};
113 float last_fw_kd_{0.0f};
114 };
115}
const foundation::MotorCalibration & getCalibration() const override
bool isDone() const override
Return whether the last position command has completed.
void setSimulatedBemfResponse(int steady_state_bemf, double time_constant_s)
void brake() override
Actively stop this motor using the platform's safest available path.
Motor(int port, bool inverted, const foundation::MotorCalibration &calibration={})
void clearSimulatedBemfResponse()
Disable the simulated BEMF response (mock platform only).
static void disableAll()
Put every motor managed by the active platform into its disabled state.
void moveRelative(int velocity, int deltaPosition) override
Closed-loop relative move in firmware-specific position units.
void moveToPosition(int velocity, int goalPosition) override
Closed-loop absolute move in firmware-specific position units.
bool isInverted() const override
Definition Motor.hpp:65
static void enableAll()
Re-enable globally disabled motors when the platform supports it.
void setVelocity(int velocity) override
Closed-loop velocity target in firmware-specific BEMF units.
void setCalibration(const foundation::MotorCalibration &calibration) override
void off() override
Disable this motor completely (no power, no brake — free-spinning).
void getLastFirmwarePidGains(float &kp, float &ki, float &kd) const override
void resetPositionCounter() override
Reset the position counter to zero.
int getPort() const override
Definition Motor.hpp:64
int getPosition() const override
Return the current motor position using the driver's sign convention.
int getBemf() const override
Return the driver's current BEMF or velocity-like feedback value.
void setSpeed(int percent) override
Open-loop percent output in the range expected by the platform driver.
void setFirmwarePidGains(float kp, float ki, float kd) override
Calibration constants commonly needed for motor encoder conversions.
Definition motor.hpp:32
Interface for motor abstraction.
Definition IMotor.hpp:17