pros_devices/
competition.rs

1//! Utilities for getting what state of the competition the robot is in.
2
3use pros_sys::misc::{COMPETITION_AUTONOMOUS, COMPETITION_CONNECTED, COMPETITION_DISABLED};
4
5// TODO: change this to use PROS' internal version once we switch to PROS 4.
6const COMPETITION_SYSTEM: u8 = 1 << 3;
7
8/// Represents a possible mode that robots can be set in during the competition lifecycle.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CompetitionMode {
11    /// The Disabled competition mode.
12    ///
13    /// When in disabled mode, voltage commands to motors are disabled. Motors are forcibly
14    /// locked to the "coast" brake mode and cannot be moved.
15    ///
16    /// Robots may be placed into disabled mode at any point in the competition after
17    /// connecting, but are typically disabled before the autonomous period, between
18    /// autonomous and opcontrol periods, and following the opcontrol period of a match.
19    Disabled,
20
21    /// The Autonomous competition mode.
22    ///
23    /// When in autonomous mode, all motors and sensors may be accessed, however user
24    /// input from controller buttons and joysticks is not available to be read.
25    ///
26    /// Robots may be placed into autonomous mode at any point in the competition after
27    /// connecting, but are typically placed into this mode at the start of a match.
28    Autonomous,
29
30    /// The Opcontrol competition mode.
31    ///
32    /// When in opcontrol mode, all device access is available including access to
33    /// controller joystick values for reading user-input from drive team members.
34    ///
35    /// Robots may be placed into opcontrol mode at any point in the competition after
36    /// connecting, but are typically placed into this mode following the autonomous
37    /// period.
38    Opcontrol,
39}
40
41/// Represents a type of system used to control competition state.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum CompetitionSystem {
44    /// Competition state is controlled by a VEX Field Controller.
45    FieldControl,
46
47    /// Competition state is controlled by a VEXnet competition switch.
48    CompetitionSwitch,
49}
50
51/// Gets the current competition mode, or phase.
52pub fn mode() -> CompetitionMode {
53    let status = unsafe { pros_sys::misc::competition_get_status() };
54
55    if status & COMPETITION_DISABLED != 0 {
56        CompetitionMode::Disabled
57    } else if status & COMPETITION_AUTONOMOUS != 0 {
58        CompetitionMode::Autonomous
59    } else {
60        CompetitionMode::Opcontrol
61    }
62}
63
64/// Checks if the robot is connected to a competition control system.
65pub fn connected() -> bool {
66    let status = unsafe { pros_sys::misc::competition_get_status() };
67
68    status & COMPETITION_CONNECTED != 0
69}
70
71/// Gets the type of system currently controlling the robot's competition state, or [`None`] if the robot
72/// is not tethered to a competition controller.
73pub fn system() -> Option<CompetitionSystem> {
74    let status = unsafe { pros_sys::misc::competition_get_status() };
75
76    if status & COMPETITION_CONNECTED != 0 {
77        if status & COMPETITION_SYSTEM == 0 {
78            Some(CompetitionSystem::FieldControl)
79        } else {
80            Some(CompetitionSystem::CompetitionSwitch)
81        }
82    } else {
83        None
84    }
85}