Add single steering/throttle grpc methods, add bounds checks to set servo value

This commit is contained in:
Piv
2022-10-02 15:44:38 +10:30
parent de0e3e3243
commit 3daa815710
2 changed files with 20 additions and 6 deletions

View File

@@ -39,14 +39,22 @@ impl<T: Vehicle + Send + Sync + 'static> CarControl for MotorControlService<T> {
&self, &self,
_request: Request<ThrottleRequest>, _request: Request<ThrottleRequest>,
) -> Result<Response<ThrottleResponse>, Status> { ) -> Result<Response<ThrottleResponse>, Status> {
unimplemented!() self.vehicle
.lock()
.unwrap()
.set_throttle(_request.into_inner().throttle as f64);
Ok(Response::new(ThrottleResponse { throttle_set: true }))
} }
async fn set_steering( async fn set_steering(
&self, &self,
_request: Request<SteeringRequest>, _request: Request<SteeringRequest>,
) -> Result<Response<SteeringResponse>, Status> { ) -> Result<Response<SteeringResponse>, Status> {
unimplemented!() self.vehicle
.lock()
.unwrap()
.set_steering(_request.into_inner().steering as f64);
Ok(Response::new(SteeringResponse { steering_set: true }))
} }
async fn stream_vehicle_2d( async fn stream_vehicle_2d(

View File

@@ -123,7 +123,14 @@ impl<T: SerialPort> Servo for Esp32SerialPwmServo<T> {
} }
fn set_value(&mut self, value: f64) { fn set_value(&mut self, value: f64) {
self.value = value; let mut temp_value = value;
// TODO: Panic when out of bounds?
if temp_value < -1. {
temp_value = -1.;
} else if temp_value > 1. {
temp_value = 1.;
}
self.value = temp_value;
let bytes_written = self let bytes_written = self
.serial_port .serial_port
.write(&[self.channel, ((value + 1.) / 2. * 255.) as u8]); .write(&[self.channel, ((value + 1.) / 2. * 255.) as u8]);
@@ -150,9 +157,8 @@ impl<T: Servo> ServoVehicle<T> {
} }
impl<T: Servo> Vehicle for ServoVehicle<T> { impl<T: Servo> Vehicle for ServoVehicle<T> {
// TODO: Set duty cycle correctly
fn get_throttle(&self) -> f64 { fn get_throttle(&self) -> f64 {
unimplemented!() self.throttle_servo.get_value()
} }
fn set_throttle(&mut self, throttle: f64) { fn set_throttle(&mut self, throttle: f64) {
@@ -160,7 +166,7 @@ impl<T: Servo> Vehicle for ServoVehicle<T> {
} }
fn get_steering(&self) -> f64 { fn get_steering(&self) -> f64 {
unimplemented!() self.steering_servo.get_value()
} }
fn set_steering(&mut self, steering: f64) { fn set_steering(&mut self, steering: f64) {