2017-05-11 23:15:01 +08:00
|
|
|
use core::num::Float;
|
|
|
|
|
2017-05-09 15:57:54 +08:00
|
|
|
use board;
|
|
|
|
use pid;
|
|
|
|
|
|
|
|
const PID_PARAMETERS: pid::Parameters = pid::Parameters {
|
2017-06-12 19:22:23 +08:00
|
|
|
kp: 0.2,
|
|
|
|
ki: 0.05,
|
2017-05-09 15:57:54 +08:00
|
|
|
kd: 0.0,
|
|
|
|
output_min: 0.0,
|
2017-06-12 19:22:23 +08:00
|
|
|
output_max: 225.0,
|
2017-05-12 11:07:35 +08:00
|
|
|
integral_min: -700.0,
|
|
|
|
integral_max: 700.0
|
2017-05-09 15:57:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Controller {
|
2017-05-11 23:15:01 +08:00
|
|
|
pid: pid::Controller,
|
|
|
|
target: f32,
|
|
|
|
last_av: Option<f32>
|
2017-05-09 15:57:54 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 11:07:35 +08:00
|
|
|
#[derive(Clone, Copy)]
|
2017-05-11 23:15:01 +08:00
|
|
|
pub struct ControllerStatus {
|
|
|
|
pub ready: bool,
|
|
|
|
pub av: Option<f32>
|
|
|
|
}
|
2017-05-09 15:57:54 +08:00
|
|
|
|
|
|
|
impl Controller {
|
|
|
|
pub const fn new() -> Controller {
|
|
|
|
Controller {
|
2017-05-11 23:15:01 +08:00
|
|
|
pid: pid::Controller::new(PID_PARAMETERS),
|
|
|
|
target: 0.0,
|
|
|
|
last_av: None
|
2017-05-09 15:57:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn adc_input(&mut self, av_sample: u16) {
|
2017-05-11 23:15:01 +08:00
|
|
|
let av = av_sample as f32/board::AV_ADC_GAIN;
|
|
|
|
self.last_av = Some(av);
|
|
|
|
|
2017-05-12 11:07:35 +08:00
|
|
|
let hv_pwm_duty = self.pid.update(av);
|
|
|
|
board::set_hv_pwm(hv_pwm_duty as u16)
|
2017-05-09 15:57:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_target(&mut self, volts: f32) {
|
2017-06-12 19:22:23 +08:00
|
|
|
self.target = volts;
|
2017-05-11 23:15:01 +08:00
|
|
|
self.pid.set_target(volts);
|
2017-05-09 15:57:54 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 23:15:01 +08:00
|
|
|
fn ready(&self) -> bool {
|
|
|
|
match self.last_av {
|
|
|
|
None => false,
|
2017-05-18 17:07:07 +08:00
|
|
|
Some(last_av) => (last_av - self.target).abs() < 2.0
|
2017-05-11 23:15:01 +08:00
|
|
|
}
|
2017-05-09 15:57:54 +08:00
|
|
|
}
|
2017-05-11 14:55:00 +08:00
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.pid.reset();
|
2017-05-11 23:15:01 +08:00
|
|
|
board::set_hv_pwm(0);
|
2017-05-11 14:55:00 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 23:15:01 +08:00
|
|
|
pub fn get_status(&self) -> ControllerStatus {
|
|
|
|
ControllerStatus {
|
|
|
|
ready: self.ready(),
|
|
|
|
av: self.last_av
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|