inital commit
This commit is contained in:
commit
7de0ea40c6
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
**/*.rs.bk
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "robotrs"
|
||||
version = "0.1.0"
|
||||
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "robotrs"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Mikhalev <alexmikhalevalex@gmail.com>"]
|
||||
|
||||
[dependencies]
|
29
src/drive.rs
Normal file
29
src/drive.rs
Normal file
@ -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
Normal file
11
src/main.rs
Normal file
@ -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
Normal file
3
src/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod robot;
|
||||
pub mod drive;
|
||||
pub mod motor_controller;
|
24
src/motor_controller.rs
Normal file
24
src/motor_controller.rs
Normal file
@ -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
Normal file
21
src/robot.rs
Normal file
@ -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…
x
Reference in New Issue
Block a user