pid: export summary as json

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

View File

@ -81,11 +81,35 @@ impl Controller {
output output
} }
#[allow(dead_code)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.integral = 0.0; self.integral = 0.0;
self.last_input = None; 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)] #[cfg(test)]
@ -126,4 +150,13 @@ mod test {
total_t += 1; 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'}');
}
} }