motor driver implementations

master
Andrew Johnson 6 years ago
parent fbb1438cda
commit 29b2f93243

@ -1,4 +1,7 @@
extern crate libc;
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate serde_json;
pub mod motor_controllers;
pub mod elevator_drivers;

@ -1,5 +1,5 @@
int motor1_adjust_motor(int target_force){
int motor1_adjust_motor(double target_force){
//real driver would do something here
//interface with physical components
return 0;

@ -1,5 +1,5 @@
int motor2_adjust_motor(int target_force){
int motor2_adjust_motor(double target_force){
//real driver would do something here
//interface with physical components
return 0;

@ -1,5 +1,5 @@
int motor3_adjust_motor(int target_force){
int motor3_adjust_motor(double target_force){
//real driver would do something here
//interface with physical components
return 0;

@ -1,16 +1,68 @@
use libc::c_int;
use libc::c_double;
#[link(name = "motor1")]
extern {
pub fn motor1_adjust_motor(target_force: c_int) -> c_int;
pub fn motor1_adjust_motor(target_force: c_double) -> c_double;
}
#[link(name = "motor2")]
extern {
pub fn motor2_adjust_motor(target_force: c_int) -> c_int;
pub fn motor2_adjust_motor(target_force: c_double) -> c_double;
}
#[link(name = "motor3")]
extern {
pub fn motor3_adjust_motor(target_force: c_int) -> c_int;
pub fn motor3_adjust_motor(target_force: c_double) -> c_double;
}
#[derive(Clone,Serialize,Deserialize,Debug)]
pub enum MotorInput
{
Motor1 { target_force: f64 },
Motor2 { target_force: f64 },
Motor3 { target_force: f64 },
}
pub trait MotorDriver
{
fn adjust_motor(input: MotorInput);
}
struct Motor1;
impl MotorDriver for Motor1
{
fn adjust_motor(input: MotorInput)
{
if let MotorInput::Motor1 { target_force: target_force } = input {
unsafe {
motor1_adjust_motor(target_force);
}
}
}
}
struct Motor2;
impl MotorDriver for Motor2
{
fn adjust_motor(input: MotorInput)
{
if let MotorInput::Motor1 { target_force: target_force } = input {
unsafe {
motor2_adjust_motor(target_force);
}
}
}
}
struct Motor3;
impl MotorDriver for Motor3
{
fn adjust_motor(input: MotorInput)
{
if let MotorInput::Motor1 { target_force: target_force } = input {
unsafe {
motor3_adjust_motor(target_force);
}
}
}
}

@ -4,12 +4,12 @@ use elevator::motor_controllers::{motor1_adjust_motor,motor2_adjust_motor,motor3
fn main(){
unsafe {
println!("motor1 adjust");
motor1_adjust_motor(0);
motor1_adjust_motor(0.0);
println!("motor2 adjust");
motor2_adjust_motor(0);
motor2_adjust_motor(0.0);
println!("motor3 adjust");
motor3_adjust_motor(0);
motor3_adjust_motor(0.0);
}
}

Loading…
Cancel
Save