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

idsp: full pid, also redo trigger logic #1014

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions hitl/loopback.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ async def test_loopback(stabilizer, telemetry_queue, set_point, gain=1, channel=
# "max": u,
# },
"biquad/0/typ": "Pid",
"biquad/0/repr/Pid/ki": -1000,
"biquad/0/repr/Pid/kp": -0.1,
"biquad/0/repr/Pid/order": "I",
"biquad/0/repr/Pid/gain/i": -1000,
"biquad/0/repr/Pid/gain/p": -0.1,
"biquad/0/repr/Pid/min": -10,
"biquad/0/repr/Pid/max": 10,
"biquad/0/repr/Pid/setpoint": set_point*gain,
"source/amplitude": 0,
"source/offset": 0,
"run": "Run",
}.items():
await stabilizer.set(f"/ch/{channel}/{k}", v)
await stabilizer.set(f"/trigger", True)
await stabilizer.set(f"/trigger", False)
await stabilizer.set(f"/trigger", None)
await telemetry_queue.__anext__() # discard
latest_values = json.loads((await telemetry_queue.__anext__()).payload)
print(f"Latest telemtry: {latest_values}")
Expand Down
4 changes: 2 additions & 2 deletions hitl/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ping -c 5 -w 20 stabilizer-hitl
# Test the MQTT interface. This uses the default broker "mqtt"
python3 -m miniconf $PREFIX '?'
python3 -m miniconf $PREFIX \
'/ch/0/gain="G2"' 'biquad/0/typ="Pid"' \
/ch/0/biquad/0/repr/Pid/ki=-10 kp=-0.1 setpoint=0 min=-30000 max=30000 \
'/ch/0/gain="G2"' '/ch/0/biquad/0/typ="Pid"' \
/ch/0/biquad/0/repr/Pid/setpoint=0 min=-30000 max=30000 gain/i=-10 gain/p=-0.1 \
'/ch/0/run="Run"'

# Test the ADC/DACs connected via loopback.
Expand Down
46 changes: 30 additions & 16 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ impl serial_settings::Settings for Settings {

#[derive(Clone, Debug, Tree)]
pub struct BiquadRepr {
/// Biquad parameters
#[tree(rename = "typ")]
repr: StrLeaf<iir::BiquadRepr<f32, f32>>,
/// Subtree access
#[tree(
rename = "repr",
typ = "iir::BiquadRepr<f32, f32>",
defer = "*self.repr"
)]
_repr: (),
/// Biquad parameters
#[tree(rename = "typ")]
repr: StrLeaf<iir::BiquadRepr<f32, f32>>,
}

impl Default for BiquadRepr {
Expand Down Expand Up @@ -181,8 +181,12 @@ impl Channel {
pub struct DualIir {
/// Channel configuration
ch: [Channel; 2],
/// Trigger handshake
#[tree(skip)]
trigger: bool,
/// Trigger both signal sources
trigger: Leaf<bool>,
#[tree(validate=self.validate_trigger, rename="trigger")]
_trigger: Leaf<()>,
/// Telemetry output period in seconds.
telemetry_period: Leaf<f32>,
/// Target IP and port for UDP streaming.
Expand All @@ -191,11 +195,23 @@ pub struct DualIir {
stream: Leaf<StreamTarget>,
}

impl DualIir {
fn validate_trigger(
&mut self,
depth: usize,
) -> Result<usize, &'static str> {
debug_assert!(!self.trigger);
self.trigger = true;
Ok(depth)
}
}

impl Default for DualIir {
fn default() -> Self {
Self {
telemetry_period: 10.0.into(),
trigger: false.into(),
telemetry_period: Leaf(10.0),
trigger: false,
_trigger: Leaf(()),
stream: Default::default(),
ch: Default::default(),
}
Expand All @@ -217,15 +233,15 @@ mod app {
#[shared]
struct Shared {
usb: UsbDevice,
network: NetworkUsers<DualIir, 7>,
network: NetworkUsers<DualIir, 8>,
settings: Settings,
active: [Active; 2],
telemetry: TelemetryBuffer,
}

#[local]
struct Local {
usb_terminal: SerialTerminal<Settings, 8>,
usb_terminal: SerialTerminal<Settings, 9>,
sampling_timer: SamplingTimer,
digital_inputs: (DigitalInput0, DigitalInput1),
afes: (AFE0, AFE1),
Expand All @@ -240,7 +256,7 @@ mod app {
let clock = SystemTimer::new(|| Systick::now().ticks());

// Configure the microcontroller
let (stabilizer, _pounder) = hardware::setup::setup::<Settings, 8>(
let (stabilizer, _pounder) = hardware::setup::setup::<Settings, 9>(
c.core,
c.device,
clock,
Expand Down Expand Up @@ -435,8 +451,8 @@ mod app {
c.local.afes.0.set_gain(*settings.dual_iir.ch[0].gain);
c.local.afes.1.set_gain(*settings.dual_iir.ch[1].gain);

if *settings.dual_iir.trigger {
settings.dual_iir.trigger = Leaf(false);
if settings.dual_iir.trigger {
settings.dual_iir.trigger = false;
let s = settings.dual_iir.ch.each_ref().map(|ch| {
let s = ch
.source
Expand All @@ -454,11 +470,6 @@ mod app {
}
});
}

c.shared
.network
.lock(|net| net.direct_stream(*settings.dual_iir.stream));

let b = settings.dual_iir.ch.each_ref().map(|ch| {
(
*ch.run,
Expand All @@ -473,6 +484,9 @@ mod app {
(a.run, a.biquad) = b;
}
});
c.shared
.network
.lock(|net| net.direct_stream(*settings.dual_iir.stream));
});
}

Expand Down
Loading