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
2 changed files with 63 additions and 14 deletions

View file

@ -1,3 +1,4 @@
deploy:
cross build --target=armv7-unknown-linux-gnueabihf --release
scp target/armv7-unknown-linux-gnueabihf/release/northbridge cruise@pelican.dyn.wpi.edu:
ssh cruise@pelican.dyn.wpi.edu systemctl --user restart nightstand

View file

@ -3,10 +3,10 @@
use std::time::Duration;
use anyhow::{Context, Result};
use common::{Command, Response, BAUDRATE};
use common::{Command, Response, SensorData, BAUDRATE};
use framed_codec::FramedCodec;
use futures::{SinkExt, StreamExt};
use tokio::{io::AsyncReadExt, net::{TcpListener, TcpSocket}, sync::{broadcast, watch::{self, Sender}}, task::JoinHandle, time::timeout};
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::{TcpListener, TcpSocket}, sync::{broadcast::{self, Receiver}, watch::{self, Sender}}, task::JoinHandle, time::timeout};
use tokio_serial::SerialPortBuilderExt;
use tokio_util::codec::Framed;
@ -16,7 +16,7 @@ mod framed_codec;
async fn main() -> Result<()> {
let mut serial = tokio_serial::new("/dev/ttyAMA0", BAUDRATE).open_native_async()?;
let (sensor_sender, mut sensor_data) = broadcast::channel(5);
let (sensor_sender, sensor_data) = broadcast::channel(5);
serial.set_exclusive(false)?;
@ -24,16 +24,18 @@ async fn main() -> Result<()> {
let (send, commands) = watch::channel(Command::Stop);
let mut print_telem = sensor_data.resubscribe();
let _: JoinHandle<Result<()>> = tokio::spawn(async move {
loop {
println!("sensor {:?}", sensor_data.recv().await?);
println!("sensor {:?}", print_telem.recv().await?);
}
});
let control_telem = sensor_data.resubscribe();
tokio::spawn(async move {
loop {
let _ = send.send(Command::Stop);
if let Err(e) = control(send.clone()).await {
if let Err(e) = control(send.clone(), control_telem.resubscribe()).await {
println!("controller exited: {e}");
}
}
@ -70,18 +72,28 @@ async fn main() -> Result<()> {
}
}
async fn control(sender: Sender<Command>) -> Result<()> {
async fn control(sender: Sender<Command>, mut telem: Receiver<SensorData>) -> Result<()> {
let listener = TcpListener::bind("0.0.0.0:3322").await?;
let (mut stream, addr) = listener.accept().await?;
let (stream, addr) = listener.accept().await?;
println!("connected to {addr:?}");
let (mut read, mut write) = stream.into_split();
let _: JoinHandle<Result<()>> = tokio::spawn(async move {
loop {
let data = telem.recv().await?;
let data = postcard::to_stdvec(&data)?;
write.write_u32(data.len() as u32).await?;
write.write_all(&data).await?;
}
});
loop {
let len = timeout(Duration::from_millis(30), stream.read_u32()).await??;
let len = timeout(Duration::from_millis(100), read.read_u32()).await??;
let mut buf = vec![0; len as usize];
timeout(Duration::from_millis(30), stream.read_exact(&mut buf)).await??;
timeout(Duration::from_millis(100), read.read_exact(&mut buf)).await??;
let cmd: Command = postcard::from_bytes(&buf)?;