From ad447cd5f9e9cd82a6ee0c54a36c1311947fee2b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 12 May 2017 11:07:35 +0800 Subject: [PATCH] use PID for FV, print all debug messages --- firmware/src/electrometer.rs | 2 +- firmware/src/loop_anode.rs | 13 +++++----- firmware/src/loop_cathode.rs | 49 +++++++++++++++++++++++++----------- firmware/src/main.rs | 6 ++--- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/firmware/src/electrometer.rs b/firmware/src/electrometer.rs index 156be27..4090ae2 100644 --- a/firmware/src/electrometer.rs +++ b/firmware/src/electrometer.rs @@ -50,7 +50,7 @@ impl Electrometer { impl ElectrometerStatus { pub fn debug_print(&self) { if self.ic.is_some() { - println!("ion current: {}nA", 1e9*self.ic.unwrap()); + println!("ion: {}nA", 1e9*self.ic.unwrap()); } } } diff --git a/firmware/src/loop_anode.rs b/firmware/src/loop_anode.rs index 1b9577f..7116cff 100644 --- a/firmware/src/loop_anode.rs +++ b/firmware/src/loop_anode.rs @@ -5,12 +5,12 @@ use pid; const PID_PARAMETERS: pid::Parameters = pid::Parameters { kp: 0.027, - ki: 0.013, + ki: 0.014, kd: 0.0, output_min: 0.0, output_max: 30.0, - integral_min: -5000.0, - integral_max: 5000.0 + integral_min: -700.0, + integral_max: 700.0 }; @@ -20,6 +20,7 @@ pub struct Controller { last_av: Option } +#[derive(Clone, Copy)] pub struct ControllerStatus { pub ready: bool, pub av: Option @@ -38,8 +39,8 @@ impl Controller { let av = av_sample as f32/board::AV_ADC_GAIN; self.last_av = Some(av); - let pid_out = self.pid.update(av); - board::set_hv_pwm(pid_out as u16) + let hv_pwm_duty = self.pid.update(av); + board::set_hv_pwm(hv_pwm_duty as u16) } pub fn set_target(&mut self, volts: f32) { @@ -69,7 +70,7 @@ impl Controller { impl ControllerStatus { pub fn debug_print(&self) { - println!("anode ready: {}", self.ready); + println!("anode rdy: {}", self.ready); if self.av.is_some() { println!("voltage: {}V", self.av.unwrap()); } diff --git a/firmware/src/loop_cathode.rs b/firmware/src/loop_cathode.rs index fb70e3d..a5e26fc 100644 --- a/firmware/src/loop_cathode.rs +++ b/firmware/src/loop_cathode.rs @@ -3,14 +3,24 @@ use core::num::Float; use board; use pid; -const PID_PARAMETERS: pid::Parameters = pid::Parameters { - kp: 0.004, - ki: 0.002, +const FBI_PID_PARAMETERS: pid::Parameters = pid::Parameters { + kp: 180.0, + ki: 90.0, + kd: 0.0, + output_min: 0.5, + output_max: 3.0, + integral_min: -0.02, + integral_max: 0.02 +}; + +const FV_PID_PARAMETERS: pid::Parameters = pid::Parameters { + kp: 1.80, + ki: 0.3, kd: 0.0, output_min: 0.0, output_max: 30.0, - integral_min: -5000.0, - integral_max: 5000.0 + integral_min: -50.0, + integral_max: 50.0 }; pub struct Controller { @@ -19,8 +29,9 @@ pub struct Controller { fbi_buffer: [f32; 16], fbi_buffer_count: usize, last_fbi: Option, - pid: pid::Controller, + fbi_pid: pid::Controller, + fv_pid: pid::Controller, last_fv: Option, fbv_target: f32, @@ -35,7 +46,6 @@ pub struct ControllerStatus { pub fbv: Option } - impl Controller { pub const fn new() -> Controller { Controller { @@ -44,8 +54,9 @@ impl Controller { fbi_buffer: [0.0; 16], fbi_buffer_count: 0, last_fbi: None, - pid: pid::Controller::new(PID_PARAMETERS), + fbi_pid: pid::Controller::new(FBI_PID_PARAMETERS), + fv_pid: pid::Controller::new(FV_PID_PARAMETERS), last_fv: None, fbv_target: 0.0, @@ -78,7 +89,14 @@ impl Controller { self.fbi_buffer_count = 0; } - self.last_fv = Some(fv_sample as f32/board::FV_ADC_GAIN); + let fv_target = self.fbi_pid.update(fbi); + self.fv_pid.set_target(fv_target); + + let fv = fv_sample as f32/board::FV_ADC_GAIN; + let fv_pwm_duty = self.fv_pid.update(fv); + board::set_fv_pwm(fv_pwm_duty as u16); + + self.last_fv = Some(fv); self.last_fbv = Some(fbv_sample as f32/board::FBV_ADC_GAIN); } @@ -102,7 +120,7 @@ impl Controller { fn emission_ready(&self) -> bool { match self.last_fbi { None => false, - Some(last_fbi) => (self.fbi_target - last_fbi).abs()/self.fbi_target < 0.02 + Some(last_fbi) => (self.fbi_target - last_fbi).abs()/self.fbi_target < 0.05 } } @@ -114,7 +132,8 @@ impl Controller { } pub fn reset(&mut self) { - self.pid.reset(); + self.fbi_pid.reset(); + self.fv_pid.reset(); self.fbi_buffer_count = 0; self.last_fbi = None; self.last_fv = None; @@ -134,15 +153,15 @@ impl Controller { impl ControllerStatus { pub fn debug_print(&self) { - println!("cathode ready: {}", self.ready); + println!("cathode rdy: {}", self.ready); if self.fbi.is_some() { - println!("emission: {}mA", 1000.0*self.fbi.unwrap()); + println!("emi: {}mA", 1000.0*self.fbi.unwrap()); } if self.fv.is_some() { - println!("fil voltage: {}V", self.fv.unwrap()); + println!("fil: {}V", self.fv.unwrap()); } if self.fbv.is_some() { - println!("bias voltage: {}V", self.fbv.unwrap()); + println!("bias: {}V", self.fbv.unwrap()); } } } diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 5863814..42c7920 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -84,7 +84,6 @@ fn main() { loop_anode.set_target(anode_cathode+cathode_bias); loop_cathode.set_emission_target(anode_cathode/10000.0); loop_cathode.set_bias_target(cathode_bias); - //board::set_fv_pwm(10); }); println!(r#" @@ -171,8 +170,9 @@ extern fn adc0_ss0(_ctxt: ADC0SS0) { time.set(time.get() + 1); if time.get() % 300 == 0 { - //loop_anode.get_status().debug_print(); - //loop_cathode.get_status().debug_print(); + println!(""); + loop_anode.get_status().debug_print(); + loop_cathode.get_status().debug_print(); electrometer.get_status().debug_print(); } });