RustPhysicsMQ/src/drone/controller.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2025-12-08 21:18:04 +00:00
use rapier3d::prelude as rp;
use std::any::Any;
use crate::drone::MotorCharacteristics;
pub trait DroneController {
// Allow downcast of trait -> class.
//
// This gives us the ability to have a Box<dyn DroneController>
// And transform it into a pointer to its class.
2025-12-08 21:18:04 +00:00
fn as_any(&self) -> &dyn Any;
fn as_mut_any(&mut self) -> &mut dyn Any;
/*
* Methods called by Drone, to transmit information to the controller.
*/
2025-12-08 21:18:04 +00:00
fn set_position(&self, position: rp::Isometry<f32>);
fn set_time(&self, time: f32);
fn set_motor_characteristics(&self, motor_characteristics: &MotorCharacteristics);
/*
* Throttle should be between 0 and 1. Values will be by the Drone class.
*/
fn get_motor_throttles(&self) -> [f32; 4];
2025-12-08 21:18:04 +00:00
}
/*
* DefaultController just sets throttle to motor_throttle, a parameter passed to it on its contructor
*/
pub struct DefaultController {
motor_throttle: f32,
2025-12-08 21:18:04 +00:00
}
impl DefaultController {
pub fn new(motor_speed: f32) -> DefaultController {
return DefaultController {
motor_throttle: motor_speed,
};
2025-12-08 21:18:04 +00:00
}
}
impl DroneController for DefaultController {
fn set_position(&self, _position: rp::Isometry<f32>) {}
fn set_time(&self, _time: f32) {}
fn set_motor_characteristics(&self, _motor_characteristics: &MotorCharacteristics) {}
fn get_motor_throttles(&self) -> [f32; 4] {
return [self.motor_throttle; 4];
2025-12-08 21:18:04 +00:00
}
/*
* These should be implemented like this for all the classes that implement DroneController
* Works as an example implementation
*/
2025-12-08 21:18:04 +00:00
fn as_any(&self) -> &dyn Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn Any {
self
}
}