Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Made the basic command architecture for the robot!
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah-Ribbens committed Feb 6, 2024
1 parent 12f9650 commit 00219c0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions commands_architecture/command.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
struct Command {}

/// A implied abstract class that all commands must implement. Each command must define the four methods below to execute
/// their functionality
impl Command {
pub fn new() -> Self {
Command {}
}

pub fn start(&self) {}
pub fn run_every_cycle(&self) {}
pub fn has_finished(&self) -> bool { return false; }
pub fn finish(&self) {}
}
67 changes: 67 additions & 0 deletions commands_architecture/commands_runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
struct CommandsRunner {
commands: Vec<Command>,
current_state: RobotStates,
}

/// In case we want to do different things in different robot modes!
enum RobotStates {
Autonomous,
Teleoperated,
Test,
Disabled
}

/// Runs all the commands of the robot
impl CommandsRunner {
pub fn new() -> Self {
CommandsRunner {
commands: Vec::new(),
}
}

/// Changes the commands that should be run
pub fn change_commands(&mut self, new_commands: Vec<Command>) {
self.commands = new_commands;
}

/// Runs all the robot states
pub fn run(&self) {
match (self.current_state) {
// All the robot states should run the commands at this moment besides disabled, which will stop all motors
RobotStates::Autonomous => self.run_commands(),
RobotStates::Teleoperated => self.run_commands(),
RobotStates::Test => self.run_commands(),
}
}

pub fn set_current_state(&mut self, new_state: RobotStates) {
self.current_state = new_state;

match (self.current_state) {
RobotStates::Disabled => {
// Stop all motors and other processes, so no more commands exist
self.commands.clear(); // Nothing else should be running on the robot
}
}
}

/// Runs all the commands of the robot
pub fn run_commands(&self) {
if (self.commands.len() == 0) { return 0; }
let current_command = self.commands[0];

// Let's run the current command
if (current_command.has_finished()) {
current_command.finish();
self.commands.remove(0);

// Now let's start the next command
if (self.commands.len() > 0) {
self.commands[0].start();
}
// If the command has not finished, let's run it
} else {
current_command.run_every_cycle();
}
}
}

0 comments on commit 00219c0

Please sign in to comment.