Initialise esp32 pwm, fix esp32 initial setup as it uses 2 bytes

This commit is contained in:
Piv
2023-01-22 15:05:32 +10:30
parent 40e5269c35
commit 34fbc40ebb
3 changed files with 7 additions and 6 deletions

View File

@@ -99,12 +99,14 @@ pub struct Esp32SerialPwmServo<T: SerialPort> {
impl<T: SerialPort> Esp32SerialPwmServo<T> { impl<T: SerialPort> Esp32SerialPwmServo<T> {
pub fn new(serial_port: Arc<Mutex<T>>, channel: u8, pin: u8) -> Esp32SerialPwmServo<T> { pub fn new(serial_port: Arc<Mutex<T>>, channel: u8, pin: u8) -> Esp32SerialPwmServo<T> {
Esp32SerialPwmServo { let mut servo = Esp32SerialPwmServo {
serial_port, serial_port,
value: 0., value: 0.,
channel, channel,
pin, pin,
} };
servo.init_pwm();
servo
} }
} }
@@ -115,7 +117,7 @@ impl<T: SerialPort> Esp32SerialPwmServo<T> {
.lock() .lock()
.unwrap() .unwrap()
.write(&[0, 1, self.channel, self.pin]); .write(&[0, 1, self.channel, self.pin]);
// TODO: Better error handling // TODO: Better error handling (even anyhow would be better)
match bytes_written { match bytes_written {
Ok(size) => println!("{}", size), Ok(size) => println!("{}", size),
Err(err) => eprintln!("{}", err), Err(err) => eprintln!("{}", err),

View File

@@ -13,7 +13,6 @@ mod grpcserver;
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:10000".parse().unwrap(); let addr = "[::1]:10000".parse().unwrap();
// TODO: Do these actually need to be shared since they're both on one port?
let serial_port = serialport::new("", 32400) let serial_port = serialport::new("", 32400)
.open_native() .open_native()
.expect("Could not open serial port"); .expect("Could not open serial port");

View File

@@ -19,8 +19,8 @@ void setup()
void setupServos(uint8_t size, uint8_t *calibrationValues) void setupServos(uint8_t size, uint8_t *calibrationValues)
{ {
// We assume there are 3 bytes per servo. Ignore if there aren't. // We assume there are 2 bytes per servo [channel number, pin number]. Ignore if there aren't.
if (size % 3 == 0) if (size % 2 == 0)
{ {
for (int i = 0; i < size; i += 2) for (int i = 0; i < size; i += 2)
{ {