vex_rt/
robot.rs

1//! For use with the [`entry!`](crate::entry!) macro.
2
3use crate::{io::println, peripherals::Peripherals, rtos::Context, state_machine};
4
5/// A trait representing a competition-ready VEX Robot.
6pub trait Robot: Send + Sync + 'static {
7    /// Runs at startup, constructing your robot. This should be non-blocking,
8    /// since the FreeRTOS scheduler doesn't start until it returns.
9    fn new(peripherals: Peripherals) -> Self;
10
11    /// Runs immediately after [`Robot::new`]. The FreeRTOS scheduler is running
12    /// by this point.
13    ///
14    /// The purpose of this method is to provide a hook to run things on startup
15    /// which require a reference to all or part of the robot structure; since
16    /// it takes `&'static self` as its parameter, the lifetime of the robot
17    /// object is guaranteed to be static (i.e., forever), and so the
18    /// implementation may pass references around (e.g., to new tasks) at will
19    /// without issue.
20    fn initialize(&mut self, _ctx: Context) {}
21
22    /// Runs during the autonomous period.
23    fn autonomous(&mut self, _ctx: Context) {
24        println!("autonomous");
25    }
26
27    /// Runs during the opcontrol period.
28    fn opcontrol(&mut self, _ctx: Context) {
29        println!("opcontrol");
30    }
31
32    /// Runs when the robot is disabled.
33    fn disabled(&mut self, _ctx: Context) {
34        println!("disabled");
35    }
36}
37
38state_machine! {
39    /// Competition state machine.
40    pub Competition<R: Robot>(robot: R) {
41        robot: R = robot,
42    } = initialize;
43
44    /// Runs on initialization.
45    initialize(ctx) [robot] {
46        robot.initialize(ctx);
47    }
48
49    /// Runs during the autonomous period.
50    autonomous(ctx) [robot] {
51        robot.autonomous(ctx);
52    }
53
54    /// Runs during the opcontrol period.
55    opcontrol(ctx) [robot] {
56        robot.opcontrol(ctx);
57    }
58
59    /// Runs when the robot is disabled.
60    disabled(ctx) [robot] {
61        robot.disabled(ctx);
62    }
63}