Compare commits

..

No commits in common. "a4dde1b8ca10e5d726a4f81be3740046cc807c01" and "94e0525002b57185980a4db105ddbad651f02352" have entirely different histories.

10 changed files with 102 additions and 49 deletions

View File

@ -100,6 +100,24 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
Ok(())
}
pub fn disable_channel(
&mut self, index: u8
) -> Result<(), SPI::Error> {
self.update_reg(&regs::Channel { index }, |data| {
data.set_enabled(false);
})?;
Ok(())
}
pub fn disable_all_channels(&mut self) -> Result<(), SPI::Error> {
for index in 0..4 {
self.update_reg(&regs::Channel { index }, |data| {
data.set_enabled(false);
})?;
}
Ok(())
}
pub fn get_calibration(&mut self, index: u8) -> Result<ChannelCalibration, SPI::Error> {
let offset = self.read_reg(&regs::Offset { index })?.offset();
let gain = self.read_reg(&regs::Gain { index })?.gain();

View File

@ -19,6 +19,8 @@ pub const SPI_MODE: spi::Mode = spi::Mode {
/// 2 MHz
pub const SPI_CLOCK: MegaHertz = MegaHertz(2);
pub const MAX_VALUE: u32 = 0xFF_FFFF;
#[derive(Clone, Copy, Debug)]
#[repr(u8)]

View File

@ -7,7 +7,7 @@ use uom::si::{
},
electric_potential::volt,
electrical_resistance::ohm,
thermodynamic_temperature::degree_celsius,
temperature_interval::kelvin,
};
use crate::{
ad7172,
@ -51,11 +51,10 @@ impl ChannelState {
}
/// Update PID state on ADC input, calculate new DAC output
pub fn update_pid(&mut self) -> Option<f64> {
let temperature = self.get_temperature()?
.get::<degree_celsius>();
let pid_output = self.pid.update(temperature);
Some(pid_output)
pub fn update_pid(&mut self) {
// Update PID controller
// self.pid.update(self.get_temperature().unwrap().get::<kelvin>())
// TODO: add output field
}
pub fn get_adc(&self) -> Option<ElectricPotential> {

View File

@ -45,8 +45,8 @@ impl Channels {
.expect("adc_calibration1");
adc.start_continuous_conversion().unwrap();
let channel0 = Channel::new(pins.channel0, adc_calibration0);
let channel1 = Channel::new(pins.channel1, adc_calibration1);
let mut channel0 = Channel::new(pins.channel0, adc_calibration0);
let mut channel1 = Channel::new(pins.channel1, adc_calibration1);
let pins_adc = pins.pins_adc;
let pwm = pins.pwm;
let mut channels = Channels { channel0, channel1, adc, pins_adc, pwm };
@ -70,16 +70,21 @@ impl Channels {
self.adc.data_ready().unwrap().map(|channel| {
let data = self.adc.read_data().unwrap();
let state = self.channel_state(channel);
let dac_value = {
let state = self.channel_state(channel);
state.update(instant, data);
let pid_output = state.update_pid();
state.update(instant, data);
match state.update_pid() {
Some(pid_output) if state.pid_engaged => {
log::info!("PID: {:.3} A", pid_output);
// Forward PID output to i_set DAC
self.set_i(channel.into(), ElectricCurrent::new::<ampere>(pid_output));
if state.pid_engaged {
Some(pid_output)
} else {
None
}
_ => {}
};
if let Some(dac_value) = dac_value {
// Forward PID output to i_set DAC
// TODO:
// self.set_dac(channel.into(), ElectricPotential::new::<volt>(dac_value));
}
channel
@ -164,6 +169,7 @@ impl Channels {
let mut prev = self.read_dac_feedback(channel);
loop {
let current = self.read_dac_feedback(channel);
use num_traits::float::Float;
if (current - prev).abs() < tolerance {
return current;
}

View File

@ -122,6 +122,17 @@ pub enum PwmPin {
MaxV,
}
impl PwmPin {
pub fn name(&self) -> &'static str {
match self {
PwmPin::ISet => "i_set",
PwmPin::MaxIPos => "max_i_pos",
PwmPin::MaxINeg => "max_i_neg",
PwmPin::MaxV => "max_v",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Command {
Quit,
@ -167,6 +178,19 @@ fn whitespace(input: &[u8]) -> IResult<&[u8], ()> {
fold_many1(char(' '), (), |(), _| ())(input)
}
fn unsigned(input: &[u8]) -> IResult<&[u8], Result<u32, Error>> {
take_while1(is_digit)(input)
.map(|(input, digits)| {
let result =
from_utf8(digits)
.map_err(|e| e.into())
.and_then(|digits| u32::from_str_radix(digits, 10)
.map_err(|e| e.into())
);
(input, result)
})
}
fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
let (input, sign) = opt(is_a("-"))(input)?;
let negative = sign.is_some();
@ -437,7 +461,7 @@ mod test {
assert_eq!(command, Ok(Command::Pwm {
channel: 1,
pin: PwmPin::ISet,
value: 16383.0,
value: 16383,
}));
}
@ -446,6 +470,7 @@ mod test {
let command = Command::parse(b"pwm 0 pid");
assert_eq!(command, Ok(Command::PwmPid {
channel: 0,
pin: PwmPin::ISet,
}));
}
@ -455,7 +480,7 @@ mod test {
assert_eq!(command, Ok(Command::Pwm {
channel: 0,
pin: PwmPin::MaxIPos,
value: 7.0,
value: 7,
}));
}
@ -465,7 +490,7 @@ mod test {
assert_eq!(command, Ok(Command::Pwm {
channel: 0,
pin: PwmPin::MaxINeg,
value: 128.0,
value: 128,
}));
}
@ -475,7 +500,7 @@ mod test {
assert_eq!(command, Ok(Command::Pwm {
channel: 0,
pin: PwmPin::MaxV,
value: 32768.0,
value: 32768,
}));
}

View File

@ -5,7 +5,7 @@ static USB_LOGGER: usb::Logger = usb::Logger;
#[cfg(not(feature = "semihosting"))]
pub fn init_log() {
let _ = log::set_logger(&USB_LOGGER);
log::set_logger(&USB_LOGGER);
}
#[cfg(feature = "semihosting")]

View File

@ -1,20 +1,24 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
#![no_std]
#![no_main]
#![feature(maybe_uninit_extra, maybe_uninit_ref)]
// TODO: #![deny(warnings, unused)]
#[cfg(not(any(feature = "semihosting", test)))]
#[cfg(not(feature = "semihosting"))]
use panic_abort as _;
#[cfg(all(feature = "semihosting", not(test)))]
#[cfg(feature = "semihosting")]
use panic_semihosting as _;
use log::{info, warn};
use core::ops::DerefMut;
use core::fmt::Write;
use cortex_m::asm::wfi;
use cortex_m_rt::entry;
use stm32f4xx_hal::{
hal::watchdog::{WatchdogEnable, Watchdog},
hal::{
self,
watchdog::{WatchdogEnable, Watchdog},
},
rcc::RccExt,
watchdog::IndependentWatchdog,
time::{U32Ext, MegaHertz},
@ -76,7 +80,6 @@ const TCP_PORT: u16 = 23;
/// Initialization and main loop
#[cfg(not(test))]
#[entry]
fn main() -> ! {
init_log();
@ -196,7 +199,7 @@ fn main() -> ! {
let state = channels.channel_state(channel);
let _ = writeln!(socket, "PID settings for channel {}", channel);
let pid = &state.pid;
let _ = writeln!(socket, "- target={:.4} °C", pid.target);
let _ = writeln!(socket, "- target={:.4}", pid.target);
macro_rules! show_pid_parameter {
($p: tt) => {
let _ = writeln!(
@ -213,7 +216,7 @@ fn main() -> ! {
show_pid_parameter!(output_min);
show_pid_parameter!(output_max);
if let Some(last_output) = pid.last_output {
let _ = writeln!(socket, "- last_output={:.3} A", last_output);
let _ = writeln!(socket, "- last_output={:.4}", last_output);
}
let _ = writeln!(socket, "");
}
@ -349,17 +352,16 @@ fn main() -> ! {
max.into_format_args(ampere, Abbreviation),
);
}
_ =>
unreachable!(),
}
}
Command::Pid { channel, parameter, value } => {
let pid = &mut channels.channel_state(channel).pid;
use command_parser::PidParameter::*;
match parameter {
Target => {
pid.target = value;
// reset pid.integral
pid.reset();
}
Target =>
pid.target = value,
KP =>
pid.parameters.kp = value,
KI =>
@ -375,6 +377,9 @@ fn main() -> ! {
IntegralMax =>
pid.parameters.integral_max = value,
}
// TODO: really reset PID state
// after each parameter change?
pid.reset();
let _ = writeln!(socket, "PID parameter updated");
}
Command::SteinhartHart { channel, parameter, value } => {

View File

@ -12,13 +12,13 @@ pub struct Parameters {
impl Default for Parameters {
fn default() -> Self {
Parameters {
kp: 1.5,
ki: 0.1,
kd: 150.0,
kp: 0.5,
ki: 0.05,
kd: 0.45,
output_min: 0.0,
output_max: 2.0,
integral_min: -10.0,
integral_max: 10.0,
output_max: 5.0,
integral_min: 0.0,
integral_max: 1.0,
}
}
}
@ -44,13 +44,10 @@ impl Controller {
}
pub fn update(&mut self, input: f64) -> f64 {
// error
let error = self.target - input;
// partial
let p = self.parameters.kp * error;
//integral
self.integral += error;
if self.integral < self.parameters.integral_min {
self.integral = self.parameters.integral_min;
@ -60,14 +57,12 @@ impl Controller {
}
let i = self.parameters.ki * self.integral;
// derivative
let d = match self.last_input {
None => 0.0,
Some(last_input) => self.parameters.kd * (last_input - input)
};
self.last_input = Some(input);
// output
let mut output = p + i + d;
if output < self.parameters.output_min {
output = self.parameters.output_min;
@ -108,7 +103,7 @@ mod test {
const DELAY: usize = 10;
let mut pid = Controller::new(PARAMETERS.clone());
pid.target = TARGET;
pid.set_target(TARGET);
let mut values = [DEFAULT; DELAY];
let mut t = 0;
@ -123,5 +118,6 @@ mod test {
t = next_t;
total_t += 1;
}
dbg!(values[t], total_t);
}
}

View File

@ -6,11 +6,13 @@ use stm32f4xx_hal::{
gpioa::*,
gpiob::*,
gpioc::*,
gpiod::*,
gpioe::*,
gpiof::*,
gpiog::*,
GpioExt,
Output, PushPull,
Speed::VeryHigh,
},
otg_fs::USB,
rcc::Clocks,

View File

@ -6,10 +6,10 @@ use stm32f4xx_hal::{
};
use usb_device::{
class_prelude::{UsbBusAllocator},
prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid},
prelude::{UsbError, UsbDevice, UsbDeviceBuilder, UsbVidPid},
};
use usbd_serial::SerialPort;
use log::{Record, Log, Metadata};
use log::{Record, Level, Log, Metadata};
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
@ -30,7 +30,7 @@ impl State {
let serial = SerialPort::new(bus);
let dev = UsbDeviceBuilder::new(bus, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("M-Labs")
.product("tecpak")
.product("thermostat")
.device_release(0x20)
.self_powered(true)
.device_class(usbd_serial::USB_CLASS_CDC)