increase command flexibility
This commit is contained in:
@@ -4,9 +4,9 @@ version = "0.0.1"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.100"
|
||||
log = { version = "0.4.28", features = ["max_level_trace", "release_max_level_debug"] }
|
||||
chrono = "0.4.42"
|
||||
anyhow = { workspace = true }
|
||||
log = { workspace = true, features = ["max_level_trace", "release_max_level_debug"] }
|
||||
chrono = { workspace = true }
|
||||
embedded-hal = "1.0.0"
|
||||
embedded-hal-bus = { version = "0.3.0", features = ["std"] }
|
||||
embedded-hal-mock = { version = "0.11.1", optional = true }
|
||||
@@ -14,7 +14,7 @@ rpi-pal = { version = "0.22.2", features = ["hal"], optional = true }
|
||||
hex = "0.4.3"
|
||||
crc = "3.3.0"
|
||||
nautilus_common = { path = "../common" }
|
||||
ciborium = { version = "0.2.2" }
|
||||
postcard = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
embedded-hal-mock = { version = "0.11.1" }
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use crate::scheduler::{CyclicTask, TaskHandle};
|
||||
use anyhow::Result;
|
||||
use log::{error, trace};
|
||||
use nautilus_common::command::Command;
|
||||
use log::{error, trace, warn};
|
||||
use nautilus_common::command::{CommandHeader, SetPin, ValidPriorityCommand};
|
||||
use nautilus_common::telemetry::{Telemetry, TelemetryMessage};
|
||||
use nautilus_common::udp::{UdpRecvCborError, UdpSocketExt};
|
||||
use nautilus_common::udp::{UdpRecvPostcardError, UdpSocketExt};
|
||||
use std::any::type_name;
|
||||
use std::fmt::Debug;
|
||||
use std::io::Cursor;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -26,15 +25,47 @@ impl TelemetrySender {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CommsTask<A: ToSocketAddrs + Debug> {
|
||||
pub struct CommsTask<A, SetPinA, SetPinB>
|
||||
where
|
||||
A: ToSocketAddrs + Debug,
|
||||
SetPinA: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
SetPinB: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
{
|
||||
udp: UdpSocket,
|
||||
ground_address: A,
|
||||
running: Arc<AtomicBool>,
|
||||
set_pin_a: SetPinA,
|
||||
set_pin_b: SetPinB,
|
||||
}
|
||||
|
||||
impl<A: ToSocketAddrs + Debug> CommsTask<A> {
|
||||
pub fn new(local_port: u16, ground_address: A, running: Arc<AtomicBool>) -> Result<Self> {
|
||||
impl<A, SetPinA, SetPinB> Debug for CommsTask<A, SetPinA, SetPinB>
|
||||
where
|
||||
A: ToSocketAddrs + Debug,
|
||||
SetPinA: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
SetPinB: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"CommsTask {{ udp: {:?}, ground_address: {:?}, running: {:?} }}",
|
||||
self.udp, self.ground_address, self.running
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, SetPinA, SetPinB> CommsTask<A, SetPinA, SetPinB>
|
||||
where
|
||||
A: ToSocketAddrs + Debug,
|
||||
SetPinA: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
SetPinB: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
{
|
||||
pub fn new(
|
||||
local_port: u16,
|
||||
ground_address: A,
|
||||
running: Arc<AtomicBool>,
|
||||
set_pin_a: SetPinA,
|
||||
set_pin_b: SetPinB,
|
||||
) -> Result<Self> {
|
||||
trace!(
|
||||
"CommsTask::new<A={}>(local_port: {local_port}, ground_address: {ground_address:?})",
|
||||
type_name::<A>()
|
||||
@@ -47,11 +78,18 @@ impl<A: ToSocketAddrs + Debug> CommsTask<A> {
|
||||
udp,
|
||||
ground_address,
|
||||
running,
|
||||
set_pin_a,
|
||||
set_pin_b,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: ToSocketAddrs + Debug> CyclicTask for CommsTask<A> {
|
||||
impl<A, SetPinA, SetPinB> CyclicTask for CommsTask<A, SetPinA, SetPinB>
|
||||
where
|
||||
A: ToSocketAddrs + Debug,
|
||||
SetPinA: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
SetPinB: Fn(ValidPriorityCommand<SetPin>) + Send + Sync,
|
||||
{
|
||||
type Message = Telemetry;
|
||||
type Data = ();
|
||||
|
||||
@@ -67,13 +105,55 @@ impl<A: ToSocketAddrs + Debug> CyclicTask for CommsTask<A> {
|
||||
"CommsTask<A={}>::step(self: {self:?}, receiver: {receiver:?}, step_time: {step_time:?})",
|
||||
type_name::<A>()
|
||||
);
|
||||
let mut buffer = Cursor::new([0u8; 512]);
|
||||
let mut buffer = [0u8; 512];
|
||||
|
||||
match self.udp.recv_cbor::<Command, _>(&mut buffer) {
|
||||
Ok((cmd, _)) => match cmd {
|
||||
Command::Shutdown => self.running.store(false, Ordering::Relaxed),
|
||||
match self.udp.recv_postcard::<CommandHeader>(&mut buffer) {
|
||||
Ok((cmd, _)) => match cmd.name {
|
||||
"/shutdown" => match postcard::take_from_bytes::<()>(cmd.data) {
|
||||
Ok(((), remainder)) => {
|
||||
if remainder.is_empty() {
|
||||
self.running.store(false, Ordering::Relaxed);
|
||||
} else {
|
||||
error!("shutdown has extra data");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse ShutdownCommand {e}");
|
||||
}
|
||||
},
|
||||
"/mcp23017a/set" => {
|
||||
match postcard::take_from_bytes::<ValidPriorityCommand<SetPin>>(cmd.data) {
|
||||
Ok((set_pin, remainder)) => {
|
||||
if remainder.is_empty() {
|
||||
(self.set_pin_a)(set_pin);
|
||||
} else {
|
||||
error!("set pin has extra data");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse SetPin {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
"/mcp23017b/set" => {
|
||||
match postcard::take_from_bytes::<ValidPriorityCommand<SetPin>>(cmd.data) {
|
||||
Ok((set_pin, remainder)) => {
|
||||
if remainder.is_empty() {
|
||||
(self.set_pin_b)(set_pin);
|
||||
} else {
|
||||
error!("set pin has extra data");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse SetPin {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
warn!("Unknown Command: {}", cmd.name);
|
||||
}
|
||||
},
|
||||
Err(UdpRecvCborError::NoData) => {}
|
||||
Err(UdpRecvPostcardError::NoData) => {}
|
||||
Err(err) => {
|
||||
error!("Rx error: {err}");
|
||||
}
|
||||
@@ -81,7 +161,10 @@ impl<A: ToSocketAddrs + Debug> CyclicTask for CommsTask<A> {
|
||||
|
||||
// Intentionally ignore Err case
|
||||
while let Ok(tlm) = receiver.try_recv() {
|
||||
if let Err(err) = self.udp.send_cbor(&tlm, &mut buffer, &self.ground_address) {
|
||||
if let Err(err) = self
|
||||
.udp
|
||||
.send_postcard(&tlm, &mut buffer, &self.ground_address)
|
||||
{
|
||||
error!("Tx Error: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,9 +107,7 @@ impl PinData {
|
||||
// Do this twice to check both the current and the current next
|
||||
// If the current is currently invalid, we'd upgrade the next to current
|
||||
for _ in 0..2 {
|
||||
let is_current_valid = self
|
||||
.valid_until
|
||||
.is_some_and(|current| current >= now);
|
||||
let is_current_valid = self.valid_until.is_some_and(|current| current >= now);
|
||||
if is_current_valid {
|
||||
self.value = self.state;
|
||||
return;
|
||||
@@ -135,10 +133,10 @@ impl PinData {
|
||||
);
|
||||
let can_replace_current = self
|
||||
.valid_until
|
||||
.is_none_or(|current| current <= valid_until);
|
||||
let can_replace_next = self
|
||||
.next_validity
|
||||
.is_none_or(|next| next <= valid_until);
|
||||
.is_none_or(|current| current <= valid_until)
|
||||
|| self.priority == priority;
|
||||
let can_replace_next = self.next_validity.is_none_or(|next| next <= valid_until)
|
||||
|| self.next_priority == priority;
|
||||
|
||||
if priority >= self.priority {
|
||||
// This is now the highest priority thing (or most recent of equal priority)
|
||||
|
||||
@@ -158,7 +158,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[allow(clippy::identity_op)]
|
||||
pub(super) fn write(&self, address: u32, data: Mct8316AVData) -> Result<()> {
|
||||
trace!("Mct8316AVDriver::write(self: {self:?}, address: {address:06x}, data: {data})");
|
||||
|
||||
@@ -78,33 +78,31 @@ where
|
||||
pub fn set_isd_config(self, isd_config: IsdConfig) -> Result<Self> {
|
||||
trace!("Mct8316AVEeprom::set_isd_config(self: {self:?}, isd_config: {isd_config:?})");
|
||||
|
||||
let expected_value = if isd_config.enable_isd { 0x4000_0000 } else { 0 }
|
||||
| if isd_config.enable_brake {
|
||||
0x2000_0000
|
||||
} else {
|
||||
0
|
||||
}
|
||||
| if isd_config.enable_high_impedance {
|
||||
0x1000_0000
|
||||
} else {
|
||||
0
|
||||
}
|
||||
| if isd_config.enable_reverse_drive {
|
||||
0x0800_0000
|
||||
} else {
|
||||
0
|
||||
}
|
||||
| if isd_config.enable_resynchronization {
|
||||
0x0400_0000
|
||||
} else {
|
||||
0
|
||||
}
|
||||
| if isd_config.enable_stationary_brake {
|
||||
0x0200_0000
|
||||
} else {
|
||||
0
|
||||
}
|
||||
| (((isd_config.stationary_detect_threshold as u32) & 0x7) << 22)
|
||||
let expected_value = if isd_config.enable_isd {
|
||||
0x4000_0000
|
||||
} else {
|
||||
0
|
||||
} | if isd_config.enable_brake {
|
||||
0x2000_0000
|
||||
} else {
|
||||
0
|
||||
} | if isd_config.enable_high_impedance {
|
||||
0x1000_0000
|
||||
} else {
|
||||
0
|
||||
} | if isd_config.enable_reverse_drive {
|
||||
0x0800_0000
|
||||
} else {
|
||||
0
|
||||
} | if isd_config.enable_resynchronization {
|
||||
0x0400_0000
|
||||
} else {
|
||||
0
|
||||
} | if isd_config.enable_stationary_brake {
|
||||
0x0200_0000
|
||||
} else {
|
||||
0
|
||||
} | (((isd_config.stationary_detect_threshold as u32) & 0x7) << 22)
|
||||
| ((isd_config.brake_mode as u32) & 0x1) << 21
|
||||
| ((isd_config.brake_config as u32) & 0x1) << 20
|
||||
| ((isd_config.brake_current_threshold as u32) & 0x7) << 17
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
use embedded_hal::digital::PinState;
|
||||
use nautilus_common::command::{SetPin, ValidPriorityCommand};
|
||||
use std::time::Instant;
|
||||
|
||||
pub trait PinDevice {
|
||||
fn set_pin(&self, pin: u8, value: PinState, valid_until: Instant, priority: u8);
|
||||
|
||||
fn new_pinset_callback(self) -> impl Fn(ValidPriorityCommand<SetPin>)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
move |cmd| {
|
||||
self.set_pin(
|
||||
cmd.pin,
|
||||
cmd.value.into(),
|
||||
cmd.get_valid_until_instant(),
|
||||
cmd.priority,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Pin {
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
#![warn(
|
||||
clippy::all,
|
||||
clippy::pedantic,
|
||||
)]
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
use crate::comms::CommsTask;
|
||||
use crate::hardware::Hardware;
|
||||
use crate::hardware::channelization::{LED_A, LED_B};
|
||||
use crate::hardware::initialize;
|
||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017State, Mcp23017Task};
|
||||
use crate::hardware::mct8316a::Mct8316a;
|
||||
use crate::hardware::pin::Pin;
|
||||
use crate::hardware::pin::PinDevice;
|
||||
use crate::scheduler::Scheduler;
|
||||
use crate::state_vector::StateVector;
|
||||
use anyhow::Result;
|
||||
use embedded_hal::digital::PinState;
|
||||
use embedded_hal::pwm::SetDutyCycle;
|
||||
use log::{debug, info};
|
||||
use log::info;
|
||||
use nautilus_common::add_ctrlc_handler;
|
||||
use nautilus_common::telemetry::{SwitchBank, TelemetryMessage};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread::sleep;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Duration;
|
||||
|
||||
mod hardware;
|
||||
|
||||
@@ -71,7 +66,13 @@ pub fn run() -> Result<()> {
|
||||
|
||||
let comms = s.run_cyclic(
|
||||
"comms-task",
|
||||
CommsTask::new(15000, "nautilus-ground:14000", running.clone())?,
|
||||
CommsTask::new(
|
||||
15000,
|
||||
"nautilus-ground:14000",
|
||||
running.clone(),
|
||||
task_a.clone().new_pinset_callback(),
|
||||
task_b.clone().new_pinset_callback(),
|
||||
)?,
|
||||
10,
|
||||
)?;
|
||||
|
||||
@@ -95,36 +96,9 @@ pub fn run() -> Result<()> {
|
||||
1,
|
||||
)?;
|
||||
|
||||
let mut led_pin_a = LED_A.get_pin(&task_a, &task_b);
|
||||
let mut led_pin_b = LED_B.get_pin(&task_a, &task_b);
|
||||
|
||||
info!("Starting Main Loop");
|
||||
loop {
|
||||
debug!("A On");
|
||||
led_pin_a.set(PinState::High, Instant::now() + Duration::from_secs(2), 0);
|
||||
sleep(Duration::from_secs(1));
|
||||
if !running.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
|
||||
debug!("B On");
|
||||
led_pin_b.set(PinState::High, Instant::now() + Duration::from_secs(2), 0);
|
||||
sleep(Duration::from_secs(1));
|
||||
if !running.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
|
||||
debug!("A Off");
|
||||
sleep(Duration::from_secs(1));
|
||||
if !running.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
|
||||
debug!("B Off");
|
||||
sleep(Duration::from_secs(1));
|
||||
if !running.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
while running.load(Ordering::Relaxed) {
|
||||
sleep(Duration::from_millis(100));
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
|
||||
@@ -51,8 +51,7 @@ where
|
||||
type Message = ();
|
||||
type Data = ();
|
||||
|
||||
fn get_data(&self) -> Self::Data {
|
||||
}
|
||||
fn get_data(&self) -> Self::Data {}
|
||||
|
||||
fn step(&mut self, _receiver: &Receiver<Self::Message>, _step_time: Instant) {
|
||||
self();
|
||||
|
||||
Reference in New Issue
Block a user