pid: export summary as json

pull/20/head
Astro 2020-09-30 20:06:47 +02:00
parent 11f2ebe961
commit 5521563c91
2 changed files with 56 additions and 23 deletions

View File

@ -96,7 +96,6 @@ fn report_to(channel: usize, channels: &mut Channels, socket: &mut TcpSocket) ->
let _ = socket.send_slice(b"\n");
// success
return true
// TODO: session.mark_report_sent(channel);
}
Ok(sent) =>
warn!("sent only {}/{} bytes of report", sent, buf.len()),
@ -208,29 +207,30 @@ fn main() -> ! {
}
Command::Show(ShowCommand::Pid) => {
for channel in 0..CHANNELS {
let state = channels.channel_state(channel);
let _ = writeln!(socket, "PID settings for channel {}", channel);
let pid = &state.pid;
let _ = writeln!(socket, "- target={:.4} °C", pid.target);
macro_rules! show_pid_parameter {
($p: tt) => {
let _ = writeln!(
socket, "- {}={:.4}",
stringify!($p), pid.parameters.$p
let send_free = socket.send_capacity() - socket.send_queue();
match channels.channel_state(channel).pid.summary(channel).to_json() {
Ok(buf) if buf.len() > send_free + 1 => {
// Not enough buffer space, skip report for now
warn!(
"TCP socket has only {}/{} needed {}",
send_free + 1, socket.send_capacity(), buf.len(),
);
};
}
Ok(buf) => {
match socket.send_slice(&buf) {
Ok(sent) if sent == buf.len() => {
let _ = socket.send_slice(b"\n");
// success
}
Ok(sent) =>
warn!("sent only {}/{} bytes of summary", sent, buf.len()),
Err(e) =>
error!("error sending summary: {:?}", e),
}
}
Err(e) =>
error!("unable to serialize pid summary: {:?}", e),
}
show_pid_parameter!(kp);
show_pid_parameter!(ki);
show_pid_parameter!(kd);
show_pid_parameter!(integral_min);
show_pid_parameter!(integral_max);
show_pid_parameter!(output_min);
show_pid_parameter!(output_max);
if let Some(last_output) = pid.last_output {
let _ = writeln!(socket, "- last_output={:.3} A", last_output);
}
let _ = writeln!(socket, "");
}
}
Command::Show(ShowCommand::Pwm) => {

View File

@ -81,11 +81,35 @@ impl Controller {
output
}
#[allow(dead_code)]
pub fn reset(&mut self) {
self.integral = 0.0;
self.last_input = None;
}
pub fn summary(&self, channel: usize) -> Summary {
Summary {
channel,
parameters: self.parameters.clone(),
target: self.target,
integral: self.integral,
}
}
}
type JsonBuffer = heapless::Vec<u8, heapless::consts::U240>;
#[derive(Clone, Serialize, Deserialize)]
pub struct Summary {
channel: usize,
parameters: Parameters,
target: f64,
integral: f64,
}
impl Summary {
pub fn to_json(&self) -> Result<JsonBuffer, serde_json_core::ser::Error> {
serde_json_core::to_vec(self)
}
}
#[cfg(test)]
@ -126,4 +150,13 @@ mod test {
total_t += 1;
}
}
#[test]
fn summary_to_json() {
let mut pid = Controller::new(PARAMETERS.clone());
pid.target = 30.0 / 1.1;
let buf = pid.summary(0).to_json().unwrap();
assert_eq!(buf[0], b'{');
assert_eq!(buf[buf.len() - 1], b'}');
}
}