From 7de0ea40c6e7aa6f3b3617314ca44995f21b0b6d Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 16 Sep 2018 23:26:48 -0600 Subject: [PATCH] inital commit --- .gitignore | 2 ++ Cargo.lock | 4 ++++ Cargo.toml | 6 ++++++ src/drive.rs | 29 +++++++++++++++++++++++++++++ src/main.rs | 11 +++++++++++ src/mod.rs | 3 +++ src/motor_controller.rs | 24 ++++++++++++++++++++++++ src/robot.rs | 21 +++++++++++++++++++++ 8 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/drive.rs create mode 100644 src/main.rs create mode 100644 src/mod.rs create mode 100644 src/motor_controller.rs create mode 100644 src/robot.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..dc90fae --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "robotrs" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a8d8558 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "robotrs" +version = "0.1.0" +authors = ["Alex Mikhalev "] + +[dependencies] diff --git a/src/drive.rs b/src/drive.rs new file mode 100644 index 0000000..fad0fbe --- /dev/null +++ b/src/drive.rs @@ -0,0 +1,29 @@ +use motor_controller::MotorControllerBox; +use motor_controller::MockMotorController; + +pub struct Drive { + left_motors: Vec, + right_motors: Vec, +} + +fn create_motor_controllers>(ids: T) -> Vec { + ids.map(MockMotorController::new_id).map(|m| Box::new(m) as MotorControllerBox).collect() +} + +impl Drive { + pub fn new() -> Self { + let left_motors = create_motor_controllers(0u16..4); + let right_motors = create_motor_controllers(4u16..8); + Drive { left_motors, right_motors } + } + + pub fn drive_powers(&mut self, left_power: f64, right_power: f64) { + for motor in &mut self.left_motors { + motor.set(left_power); + } + for motor in &mut self.right_motors { + motor.set(right_power); + } + } +} + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1d4a7a0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +mod robot; +mod drive; +mod motor_controller; + +use robot::Robot; + +fn main() { + let mut robot = Robot::new(); + + robot.start(); +} diff --git a/src/mod.rs b/src/mod.rs new file mode 100644 index 0000000..fd07071 --- /dev/null +++ b/src/mod.rs @@ -0,0 +1,3 @@ +pub mod robot; +pub mod drive; +pub mod motor_controller; diff --git a/src/motor_controller.rs b/src/motor_controller.rs new file mode 100644 index 0000000..25f0549 --- /dev/null +++ b/src/motor_controller.rs @@ -0,0 +1,24 @@ +pub trait MotorController { + fn set(&mut self, value: f64); +} + +pub type MotorControllerBox = Box; + +pub struct MockMotorController { + id: u16, + value: f64, +} + +impl MockMotorController { + pub fn new_id(id: u16) -> Self { + MockMotorController{ id: id, value: 0.0 } + } +} + +impl MotorController for MockMotorController { + + fn set(&mut self, value: f64) { + self.value = value; + println!("MotorController id {} value={}", self.id, self.value); + } +} diff --git a/src/robot.rs b/src/robot.rs new file mode 100644 index 0000000..1d99824 --- /dev/null +++ b/src/robot.rs @@ -0,0 +1,21 @@ +use drive::Drive; + +pub struct Robot { + drive: Drive, +} + +impl Robot { + pub fn new() -> Self { + Robot{ + drive: Drive::new(), + } + } + + pub fn start(&mut self) { + println!("Starting RobotRS"); + + for _ in 0..10 { + self.drive.drive_powers(1.0, 1.0); + } + } +}