RaidZeroLib
Coroutine robot framework for VEX V5
Loading...
Searching...
No Matches
MotorDevice.hpp
1#pragma once
2#include "RaidZeroLib/api/Hardware/Device.hpp"
3#include "RaidZeroLib/api/Hardware/Direction.hpp"
4#include "RaidZeroLib/api/Hardware/Motor.hpp"
5#include "RaidZeroLib/api/Hardware/Sensor.hpp"
6#include "RaidZeroLib/api/Units/Units.hpp"
7
8namespace rz {
9
11class MotorDevice final : public Device, public Motor {
12 public:
13 enum class Cartridge {
14 Red,
15 Green,
16 Blue,
17 };
18
24
25 explicit MotorDevice(std::uint8_t port) : MotorDevice(port, Config{}) {}
26 MotorDevice(std::uint8_t port, Config config);
27
28 void setVoltage(Voltage voltage) override;
29 [[nodiscard]] bool connected() const override { return deviceConnected(); }
30
32 [[nodiscard]] Sensor<Angle>& encoder() noexcept { return encoderSensor; }
33
35 [[nodiscard]] Sensor<AngularVelocity>& velocity() noexcept { return velocitySensor; }
36
37 private:
38 class Encoder final : public Sensor<Angle> {
39 public:
40 explicit Encoder(const MotorDevice& motor) : motor(motor) {}
41 [[nodiscard]] bool connected() const override { return motor.deviceConnected(); }
42
43 protected:
44 [[nodiscard]] Angle raw() const override { return motor.position; }
45 [[nodiscard]] std::uint32_t epoch() const override { return motor.deviceEpoch(); }
46
47 private:
48 const MotorDevice& motor;
49 };
50
51 class Velocity final : public Sensor<AngularVelocity> {
52 public:
53 explicit Velocity(const MotorDevice& motor) : motor(motor) {}
54 [[nodiscard]] bool connected() const override { return motor.deviceConnected(); }
55
56 protected:
57 [[nodiscard]] AngularVelocity raw() const override { return motor.speed; }
58 [[nodiscard]] std::uint32_t epoch() const override { return motor.deviceEpoch(); }
59
60 private:
61 const MotorDevice& motor;
62 };
63
64 bool poll() override;
65 void onConnect() override;
66
67 Config config;
68 Angle position{};
69 AngularVelocity speed{};
70 Encoder encoderSensor{*this};
71 Velocity velocitySensor{*this};
72};
73
74} // namespace rz
bool deviceConnected() const noexcept
Whether this tick's read succeeded.
Definition Device.hpp:23
std::uint8_t port() const noexcept
Definition Device.hpp:20
Device(std::uint8_t port, std::string_view kind)
V5 Smart Motor. Its built-in encoder is exposed as sensors on the same snapshot.
Definition MotorDevice.hpp:11
void setVoltage(Voltage voltage) override
Commands a voltage, clamped to the motor's range. Positive drives the mechanism forward.
Sensor< AngularVelocity > & velocity() noexcept
Output shaft velocity as estimated by the motor.
Definition MotorDevice.hpp:35
bool connected() const override
Definition MotorDevice.hpp:29
Sensor< Angle > & encoder() noexcept
Output shaft angle (after the cartridge).
Definition MotorDevice.hpp:32
Cartridge
Definition MotorDevice.hpp:13
@ Green
200 rpm
Definition MotorDevice.hpp:15
MotorDevice(std::uint8_t port)
Definition MotorDevice.hpp:25
MotorDevice(std::uint8_t port, Config config)
Motor()=default
A reading from hardware, or computed from other sensors.
Definition Sensor.hpp:25
virtual bool connected() const=0
Direction
A way a shaft can turn, seen looking at the face the shaft comes out of.
Definition Direction.hpp:6
@ Counterclockwise
Definition Direction.hpp:8
au::QuantityD< au::UnitQuotientT< au::Radians, au::Seconds > > AngularVelocity
Definition Units.hpp:14
au::QuantityD< au::Volts > Voltage
Definition Units.hpp:15
au::QuantityD< au::Radians > Angle
Definition Units.hpp:13
Definition DriverControl.hpp:11
Definition MotorDevice.hpp:19
Cartridge cartridge
Definition MotorDevice.hpp:20
Direction positive
The way the output shaft turns for a positive voltage, and the way the encoder counts up.
Definition MotorDevice.hpp:22