compute emission current, filament voltage and bias voltage

This commit is contained in:
Sebastien Bourdeauducq 2017-05-10 00:10:52 +08:00
parent 9662570999
commit 8975f8c240
3 changed files with 64 additions and 11 deletions

View File

@ -31,9 +31,17 @@ const UART_DIV_16P6: u32 = /*altclk*/16_000_000 * (1 << /*len(divfrac)*/6) /
pub const AV_ADC_GAIN: f32 = 6.792703150912105; pub const AV_ADC_GAIN: f32 = 6.792703150912105;
pub const FV_ADC_GAIN: f32 = 501.83449105726623; pub const FV_ADC_GAIN: f32 = 501.83449105726623;
pub const FBI_ADC_GAIN: f32 = 1333.3333333333333;
pub const FBI_ADC_OFFSET: f32 = 96.0;
pub const FD_ADC_GAIN: f32 = 4170.212765957447;
pub const FD_ADC_OFFSET: f32 = 96.0;
pub const FBV_ADC_GAIN: f32 = 49.13796058269066; pub const FBV_ADC_GAIN: f32 = 49.13796058269066;
pub const FBV_PWM_GAIN: f32 = 0.5730803571428571; pub const FBV_PWM_GAIN: f32 = 0.5730803571428571;
pub const FBI_R223: f32 = 200.0;
pub const FBI_R224: f32 = 39.0;
pub const FBI_R225: f32 = 22000.0;
pub fn set_led(nr: u8, state: bool) { pub fn set_led(nr: u8, state: bool) {
let bit = match nr { let bit = match nr {
@ -72,7 +80,8 @@ pub fn set_fbv_pwm(duty: u16) {
}); });
} }
#[allow(dead_code)]
#[derive(Clone, Copy)]
pub enum EmissionRange { pub enum EmissionRange {
Low, // 22K Low, // 22K
Med, // 22K//(200Ω + compensated diode) Med, // 22K//(200Ω + compensated diode)

View File

@ -14,9 +14,14 @@ const PID_PARAMETERS: pid::Parameters = pid::Parameters {
}; };
pub struct Controller { pub struct Controller {
fbi_target: f32,
fbi_range: board::EmissionRange,
last_fbi: Option<f32>,
pid: pid::Controller, pid: pid::Controller,
fbv_target: f32,
last_fv: Option<f32>, last_fv: Option<f32>,
fbv_target: f32,
last_fbv: Option<f32> last_fbv: Option<f32>
} }
@ -24,16 +29,46 @@ pub struct Controller {
impl Controller { impl Controller {
pub const fn new() -> Controller { pub const fn new() -> Controller {
Controller { Controller {
fbi_target: 0.0,
fbi_range: board::EmissionRange::Low,
last_fbi: None,
pid: pid::Controller::new(PID_PARAMETERS), pid: pid::Controller::new(PID_PARAMETERS),
fbv_target: 0.0,
last_fv: None, last_fv: None,
fbv_target: 0.0,
last_fbv: None, last_fbv: None,
} }
} }
pub fn adc_input(&mut self, _fbi_sample: u16, _fd_sample: u16, fv_sample: u16, fbv_sample: u16) { pub fn adc_input(&mut self, fbi_sample: u16, fd_sample: u16, fv_sample: u16, fbv_sample: u16) {
self.last_fbv = Some(fbv_sample as f32/board::FBV_ADC_GAIN); let fbi_voltage = ((fbi_sample as f32) - board::FBI_ADC_OFFSET)/board::FBI_ADC_GAIN;
let fbi_r225 = fbi_voltage/board::FBI_R225;
self.last_fbi = Some(match self.fbi_range {
board::EmissionRange::Low => fbi_r225,
board::EmissionRange::Med => {
let fd_voltage = ((fd_sample as f32) - board::FD_ADC_OFFSET)/board::FD_ADC_GAIN;
fbi_r225 + (fbi_voltage - fd_voltage)/board::FBI_R223
},
board::EmissionRange::High => {
let fd_voltage = 0.9;
fbi_r225 + (fbi_voltage - fd_voltage)/board::FBI_R224
}
});
self.last_fv = Some(fv_sample as f32/board::FV_ADC_GAIN); self.last_fv = Some(fv_sample as f32/board::FV_ADC_GAIN);
self.last_fbv = Some(fbv_sample as f32/board::FBV_ADC_GAIN);
}
pub fn set_emission_target(&mut self, amperes: f32) {
self.fbi_target = amperes;
self.fbi_range = board::EmissionRange::Low;
if amperes > 120e-6 {
self.fbi_range = board::EmissionRange::Med;
}
if amperes > 8e-3 {
self.fbi_range = board::EmissionRange::High;
}
board::set_emission_range(self.fbi_range);
} }
pub fn set_bias_target(&mut self, volts: f32) { pub fn set_bias_target(&mut self, volts: f32) {
@ -53,6 +88,9 @@ impl Controller {
} }
pub fn ready(&self) -> bool { pub fn ready(&self) -> bool {
hprintln!("emission current: {}mA", 1000.0*self.last_fbi.unwrap());
hprintln!("filament voltage: {}V", self.last_fv.unwrap());
hprintln!("bias voltage: {}V", self.last_fbv.unwrap());
self.emission_ready() & self.bias_ready() self.emission_ready() & self.bias_ready()
} }
} }

View File

@ -68,11 +68,14 @@ fn main() {
let nvic = tm4c129x::NVIC.borrow(cs); let nvic = tm4c129x::NVIC.borrow(cs);
nvic.enable(Interrupt::ADC0SS0); nvic.enable(Interrupt::ADC0SS0);
board::set_emission_range(board::EmissionRange::High); let mut loop_anode = LOOP_ANODE.borrow(cs).borrow_mut();
let bias = 15.0; let mut loop_cathode = LOOP_CATHODE.borrow(cs).borrow_mut();
LOOP_ANODE.borrow(cs).borrow_mut().set_target(70.0+bias); let anode_cathode = 60.0;
LOOP_CATHODE.borrow(cs).borrow_mut().set_bias_target(bias); let cathode_bias = 12.0;
//board::set_fv_pwm(10); 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!("ready"); println!("ready");
@ -115,7 +118,10 @@ extern fn adc0_ss0(ctxt: ADC0SS0) {
board::set_led(1, false); board::set_led(1, false);
board::set_led(2, true); board::set_led(2, true);
} }
}) if elapsed.get() == 1000 {
loop_cathode.ready();
}
});
} }
#[used] #[used]