Sylib
A C++ Library For V5
Loading...
Searching...
No Matches
motor.hpp
1
12#pragma once
13#include "sylib/env.hpp"
14#include "sylib/math.hpp"
15#include "sylib/system.hpp"
16#include "sylib/sylib_apitypes.hpp"
17
18namespace sylib {
19
24 private:
25 sylib::SMAFilter smaFilterVelocity;
26 sylib::SMAFilter smaFilterAccel;
27 sylib::SMAFilter smaFilterJerk;
28 sylib::SMAFilter smaFilterSnap;
29 sylib::MedianFilter medianFilter;
30 sylib::EMAFilter emaFilter;
31 double motorGearing;
32 sylib::SympleDerivativeSolver preFilterAccelSolver;
33 sylib::SympleDerivativeSolver preFilterJerkSolver;
34 sylib::SympleDerivativeSolver preFilterSnapSolver;
35 sylib::SympleDerivativeSolver outputAccelSolver;
36 sylib::SympleDerivativeSolver outputJerkSolver;
37 sylib::SympleDerivativeSolver outputSnapSolver;
38 sylib::RangeExtremaFilter maxFilterAccel;
39
40
41 uint32_t previousInternalMotorClock;
42 double oldMotorTicks;
43 double dN;
44 double dT;
45 double rawVelocity;
46 double motorVoltageTarget;
47 double emaFilteredVelocity;
48 double smaFilteredVelocity;
49 double smaFilteredAccel;
50 double medianFilteredVelocity;
51 double outputVelocity;
52 double preFilterAcceleration;
53 double preFilterJerk;
54 double preFilterSnap;
55 double outputAcceleration;
56 double outputJerk;
57 double outputSnap;
58 double accelCalculatedkA;
59 double maxFilteredAccel;
60 double smaFilteredJerk;
61 double smaFilteredSnap;
62
63
64
65
66
67
68
69
70 public:
78 SylviesPogVelocityEstimator(double motorGearing = 200);
79
88 double getVelocity(double currentMotorTicks, std::uint32_t currentInternalMotorClock);
89
96
105 double getRawVelocity() const;
106
112 double getRawPosition() const;
113
122
132
141
150 double getAcceleration() const;
151
162
171 double getJerk() const;
172
181 double getSnap() const;
182
189 double getCalculatedkA() const;
190
201};
202
206class Motor : private Device {
207 private:
208 const std::uint8_t smart_port;
209 const double gearing;
210 bool reversed;
211 const V5_DeviceT device;
212 void set_motor_controller();
213 sylib::VoltageEstimation motorVoltageEstimator;
214 // Config settings
215
216
217
218 sylib::SylviesPogVelocityEstimator motorVelocityTracker;
219
220 sylib::ProportionalController motorPController;
221 sylib::IntegralController motorIController;
222 sylib::DerivativeController motorDController;
223 sylib::TakeBackHalfController motorTBHController;
224 sylib::SpeedControllerInfo speedController;
225
226 // Updating values
227 uint32_t internalClock;
228 double velocity;
229 double velocity_error;
230 double raw_velocity;
231 double vexos_velocity;
232 double sma_velocity;
233 double acceleration;
234 double position;
235 double temperature;
236 double power;
237 double target_position;
238 double target_velocity;
239 std::int32_t current_draw;
240 std::int32_t direction;
241 std::int32_t efficiency;
242 std::int32_t faults;
243 std::int32_t flags;
244 double torque;
245 std::int32_t voltage;
246 std::int32_t zero_position_flag;
247 std::int32_t stopped;
248 std::int32_t over_temp;
249 std::int32_t over_current;
250 V5MotorBrakeMode brake_mode;
251 std::int32_t current_limit;
252 std::int32_t voltage_limit;
253
254 // Target values
255 SylibMotorControlMode sylib_control_mode;
256 double position_target;
257 double velocity_target;
258 std::int32_t voltage_target;
259
260
261 /*
262 smart_port(smart_port),
263 gearing(gearing),
264 reversed(reverse),
265 device(vexDeviceGetByIndex(smart_port - 1)),
266 motorVoltageEstimator{speedController.kV, gearing},
267 motorPController{speedController.kP,
268 std::shared_ptr<double>(&velocity_error),
269 gearing,
270 true,
271 speedController.kP2,
272 speedController.pRange},
273 motorIController{speedController.kI, std::shared_ptr<double>(&velocity_error), gearing, true,
274 speedController.antiWindupRange},
275 motorDController{speedController.kD, std::shared_ptr<double>(&velocity_error), gearing},
276 motorTBHController{speedController.kH, std::shared_ptr<double>(&velocity_error)},
277 speedController(speedController) {
278 */
279
280 public:
297 Motor(const uint8_t smart_port, const double gearing = 200, const bool reverse = false,
298 const SpeedControllerInfo speedController = SpeedControllerInfo());
299
300 // Background control stuff
301
310 void update() override;
311
312 // Motor control
313
325 void set_position_target_absolute(double new_position, std::int32_t velocity_cap = 200);
326
338 void set_position_target_relative(double new_position, std::int32_t velocity_cap = 200);
339
348 void set_velocity_custom_controller(std::int16_t new_velocity_target);
349
353 void stop();
354
361 void set_voltage(std::int16_t new_voltage_target);
362
369 void set_braking_mode(V5MotorBrakeMode mode);
370
377 void set_amps_limit(std::int32_t limit);
378
385 void set_is_reversed(bool reverse);
386
393 void set_volts_limit(std::int32_t limit);
394
399
408 void set_velocity(std::int32_t new_velocity_target);
409
417
418 // Motor telemetry
419
427 double get_velocity_error() const;
428
434 std::int32_t get_p_voltage() const;
435
441 std::int32_t get_i_voltage() const;
442
448 std::int32_t get_d_voltage() const;
449
455 std::int32_t get_estimator_voltage() const;
456
462 std::int32_t get_tbh_voltage() const;
463
469 double get_position() const;
470
476 double get_position_target() const;
477
485 double get_velocity() const;
486
492 double get_velocity_target() const;
493
502
510 double get_velocity_raw() const;
511
520
528 double get_acceleration() const;
529
535 double get_temperature() const;
536
542 double get_torque() const;
543
549 double get_watts() const;
550
556 std::int32_t get_amps() const;
557
563 std::int32_t get_amps_limit() const;
564
571 std::int32_t get_applied_voltage() const;
572
578 std::int32_t get_volts_limit() const;
579
588 std::int32_t get_efficiency() const;
589
595 std::int32_t get_faults() const;
596
602 std::int32_t get_flags() const;
603
612 std::uint32_t get_device_timestamp() const;
613
621 std::int32_t get_position_and_timestamp(std::uint32_t* timestamp);
622
628 std::int32_t get_braking_mode() const;
629
635 std::int32_t get_gearing() const;
636
642 std::int32_t get_smart_port() const;
643
649 bool is_stopped() const;
650
656 bool is_over_current() const;
657
663 bool is_over_temp() const;
664
670 bool is_reversed() const;
671};
672} // namespace sylib
D Controller.
Definition: math.hpp:582
Device Parent Class.
Definition: system.hpp:238
Exponential Moving Average Filter.
Definition: math.hpp:19
I Controller.
Definition: math.hpp:469
Median Filter.
Definition: math.hpp:124
V5 Smart Motor.
Definition: motor.hpp:206
std::int32_t get_braking_mode() const
Gets the current motor brake mode.
void stop()
Stops the motor.
void set_amps_limit(std::int32_t limit)
Sets the motor amperage limit.
std::int32_t get_tbh_voltage() const
Gets the current output of the user-provided TBH controller.
void set_volts_limit(std::int32_t limit)
Sets the motor voltage limit.
double get_position() const
Gets the current raw motor position.
double get_temperature() const
Gets the motor temperature.
std::int32_t get_volts_limit() const
Gets the user-provided motor voltage limit.
std::int32_t get_flags() const
Gets the motor-reported flags.
void set_voltage(std::int16_t new_voltage_target)
Sets the voltage sent to the motor directly.
bool is_over_temp() const
Gets if the motor is over temperature, as reported by the motor.
Motor(const uint8_t smart_port, const double gearing=200, const bool reverse=false, const SpeedControllerInfo speedController=SpeedControllerInfo())
Creates a smart motor object.
void update() override
Motor update loop.
std::int32_t get_position_and_timestamp(std::uint32_t *timestamp)
Gets the current motor tick count and timestamp value.
std::int32_t get_faults() const
Gets the motor-reported faults.
double get_velocity_error() const
Gets the current error between the motor's actual velocity and the velocity target.
double get_acceleration() const
Gets the motor acceleration.
void set_velocity_custom_controller(std::int16_t new_velocity_target)
Sets the motor velocity target and changes control modes.
std::uint32_t get_device_timestamp() const
Gets the most recent time at which the motor has updated its tick count.
void set_is_reversed(bool reverse)
Sets the motor reversed flag.
double get_position_target() const
Gets the current target motor position.
void set_braking_mode(V5MotorBrakeMode mode)
Sets the motor braking mode.
std::int32_t get_estimator_voltage() const
Gets the current output of the user-provided feedforward controller.
std::int32_t get_d_voltage() const
Gets the current output of the user-provided D controller.
void set_position_target_relative(double new_position, std::int32_t velocity_cap=200)
Sets a motor position target relative to its current position.
std::int32_t get_amps() const
Gets the motor current draw.
void tare_encoder()
Tares the motor encoder.
void set_position_target_absolute(double new_position, std::int32_t velocity_cap=200)
Sets the absolute motor position target.
std::int32_t get_smart_port() const
Gets 1-indexed smart port used by the motor.
std::int32_t get_p_voltage() const
Gets the current output of the user-provided P controller.
bool is_reversed() const
Gets if the motor reversed flag is set.
std::int32_t get_efficiency() const
Gets the motor-reported efficiency value in percent.
bool is_stopped() const
Gets if the motor is stopped.
double get_velocity_motor_reported() const
Gets the current motor velocity as reported by the motor itself.
std::int32_t get_applied_voltage() const
Gets the voltage most recently sent to the motor, when using a sylib control mode.
std::int32_t get_gearing() const
Gets the user-set motor gearing.
double get_velocity() const
Gets the current motor velocity.
void set_velocity(std::int32_t new_velocity_target)
Sets the motor velocity target.
double get_velocity_sma_filter_only() const
Gets the partially-filtered motor velocity.
double get_velocity_target() const
Gets the current motor velocity target.
double get_watts() const
Gets the motor power consumption.
std::int32_t get_i_voltage() const
Gets the current output of the user-provided I controller.
bool is_over_current() const
Gets if the motor is over current, as reported by the motor.
std::int32_t get_amps_limit() const
Gets the user-provided motor current limit.
double get_torque() const
Gets the motor torque.
void updateSpeedController(sylib::SpeedControllerInfo controller)
Sets the custom speed controller used by set_velocity_custom_controller.
double get_velocity_raw() const
Gets the unfiltered motor velocity.
P Controller.
Definition: math.hpp:363
Extrema Filter.
Definition: math.hpp:199
Simple Moving Average Filter.
Definition: math.hpp:62
Accurate Motor Velocity Estimator.
Definition: motor.hpp:23
double getSnap() const
Gets the motor snap.
double getRawPosition() const
Gets the most recent user-provided motor position.
SylviesPogVelocityEstimator(double motorGearing=200)
Creates a velocity estimator for a smart motor.
double getAcceleration() const
Gets the motor acceleration.
double getEmaFilteredVelocity() const
Gets the motor velocity put through a an exponential moving average filter.
double getRawVelocity() const
Gets the raw motor velocity.
double getSmaFilteredVelocity() const
Gets the motor velocity put through a 3-tap simple moving average.
double getPreFilterAcceleration() const
Gets the raw motor acceleration based on a partially filtered velocity calculation.
double getMedianFilteredVelocity() const
Gets the motor velocity put through a median filter.
double getMaxFilteredAcceleration() const
Gets the absolute value of the largest or smallest acceleration measurement in the last 200ms.
double getVelocity(double currentMotorTicks, std::uint32_t currentInternalMotorClock)
Calculates motor velocity.
double getJerk() const
Gets the motor jerk.
double getCalculatedkA() const
Gets the current value of the variable gain used for the velocity EMA filter.
double getVelocityNoCalculations() const
Gets the most recent calculated motor velocity.
Generic Derivative Solver.
Definition: math.hpp:253
TBH Controller.
Definition: math.hpp:664
Feedforward Controller.
Definition: math.hpp:300
Definition: addrled.hpp:20
SylibMotorControlMode
Definition: sylib_apitypes.hpp:14
Custom Velocity Controller.
Definition: math.hpp:743