Alex Mikhalev
6 years ago
commit
7de0ea40c6
8 changed files with 100 additions and 0 deletions
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
[[package]] |
||||
name = "robotrs" |
||||
version = "0.1.0" |
||||
|
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
[package] |
||||
name = "robotrs" |
||||
version = "0.1.0" |
||||
authors = ["Alex Mikhalev <alexmikhalevalex@gmail.com>"] |
||||
|
||||
[dependencies] |
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
use motor_controller::MotorControllerBox; |
||||
use motor_controller::MockMotorController; |
||||
|
||||
pub struct Drive { |
||||
left_motors: Vec<MotorControllerBox>, |
||||
right_motors: Vec<MotorControllerBox>, |
||||
} |
||||
|
||||
fn create_motor_controllers<T: Iterator<Item = u16>>(ids: T) -> Vec<MotorControllerBox> { |
||||
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); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
mod robot; |
||||
mod drive; |
||||
mod motor_controller; |
||||
|
||||
use robot::Robot; |
||||
|
||||
fn main() { |
||||
let mut robot = Robot::new(); |
||||
|
||||
robot.start(); |
||||
} |
@ -0,0 +1,3 @@
@@ -0,0 +1,3 @@
|
||||
pub mod robot; |
||||
pub mod drive; |
||||
pub mod motor_controller; |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
pub trait MotorController { |
||||
fn set(&mut self, value: f64); |
||||
} |
||||
|
||||
pub type MotorControllerBox = Box<dyn MotorController>; |
||||
|
||||
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); |
||||
} |
||||
} |
@ -0,0 +1,21 @@
@@ -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); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue