Add grpc rust codegen + stubs, add rppal compile feature

for raspberry pi compilation.

This is because rppal won't compile on mac, so we
only want to bring in the dependency when actually compiling
for the raspberry pi, and so will manually need to enable the
dependency.
This commit is contained in:
Piv
2022-08-06 20:34:00 +09:30
parent 26647017c3
commit d876fcbb2e
7 changed files with 395 additions and 32 deletions

View File

@@ -1,8 +1,8 @@
use rppal::pwm::{Channel, Pwm};
use serialport::SerialPort;
pub trait Servo {
fn get_duty_cycle(&self) -> f64;
// TODO: Some kind of error handling here, in case we fail to set the duty cycel
// TODO: Some kind of error handling here, in case we fail to set the duty cycle
fn set_duty_cycle(&self, pwm: f64);
fn get_frequency(&self) -> f64;
@@ -14,43 +14,69 @@ pub trait Vechicle<T: Servo> {
fn get_throttle_servo() -> T;
fn get_steering_servo() -> T;
}
#[cfg(feature = "rppal")]
pub mod rppal {
use rppal::pwm::{Channel, Pwm};
pub struct RpiPwmServo {
pwm: Pwm,
}
impl RpiPwmServo {
pub fn new(pwm: Pwm) -> RpiPwmServo {
RpiPwmServo { pwm }
pub struct RpiPwmServo {
pwm: Pwm,
}
}
impl Default for RpiPwmServo {
fn default() -> Self {
Self {
pwm: Pwm::new(Channel::Pwm0).expect("Failed to initialise Pwm servo"),
impl RpiPwmServo {
pub fn new(pwm: Pwm) -> RpiPwmServo {
RpiPwmServo { pwm }
}
}
impl Default for RpiPwmServo {
fn default() -> Self {
Self {
pwm: Pwm::new(Channel::Pwm0).expect("Failed to initialise Pwm servo"),
}
}
}
impl Servo for RpiPwmServo {
fn get_duty_cycle(&self) -> f64 {
self.pwm.duty_cycle().unwrap_or(0.)
}
fn set_duty_cycle(&self, pwm: f64) {
self.pwm
.set_duty_cycle(pwm)
.expect("Failed to write duty cycle");
}
fn get_frequency(&self) -> f64 {
self.pwm.duty_cycle().unwrap_or(0.0)
}
fn set_frequency(&self, frequency: f64) {
self.pwm
.set_frequency(frequency, self.get_duty_cycle())
.expect("Failed to set Frequency")
}
}
}
impl Servo for RpiPwmServo {
pub struct Esp32SerialPwmServo<T: SerialPort> {
serial_port: T,
}
impl<T: SerialPort> Servo for Esp32SerialPwmServo<T> {
fn get_duty_cycle(&self) -> f64 {
self.pwm.duty_cycle().unwrap_or(0.)
todo!()
}
fn set_duty_cycle(&self, pwm: f64) {
self.pwm
.set_duty_cycle(pwm)
.expect("Failed to write duty cycle");
todo!()
}
fn get_frequency(&self) -> f64 {
self.pwm.duty_cycle().unwrap_or(0.0)
todo!()
}
fn set_frequency(&self, frequency: f64) {
self.pwm
.set_frequency(frequency, self.get_duty_cycle())
.expect("Failed to set Frequency")
todo!()
}
}