Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/telemetry #341

Merged
merged 43 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
330f67d
Adding WIP telemetry implementation for dual-iir
ryan-summers Apr 15, 2021
854f45a
Updating tick rates
ryan-summers Apr 15, 2021
f442418
Merge branch 'master' into feature/telemetry
ryan-summers Apr 15, 2021
403ff16
Resetting IP
ryan-summers Apr 15, 2021
1c9f30b
Merge branch 'rs/issue-276/unique-identifiers' into feature/telemetry
ryan-summers Apr 20, 2021
afcf058
Refactoring telemetry to support binaries
ryan-summers Apr 20, 2021
3ae319d
Updating readme
ryan-summers Apr 20, 2021
106236a
Merge branch 'master' into feature/telemetry
ryan-summers Apr 20, 2021
f49ba30
Merge branch 'feature/mqtt-rework' into feature/telemetry
ryan-summers Apr 29, 2021
9e355ca
Merge branch 'feature/mqtt-rework' into feature/telemetry
ryan-summers Apr 29, 2021
e746e2a
Adding WIP telemetry
ryan-summers Apr 29, 2021
4169cd8
Adding AFE functions
ryan-summers Apr 29, 2021
b35250e
Fixing ADC/voltage conversion functions
ryan-summers Apr 29, 2021
5767973
Merge branch 'feature/mqtt-rework' into feature/telemetry
ryan-summers May 4, 2021
4888f18
Adding rework to network module
ryan-summers May 4, 2021
06b328f
Adding WIP updates for telemetry
ryan-summers May 5, 2021
4a656ee
Adding refactor for telemetry
ryan-summers May 5, 2021
8144b3a
Updating constructors
ryan-summers May 5, 2021
0c69355
Adding updated telemetry implementation
ryan-summers May 5, 2021
8efd7d4
Fixing design parameters file
ryan-summers May 5, 2021
740e41d
Renaming files
ryan-summers May 5, 2021
e9c3e24
Merge branch 'master' into feature/telemetry
ryan-summers May 5, 2021
4cbc826
Adding documentation
ryan-summers May 6, 2021
949ea9c
Fixing build, formatting
ryan-summers May 6, 2021
81a292a
Fixing system timer
ryan-summers May 6, 2021
8c581ea
Merging lockin app functions
ryan-summers May 6, 2021
80b8716
Finalizing merge
ryan-summers May 6, 2021
03adb72
Fixing clippy
ryan-summers May 6, 2021
eeee5af
Updating dependencies
ryan-summers May 6, 2021
e07f0a4
Formatting
ryan-summers May 6, 2021
61321c4
Merge branch 'master' into feature/telemetry
ryan-summers May 6, 2021
ff79e0a
Fixing merge
ryan-summers May 6, 2021
89eaefd
Updating delay
ryan-summers May 6, 2021
7b76b1f
Updating after review
ryan-summers May 7, 2021
d68fa87
Simplifying settings lock
ryan-summers May 7, 2021
b73a4d9
Adding adc/dac code conversion utilities
ryan-summers May 7, 2021
923790b
Updating float conversion
ryan-summers May 7, 2021
60b1b11
Renaming AdcSample -> AdcCode
ryan-summers May 7, 2021
6e94ffc
Update src/hardware/dac.rs
ryan-summers May 7, 2021
fcda2d5
Addressing review feedback
ryan-summers May 10, 2021
65eb74b
Merge branch 'feature/telemetry' of github.com:quartiq/stabilizer int…
ryan-summers May 10, 2021
fa886d2
Cleaning up conversion + comments
ryan-summers May 10, 2021
81bc569
Simplifying unit conversions
ryan-summers May 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dsp = { path = "dsp" }
ad9959 = { path = "ad9959" }
generic-array = "0.14"
miniconf = "0.1.0"
shared-bus = {version = "0.2.2", features = ["cortex-m"] }
serde-json-core = "0.3"

[dependencies.mcp23017]
git = "https://github.com/mrd0ll4r/mcp23017.git"
Expand All @@ -60,11 +62,11 @@ rev = "c6f2b28"

[dependencies.smoltcp-nal]
git = "https://github.com/quartiq/smoltcp-nal.git"
rev = "8468f11"
rev = "4a1711c"

[dependencies.minimq]
git = "https://github.com/quartiq/minimq.git"
rev = "b3f364d"
rev = "d2ec3e8"

[features]
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]
Expand Down
97 changes: 67 additions & 30 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use serde::Deserialize;

use dsp::iir;
use hardware::{
Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, DigitalInput1,
InputPin, AFE0, AFE1,
Adc0Input, Adc1Input, AdcCode, AfeGain, Dac0Output, Dac1Output, DacCode,
DigitalInput0, DigitalInput1, InputPin, SystemTimer, AFE0, AFE1,
};

use net::{Action, MqttInterface};
use net::{NetworkUsers, Telemetry, TelemetryBuffer, UpdateState};

const SCALE: f32 = i16::MAX as _;

Expand All @@ -26,6 +26,7 @@ pub struct Settings {
iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2],
allow_hold: bool,
force_hold: bool,
telemetry_period: u16,
}

