Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

refactor(breaking): Motor API #66

Merged
merged 38 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c706b04
refactor: Motor API
Tropix126 Jan 19, 2024
87bfbea
docs: update changelog
Tropix126 Jan 19, 2024
86654f0
Merge remote-tracking branch 'upstream/main' into refactor/motor
Tropix126 Jan 19, 2024
2d60528
docs: fix examples
Tropix126 Jan 19, 2024
6a3714b
Merge branch 'main' into refactor/motor
Tropix126 Feb 26, 2024
5f2c100
feat: (hopefully) full coverage of the PROS motor api
Tropix126 Mar 1, 2024
97f1f12
chore: fmt
Tropix126 Mar 1, 2024
c45a047
docs: reword warning
Tropix126 Mar 1, 2024
12ec99d
docs: add comments for missing functions
Tropix126 Mar 1, 2024
42caf61
feat: set_profiled_velocity
Tropix126 Mar 1, 2024
1625a58
refactor/docs: clean up MotorTuningConstants
Tropix126 Mar 1, 2024
5f4f173
refactor: various subjective API tweaks
Tropix126 Mar 2, 2024
d522bca
docs: document `voltage_limit` unit uncertainty
Tropix126 Mar 2, 2024
391ed9c
Merge remote-tracking branch 'upstream/main' into refactor/motor
Tropix126 Mar 2, 2024
68a7404
fix: `dangerous_motor_tuning` feature flag
Tropix126 Mar 2, 2024
42d1614
refactor: rename `MotorTarget` to `MotorControl`
Tropix126 Mar 6, 2024
427ba08
Merge branch 'main' into refactor/motor
Tropix126 Mar 6, 2024
c2f96c1
refactor: make `voltage_limit` non-optional
Tropix126 Mar 6, 2024
9a0aedd
Merge branch 'refactor/motor' of https://github.com/Tropix126/pros-rs…
Tropix126 Mar 6, 2024
b6924b2
refactor: use `bitflags` crate for status structs, deprecate MotorSta…
Tropix126 Mar 6, 2024
a88c9c4
refactor: `InertialStatus` bitflags
Tropix126 Mar 6, 2024
a70fa58
refactor: `Direction` enum
Tropix126 Mar 6, 2024
cf9899a
refactor: switch to i8 port indexes in `pros_sys`
Tropix126 Mar 7, 2024
cb7c6d0
refactor: use coast braking on velocity/position control
Tropix126 Mar 7, 2024
b25cd5f
docs: update description
Tropix126 Mar 7, 2024
eda3dd2
fix: remove `voltage_limit` until kernel fix
Tropix126 Mar 8, 2024
5ad9b43
refactor: rename `rotate_to_position` to `set_position_target`
Tropix126 Mar 8, 2024
86125ae
feat: add max RPM getter for `Gearset`
Tropix126 Mar 8, 2024
0555f6f
docs: fix example
Tropix126 Mar 8, 2024
f71cfea
docs: update changelog
Tropix126 Mar 8, 2024
a7f7dd3
refactor: make `efficiency` return 0.0-1.0
Tropix126 Mar 8, 2024
127f7e2
feat: add TPR getter
Tropix126 Mar 8, 2024
460c982
refactor: raw_position timestamp outvalue
Tropix126 Mar 8, 2024
d34a4f3
chore: fmt :]
Tropix126 Mar 8, 2024
1d7cdf7
refactor: `SmartDeviceTimestamp`
Tropix126 Mar 8, 2024
10d8eaf
chore: fmt 🪈
Tropix126 Mar 8, 2024
ad92aa9
chore: remove extraneous dlmalloc dep
Tropix126 Mar 11, 2024
8fa3eaf
fix: typo in changelog
Tropix126 Mar 13, 2024
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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,39 @@ Before releasing:
## [Unreleased]

