1
Fork 0

stop robot on disconnect

This commit is contained in:
Andy Killorin 2025-07-20 20:49:12 -05:00
parent 09c1fe5982
commit 38ca539af8
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -1,4 +1,4 @@
use std::{f32::NAN, io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration}; use std::{io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration};
use common::{Command, Telemetry}; use common::{Command, Telemetry};
use postcard::to_vec_cobs; use postcard::to_vec_cobs;
@ -73,6 +73,14 @@ fn handle_connection(command: Arc<Mutex<Command>>, telemetry: Arc<Mutex<Telemetr
let mut data_buf = std::vec::Vec::new(); let mut data_buf = std::vec::Vec::new();
let len = reader.read_until(b'\n', &mut data_buf)?; let len = reader.read_until(b'\n', &mut data_buf)?;
// eof
if len == 0 {
*command.lock().unwrap() = Command::default(); // stop motors
println!("disconnected from LabVIEW");
return anyhow::Ok(());
}
let Ok(new_command) = serde_json::from_slice::<Command>(&data_buf[0..len]) else {continue;}; let Ok(new_command) = serde_json::from_slice::<Command>(&data_buf[0..len]) else {continue;};
*command.lock().unwrap() = new_command.clone(); *command.lock().unwrap() = new_command.clone();