pros_devices/smart/
gps.rs1use pros_core::{bail_on, error::PortError, map_errno};
7use pros_sys::{PROS_ERR, PROS_ERR_F};
8use snafu::Snafu;
9
10use super::{SmartDevice, SmartDeviceType, SmartPort};
11
12#[derive(Default, Debug, Clone, Copy, PartialEq)]
14pub struct GpsStatus {
16 pub x: f64,
18 pub y: f64,
20 pub pitch: f64,
22 pub roll: f64,
24 pub yaw: f64,
26 pub heading: f64,
28
29 pub accel_x: f64,
31 pub accel_y: f64,
33 pub accel_z: f64,
35}
36
37#[derive(Debug, Eq, PartialEq)]
39pub struct GpsSensor {
40 port: SmartPort,
41}
42
43impl GpsSensor {
44 pub fn new(port: SmartPort) -> Result<Self, GpsError> {
46 unsafe {
47 bail_on!(
48 PROS_ERR,
49 pros_sys::gps_initialize_full(port.index(), 0.0, 0.0, 0.0, 0.0, 0.0)
50 );
51 }
52
53 Ok(Self { port })
54 }
55
56 pub fn set_offset(&mut self, x: f64, y: f64) -> Result<(), GpsError> {
58 unsafe {
59 bail_on!(PROS_ERR, pros_sys::gps_set_offset(self.port.index(), x, y));
60 }
61 Ok(())
62 }
63
64 pub fn rms_error(&self) -> Result<f64, GpsError> {
66 Ok(unsafe { bail_on!(PROS_ERR_F, pros_sys::gps_get_error(self.port.index())) })
67 }
68
69 pub fn status(&self) -> Result<GpsStatus, GpsError> {
71 unsafe {
72 let status = pros_sys::gps_get_status(self.port.index());
73 bail_on!(PROS_ERR_F, status.x);
74 let accel = pros_sys::gps_get_accel(self.port.index());
75 bail_on!(PROS_ERR_F, accel.x);
76 let heading = bail_on!(PROS_ERR_F, pros_sys::gps_get_heading(self.port.index()));
77
78 Ok(GpsStatus {
79 x: status.x,
80 y: status.y,
81 pitch: status.pitch,
82 roll: status.roll,
83 yaw: status.yaw,
84 heading,
85
86 accel_x: accel.x,
87 accel_y: accel.y,
88 accel_z: accel.z,
89 })
90 }
91 }
92
93 pub fn zero_rotation(&mut self) -> Result<(), GpsError> {
95 unsafe {
96 bail_on!(PROS_ERR, pros_sys::gps_tare_rotation(self.port.index()));
97 }
98 Ok(())
99 }
100}
101
102impl SmartDevice for GpsSensor {
103 fn port_index(&self) -> u8 {
104 self.port.index()
105 }
106
107 fn device_type(&self) -> SmartDeviceType {
108 SmartDeviceType::Gps
109 }
110}
111
112#[derive(Debug, Snafu)]
113pub enum GpsError {
115 StillCalibrating,
117 #[snafu(display("{source}"), context(false))]
118 Port {
120 source: PortError,
122 },
123}
124
125map_errno! {
126 GpsError {
127 EAGAIN => Self::StillCalibrating,
128 }
129 inherit PortError;
130}