### Added
- New Motor API (**Breaking Change**) (#66)
- Added `MotorControl` enum for controlling motor targets (#66).
- Added support for onboard velocity and position control in motors (#66).
- Added missing motor telemetry functions `velocity`, `efficiency`, `gearset`, `current_limit`.
- Added support for current and voltage limiting in motors.
- Added `Motor::raw_position` for getting raw IME ticks at a given timestamp.
- Added support for getting motor fault flags (e.g. over-temperature, over-current, H-bridge faults).
- Added support for internal motor PID tuning. Feature gated behind `dangerous_motor_tuning`, as this can cause hardware damage and is not recommended.
- Added various constants for convenience around `Motor` and `Gearset`.

### Fixed

- `pros_sys` bindings to the Motors C API now takes the correct port type (`i8`) as of PROS 4 (**Breaking Change**) (#66).

### Changed

- Refactored the Motor API (**Breaking Change**) (#66)
- Adjusts constructor arguments for `Motor` to allow passing `Gearset` and a direction instead of `brake_mode` at construction. (**Breaking Change**) (#66)
- Renamed `Motor::get_state` to `Motor::state`. (**Breaking Change**) (#66)
- Changed `Motor::reversed` to return `Result<bool, _>`` rather than just `false` if `PROS_ERR` is returned. (**Breaking Change**) (#66)
- Adjusted motor targeting to work around the `MotorControl` enum.
- Adjusted motor reverse flag to use `Direction` enum rather than a boolean.
- Motor braking is now stateless and requires an explicit method to be called to use `BrakeMode`s other than `BrakeMode::Coast`.
- Renamed `Motor::current_draw` to `Motor::current`.
- Renamed `Motor::get_state` to `Motor::status`.
- Status structs containing device bits now use the `bitflags!` crate. (**Breaking Change**) (#66)
- Renamed `InertialSensor::calibrating` to `InertialSensor::calibrating` (**Breaking CHange**) (#66)

### Removed

- Removed `MotorStoppedFuture`, as it was broken due to hardware not returning the `stopped` flag (**Breaking Change**) (#66).
- Removed `Motor::set_output` and `Motor::set_raw_output` in favor of `set_voltage`.

## [0.8.0]

### Added
Expand All @@ -40,6 +66,8 @@ Before releasing:
- Added `AdiSolenoid`, a wrapper over `AdiDigitalOut` for actuating SMC pneumatic solenoids. (#61)
- Added `AdiSwitch`, another `AdiDigitalOut` wrapper that abstracts bumper switches and limit switches. (#61)
- Added `AdiLineTracker` for abstracting the EDR line tracker sensor.
- Implements TryFrom for Gearset.
Tropix126 marked this conversation as resolved.
Show resolved Hide resolved
- Adds support for getting brake modes from motors. (#66)

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions packages/pros-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ snafu = { version = "0.8.0", default-features = false, features = [
"unstable-core-error",
] }
no_std_io = { version = "0.6.0", features = ["alloc"] }
bitflags = "2.4.2"

[lints]
workspace = true

[features]
dangerous_motor_tuning = []
33 changes: 14 additions & 19 deletions packages/pros-devices/src/smart/imu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
time::Duration,
};

use bitflags::bitflags;
use pros_core::{
bail_on,
error::{take_errno, FromErrno, PortError},
Expand Down Expand Up @@ -56,8 +57,8 @@ impl InertialSensor {
}

/// Check if the Intertial Sensor is currently calibrating.
pub fn calibrating(&mut self) -> Result<bool, InertialError> {
Ok(self.status()?.calibrating())
pub fn is_calibrating(&mut self) -> Result<bool, InertialError> {
Ok(self.status()?.contains(InertialStatus::CALIBRATING))
}

/// Get the total number of degrees the Inertial Sensor has spun about the z-axis.
Expand Down Expand Up @@ -103,7 +104,11 @@ impl InertialSensor {

/// Read the inertial sensor's status code.
pub fn status(&self) -> Result<InertialStatus, InertialError> {
unsafe { pros_sys::imu_get_status(self.port.index()).try_into() }
let bits = bail_on!(pros_sys::E_IMU_STATUS_ERROR, unsafe {
pros_sys::imu_get_status(self.port.index())
});

Ok(InertialStatus::from_bits_retain(bits))
}

/// Get a quaternion representing the Inertial Sensor’s orientation.
Expand Down Expand Up @@ -376,22 +381,12 @@ impl TryFrom<pros_sys::imu_raw_s> for InertialRaw {
}
}

/// Represents a status code returned by the Inertial Sensor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InertialStatus(pub u32);

impl InertialStatus {
/// Determine if the sensor is currently calibrating.
pub const fn calibrating(&self) -> bool {
self.0 & pros_sys::E_IMU_STATUS_CALIBRATING != 0
}
}

impl TryFrom<pros_sys::imu_status_e_t> for InertialStatus {
Tropix126 marked this conversation as resolved.
Show resolved Hide resolved
type Error = InertialError;

fn try_from(value: pros_sys::imu_status_e_t) -> Result<Self, Self::Error> {
Ok(Self(bail_on!(pros_sys::E_IMU_STATUS_ERROR, value)))
bitflags! {
/// The status bits returned by an [`InertialSensor`].
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct InertialStatus: u32 {
/// The sensor is currently calibrating.
const CALIBRATING = pros_sys::E_IMU_STATUS_CALIBRATING;
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/pros-devices/src/smart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod optical;
pub mod rotation;
pub mod vision;

use core::fmt;

pub use distance::DistanceSensor;
pub use expander::AdiExpander;
pub use gps::GpsSensor;
Expand Down Expand Up @@ -238,3 +240,23 @@ impl From<SmartDeviceType> for pros_sys::apix::v5_device_e_t {
value as _
}
}

/// Represents a timestamp on a smart device's internal clock. This type offers
/// no guarantees that the device's clock is in sync with the internal clock of
/// the brain, and thus cannot be safely compared with [`pros_core::time::Instant`]s.
///
/// There is additionally no guarantee that this is in sync with other smart devices,
/// or even the same device if a disconnect occurred causing the clock to reset. As such,
/// this is effectively a newtype wrapper of `u32`.
///
/// # Precision
///
/// This type has a precision of 1 millisecond.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SmartDeviceTimestamp(pub u32);

impl fmt::Debug for SmartDeviceTimestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
Loading
Loading