Initial node implementation #4

Merged
zack merged 51 commits from develop into master 2023-07-19 18:09:13 +00:00
42 changed files with 1546 additions and 44 deletions
Showing only changes of commit 2a0013481c - Show all commits

View File

@ -142,6 +142,8 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
let publisher_path: Path = parse_quote!(physical_node::transducer::Publisher);
let cellview_path: Path = parse_quote!(physical_node::cell::CellView);
let error_path: Path = parse_quote!(physical_node::CriticalError);
let expanded = quote! {
// ----- Stateful struct ----------------------------------
#vis struct #stateful_variant_ident #og_generics #og_where_clause {
@ -153,11 +155,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
impl #og_impl_generics #poll_path for #stateful_variant_ident #og_type_generics #og_where_clause {
type Value = #value_type_path;
#[inline(always)]
async fn poll(&self) -> Self::Value {
let value = self.poll.poll().await;
#[inline]
async fn poll(&self) -> Result<Self::Value, #error_path> {
let result = self.poll.poll().await;
if let Ok(value) = result {
self.state.update(value);
value
}
result
}
}
@ -187,11 +191,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
impl #publ_impl_generics #poll_path for #publish_variant_ident #publ_type_generics #publ_where_clause {
type Value = #value_type_path;
#[inline(always)]
async fn poll(&self) -> Self::Value {
let value = self.poll.poll().await;
#[inline]
async fn poll(&self) -> Result<Self::Value, #error_path> {
let result = self.poll.poll().await;
if let Ok(value) = result {
self.publisher.update(value);
value
}
result
}
}
@ -221,11 +227,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
type Value = #value_type_path;
#[inline]
async fn poll(&self) -> Self::Value {
let value = self.poll.poll().await;
async fn poll(&self) -> Result<Self::Value, #error_path> {
let result = self.poll.poll().await;
if let Ok(value) = result {
self.state.update(value);
self.publisher.update(value);
value
}
result
}
}

View File

@ -1,6 +1,7 @@
#![feature(async_fn_in_trait, impl_trait_projections)]
use node_poll_variants::PollVariants;
use physical_node::CriticalError;
use physical_node::transducer::input::Poll;
#[derive(PollVariants)]
@ -21,8 +22,8 @@ where
{
type Value = SecondT;
async fn poll(&self) -> Self::Value {
self.second
async fn poll(&self) -> Result<Self::Value, CriticalError> {
Ok(self.second)
}
}

View File

@ -6,3 +6,5 @@ pub mod transducer;
pub mod cell {
pub use physical::cell::*;
}
pub use physical::CriticalError;

View File

@ -1,11 +1,12 @@
use ads1256::{
AdControl, Ads1256, BlockingDelay, DataRate, Multiplexer, OutputPin, SpiBus, Status, Wait,
AdControl, Ads1256, BlockingDelay, DataRate, Gain, Multiplexer, OutputPin, SpiBus, Status, Wait,
};
use core::ops::DerefMut;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex;
use node_poll_variants::PollVariants;
use physical_node::transducer::input::Poll;
use physical_node::CriticalError;
use uom::si::f32;
#[derive(PollVariants)]
@ -36,8 +37,9 @@ where
{
type Value = f32::ElectricPotential;
async fn poll(&self) -> Self::Value {
self.ads1256
async fn poll(&self) -> Result<Self::Value, CriticalError> {
let result = self
.ads1256
.lock()
.await
.deref_mut()
@ -49,13 +51,20 @@ where
self.input_mod.data_rate(),
true,
)
.await
.await;
match result {
Ok(conversion) => Ok(conversion.to_voltage(self.input_mod.gain())),
Err(_) => Err(CriticalError::Communication),
}
}
}
pub trait ModInput {
pub trait ModInput: Copy {
fn multiplexer(self) -> Multiplexer;
fn gain(self) -> Gain;
fn status(self) -> Option<Status>;
fn ad_control(self) -> Option<AdControl>;
@ -64,8 +73,12 @@ pub trait ModInput {
}
/// buffer
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatus {
pub multiplexer: Multiplexer,
/// Only used for converting to voltage, this value must match what is set on the ADS1256, it
/// will not *be* set unlike the other fields.
pub gain: Gain,
pub status: Status,
}
@ -75,6 +88,11 @@ impl ModInput for ModInStatus {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)]
fn status(self) -> Option<Status> {
Some(self.status)
@ -92,6 +110,7 @@ impl ModInput for ModInStatus {
}
/// gain
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAdControl {
pub multiplexer: Multiplexer,
pub ad_control: AdControl,
@ -103,6 +122,11 @@ impl ModInput for ModInAdControl {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)]
fn status(self) -> Option<Status> {
None
@ -120,8 +144,12 @@ impl ModInput for ModInAdControl {
}
/// data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInDataRate {
pub multiplexer: Multiplexer,
/// Only used for converting to voltage, this value must match what is set on the ADS1256, it
/// will not *be* set unlike the other fields.
pub gain: Gain,
pub data_rate: DataRate,
}
@ -131,6 +159,11 @@ impl ModInput for ModInDataRate {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)]
fn status(self) -> Option<Status> {
None
@ -148,6 +181,7 @@ impl ModInput for ModInDataRate {
}
/// buffer, gain
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatAdc {
pub multiplexer: Multiplexer,
pub status: Status,
@ -160,6 +194,11 @@ impl ModInput for ModInStatAdc {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)]
fn status(self) -> Option<Status> {
Some(self.status)
@ -177,8 +216,12 @@ impl ModInput for ModInStatAdc {
}
/// buffer, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatDrate {
pub multiplexer: Multiplexer,
/// Only used for converting to voltage, this value must match what is set on the ADS1256, it
/// will not *be* set unlike the other fields.
pub gain: Gain,
pub status: Status,
pub data_rate: DataRate,
}
@ -189,6 +232,11 @@ impl ModInput for ModInStatDrate {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)]
fn status(self) -> Option<Status> {
Some(self.status)
@ -206,6 +254,7 @@ impl ModInput for ModInStatDrate {
}
// gain, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAdcDrate {
pub multiplexer: Multiplexer,
pub ad_control: AdControl,
@ -218,6 +267,11 @@ impl ModInput for ModInAdcDrate {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)]
fn status(self) -> Option<Status> {
None
@ -235,6 +289,7 @@ impl ModInput for ModInAdcDrate {
}
/// buffer, gain, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAll {
pub multiplexer: Multiplexer,
pub status: Status,
@ -248,6 +303,11 @@ impl ModInput for ModInAll {
self.multiplexer
}
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)]
fn status(self) -> Option<Status> {
Some(self.status)

View File

@ -5,4 +5,4 @@ pub mod transducer;
pub mod cell;
mod error;
pub use error::*;
pub use error::CriticalError;

View File

@ -1,5 +1,7 @@
use crate::CriticalError;
pub trait Poll {
type Value: Copy;
async fn poll(&self) -> Self::Value;
async fn poll(&self) -> Result<Self::Value, CriticalError>;
}