From b969f5c057085ead5f1fd21a1352119cf5523e43 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 14 Sep 2019 21:35:06 +0200 Subject: [PATCH] command_parser: complete to all PidParameter tokens --- firmware/src/command_parser.rs | 45 ++++++++++++++++++++-------------- firmware/src/main.rs | 20 +++++++-------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index b597336..7fdd9e6 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -69,14 +69,14 @@ pub enum ShowCommand { #[derive(Debug, Clone)] pub enum PidParameter { - Target(f32), - KP(f32), - KI(f32), - KD(f32), - OutputMin(f32), - OutputMax(f32), - IntegralMin(f32), - IntegralMax(f32), + Target, + KP, + KI, + KD, + OutputMin, + OutputMax, + IntegralMin, + IntegralMax, } #[derive(Debug, Clone)] @@ -88,7 +88,10 @@ pub enum Command { width: u32, total: u32, }, - Pid(PidParameter), + Pid { + parameter: PidParameter, + value: f32, + }, } fn whitespace(input: &[u8]) -> IResult<&[u8], ()> { @@ -149,17 +152,24 @@ fn pwm(input: &[u8]) -> IResult<&[u8], Result> { Ok((input, Ok(Command::Pwm { width, total }))) } -fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result> { - let (input, parameter_f) = - alt((value(PidParameter::KP, tag("kp")), - value(PidParameter::KP, tag("ki")) +fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result> { + let (input, parameter) = + alt((value(PidParameter::Target, tag("target")), + value(PidParameter::KP, tag("kp")), + value(PidParameter::KI, tag("ki")), + value(PidParameter::KD, tag("kd")), + value(PidParameter::OutputMin, tag("output_min")), + value(PidParameter::OutputMax, tag("output_max")), + value(PidParameter::IntegralMin, tag("integral_min")), + value(PidParameter::IntegralMax, tag("integral_max")) ))(input)?; let (input, _) = whitespace(input)?; // TODO: parse float let (input, value) = unsigned(input)?; - let value = value.map(|value| parameter_f(value as f32)) + let result = value + .map(|value| Command::Pid { parameter, value: value as f32 }) .map_err(|e| e.into()); - Ok((input, value)) + Ok((input, result)) } fn pid(input: &[u8]) -> IResult<&[u8], Result> { @@ -169,10 +179,7 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result> { alt(( preceded( whitespace, - |input| pid_parameter(input) - .map(|(input, parameter)| { - (input, parameter.map(Command::Pid)) - }) + pid_parameter ), |input| Ok((input, Ok(Command::Show(ShowCommand::Pid)))) ))(input) diff --git a/firmware/src/main.rs b/firmware/src/main.rs index b109f83..68d83cf 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -286,24 +286,24 @@ fn main() -> ! { board::set_timer_pwm(width, total); let _ = writeln!(socket, "PWM duty cycle: {}/{}", width, total); } - Command::Pid(p) => { + Command::Pid { parameter, value } => { use command_parser::PidParameter::*; - match p { - Target(value) => + match parameter { + Target => pid.set_target(value), - KP(value) => + KP => pid.update_parameters(|parameters| parameters.kp = value), - KI(value) => + KI => pid.update_parameters(|parameters| parameters.ki = value), - KD(value) => + KD => pid.update_parameters(|parameters| parameters.kd = value), - OutputMin(value) => + OutputMin => pid.update_parameters(|parameters| parameters.output_min = value), - OutputMax(value) => + OutputMax => pid.update_parameters(|parameters| parameters.output_max = value), - IntegralMin(value) => + IntegralMin => pid.update_parameters(|parameters| parameters.integral_min = value), - IntegralMax(value) => + IntegralMax => pid.update_parameters(|parameters| parameters.integral_max = value), } let _ = writeln!(socket, "PID parameter updated");