propagate voltage to controller

This commit is contained in:
Andy Killorin 2025-02-08 22:22:17 -05:00
commit 852b95dcc0
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -1,9 +1,10 @@
use std::time::Duration;
use anyhow::Context;
use common::Command;
use common::{Command, SensorData};
use futures::Stream;
use gilrs::{Axis, Event, Gilrs};
use tokio::{io::{AsyncWriteExt, BufWriter}, net::TcpStream, time::sleep};
use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter}, net::{tcp::OwnedReadHalf, TcpStream}, task::JoinHandle, time::{sleep, Instant}};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
@ -17,16 +18,19 @@ async fn main() -> anyhow::Result<()> {
let mut robot = TcpStream::connect("pelican.dyn.wpi.edu:3322").await?;
robot.set_nodelay(true)?;
let (telem, robot_controller) = robot.into_split();
let (_telem, robot_controller) = robot.split();
tokio::spawn(print_telem(telem));
let mut robot_controller = BufWriter::new(robot_controller);
let mut active_gamepad = None;
let mut last_event = Instant::now();
loop {
while let Some(Event { id,..}) = gamepads.next_event() {
active_gamepad = Some(id);
last_event = Instant::now();
}
let Some(gamepad) = active_gamepad else {
@ -49,6 +53,11 @@ async fn main() -> anyhow::Result<()> {
}
}
if !gamepad.is_connected() {
ly = 0.0;
rx = 0.0;
}
let cmd = Command::Twist(ly, rx);
let encoded = postcard::to_stdvec(&cmd)?;
@ -56,7 +65,34 @@ async fn main() -> anyhow::Result<()> {
robot_controller.write_u32(encoded.len() as u32).await?;
robot_controller.write_all(&encoded).await?;
robot_controller.flush().await?;
if let Err(e) = robot_controller.flush().await {
println!("flush fail: {e}, reconnecting");
robot = TcpStream::connect("pelican.dyn.wpi.edu:3322").await?;
robot.set_nodelay(true)?;
let (telem, robot) = robot.into_split();
tokio::spawn(print_telem(telem));
robot_controller = BufWriter::new(robot);
};
sleep(Duration::from_micros(3500)).await;
}
}
async fn print_telem(mut telem: OwnedReadHalf) -> anyhow::Result<()> {
loop {
let len = telem.read_u32().await? as usize;
let mut buf = vec![0; len];
telem.read_exact(&mut buf).await?;
let telem: SensorData = postcard::from_bytes(&buf)?;
match telem {
SensorData::BusVoltage(voltage) => {
println!("voltage: {voltage}");
}
_ => {}
}
}
}