Remove swift source, update esp32 usb servo to share serial port
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use serialport::SerialPort;
|
||||
|
||||
// TODO: Should be returning results in these traits
|
||||
@@ -89,14 +91,14 @@ pub mod rppal {
|
||||
}
|
||||
|
||||
pub struct Esp32SerialPwmServo<T: SerialPort> {
|
||||
serial_port: T,
|
||||
serial_port: Arc<Mutex<T>>,
|
||||
value: f64,
|
||||
channel: u8,
|
||||
pin: u8,
|
||||
}
|
||||
|
||||
impl<T: SerialPort> Esp32SerialPwmServo<T> {
|
||||
pub fn new(serial_port: T, channel: u8, pin: u8) -> Esp32SerialPwmServo<T> {
|
||||
pub fn new(serial_port: Arc<Mutex<T>>, channel: u8, pin: u8) -> Esp32SerialPwmServo<T> {
|
||||
Esp32SerialPwmServo {
|
||||
serial_port,
|
||||
value: 0.,
|
||||
@@ -108,7 +110,11 @@ impl<T: SerialPort> Esp32SerialPwmServo<T> {
|
||||
|
||||
impl<T: SerialPort> Esp32SerialPwmServo<T> {
|
||||
fn init_pwm(&mut self) {
|
||||
let bytes_written = self.serial_port.write(&[0, 1, self.channel, self.pin]);
|
||||
let bytes_written = self
|
||||
.serial_port
|
||||
.lock()
|
||||
.unwrap()
|
||||
.write(&[0, 1, self.channel, self.pin]);
|
||||
// TODO: Better error handling
|
||||
match bytes_written {
|
||||
Ok(size) => println!("{}", size),
|
||||
@@ -133,6 +139,8 @@ impl<T: SerialPort> Servo for Esp32SerialPwmServo<T> {
|
||||
self.value = temp_value;
|
||||
let bytes_written = self
|
||||
.serial_port
|
||||
.lock()
|
||||
.unwrap()
|
||||
.write(&[self.channel, ((value + 1.) / 2. * 255.) as u8]);
|
||||
// TODO: Better error handling
|
||||
match bytes_written {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use car_rs::{Esp32SerialPwmServo, ServoVehicle};
|
||||
use grpcserver::{
|
||||
motor_control_service::car_control_server::CarControlServer, MotorControlService,
|
||||
@@ -15,13 +17,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let serial_port = serialport::new("", 32400)
|
||||
.open_native()
|
||||
.expect("Could not open serial port");
|
||||
let throttle_port = serialport::new("", 32400)
|
||||
.open_native()
|
||||
.expect("Could not open serial port");
|
||||
let servo = Esp32SerialPwmServo::new(serial_port, 1, 12);
|
||||
let throttle_servo = Esp32SerialPwmServo::new(throttle_port, 2, 18);
|
||||
let serial_servo = Arc::new(Mutex::new(serial_port));
|
||||
let steering_servo = Esp32SerialPwmServo::new(serial_servo.clone(), 1, 12);
|
||||
let throttle_servo = Esp32SerialPwmServo::new(serial_servo.clone(), 2, 18);
|
||||
|
||||
let motor_control = MotorControlService::new(ServoVehicle::new(servo, throttle_servo));
|
||||
let motor_control = MotorControlService::new(ServoVehicle::new(steering_servo, throttle_servo));
|
||||
|
||||
let svc = CarControlServer::new(motor_control);
|
||||
Server::builder().add_service(svc).serve(addr).await?;
|
||||
|
||||
Reference in New Issue
Block a user