This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DriveTrain' into master
- Loading branch information
Showing
24 changed files
with
1,462 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,3 +311,5 @@ fabric.properties | |
|
||
/rmc-beaglebone-core-2018-2019.iml | ||
/.idea/ | ||
/RMC_Logs/ | ||
RMC_Logs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
[package] | ||
name = "rmc-beaglebone-core-2018-2019" | ||
version = "0.1.0" | ||
authors = ["noah <nomaxx117@gmail.com>"] | ||
authors = ["Noah Kennedy <noah[email protected]>", "Isaiah Rondeau <isaiahrondeau@gmail.com>", "Wallace Watler <[email protected]>", "Cody Nettesheim <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
chrono = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use std::sync::mpsc::Receiver; | ||
use std::sync::mpsc::Sender; | ||
use std::sync::mpsc::TryRecvError; | ||
use std::thread::spawn; | ||
|
||
use crate::comms::Communicator; | ||
use crate::comms::driver_station::parsing::*; | ||
use crate::comms::SendableMessage; | ||
use crate::drive_train::DriveTrainCommand; | ||
use crate::logging::log_data::LogData; | ||
|
||
mod parsing; | ||
|
||
const ADDRESS: &str = "127.0.0.1"; | ||
const PORT: u16 = 2401; | ||
|
||
|
||
pub struct DriverStationComms { | ||
message_queue: Receiver<Box<SendableMessage>>, | ||
logging_channel: Sender<LogData>, | ||
communicator: Communicator, | ||
drive_train_channel: Sender<DriveTrainCommand>, | ||
} | ||
|
||
impl DriverStationComms { | ||
/// Instantiates the comms. | ||
/// This constructor will bind the listener. | ||
pub fn new(logging_channel: Sender<LogData>, message_queue: Receiver<Box<SendableMessage>>, | ||
drive_train_channel: Sender<DriveTrainCommand>) -> DriverStationComms { | ||
let communicator = Communicator::from(ADDRESS, PORT); | ||
|
||
DriverStationComms { | ||
message_queue, | ||
logging_channel, | ||
communicator, | ||
drive_train_channel, | ||
} | ||
} | ||
|
||
/// Starts the comms in a <b>new</b> thread. | ||
pub fn start(mut self) { | ||
spawn(move || { | ||
loop { | ||
self.run(); | ||
} | ||
}); | ||
} | ||
|
||
fn run(&mut self) { | ||
self.check_connections(); | ||
self.receive_messages(); | ||
self.send_messages(); | ||
} | ||
|
||
fn check_connections(&mut self) { | ||
if let Err(error) = self.communicator.check_connections() { | ||
self.logging_channel.send(error).unwrap(); | ||
} | ||
} | ||
|
||
fn send_messages(&mut self) { | ||
match self.message_queue.try_recv() { | ||
Ok(message) => self.send_message(message.as_ref()), | ||
Err(try_error) => { | ||
if let TryRecvError::Disconnected = try_error { | ||
self.handle_sending_channel_disconnect(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn receive_messages(&mut self) { | ||
for result in self.communicator.receive_next_lines() { | ||
match result { | ||
Ok(message) => self.handle_message(message.as_str()), | ||
Err(error) => self.logging_channel.send(error).unwrap() | ||
} | ||
} | ||
} | ||
|
||
fn handle_message(&mut self, message: &str) { | ||
let parsed_result = parse_message(message); | ||
match parsed_result { | ||
Ok(parsed_message) => self.handle_valid_command(parsed_message), | ||
Err(log) => self.logging_channel.send(log).unwrap(), | ||
} | ||
} | ||
|
||
fn send_message(&mut self, message: &SendableMessage) { | ||
let sending_string = message.encode(); | ||
|
||
self.communicator.send_line(sending_string).expect("Error in sending a line!"); | ||
} | ||
|
||
fn handle_sending_channel_disconnect(&mut self) { | ||
let log = LogData::fatal("Sending channel disconnected in external comms!"); | ||
self.logging_channel.send(log) | ||
.expect("Sending channel and logging channel disconnected in Driver Station Comms!"); | ||
panic!("{}", "Sending channel disconnected in external comms!"); | ||
} | ||
|
||
fn handle_valid_command(&mut self, message: ReceivableMessage) { | ||
match message { | ||
ReceivableMessage::Kill => self.handle_kill_command(), | ||
ReceivableMessage::Revive => self.handle_revive_command(), | ||
ReceivableMessage::Enable(subsystem) => self.handle_enable_command(subsystem), | ||
ReceivableMessage::Disable(subsystem) => self.handle_disable_command(subsystem), | ||
ReceivableMessage::Drive(left_speed, right_speed) => self.handle_drive_command(left_speed, right_speed), | ||
ReceivableMessage::Brake => self.handle_brake_command(), | ||
} | ||
} | ||
|
||
fn handle_kill_command(&mut self) { | ||
self.drive_train_channel.send(DriveTrainCommand::Kill).unwrap(); | ||
} | ||
|
||
fn handle_revive_command(&mut self) { | ||
self.drive_train_channel.send(DriveTrainCommand::Revive).unwrap(); | ||
} | ||
|
||
fn handle_enable_command(&mut self, subsystem: ProtocolSubsystem) { | ||
match subsystem { | ||
ProtocolSubsystem::DriveTrain => self.drive_train_channel | ||
.send(DriveTrainCommand::Enable) | ||
.unwrap(), | ||
} | ||
} | ||
|
||
fn handle_disable_command(&mut self, subsystem: ProtocolSubsystem) { | ||
match subsystem { | ||
ProtocolSubsystem::DriveTrain => self.drive_train_channel | ||
.send(DriveTrainCommand::Disable) | ||
.unwrap(), | ||
} | ||
} | ||
|
||
fn handle_drive_command(&mut self, left_speed: f32, right_speed: f32) { | ||
self.drive_train_channel.send(DriveTrainCommand::Drive(left_speed, right_speed)).unwrap(); | ||
} | ||
|
||
fn handle_brake_command(&mut self) { | ||
self.drive_train_channel.send(DriveTrainCommand::Stop).unwrap(); | ||
} | ||
} |
Oops, something went wrong.