impl Default for Settings {
Expand All @@ -43,42 +44,44 @@ impl Default for Settings {
allow_hold: false,
// Force suppress filter output updates.
force_hold: false,
// The default telemetry period in seconds.
telemetry_period: 10,
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = stabilizer::hardware::SystemTimer)]
const APP: () = {
struct Resources {
afes: (AFE0, AFE1),
digital_input1: DigitalInput1,
digital_inputs: (DigitalInput0, DigitalInput1),
adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output),
mqtt: MqttInterface<Settings>,
network: NetworkUsers<Settings, Telemetry>,

settings: Settings,
telemetry: TelemetryBuffer,

#[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])]
iir_state: [[iir::Vec5; IIR_CASCADE_LENGTH]; 2],
settings: Settings,
}

#[init(spawn=[settings_update])]
#[init(spawn=[telemetry, settings_update])]
fn init(c: init::Context) -> init::LateResources {
// Configure the microcontroller
let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device);

let mqtt = MqttInterface::new(
let network = NetworkUsers::new(
stabilizer.net.stack,
"",
&net::get_device_prefix(
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
),
stabilizer.net.phy,
stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
);

// Spawn a settings update for default settings.
c.spawn.settings_update().unwrap();
c.spawn.telemetry().unwrap();

// Enable ADC/DAC events
stabilizer.adcs.0.start();
Expand All @@ -93,8 +96,9 @@ const APP: () = {
afes: stabilizer.afes,
adcs: stabilizer.adcs,
dacs: stabilizer.dacs,
mqtt,
digital_input1: stabilizer.digital_inputs.1,
network,
digital_inputs: stabilizer.digital_inputs,
telemetry: net::TelemetryBuffer::default(),
settings: Settings::default(),
}
}
Expand All @@ -115,7 +119,7 @@ const APP: () = {
///
/// Because the ADC and DAC operate at the same rate, these two constraints actually implement
/// the same time bounds, meeting one also means the other is also met.
#[task(binds=DMA1_STR4, resources=[adcs, digital_input1, dacs, iir_state, settings], priority=2)]
#[task(binds=DMA1_STR4, resources=[adcs, digital_inputs, dacs, iir_state, settings, telemetry], priority=2)]
fn process(c: process::Context) {
let adc_samples = [
c.resources.adcs.0.acquire_buffer(),
Expand All @@ -127,9 +131,13 @@ const APP: () = {
c.resources.dacs.1.acquire_buffer(),
];

let digital_inputs = [
c.resources.digital_inputs.0.is_high().unwrap(),
c.resources.digital_inputs.1.is_high().unwrap(),
];

let hold = c.resources.settings.force_hold
|| (c.resources.digital_input1.is_high().unwrap()
&& c.resources.settings.allow_hold);
|| (digital_inputs[1] && c.resources.settings.allow_hold);

for channel in 0..adc_samples.len() {
for sample in 0..adc_samples[0].len() {
Expand All @@ -145,36 +153,65 @@ const APP: () = {
// The truncation introduces 1/2 LSB distortion.
let y = unsafe { y.to_int_unchecked::<i16>() };
// Convert to DAC code
dac_samples[channel][sample] = y as u16 ^ 0x8000;
dac_samples[channel][sample] = DacCode::from(y).0;
}
}

// Update telemetry measurements.
c.resources.telemetry.adcs =
[AdcCode(adc_samples[0][0]), AdcCode(adc_samples[1][0])];

c.resources.telemetry.dacs =
[DacCode(dac_samples[0][0]), DacCode(dac_samples[1][0])];

c.resources.telemetry.digital_inputs = digital_inputs;
}

#[idle(resources=[mqtt], spawn=[settings_update])]
#[idle(resources=[network], spawn=[settings_update])]
fn idle(mut c: idle::Context) -> ! {
loop {
match c.resources.mqtt.lock(|mqtt| mqtt.update()) {
Some(Action::Sleep) => cortex_m::asm::wfi(),
Some(Action::UpdateSettings) => {
c.spawn.settings_update().unwrap()
}
_ => {}
match c.resources.network.lock(|net| net.update()) {
UpdateState::Updated => c.spawn.settings_update().unwrap(),
UpdateState::NoChange => cortex_m::asm::wfi(),
}
}
}

#[task(priority = 1, resources=[mqtt, afes, settings])]
#[task(priority = 1, resources=[network, afes, settings])]
fn settings_update(mut c: settings_update::Context) {
let settings = c.resources.mqtt.settings();

// Update the IIR channels.
let settings = c.resources.network.miniconf.settings();
c.resources.settings.lock(|current| *current = *settings);

// Update AFEs
c.resources.afes.0.set_gain(settings.afe[0]);
c.resources.afes.1.set_gain(settings.afe[1]);
}

#[task(priority = 1, resources=[network, settings, telemetry], schedule=[telemetry])]
fn telemetry(mut c: telemetry::Context) {
let telemetry: TelemetryBuffer =
c.resources.telemetry.lock(|telemetry| *telemetry);

let (gains, telemetry_period) = c
.resources
.settings
.lock(|settings| (settings.afe, settings.telemetry_period));

c.resources
.network
.telemetry
.publish(&telemetry.finalize(gains[0], gains[1]));

// Schedule the telemetry task in the future.
c.schedule
.telemetry(
c.scheduled
+ SystemTimer::ticks_from_secs(telemetry_period as u32),
)
.unwrap();
}

#[task(binds = ETH, priority = 1)]
fn eth(_: eth::Context) {
unsafe { stm32h7xx_hal::ethernet::interrupt_handler() }
Expand Down
Loading