Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'DriveTrain' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah-Kennedy authored Dec 21, 2018
2 parents 702236f + e306049 commit 8896a5f
Show file tree
Hide file tree
Showing 24 changed files with 1,462 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,5 @@ fabric.properties

/rmc-beaglebone-core-2018-2019.iml
/.idea/
/RMC_Logs/
RMC_Logs/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ after_success: |
rm -rf kcov-master &&
for file in target/debug/examplerust-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; ./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
bash <(curl -s https://codecov.io/bash) &&
echo "Uploaded code coverage"
echo "Uploaded code coverage"
3 changes: 2 additions & 1 deletion Cargo.toml
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 = ""
144 changes: 144 additions & 0 deletions src/comms/driver_station/mod.rs
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();
}
}
Loading

0 comments on commit 8896a5f

Please sign in to comment.