Browse Source

inital commit

master
Alex Mikhalev 6 years ago
commit
7de0ea40c6
  1. 2
      .gitignore
  2. 4
      Cargo.lock
  3. 6
      Cargo.toml
  4. 29
      src/drive.rs
  5. 11
      src/main.rs
  6. 3
      src/mod.rs
  7. 24
      src/motor_controller.rs
  8. 21
      src/robot.rs

2
.gitignore vendored

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
/target
**/*.rs.bk

4
Cargo.lock generated

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
[[package]]
name = "robotrs"
version = "0.1.0"

6
Cargo.toml

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
[package]
name = "robotrs"
version = "0.1.0"
authors = ["Alex Mikhalev <alexmikhalevalex@gmail.com>"]
[dependencies]

29
src/drive.rs

@ -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);
}
}
}

11
src/main.rs

@ -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();
}

3
src/mod.rs

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
pub mod robot;
pub mod drive;
pub mod motor_controller;

24
src/motor_controller.rs

@ -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);
}
}

21
src/robot.rs

@ -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…
Cancel
Save