safe_vex/
rotation.rs

1//! # Rotation Sensor API
2
3use crate::{bindings, error::{PROSErr, PROSResult}, port::SmartPort};
4
5/// Resets the Rotation Sensor
6///
7/// Reset the current absolute position to be the same as the Rotation Sensor angle
8///
9/// # Errors
10/// 
11/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
12pub fn reset(port: SmartPort) -> Result<(), PROSErr> {
13    unsafe {
14        bindings::rotation_reset(port as u8)
15    }.check().map(|_| ())
16}
17
18/// Sets the Rotation Sensor's refresh interval in milliseconds
19///
20/// The rate may be specified in increments of 5ms, and will be rounded down to the nearest increment. The minimum allowable refresh rate is 5ms. The default rate is 10ms.
21///
22/// # Errors
23/// 
24/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
25pub fn set_data_rate(port: SmartPort, rate: u32) -> Result<(), PROSErr> {
26    unsafe {
27        bindings::rotation_set_data_rate(port as u8, rate)
28    }.check().map(|_| ())
29}
30
31/// Sets the Rotation Sensor position reading to a desired rotation value
32///
33/// # Errors
34/// 
35/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
36pub fn set_position(port: SmartPort, position: u32) -> Result<(), PROSErr> {
37    unsafe {
38        bindings::rotation_set_position(port as u8, position)
39    }.check().map(|_| ())
40}
41
42/// Resets the Rotation Sensor position to 0
43///
44/// # Errors
45/// 
46/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
47pub fn reset_position(port: SmartPort) -> Result<(), PROSErr> {
48    unsafe {
49        bindings::rotation_reset_position(port as u8)
50    }.check().map(|_| ())
51}
52
53/// Gets the Rotation Sensor's current position in centidegrees
54///
55/// # Errors
56/// 
57/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
58pub fn get_position(port: SmartPort) -> Result<i32, PROSErr> {
59    unsafe {
60        bindings::rotation_get_position(port as u8)
61    }.check()
62}
63
64/// Gets the Rotation Sensor's current velocity in centidegrees per second
65///
66/// # Errors
67/// 
68/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
69pub fn get_velocity(port: SmartPort) -> Result<i32, PROSErr> {
70    unsafe {
71        bindings::rotation_get_velocity(port as u8)
72    }.check()
73}
74
75/// Gets the Rotation Sensor's current angle in centidegrees (0-36000)
76///
77/// # Errors
78/// 
79/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
80pub fn get_angle(port: SmartPort) -> Result<i32, PROSErr> {
81    unsafe {
82        bindings::rotation_get_angle(port as u8)
83    }.check()
84}
85
86/// Sets the Rotation Sensor reversed flag
87///
88/// # Errors
89/// 
90/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
91pub fn set_reversed(port: SmartPort, is_reversed: bool) -> Result<(), PROSErr> {
92    unsafe {
93        bindings::rotation_set_reversed(port as u8, is_reversed)
94    }.check().map(|_| ())
95}
96
97/// Gets the Rotation Sensor's reversed flag
98///
99/// # Errors
100/// 
101/// Returns `PROSErr::NoDev` if the port cannot be configured as a Rotation Sensor
102pub fn get_reversed(port: SmartPort) -> Result<bool, PROSErr> {
103    unsafe {
104        bindings::rotation_get_reversed(port as u8)
105    }.check().map(|x| x != 0)
106}