From a50919b22aae410572a5097d0c4ef9938e8bc7f1 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:15:28 -0500 Subject: [PATCH 1/7] refactor: make `AdiEncoder` return `Position` --- CHANGELOG.md | 1 + packages/pros-devices/src/adi/encoder.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8942371..9a64a91d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Before releasing: - 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) +- AdiEncoder now returns `Position` rather than just degrees. ### Removed diff --git a/packages/pros-devices/src/adi/encoder.rs b/packages/pros-devices/src/adi/encoder.rs index ee0ae8f9..27aa0824 100644 --- a/packages/pros-devices/src/adi/encoder.rs +++ b/packages/pros-devices/src/adi/encoder.rs @@ -3,6 +3,8 @@ use pros_core::bail_on; use pros_sys::{ext_adi_encoder_t, PROS_ERR}; +use crate::Position; + use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort}; /// ADI encoder device. @@ -49,10 +51,12 @@ impl AdiEncoder { } /// Gets the number of ticks recorded by the encoder. - pub fn position(&self) -> Result { - Ok(bail_on!(PROS_ERR, unsafe { + pub fn position(&self) -> Result { + let degrees = bail_on!(PROS_ERR, unsafe { pros_sys::adi_encoder_get(self.raw) - })) + }); + + Ok(Position::from_degrees(degrees as f64)) } } From 7e7f4f1b97717b793de2ba973f9150b0b669114c Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:17:05 -0500 Subject: [PATCH 2/7] docs: more complete changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a64a91d..8c8eb379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ Before releasing: - 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) -- AdiEncoder now returns `Position` rather than just degrees. +- AdiEncoder now returns `Position` rather than just degrees (**Breaking Change**) (#106). ### Removed From 5f36c186ad9c4c2dd62cfa1c333cf50fbb170fdf Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:17:41 -0500 Subject: [PATCH 3/7] chore: fmt :trollface: --- packages/pros-devices/src/adi/encoder.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/pros-devices/src/adi/encoder.rs b/packages/pros-devices/src/adi/encoder.rs index 27aa0824..da46c0d9 100644 --- a/packages/pros-devices/src/adi/encoder.rs +++ b/packages/pros-devices/src/adi/encoder.rs @@ -3,9 +3,8 @@ use pros_core::bail_on; use pros_sys::{ext_adi_encoder_t, PROS_ERR}; -use crate::Position; - use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort}; +use crate::Position; /// ADI encoder device. /// Requires two adi ports. @@ -52,9 +51,7 @@ impl AdiEncoder { /// Gets the number of ticks recorded by the encoder. pub fn position(&self) -> Result { - let degrees = bail_on!(PROS_ERR, unsafe { - pros_sys::adi_encoder_get(self.raw) - }); + let degrees = bail_on!(PROS_ERR, unsafe { pros_sys::adi_encoder_get(self.raw) }); Ok(Position::from_degrees(degrees as f64)) } From 33c49593a1345d68bb0fe6af34a57eb112246835 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:21:04 -0500 Subject: [PATCH 4/7] fix: safe initialization context of robot structs --- packages/pros-async/src/lib.rs | 6 ++++-- packages/pros-sync/src/lib.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/pros-async/src/lib.rs b/packages/pros-async/src/lib.rs index 6d1beb30..004a082f 100644 --- a/packages/pros-async/src/lib.rs +++ b/packages/pros-async/src/lib.rs @@ -185,8 +185,9 @@ macro_rules! async_robot { #[no_mangle] extern "C" fn initialize() { + let robot = Default::default(); unsafe { - ROBOT = Some(Default::default()); + ROBOT = Some(robot); } } }; @@ -195,8 +196,9 @@ macro_rules! async_robot { #[no_mangle] extern "C" fn initialize() { + let robot = $init; unsafe { - ROBOT = Some($init); + ROBOT = Some(robot); } } }; diff --git a/packages/pros-sync/src/lib.rs b/packages/pros-sync/src/lib.rs index 6f7df18a..6c252308 100644 --- a/packages/pros-sync/src/lib.rs +++ b/packages/pros-sync/src/lib.rs @@ -123,8 +123,9 @@ macro_rules! sync_robot { #[no_mangle] extern "C" fn initialize() { + let robot = Default::default(); unsafe { - ROBOT = Some(Default::default()); + ROBOT = Some(robot); } } }; @@ -133,8 +134,9 @@ macro_rules! sync_robot { #[no_mangle] extern "C" fn initialize() { + let robot = $init; unsafe { - ROBOT = Some($init); + ROBOT = Some(robot); } } }; From 5f39db3413ef247fbbbd9c281adabcca07da1294 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:22:35 -0500 Subject: [PATCH 5/7] docs: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c8eb379..28215727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Before releasing: ### Fixed - `pros_sys` bindings to the Motors C API now takes the correct port type (`i8`) as of PROS 4 (**Breaking Change**) (#66). +- Fixed the unintended `unsafe` context present in the `sync_robot` and `async_robot` family of macros (**Breaking Change**) (#107). ### Changed From d5339b2a887b47a191c463f13ae3939668bd9f74 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+Tropix126@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:33:56 -0500 Subject: [PATCH 6/7] fix: add `Controller` to prelude --- CHANGELOG.md | 2 +- packages/pros/src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8942371..042eb696 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ Before releasing: - 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`. - +- Added `Controller` API to the `pros::prelude` module. (#108) ### Fixed - `pros_sys` bindings to the Motors C API now takes the correct port type (`i8`) as of PROS 4 (**Breaking Change**) (#66). diff --git a/packages/pros/src/lib.rs b/packages/pros/src/lib.rs index e14accd7..de971ad9 100644 --- a/packages/pros/src/lib.rs +++ b/packages/pros/src/lib.rs @@ -100,6 +100,7 @@ pub mod prelude { peripherals::{DynamicPeripherals, Peripherals}, position::Position, screen::{Circle, Line, Rect, Screen, Text, TextFormat, TextPosition, TouchState}, + controller::Controller, smart::{ distance::DistanceSensor, expander::AdiExpander, From 435cb5968df985e403b89df904e9441c447f07cd Mon Sep 17 00:00:00 2001 From: Gavin-Niederman Date: Wed, 13 Mar 2024 15:43:34 -0700 Subject: [PATCH 7/7] chore: fmt --- CHANGELOG.md | 1 + packages/pros/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 042eb696..5ddcf8c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Before releasing: - 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`. - Added `Controller` API to the `pros::prelude` module. (#108) + ### Fixed - `pros_sys` bindings to the Motors C API now takes the correct port type (`i8`) as of PROS 4 (**Breaking Change**) (#66). diff --git a/packages/pros/src/lib.rs b/packages/pros/src/lib.rs index de971ad9..9f10b46f 100644 --- a/packages/pros/src/lib.rs +++ b/packages/pros/src/lib.rs @@ -97,10 +97,10 @@ pub mod prelude { AdiDevice, AdiPort, }, color::Rgb, + controller::Controller, peripherals::{DynamicPeripherals, Peripherals}, position::Position, screen::{Circle, Line, Rect, Screen, Text, TextFormat, TextPosition, TouchState}, - controller::Controller, smart::{ distance::DistanceSensor, expander::AdiExpander,