Compare commits

...

9 Commits

14 changed files with 463 additions and 368 deletions

43
Cargo.lock generated
View File

@ -20,6 +20,12 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]] [[package]]
name = "bare-metal" name = "bare-metal"
version = "0.2.5" version = "0.2.5"
@ -149,14 +155,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "857afb5ee9e767c3a73b2ad7212b6deea0c3761a27db1e20ea0ed57ee352cfef" checksum = "857afb5ee9e767c3a73b2ad7212b6deea0c3761a27db1e20ea0ed57ee352cfef"
[[package]] [[package]]
name = "lexical-core" name = "libm"
version = "0.7.4" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
dependencies = [
"bitflags",
"cfg-if",
]
[[package]] [[package]]
name = "log" name = "log"
@ -195,6 +197,16 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "num-traits"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [
"autocfg",
"libm",
]
[[package]] [[package]]
name = "panic-abort" name = "panic-abort"
version = "0.3.2" version = "0.3.2"
@ -235,6 +247,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.2.3" version = "0.2.3"
@ -290,9 +308,9 @@ dependencies = [
[[package]] [[package]]
name = "stm32f4" name = "stm32f4"
version = "0.9.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88640ad08c62e0651a1320187f38c3655d025ed580a10f0e4d85a2cc4829069f" checksum = "44a3d6c58b14e63926273694e7dd644894513c5e35ce6928c4657ddb62cae976"
dependencies = [ dependencies = [
"bare-metal", "bare-metal",
"cortex-m", "cortex-m",
@ -302,8 +320,8 @@ dependencies = [
[[package]] [[package]]
name = "stm32f4xx-hal" name = "stm32f4xx-hal"
version = "0.6.0" version = "0.7.0"
source = "git+https://github.com/thalesfragoso/stm32f4xx-hal?branch=pwm-impl#ef939935b90581553dc03f9146d05510b3ceba58" source = "git+https://github.com/thalesfragoso/stm32f4xx-hal?branch=pwm-impl#cfd073e094daa9be9dd2b0a1f859a4e1c6be2b77"
dependencies = [ dependencies = [
"bare-metal", "bare-metal",
"cast", "cast",
@ -311,6 +329,7 @@ dependencies = [
"cortex-m-rt", "cortex-m-rt",
"embedded-hal", "embedded-hal",
"nb", "nb",
"rand_core",
"stm32f4", "stm32f4",
"void", "void",
] ]
@ -338,9 +357,9 @@ dependencies = [
"cortex-m-rt", "cortex-m-rt",
"embedded-hal", "embedded-hal",
"hash2hwaddr", "hash2hwaddr",
"lexical-core",
"log", "log",
"nom", "nom",
"num-traits",
"panic-abort", "panic-abort",
"panic-semihosting", "panic-semihosting",
"smoltcp", "smoltcp",

View File

@ -30,7 +30,7 @@ hash2hwaddr = { version = "0.0", optional = true }
bit_field = "0.10" bit_field = "0.10"
byteorder = { version = "1", default-features = false } byteorder = { version = "1", default-features = false }
nom = { version = "5", default-features = false } nom = { version = "5", default-features = false }
lexical-core = { version = "0.7", default-features = false } num-traits = { version = "0.2", default-features = false, features = ["libm"] }
[features] [features]
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]

View File

@ -14,6 +14,8 @@ pub const SPI_MODE: spi::Mode = spi::Mode {
/// 30 MHz /// 30 MHz
pub const SPI_CLOCK: MegaHertz = MegaHertz(30); pub const SPI_CLOCK: MegaHertz = MegaHertz(30);
pub const MAX_VALUE: u32 = 0x20000;
/// [AD5680](https://www.analog.com/media/en/technical-documentation/data-sheets/AD5680.pdf) DAC /// [AD5680](https://www.analog.com/media/en/technical-documentation/data-sheets/AD5680.pdf) DAC
pub struct Dac<SPI: Transfer<u8>, S: OutputPin> { pub struct Dac<SPI: Transfer<u8>, S: OutputPin> {
spi: SPI, spi: SPI,

View File

@ -1,7 +1,7 @@
use core::fmt; use core::fmt;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_hal::blocking::spi::Transfer; use embedded_hal::blocking::spi::Transfer;
use log::{info, warn}; use log::info;
use super::checksum::{ChecksumMode, Checksum}; use super::checksum::{ChecksumMode, Checksum};
use super::AdcError; use super::AdcError;
use super::{ use super::{
@ -129,7 +129,7 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
} }
/// Get data /// Get data
pub fn read_data(&mut self) -> Result<i32, AdcError<SPI::Error>> { pub fn read_data(&mut self) -> Result<u32, AdcError<SPI::Error>> {
self.read_reg(&regs::Data) self.read_reg(&regs::Data)
.map(|data| data.data()) .map(|data| data.data())
} }

View File

@ -1,7 +1,7 @@
use core::fmt; use core::fmt;
use lexical_core::Float; use num_traits::float::Float;
use stm32f4xx_hal::{ use stm32f4xx_hal::{
time::{MegaHertz, U32Ext}, time::MegaHertz,
spi, spi,
}; };
@ -19,6 +19,8 @@ pub const SPI_MODE: spi::Mode = spi::Mode {
/// 2 MHz /// 2 MHz
pub const SPI_CLOCK: MegaHertz = MegaHertz(2); pub const SPI_CLOCK: MegaHertz = MegaHertz(2);
pub const MAX_VALUE: u32 = 0xFF_FFFF;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum AdcError<SPI> { pub enum AdcError<SPI> {
SPI(SPI), SPI(SPI),

View File

@ -160,16 +160,10 @@ impl if_mode::Data {
def_reg!(Data, data, 0x04, 3); def_reg!(Data, data, 0x04, 3);
impl data::Data { impl data::Data {
pub fn data(&self) -> i32 { pub fn data(&self) -> u32 {
let raw = (u32::from(self.0[0]) << 16) |
(u32::from(self.0[0]) << 16) | (u32::from(self.0[1]) << 8) |
(u32::from(self.0[1]) << 8) | u32::from(self.0[2])
u32::from(self.0[2]);
if raw & 0x80_0000 != 0 {
((raw & 0x7F_FFFF) | 0x8000_0000) as i32
} else {
raw as i32
}
} }
} }
@ -205,21 +199,6 @@ impl channel::Data {
} }
reg_bits!(a_in_neg, set_a_in_neg, 1, 0..=4, Input, reg_bits!(a_in_neg, set_a_in_neg, 1, 0..=4, Input,
"Which input is connected to negative input of this channel"); "Which input is connected to negative input of this channel");
// const PROPS: &'static [Property<Self>] = &[
// Property::named("enable")
// .readable(&|self_: &Self| self_.enabled().into())
// .writebale(&|self_: &mut Self, value| self_.set_enabled(value != 0)),
// Property::named("setup")
// .readable(&|self_: &Self| self_.0[0].get_bits(4..=5).into())
// .writeable(&|self_: &mut Self, value| {
// self_.0[0].set_bits(4..=5, value as u8);
// }),
// ];
// pub fn props() -> &'static [Property<Self>] {
// Self::PROPS
// }
} }
def_reg!(SetupCon, u8, setup_con, 0x20, 2); def_reg!(SetupCon, u8, setup_con, 0x20, 2);

39
src/channel_state.rs Normal file
View File

@ -0,0 +1,39 @@
use smoltcp::time::Instant;
use crate::{ad5680, ad7172, pid, steinhart_hart as sh};
pub struct ChannelState {
pub adc_data: Option<u32>,
pub adc_time: Instant,
pub dac_value: u32,
pub pid_enabled: bool,
pub pid: pid::Controller,
pub sh: sh::Parameters,
}
impl Default for ChannelState {
fn default() -> Self {
ChannelState {
adc_data: None,
adc_time: Instant::from_secs(0),
dac_value: 0,
pid_enabled: false,
pid: pid::Controller::new(pid::Parameters::default()),
sh: sh::Parameters::default(),
}
}
}
impl ChannelState {
/// Update PID state on ADC input, calculate new DAC output
pub fn update_adc(&mut self, now: Instant, adc_data: u32) {
self.adc_data = Some(adc_data);
self.adc_time = now;
// Update PID controller
let input = (adc_data as f64) / (ad7172::MAX_VALUE as f64);
let temperature = self.sh.get_temperature(input);
let output = self.pid.update(temperature);
self.dac_value = (output * (ad5680::MAX_VALUE as f64)) as u32;
}
}

View File

@ -1,15 +1,17 @@
use core::fmt; use core::fmt;
use core::num::ParseIntError;
use core::str::{from_utf8, Utf8Error};
use nom::{ use nom::{
IResult, IResult,
branch::alt, branch::alt,
bytes::complete::{is_a, tag, take_while1}, bytes::complete::{is_a, tag, take_while1},
character::{is_digit, complete::{char, one_of}}, character::{is_digit, complete::{char, one_of}},
combinator::{complete, map, opt, value}, combinator::{complete, map, opt, value},
sequence::{preceded, separated_pair}, sequence::preceded,
multi::{fold_many0, fold_many1}, multi::{fold_many0, fold_many1},
error::ErrorKind, error::ErrorKind,
}; };
use lexical_core as lexical; use num_traits::{Num, ParseFloatError};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -17,7 +19,10 @@ pub enum Error {
Parser(ErrorKind), Parser(ErrorKind),
Incomplete, Incomplete,
UnexpectedInput(u8), UnexpectedInput(u8),
ParseNumber(lexical::Error) Utf8(Utf8Error),
ParseInt(ParseIntError),
// `num_traits::ParseFloatError` does not impl Clone
ParseFloat,
} }
impl<'t> From<nom::Err<(&'t [u8], ErrorKind)>> for Error { impl<'t> From<nom::Err<(&'t [u8], ErrorKind)>> for Error {
@ -33,9 +38,21 @@ impl<'t> From<nom::Err<(&'t [u8], ErrorKind)>> for Error {
} }
} }
impl From<lexical::Error> for Error { impl From<Utf8Error> for Error {
fn from(e: lexical::Error) -> Self { fn from(e: Utf8Error) -> Self {
Error::ParseNumber(e) Error::Utf8(e)
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Error::ParseInt(e)
}
}
impl From<ParseFloatError> for Error {
fn from(_: ParseFloatError) -> Self {
Error::ParseFloat
} }
} }
@ -52,10 +69,17 @@ impl fmt::Display for Error {
"parser: ".fmt(fmt)?; "parser: ".fmt(fmt)?;
(e as &dyn core::fmt::Debug).fmt(fmt) (e as &dyn core::fmt::Debug).fmt(fmt)
} }
Error::ParseNumber(e) => { Error::Utf8(e) => {
"parsing number: ".fmt(fmt)?; "utf8: ".fmt(fmt)?;
(e as &dyn core::fmt::Debug).fmt(fmt) (e as &dyn core::fmt::Debug).fmt(fmt)
} }
Error::ParseInt(e) => {
"parsing int: ".fmt(fmt)?;
(e as &dyn core::fmt::Debug).fmt(fmt)
}
Error::ParseFloat => {
"parsing float".fmt(fmt)
}
} }
} }
} }
@ -85,30 +109,28 @@ pub enum PidParameter {
/// Steinhart-Hart equation parameter /// Steinhart-Hart equation parameter
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ShParameter { pub enum ShParameter {
A, T0,
B, B,
C, R0,
ParallelR,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct PwmConfig { pub enum PwmPin {
pub width: u16, ISet,
pub total: u16, MaxIPos,
MaxINeg,
MaxV,
} }
#[derive(Debug, Clone, PartialEq)] impl PwmPin {
pub enum PwmMode { pub fn name(&self) -> &'static str {
Manual(PwmConfig), match self {
Pid, PwmPin::ISet => "i_set",
} PwmPin::MaxIPos => "max_i_pos",
PwmPin::MaxINeg => "max_i_neg",
#[derive(Debug, Clone, PartialEq)] PwmPin::MaxV => "max_v",
pub enum PwmSetup { }
ISet(PwmMode), }
MaxIPos(PwmConfig),
MaxINeg(PwmConfig),
MaxV(PwmConfig),
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -116,10 +138,17 @@ pub enum Command {
Quit, Quit,
Show(ShowCommand), Show(ShowCommand),
Reporting(bool), Reporting(bool),
/// PWM parameter setting
Pwm { Pwm {
channel: usize, channel: usize,
setup: PwmSetup, pin: PwmPin,
duty: u32,
}, },
/// Enable PID control for `i_set`
PwmPid {
channel: usize,
},
/// PID parameter setting
Pid { Pid {
channel: usize, channel: usize,
parameter: PidParameter, parameter: PidParameter,
@ -149,11 +178,15 @@ fn whitespace(input: &[u8]) -> IResult<&[u8], ()> {
fold_many1(char(' '), (), |(), _| ())(input) fold_many1(char(' '), (), |(), _| ())(input)
} }
fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u16, Error>> { fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
take_while1(is_digit)(input) take_while1(is_digit)(input)
.map(|(input, digits)| { .map(|(input, digits)| {
let result = lexical::parse(digits) let result =
.map_err(|e| e.into()); from_utf8(digits)
.map_err(|e| e.into())
.and_then(|digits| u32::from_str_radix(digits, 10)
.map_err(|e| e.into())
);
(input, result) (input, result)
}) })
} }
@ -162,9 +195,13 @@ fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
let (input, sign) = opt(is_a("-"))(input)?; let (input, sign) = opt(is_a("-"))(input)?;
let negative = sign.is_some(); let negative = sign.is_some();
let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?; let (input, digits) = take_while1(|c| is_digit(c) || c == '.' as u8)(input)?;
let result = lexical::parse(digits) let result =
.map(|result: f64| if negative { -result } else { result }) from_utf8(digits)
.map_err(|e| e.into()); .map_err(|e| e.into())
.and_then(|digits| f64::from_str_radix(digits, 10)
.map_err(|e| e.into())
)
.map(|result: f64| if negative { -result } else { result });
Ok((input, result)) Ok((input, result))
} }
@ -202,82 +239,77 @@ fn report(input: &[u8]) -> IResult<&[u8], Command> {
)(input) )(input)
} }
/// `pwm ... <width> <total>` - Set pwm duty cycle fn pwm_setup(input: &[u8]) -> IResult<&[u8], Result<(PwmPin, u32), Error>> {
fn pwm_config(input: &[u8]) -> IResult<&[u8], Result<PwmConfig, Error>> { let result_with_pin = |pin: PwmPin|
let (input, width) = unsigned(input)?; move |result: Result<u32, Error>|
let width = match width { result.map(|duty| (pin, duty));
Ok(width) => width,
Err(e) => return Ok((input, Err(e.into()))),
};
let (input, _) = whitespace(input)?;
let (input, total) = unsigned(input)?;
let total = match total {
Ok(total) => total,
Err(e) => return Ok((input, Err(e.into()))),
};
Ok((input, Ok(PwmConfig { width, total })))
}
fn pwm_setup(input: &[u8]) -> IResult<&[u8], Result<PwmSetup, Error>> {
alt(( alt((
map( map(
preceded( preceded(
tag("max_i_pos"), tag("max_i_pos"),
preceded( preceded(
whitespace, whitespace,
pwm_config unsigned
) )
), ),
|result| result.map(PwmSetup::MaxIPos) result_with_pin(PwmPin::MaxIPos)
), ),
map( map(
preceded( preceded(
tag("max_i_neg"), tag("max_i_neg"),
preceded( preceded(
whitespace, whitespace,
pwm_config unsigned
) )
), ),
|result| result.map(PwmSetup::MaxINeg) result_with_pin(PwmPin::MaxINeg)
), ),
map( map(
preceded( preceded(
tag("max_v"), tag("max_v"),
preceded( preceded(
whitespace, whitespace,
pwm_config unsigned
) )
), ),
|result| result.map(PwmSetup::MaxV) result_with_pin(PwmPin::MaxV)
), ),
map(pwm_config, |result| result.map(|config| { map(unsigned, result_with_pin(PwmPin::ISet)
PwmSetup::ISet(PwmMode::Manual(config)) ))
})) )(input)
))(input)
} }
/// `pwm <0-1> pid` - Set PWM to be controlled by PID /// `pwm <0-1> pid` - Set PWM to be controlled by PID
fn pwm_pid(input: &[u8]) -> IResult<&[u8], Result<PwmSetup, Error>> { fn pwm_pid(input: &[u8]) -> IResult<&[u8], ()> {
value(Ok(PwmSetup::ISet(PwmMode::Pid)), tag("pid"))(input) value((), tag("pid"))(input)
} }
fn pwm(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn pwm(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = tag("pwm")(input)?; let (input, _) = tag("pwm")(input)?;
alt(( alt((
preceded( |input| {
whitespace, let (input, _) = whitespace(input)?;
map( let (input, channel) = channel(input)?;
separated_pair( let (input, _) = whitespace(input)?;
channel, let (input, result) = alt((
whitespace, |input| {
alt(( let (input, ()) = pwm_pid(input)?;
pwm_pid, Ok((input, Ok(Command::PwmPid { channel })))
pwm_setup },
)) |input| {
), let (input, config) = pwm_setup(input)?;
|(channel, setup)| setup.map(|setup| Command::Pwm { channel, setup }) match config {
) Ok((pin, duty)) =>
), Ok((input, Ok(Command::Pwm { channel, pin, duty }))),
Err(e) =>
Ok((input, Err(e))),
}
},
))(input)?;
end(input)?;
Ok((input, result))
},
value(Ok(Command::Show(ShowCommand::Pwm)), end) value(Ok(Command::Show(ShowCommand::Pwm)), end)
))(input) ))(input)
} }
@ -320,10 +352,9 @@ fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Erro
let (input, channel) = channel(input)?; let (input, channel) = channel(input)?;
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
let (input, parameter) = let (input, parameter) =
alt((value(ShParameter::A, tag("a")), alt((value(ShParameter::T0, tag("t0")),
value(ShParameter::B, tag("b")), value(ShParameter::B, tag("b")),
value(ShParameter::C, tag("c")), value(ShParameter::R0, tag("r0"))
value(ShParameter::ParallelR, tag("parallel_r"))
))(input)?; ))(input)?;
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
let (input, value) = float(input)?; let (input, value) = float(input)?;
@ -426,58 +457,50 @@ mod test {
#[test] #[test]
fn parse_pwm_manual() { fn parse_pwm_manual() {
let command = Command::parse(b"pwm 1 16383 65535"); let command = Command::parse(b"pwm 1 16383");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 1, channel: 1,
setup: PwmSetup::ISet(PwmMode::Manual(PwmConfig { pin: PwmPin::ISet,
width: 16383, duty: 16383,
total: 65535,
})),
})); }));
} }
#[test] #[test]
fn parse_pwm_pid() { fn parse_pwm_pid() {
let command = Command::parse(b"pwm 0 pid"); let command = Command::parse(b"pwm 0 pid");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::PwmPid {
channel: 0, channel: 0,
setup: PwmSetup::ISet(PwmMode::Pid), pin: PwmPin::ISet,
})); }));
} }
#[test] #[test]
fn parse_pwm_max_i_pos() { fn parse_pwm_max_i_pos() {
let command = Command::parse(b"pwm 0 max_i_pos 7 13"); let command = Command::parse(b"pwm 0 max_i_pos 7");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
setup: PwmSetup::MaxIPos(PwmConfig { pin: PwmPin::MaxIPos,
width: 7, duty: 7,
total: 13,
}),
})); }));
} }
#[test] #[test]
fn parse_pwm_max_i_neg() { fn parse_pwm_max_i_neg() {
let command = Command::parse(b"pwm 0 max_i_neg 128 65535"); let command = Command::parse(b"pwm 0 max_i_neg 128");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
setup: PwmSetup::MaxINeg(PwmConfig { pin: PwmPin::MaxINeg,
width: 128, duty: 128,
total: 65535,
}),
})); }));
} }
#[test] #[test]
fn parse_pwm_max_v() { fn parse_pwm_max_v() {
let command = Command::parse(b"pwm 0 max_v 32768 65535"); let command = Command::parse(b"pwm 0 max_v 32768");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
setup: PwmSetup::MaxV(PwmConfig { pin: PwmPin::MaxV,
width: 32768, duty: 32768,
total: 65535,
}),
})); }));
} }
@ -515,10 +538,10 @@ mod test {
#[test] #[test]
fn parse_steinhart_hart_parallel_r() { fn parse_steinhart_hart_parallel_r() {
let command = Command::parse(b"s-h 1 parallel_r 23.05"); let command = Command::parse(b"s-h 1 t0 23.05");
assert_eq!(command, Ok(Command::SteinhartHart { assert_eq!(command, Ok(Command::SteinhartHart {
channel: 1, channel: 1,
parameter: ShParameter::ParallelR, parameter: ShParameter::T0,
value: 23.05, value: 23.05,
})); }));
} }

View File

@ -9,11 +9,15 @@ use panic_semihosting as _;
use log::{info, warn}; use log::{info, warn};
use core::ops::DerefMut;
use core::fmt::Write; use core::fmt::Write;
use cortex_m::asm::wfi; use cortex_m::asm::wfi;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embedded_hal::watchdog::{WatchdogEnable, Watchdog};
use stm32f4xx_hal::{ use stm32f4xx_hal::{
hal::{
self,
watchdog::{WatchdogEnable, Watchdog},
},
rcc::RccExt, rcc::RccExt,
watchdog::IndependentWatchdog, watchdog::IndependentWatchdog,
time::{U32Ext, MegaHertz}, time::{U32Ext, MegaHertz},
@ -36,28 +40,15 @@ use server::Server;
mod session; mod session;
use session::{CHANNELS, Session, SessionOutput}; use session::{CHANNELS, Session, SessionOutput};
mod command_parser; mod command_parser;
use command_parser::{Command, ShowCommand, PwmSetup, PwmMode}; use command_parser::{Command, ShowCommand, PwmPin};
mod timer; mod timer;
mod pid; mod pid;
mod steinhart_hart; mod steinhart_hart;
mod channel_state;
use channel_state::ChannelState;
#[derive(Clone, Copy, Debug)] const HSE: MegaHertz = MegaHertz(8);
struct ChannelState {
adc_data: Option<i32>,
adc_time: Instant,
}
impl Default for ChannelState {
fn default() -> Self {
ChannelState {
adc_data: None,
adc_time: Instant::from_secs(0),
}
}
}
#[cfg(not(feature = "semihosting"))] #[cfg(not(feature = "semihosting"))]
const WATCHDOG_INTERVAL: u32 = 100; const WATCHDOG_INTERVAL: u32 = 100;
#[cfg(feature = "semihosting")] #[cfg(feature = "semihosting")]
@ -65,12 +56,9 @@ const WATCHDOG_INTERVAL: u32 = 10_000;
#[cfg(not(feature = "generate-hwaddr"))] #[cfg(not(feature = "generate-hwaddr"))]
const NET_HWADDR: [u8; 6] = [0x02, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; const NET_HWADDR: [u8; 6] = [0x02, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
const TCP_PORT: u16 = 23; const TCP_PORT: u16 = 23;
const HSE: MegaHertz = MegaHertz(8);
/// Initialization and main loop /// Initialization and main loop
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@ -105,9 +93,10 @@ fn main() -> ! {
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap(); let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
let mut dac0 = ad5680::Dac::new(pins.dac0_spi, pins.dac0_sync); let mut dac0 = ad5680::Dac::new(pins.dac0_spi, pins.dac0_sync);
dac0.set(0); dac0.set(0).unwrap();
let mut dac1 = ad5680::Dac::new(pins.dac1_spi, pins.dac1_sync); let mut dac1 = ad5680::Dac::new(pins.dac1_spi, pins.dac1_sync);
dac1.set(0); dac1.set(0).unwrap();
let mut pwm = pins.pwm;
timer::setup(cp.SYST, clocks); timer::setup(cp.SYST, clocks);
@ -120,7 +109,9 @@ fn main() -> ! {
}; };
info!("Net hwaddr: {}", hwaddr); info!("Net hwaddr: {}", hwaddr);
let mut channel_states = [ChannelState::default(); CHANNELS]; let mut channel_states: [ChannelState; CHANNELS] = [
ChannelState::default(), ChannelState::default()
];
net::run(dp.ETHERNET_MAC, dp.ETHERNET_DMA, hwaddr, |iface| { net::run(dp.ETHERNET_MAC, dp.ETHERNET_DMA, hwaddr, |iface| {
Server::<Session>::run(iface, |server| { Server::<Session>::run(iface, |server| {
@ -138,8 +129,20 @@ fn main() -> ! {
let data = adc.read_data().unwrap(); let data = adc.read_data().unwrap();
let state = &mut channel_states[usize::from(channel)]; let state = &mut channel_states[usize::from(channel)];
state.adc_data = Some(data); state.update_adc(instant, data);
state.adc_time = instant;
if state.pid_enabled {
// Forward PID output to i_set DAC
match channel {
0 =>
dac0.set(state.dac_value).unwrap(),
1 =>
dac1.set(state.dac_value).unwrap(),
_ =>
unreachable!(),
}
}
server.for_each(|_, session| session.set_report_pending(channel.into())); server.for_each(|_, session| session.set_report_pending(channel.into()));
}); });
@ -147,7 +150,9 @@ fn main() -> ! {
server.for_each(|mut socket, session| { server.for_each(|mut socket, session| {
if ! socket.is_open() { if ! socket.is_open() {
let _ = socket.listen(TCP_PORT); let _ = socket.listen(TCP_PORT);
session.reset(); if session.is_dirty() {
session.reset();
}
} else if socket.can_send() && socket.can_recv() && socket.send_capacity() - socket.send_queue() > 128 { } else if socket.can_send() && socket.can_recv() && socket.send_capacity() - socket.send_queue() > 128 {
match socket.recv(|buf| session.feed(buf)) { match socket.recv(|buf| session.feed(buf)) {
Ok(SessionOutput::Nothing) => {} Ok(SessionOutput::Nothing) => {}
@ -167,170 +172,211 @@ fn main() -> ! {
socket, "t={} raw{}=0x{:06X}", socket, "t={} raw{}=0x{:06X}",
state.adc_time, channel, adc_data state.adc_time, channel, adc_data
); );
// TODO: show pwm status et al
} }
} }
} }
Command::Show(ShowCommand::Pid) => { Command::Show(ShowCommand::Pid) => {
// for (channel, state) in states.iter().enumerate() { for (channel, state) in channel_states.iter().enumerate() {
// let _ = writeln!(socket, "PID settings for channel {}", channel); let _ = writeln!(socket, "PID settings for channel {}", channel);
// let pid = &state.pid; let pid = &state.pid;
// let _ = writeln!(socket, "- target={:.4}", pid.get_target()); let _ = writeln!(socket, "- target={:.4}", pid.target);
// let p = pid.get_parameters(); macro_rules! show_pid_parameter {
// macro_rules! out { ($p: tt) => {
// ($p: tt) => { let _ = writeln!(
// let _ = writeln!(socket, "- {}={:.4}", stringify!($p), p.$p); socket, "- {}={:.4}",
// }; stringify!($p), pid.parameters.$p
// } );
// out!(kp); };
// out!(ki); }
// out!(kd); show_pid_parameter!(kp);
// out!(output_min); show_pid_parameter!(ki);
// out!(output_max); show_pid_parameter!(kd);
// out!(integral_min); show_pid_parameter!(output_min);
// out!(integral_max); show_pid_parameter!(output_max);
// let _ = writeln!(socket, ""); show_pid_parameter!(integral_min);
// } show_pid_parameter!(integral_max);
if let Some(last_output) = pid.last_output {
let _ = writeln!(socket, "- output={:.4}", last_output);
}
let _ = writeln!(socket, "");
}
} }
Command::Show(ShowCommand::Pwm) => { Command::Show(ShowCommand::Pwm) => {
// for (channel, state) in states.iter().enumerate() { for (channel, state) in channel_states.iter().enumerate() {
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: PID={}", socket, "channel {}: PID={}",
// channel, channel,
// if state.pid_enabled { "engaged" } else { "disengaged" } if state.pid_enabled { "engaged" } else { "disengaged" }
// ); );
// for pin in TecPin::VALID_VALUES { let _ = writeln!(socket, "- i_set={}/{}", state.dac_value, ad5680::MAX_VALUE);
// let (width, total) = match channel { fn show_pwm_channel<S, P>(mut socket: S, name: &str, pin: &P)
// 0 => tec0.get(*pin), where
// 1 => tec1.get(*pin), S: core::fmt::Write,
// _ => unreachable!(), P: hal::PwmPin<Duty=u16>,
// }; {
// let _ = writeln!(socket, "- {}={}/{}", pin, width, total); let _ = writeln!(
// } socket,
// let _ = writeln!(socket, ""); "- {}={}/{}",
// } name, pin.get_duty(), pin.get_max_duty()
);
}
match channel {
0 => {
show_pwm_channel(socket.deref_mut(), "max_v", &pwm.max_v0);
show_pwm_channel(socket.deref_mut(), "max_i_pos", &pwm.max_i_pos0);
show_pwm_channel(socket.deref_mut(), "max_i_neg", &pwm.max_i_neg0);
}
1 => {
show_pwm_channel(socket.deref_mut(), "max_v", &pwm.max_v1);
show_pwm_channel(socket.deref_mut(), "max_i_pos", &pwm.max_i_pos1);
show_pwm_channel(socket.deref_mut(), "max_i_neg", &pwm.max_i_neg1);
}
_ => unreachable!(),
}
let _ = writeln!(socket, "");
}
} }
Command::Show(ShowCommand::SteinhartHart) => { Command::Show(ShowCommand::SteinhartHart) => {
// for (channel, state) in states.iter().enumerate() { for (channel, state) in channel_states.iter().enumerate() {
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: Steinhart-Hart equation parameters", socket, "channel {}: Steinhart-Hart equation parameters",
// channel, channel,
// ); );
// let _ = writeln!(socket, "- a={}", state.sh.a); let _ = writeln!(socket, "- t0={}", state.sh.t0);
// let _ = writeln!(socket, "- b={}", state.sh.b); let _ = writeln!(socket, "- b={}", state.sh.b);
// let _ = writeln!(socket, "- c={}", state.sh.c); let _ = writeln!(socket, "- r0={}", state.sh.r0);
// let _ = writeln!(socket, "- parallel_r={}", state.sh.parallel_r); let _ = writeln!(socket, "");
// let _ = writeln!(socket, ""); }
// }
} }
Command::Show(ShowCommand::PostFilter) => { Command::Show(ShowCommand::PostFilter) => {
// for (channel, _) in states.iter().enumerate() { for (channel, _) in channel_states.iter().enumerate() {
// match adc.get_postfilter(channel as u8).unwrap() { match adc.get_postfilter(channel as u8).unwrap() {
// Some(filter) => { Some(filter) => {
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: postfilter={:.2} SPS", socket, "channel {}: postfilter={:.2} SPS",
// channel, filter.output_rate().unwrap() channel, filter.output_rate().unwrap()
// ); );
// } }
// None => { None => {
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: no postfilter", socket, "channel {}: no postfilter",
// channel channel
// ); );
// } }
// } }
// } }
} }
Command::Pwm { channel, setup: PwmSetup::ISet(PwmMode::Pid) } => { Command::PwmPid { channel } => {
// states[channel].pid_enabled = true; channel_states[channel].pid_enabled = true;
// let _ = writeln!(socket, "channel {}: PID enabled to control PWM", channel let _ = writeln!(socket, "channel {}: PID enabled to control PWM", channel
// ); );
} }
Command::Pwm { channel, setup: PwmSetup::ISet(PwmMode::Manual(config))} => { Command::Pwm { channel, pin: PwmPin::ISet, duty } if duty <= ad5680::MAX_VALUE => {
// states[channel].pid_enabled = false; channel_states[channel].pid_enabled = false;
// let PwmConfig { width, total } = config; match channel {
// match channel { 0 => dac0.set(duty).unwrap(),
// 0 => tec0.set(TecPin::ISet, width, total), 1 => dac1.set(duty).unwrap(),
// 1 => tec1.set(TecPin::ISet, width, total), _ => unreachable!(),
// _ => unreachable!(), }
// } channel_states[channel].dac_value = duty;
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: PWM duty cycle manually set to {}/{}", socket, "channel {}: PWM duty cycle manually set to {}/{}",
// channel, config.width, config.total channel, duty, ad5680::MAX_VALUE
// ); );
} }
Command::Pwm { channel, setup } => { Command::Pwm { pin: PwmPin::ISet, duty, .. } if duty > ad5680::MAX_VALUE => {
// let (pin, config) = match setup { let _ = writeln!(
// PwmSetup::ISet(_) => socket, "error: PWM duty range must not exceed {}",
// // Handled above ad5680::MAX_VALUE
// unreachable!(), );
// PwmSetup::MaxIPos(config) => }
// (TecPin::MaxIPos, config), Command::Pwm { channel, pin, duty } if duty <= 0xFFFF => {
// PwmSetup::MaxINeg(config) => let duty = duty as u16;
// (TecPin::MaxINeg, config),
// PwmSetup::MaxV(config) => fn set_pwm_channel<P: hal::PwmPin<Duty=u16>>(pin: &mut P, duty: u16) -> u16 {
// (TecPin::MaxV, config), pin.set_duty(duty);
// }; pin.get_max_duty()
// let PwmConfig { width, total } = config; }
// match channel { let max = match (channel, pin) {
// 0 => tec0.set(pin, width, total), (_, PwmPin::ISet) =>
// 1 => tec1.set(pin, width, total), // Handled above
// _ => unreachable!(), unreachable!(),
// } (0, PwmPin::MaxIPos) =>
// let _ = writeln!( set_pwm_channel(&mut pwm.max_i_pos0, duty),
// socket, "channel {}: PWM {} reconfigured to {}/{}", (0, PwmPin::MaxINeg) =>
// channel, pin, width, total set_pwm_channel(&mut pwm.max_i_neg0, duty),
// ); (0, PwmPin::MaxV) =>
set_pwm_channel(&mut pwm.max_v0, duty),
(1, PwmPin::MaxIPos) =>
set_pwm_channel(&mut pwm.max_i_pos1, duty),
(1, PwmPin::MaxINeg) =>
set_pwm_channel(&mut pwm.max_i_neg1, duty),
(1, PwmPin::MaxV) =>
set_pwm_channel(&mut pwm.max_v1, duty),
_ =>
unreachable!(),
};
let _ = writeln!(
socket, "channel {}: PWM {} reconfigured to {}/{}",
channel, pin.name(), duty, max
);
}
Command::Pwm { duty, .. } if duty > 0xFFFF => {
let _ = writeln!(socket, "error: PWM duty range must fit 16 bits");
} }
Command::Pid { channel, parameter, value } => { Command::Pid { channel, parameter, value } => {
// let pid = &mut states[channel].pid; let pid = &mut channel_states[channel].pid;
// use command_parser::PidParameter::*; use command_parser::PidParameter::*;
// match parameter { match parameter {
// Target => Target =>
// pid.set_target(value), pid.target = value,
// KP => KP =>
// pid.update_parameters(|parameters| parameters.kp = value), pid.parameters.kp = value,
// KI => KI =>
// pid.update_parameters(|parameters| parameters.ki = value), pid.parameters.ki = value,
// KD => KD =>
// pid.update_parameters(|parameters| parameters.kd = value), pid.parameters.kd = value,
// OutputMin => OutputMin =>
// pid.update_parameters(|parameters| parameters.output_min = value), pid.parameters.output_min = value,
// OutputMax => OutputMax =>
// pid.update_parameters(|parameters| parameters.output_max = value), pid.parameters.output_max = value,
// IntegralMin => IntegralMin =>
// pid.update_parameters(|parameters| parameters.integral_min = value), pid.parameters.integral_min = value,
// IntegralMax => IntegralMax =>
// pid.update_parameters(|parameters| parameters.integral_max = value), pid.parameters.integral_max = value,
// } }
// pid.reset(); // TODO: really reset PID state
// let _ = writeln!(socket, "PID parameter updated"); // after each parameter change?
pid.reset();
let _ = writeln!(socket, "PID parameter updated");
} }
Command::SteinhartHart { channel, parameter, value } => { Command::SteinhartHart { channel, parameter, value } => {
// let sh = &mut states[channel].sh; let sh = &mut channel_states[channel].sh;
// use command_parser::ShParameter::*; use command_parser::ShParameter::*;
// match parameter { match parameter {
// A => sh.a = value, T0 => sh.t0 = value,
// B => sh.b = value, B => sh.b = value,
// C => sh.c = value, R0 => sh.r0 = value,
// ParallelR => sh.parallel_r = value, }
// } let _ = writeln!(socket, "Steinhart-Hart equation parameter updated");
// let _ = writeln!(socket, "Steinhart-Hart equation parameter updated");
} }
Command::PostFilter { channel, rate } => { Command::PostFilter { channel, rate } => {
// let filter = ad7172::PostFilter::closest(rate); let filter = ad7172::PostFilter::closest(rate);
// match filter { match filter {
// Some(filter) => { Some(filter) => {
// adc.set_postfilter(channel as u8, Some(filter)).unwrap(); adc.set_postfilter(channel as u8, Some(filter)).unwrap();
// let _ = writeln!( let _ = writeln!(
// socket, "channel {}: postfilter set to {:.2} SPS", socket, "channel {}: postfilter set to {:.2} SPS",
// channel, filter.output_rate().unwrap() channel, filter.output_rate().unwrap()
// ); );
// } }
// None => { None => {
// let _ = writeln!(socket, "Unable to choose postfilter"); let _ = writeln!(socket, "Unable to choose postfilter");
// } }
// } }
}
cmd => {
let _ = writeln!(socket, "Not yet implemented: {:?}", cmd);
} }
} }
Ok(SessionOutput::Error(e)) => { Ok(SessionOutput::Error(e)) => {

View File

@ -9,12 +9,27 @@ pub struct Parameters {
pub integral_max: f64 pub integral_max: f64
} }
impl Default for Parameters {
fn default() -> Self {
Parameters {
kp: 0.5,
ki: 0.05,
kd: 0.45,
output_min: 0.0,
output_max: 1.0,
integral_min: 0.0,
integral_max: 1.0,
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Controller { pub struct Controller {
parameters: Parameters, pub parameters: Parameters,
target: f64, pub target: f64,
integral: f64, integral: f64,
last_input: Option<f64> last_input: Option<f64>,
pub last_output: Option<f64>,
} }
impl Controller { impl Controller {
@ -23,7 +38,8 @@ impl Controller {
parameters: parameters, parameters: parameters,
target: 0.0, target: 0.0,
last_input: None, last_input: None,
integral: 0.0 integral: 0.0,
last_output: None,
} }
} }
@ -54,25 +70,10 @@ impl Controller {
if output > self.parameters.output_max { if output > self.parameters.output_max {
output = self.parameters.output_max; output = self.parameters.output_max;
} }
self.last_output = Some(output);
output output
} }
pub fn get_target(&self) -> f64 {
self.target
}
pub fn set_target(&mut self, target: f64) {
self.target = target;
}
pub fn get_parameters(&self) -> &Parameters {
&self.parameters
}
pub fn update_parameters<F: FnOnce(&mut Parameters)>(&mut self, f: F) {
f(&mut self.parameters);
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.integral = 0.0; self.integral = 0.0;

View File

@ -1,7 +1,3 @@
use embedded_hal::{
blocking::spi::Transfer,
digital::v2::OutputPin,
};
use stm32f4xx_hal::{ use stm32f4xx_hal::{
gpio::{ gpio::{
AF5, Alternate, AF5, Alternate,
@ -17,9 +13,9 @@ use stm32f4xx_hal::{
}, },
rcc::Clocks, rcc::Clocks,
pwm::{self, PwmChannels}, pwm::{self, PwmChannels},
spi::{self, Spi, NoMiso}, spi::{Spi, NoMiso},
stm32::{GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3}, stm32::{GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3},
time::{U32Ext, Hertz, MegaHertz}, time::U32Ext,
}; };
@ -171,12 +167,12 @@ impl Pins {
} }
pub struct PwmPins { pub struct PwmPins {
max_v0: PwmChannels<TIM3, pwm::C1>, pub max_v0: PwmChannels<TIM3, pwm::C1>,
max_v1: PwmChannels<TIM3, pwm::C2>, pub max_v1: PwmChannels<TIM3, pwm::C2>,
max_i_pos0: PwmChannels<TIM1, pwm::C1>, pub max_i_pos0: PwmChannels<TIM1, pwm::C1>,
max_i_pos1: PwmChannels<TIM1, pwm::C2>, pub max_i_pos1: PwmChannels<TIM1, pwm::C2>,
max_i_neg0: PwmChannels<TIM1, pwm::C3>, pub max_i_neg0: PwmChannels<TIM1, pwm::C3>,
max_i_neg1: PwmChannels<TIM1, pwm::C4>, pub max_i_neg1: PwmChannels<TIM1, pwm::C4>,
} }
impl PwmPins { impl PwmPins {

View File

@ -1,4 +1,3 @@
use core::fmt;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use smoltcp::{ use smoltcp::{
iface::EthernetInterface, iface::EthernetInterface,

View File

@ -1,4 +1,3 @@
use core::ops::Deref;
use super::command_parser::{Command, Error as ParserError}; use super::command_parser::{Command, Error as ParserError};
const MAX_LINE_LEN: usize = 64; const MAX_LINE_LEN: usize = 64;

View File

@ -1,41 +1,31 @@
use lexical_core::Float; use num_traits::float::Float;
/// Steinhart-Hart equation parameters /// Steinhart-Hart equation parameters
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Parameters { pub struct Parameters {
pub t0: f64, pub t0: f64,
pub r: f64, pub b: f64,
pub r0: f64, pub r0: f64,
r_fact: f64,
} }
impl Parameters { impl Parameters {
/// Update the cached r_fact
pub fn update(&mut self) {
self.r_fact = (self.r / self.r0).ln();
}
/// Perform the voltage to temperature conversion. /// Perform the voltage to temperature conversion.
/// ///
/// Result unit: Kelvin /// Result unit: Kelvin
/// ///
/// TODO: verify /// TODO: verify
pub fn get_temperature(&self, b: f64) -> f64 { pub fn get_temperature(&self, r: f64) -> f64 {
let inv_temp = 1.0 / self.t0 + self.r_fact / b; let inv_temp = 1.0 / self.t0 + (r / self.r0).ln() / self.b;
1.0 / inv_temp 1.0 / inv_temp
} }
} }
impl Default for Parameters { impl Default for Parameters {
fn default() -> Self { fn default() -> Self {
let mut p = Parameters { Parameters {
t0: 0.001_4, t0: 0.001_4,
r: 0.000_000_099, b: 0.000_000_099,
r0: 5_110.0, r0: 5_110.0,
r_fact: 0.0, }
};
p.update();
p
} }
} }