use std::{cell::RefCell, rc::Rc};
use log::warn;
use vexide::{
controller::ControllerState,
math::Angle,
prelude::{Controller, Motor},
smart::{PortError, motor::BrakeMode},
};
#[derive(Clone)]
#[allow(dead_code)]
pub struct Differential {
pub left: Rc<RefCell<dyn AsMut<[Motor]>>>,
pub right: Rc<RefCell<dyn AsMut<[Motor]>>>,
}
#[allow(dead_code)]
impl Differential {
pub fn new<L: AsMut<[Motor]> + 'static, R: AsMut<[Motor]> + 'static>(
left: L,
right: R,
) -> Self {
Self {
left: Rc::new(RefCell::new(left)),
right: Rc::new(RefCell::new(right)),
}
}
pub fn tank(&self, controller: &Controller) {
let state = controller.state().unwrap_or_else(|e| {
warn!("Controller State Error: {}", e);
ControllerState::default()
});
let left_power = state.left_stick.y();
let right_power = state.right_stick.y();
let left_voltage = left_power * 12.0;
let right_voltage = right_power * 12.0;
if let Ok(mut left_motors) = self.left.try_borrow_mut() {
for motor in left_motors.as_mut() {
let _ = motor.set_voltage(left_voltage);
}
}
if let Ok(mut right_motors) = self.right.try_borrow_mut() {
for motor in right_motors.as_mut() {
let _ = motor.set_voltage(right_voltage);
}
}
}
pub fn arcade(&self, controller: &Controller) {
let state = controller.state().unwrap_or_else(|e| {
warn!("Controller State Error: {}", e);
ControllerState::default()
});
let fwd = state.left_stick.y();
let turn = state.right_stick.x();
let left_voltage = (fwd + turn) * 12.0;
let right_voltage = (fwd - turn) * 12.0;
if let Ok(mut left_motors) = self.left.try_borrow_mut() {
for motor in left_motors.as_mut() {
let _ = motor.set_voltage(left_voltage);
}
}
if let Ok(mut right_motors) = self.right.try_borrow_mut() {
for motor in right_motors.as_mut() {
let _ = motor.set_voltage(right_voltage);
}
}
}
pub fn reverse_tank(&self, controller: &Controller) {
let state = controller.state().unwrap_or_else(|e| {
warn!("Controller State Error: {}", e);
ControllerState::default()
});
let left_voltage = (-state.right_stick.y()) * 12.0;
let right_voltage = (-state.left_stick.y()) * 12.0;
if let Ok(mut left_motors) = self.left.try_borrow_mut() {
for motor in left_motors.as_mut() {
let _ = motor.set_voltage(left_voltage);
}
}
if let Ok(mut right_motors) = self.right.try_borrow_mut() {
for motor in right_motors.as_mut() {
let _ = motor.set_voltage(right_voltage);
}
}
}
pub fn reverse_arcade(&self, controller: &Controller) {
let state = controller.state().unwrap_or_else(|e| {
warn!("Controller State Error: {}", e);
ControllerState::default()
});
let fwd = -state.left_stick.y();
let turn = -state.right_stick.x();
let left_voltage = (fwd + turn) * 12.0;
let right_voltage = (fwd - turn) * 12.0;
if let Ok(mut left_motors) = self.left.try_borrow_mut() {
for motor in left_motors.as_mut() {
let _ = motor.set_voltage(left_voltage);
}
}
if let Ok(mut right_motors) = self.right.try_borrow_mut() {
for motor in right_motors.as_mut() {
let _ = motor.set_voltage(right_voltage);
}
}
}
pub fn set_brakemode(&self, brakemode: BrakeMode) {
let left = self.left.try_borrow_mut();
let right = self.right.try_borrow_mut();
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
let _ = motor.brake(brakemode);
}
}
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
let _ = motor.brake(brakemode);
}
}
}
pub fn position(&self) -> Angle {
let left = self.left.try_borrow_mut();
let right = self.right.try_borrow_mut();
let mut angle: Angle = Angle::from_degrees(0.0);
let mut denom: f64 = 0.0;
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
angle += motor.position().unwrap_or_else(|e| {
warn!("Error Getting Motor Encoder Position: {}", e);
denom -= 1.0;
Angle::from_radians(0.0)
});
denom += 1.0;
}
} else if let Err(e) = left {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
angle += motor.position().unwrap_or_else(|e| {
warn!("Error Getting Motor Encoder Position: {}", e);
denom -= 1.0;
Angle::from_radians(0.0)
});
denom += 1.0;
}
} else if let Err(e) = right {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
angle / denom
}
pub fn left_position(&self) -> Angle {
let left = self.left.try_borrow_mut();
let mut angle: Angle = Angle::from_degrees(0.0);
let mut denom: f64 = 0.0;
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
angle += motor.position().unwrap_or_else(|e| {
warn!("Error Getting Motor Encoder Position: {}", e);
denom -= 1.0;
Angle::from_radians(0.0)
});
denom += 1.0;
}
} else if let Err(e) = left {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
angle / denom
}
pub fn right_position(&self) -> Angle {
let right = self.right.try_borrow_mut();
let mut angle: Angle = Angle::from_degrees(0.0);
let mut denom: f64 = 0.0;
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
angle += motor.position().unwrap_or_else(|e| {
warn!("Error Getting Motor Encoder Position: {}", e);
denom -= 1.0;
Angle::from_radians(0.0)
});
denom += 1.0;
}
} else if let Err(e) = right {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
angle / denom
}
pub fn reset_position(&self) -> Result<(), PortError> {
let left = self.left.try_borrow_mut();
let right = self.right.try_borrow_mut();
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
motor.reset_position()?
}
} else if let Err(e) = left {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
motor.reset_position()?
}
} else if let Err(e) = right {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
Ok(())
}
pub fn set_position(&self, position: Angle) -> Result<(), PortError> {
let left = self.left.try_borrow_mut();
let right = self.right.try_borrow_mut();
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
motor.set_position(position)?
}
} else if let Err(e) = left {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
motor.set_position(position)?
}
} else if let Err(e) = right {
warn!("Error Borrowing Mutex: {}", e);
} else {
warn!("Error Borrowing Mutex");
}
Ok(())
}
pub fn set_voltage(&self, voltage: f64) {
let left = self.left.try_borrow_mut();
let right = self.right.try_borrow_mut();
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
let _ = motor.set_voltage(voltage);
}
}
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
let _ = motor.set_voltage(voltage);
}
}
}
pub fn set_left_voltage(&self, voltage: f64) {
let left = self.left.try_borrow_mut();
if let Ok(mut motors) = left {
for motor in motors.as_mut() {
let _ = motor.set_voltage(voltage);
}
}
}
pub fn set_right_voltage(&self, voltage: f64) {
let right = self.right.try_borrow_mut();
if let Ok(mut motors) = right {
for motor in motors.as_mut() {
let _ = motor.set_voltage(voltage);
}
}
}
pub fn from_shared<L: AsMut<[Motor]> + 'static, R: AsMut<[Motor]> + 'static>(
left: Rc<RefCell<L>>,
right: Rc<RefCell<R>>,
) -> Self {
Self { left, right }
}
}