switched to a state instead of change architecture
southbridge currently hangs, embassy does not make such things easy to diagnose
This commit is contained in:
parent
b131ad8957
commit
cc18f76e55
2 changed files with 134 additions and 154 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::result;
|
||||
|
||||
use anyhow::{anyhow, Ok};
|
||||
use common::{Command, Response};
|
||||
use common::{Response, RobotState};
|
||||
use framed::{BoxPayload, FRAME_END_SYMBOL};
|
||||
use tokio_util::{bytes::BytesMut, codec::{Decoder, Encoder}};
|
||||
|
||||
|
|
@ -55,10 +55,10 @@ impl Encoder<Vec<u8>> for FramedCodec {
|
|||
}
|
||||
}
|
||||
|
||||
impl Encoder<Command> for FramedCodec {
|
||||
impl Encoder<RobotState> for FramedCodec {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn encode(&mut self, item: Command, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
fn encode(&mut self, item: RobotState, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
let data = postcard::to_stdvec(&item)?;
|
||||
Ok(self.encode(data, dst).map_err(|e| anyhow!("encode fail: {e:?}"))?)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,19 @@
|
|||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use common::{Command, Response, SensorData, BAUDRATE};
|
||||
use common::{Response, RobotState, SensorData, BAUDRATE};
|
||||
use crossbeam::queue::ArrayQueue;
|
||||
use framed_codec::FramedCodec;
|
||||
use futures::{SinkExt, StreamExt, TryStreamExt};
|
||||
use futures::{FutureExt, SinkExt, StreamExt, TryStreamExt};
|
||||
use nalgebra::{SimdPartialOrd, SimdValue, Vector3};
|
||||
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::{TcpListener, TcpSocket}, sync::{broadcast::{self, error::RecvError, Sender, Receiver}, watch::{self }, RwLock}, task::JoinHandle, time::{error::Elapsed, timeout}};
|
||||
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::{TcpListener, TcpSocket}, sync::{broadcast::{self, error::RecvError, Receiver, Sender}, watch::{self, channel }, RwLock}, task::JoinHandle, time::{error::Elapsed, timeout}};
|
||||
use tokio_serial::SerialPortBuilderExt;
|
||||
use tokio_util::codec::Framed;
|
||||
|
||||
mod framed_codec;
|
||||
|
||||
#[derive(Default)]
|
||||
struct RobotState {
|
||||
struct RobotInfo {
|
||||
bus_voltage: f32,
|
||||
}
|
||||
|
||||
|
|
@ -29,9 +29,13 @@ async fn main() -> Result<()> {
|
|||
|
||||
let (mut write, mut read) = Framed::new(serial, FramedCodec::new()).split();
|
||||
|
||||
let commands = Arc::new(ArrayQueue::new(5));
|
||||
loop {
|
||||
write.send(RobotState::stopped()).await?;
|
||||
}
|
||||
|
||||
let state = Arc::new(RwLock::new(RobotState {
|
||||
let (sender, mut command_receiver) = channel(RobotState::stopped());
|
||||
|
||||
let state = Arc::new(RwLock::new(RobotInfo {
|
||||
bus_voltage: f32::MAX,
|
||||
..Default::default()
|
||||
}));
|
||||
|
|
@ -39,57 +43,34 @@ async fn main() -> Result<()> {
|
|||
tokio::spawn(update_telem(state.clone(), sensor_data.resubscribe()));
|
||||
|
||||
let control_telem = sensor_data.resubscribe();
|
||||
let sender = commands.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let _ = sender.force_push(Command::Stop);
|
||||
if let Err(e) = control(sender.clone(), control_telem.resubscribe()).await {
|
||||
println!("controller exited: {e}");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
println!("starting");
|
||||
write.send(Command::Enable).await?;
|
||||
write.flush().await?;
|
||||
println!("enable");
|
||||
tokio::spawn(async move {
|
||||
let telem_sender = sensor_sender.clone();
|
||||
read.for_each( async |telem| {
|
||||
let Ok(telem) = telem else {return;};
|
||||
let Some(data) = telem.sensor_data else {return;};
|
||||
telem_sender.send(data).unwrap();
|
||||
}).await;
|
||||
});
|
||||
|
||||
loop {
|
||||
let Some(Ok(data)) = read.next().await else {
|
||||
continue;
|
||||
};
|
||||
if data.enabled {
|
||||
write.send(Command::FeedWatchdog).await?;
|
||||
let _ = timeout(Duration::from_millis(20), command_receiver.changed());
|
||||
let command = command_receiver.borrow();
|
||||
|
||||
} else {
|
||||
println!("enabled southbridge");
|
||||
write.send(Command::Enable).await?;
|
||||
}
|
||||
println!("sending {command:?}");
|
||||
|
||||
if let Some(data) = data.sensor_data {
|
||||
let _ = sensor_sender.send(data);
|
||||
}
|
||||
|
||||
let voltage = state.read().await.bus_voltage;
|
||||
|
||||
// 100% effort at 9.5v, 0% effort at 7.5v
|
||||
let brownout = ((voltage-7.5)/2.0).clamp(0., 1.);
|
||||
|
||||
let mut comand = Vec::new();
|
||||
for _ in 0..commands.len().min(2) { // while let will never exit
|
||||
if let Some(command) = commands.pop() {
|
||||
comand.push(command);
|
||||
}
|
||||
}
|
||||
for command in dbg!(comand) {
|
||||
write.feed(command.brownout(brownout)).await?;
|
||||
write.feed(Command::FeedWatchdog).await?;
|
||||
}
|
||||
write.flush().await?;
|
||||
write.send(command.clone()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn control(sender: Arc<ArrayQueue<Command>>, mut telem: Receiver<SensorData>) -> Result<()> {
|
||||
async fn control(sender: watch::Sender<RobotState>, mut telem: Receiver<SensorData>) -> Result<()> {
|
||||
let listener = TcpListener::bind("0.0.0.0:3322").await?;
|
||||
|
||||
let (stream, addr) = listener.accept().await?;
|
||||
|
|
@ -116,7 +97,7 @@ async fn control(sender: Arc<ArrayQueue<Command>>, mut telem: Receiver<SensorDat
|
|||
match timeout(Duration::from_millis(150), read.read_u32()).await {
|
||||
Ok(it) => { break it?; },
|
||||
Err(Elapsed) => {
|
||||
sender.force_push(Command::Stop);
|
||||
sender.send(RobotState::stopped())?;
|
||||
continue;
|
||||
},
|
||||
};
|
||||
|
|
@ -124,13 +105,13 @@ async fn control(sender: Arc<ArrayQueue<Command>>, mut telem: Receiver<SensorDat
|
|||
let mut buf = vec![0; len as usize];
|
||||
timeout(Duration::from_millis(100), read.read_exact(&mut buf)).await??;
|
||||
|
||||
let cmd: Command = postcard::from_bytes(&buf)?;
|
||||
let cmd: RobotState = postcard::from_bytes(&buf)?;
|
||||
|
||||
sender.force_push(cmd);
|
||||
sender.send(cmd)?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_telem(state: Arc<RwLock<RobotState>>, mut telem: Receiver<SensorData>) -> Result<()> {
|
||||
async fn update_telem(state: Arc<RwLock<RobotInfo>>, mut telem: Receiver<SensorData>) -> Result<()> {
|
||||
loop {
|
||||
let telem = telem.recv().await?;
|
||||
println!("sensor {telem:?}");
|
||||
|
|
@ -144,26 +125,3 @@ async fn update_telem(state: Arc<RwLock<RobotState>>, mut telem: Receiver<Sensor
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
trait Brownout {
|
||||
/// limit exertion to factor of full output (0 to 1)
|
||||
fn brownout(self, factor: f32) -> Self;
|
||||
}
|
||||
|
||||
impl Brownout for Command {
|
||||
fn brownout(self, factor: f32) -> Self {
|
||||
// nothing is preserved
|
||||
match self {
|
||||
Self::Twist(fwd, rot) => Self::Twist(
|
||||
fwd.clamp(-factor, factor),
|
||||
rot.clamp(-factor, factor)
|
||||
),
|
||||
Self::SetLed(rgb) => {
|
||||
Self::SetLed(rgb.simd_min(Vector3::repeat((factor * u8::MAX as f32) as u8)))
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue