command_parser: complete to all PidParameter tokens

This commit is contained in:
Astro 2019-09-14 21:35:06 +02:00
parent 700ab47f0e
commit b969f5c057
2 changed files with 36 additions and 29 deletions

View File

@ -69,14 +69,14 @@ pub enum ShowCommand {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum PidParameter { pub enum PidParameter {
Target(f32), Target,
KP(f32), KP,
KI(f32), KI,
KD(f32), KD,
OutputMin(f32), OutputMin,
OutputMax(f32), OutputMax,
IntegralMin(f32), IntegralMin,
IntegralMax(f32), IntegralMax,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -88,7 +88,10 @@ pub enum Command {
width: u32, width: u32,
total: u32, total: u32,
}, },
Pid(PidParameter), Pid {
parameter: PidParameter,
value: f32,
},
} }
fn whitespace(input: &[u8]) -> IResult<&[u8], ()> { fn whitespace(input: &[u8]) -> IResult<&[u8], ()> {
@ -149,17 +152,24 @@ fn pwm(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
Ok((input, Ok(Command::Pwm { width, total }))) Ok((input, Ok(Command::Pwm { width, total })))
} }
fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<PidParameter, Error>> { fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, parameter_f) = let (input, parameter) =
alt((value(PidParameter::KP, tag("kp")), alt((value(PidParameter::Target, tag("target")),
value(PidParameter::KP, tag("ki")) 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)?; ))(input)?;
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
// TODO: parse float // TODO: parse float
let (input, value) = unsigned(input)?; 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()); .map_err(|e| e.into());
Ok((input, value)) Ok((input, result))
} }
fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
@ -169,10 +179,7 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
alt(( alt((
preceded( preceded(
whitespace, whitespace,
|input| pid_parameter(input) pid_parameter
.map(|(input, parameter)| {
(input, parameter.map(Command::Pid))
})
), ),
|input| Ok((input, Ok(Command::Show(ShowCommand::Pid)))) |input| Ok((input, Ok(Command::Show(ShowCommand::Pid))))
))(input) ))(input)

View File

@ -286,24 +286,24 @@ fn main() -> ! {
board::set_timer_pwm(width, total); board::set_timer_pwm(width, total);
let _ = writeln!(socket, "PWM duty cycle: {}/{}", width, total); let _ = writeln!(socket, "PWM duty cycle: {}/{}", width, total);
} }
Command::Pid(p) => { Command::Pid { parameter, value } => {
use command_parser::PidParameter::*; use command_parser::PidParameter::*;
match p { match parameter {
Target(value) => Target =>
pid.set_target(value), pid.set_target(value),
KP(value) => KP =>
pid.update_parameters(|parameters| parameters.kp = value), pid.update_parameters(|parameters| parameters.kp = value),
KI(value) => KI =>
pid.update_parameters(|parameters| parameters.ki = value), pid.update_parameters(|parameters| parameters.ki = value),
KD(value) => KD =>
pid.update_parameters(|parameters| parameters.kd = value), pid.update_parameters(|parameters| parameters.kd = value),
OutputMin(value) => OutputMin =>
pid.update_parameters(|parameters| parameters.output_min = value), pid.update_parameters(|parameters| parameters.output_min = value),
OutputMax(value) => OutputMax =>
pid.update_parameters(|parameters| parameters.output_max = value), pid.update_parameters(|parameters| parameters.output_max = value),
IntegralMin(value) => IntegralMin =>
pid.update_parameters(|parameters| parameters.integral_min = value), pid.update_parameters(|parameters| parameters.integral_min = value),
IntegralMax(value) => IntegralMax =>
pid.update_parameters(|parameters| parameters.integral_max = value), pid.update_parameters(|parameters| parameters.integral_max = value),
} }
let _ = writeln!(socket, "PID parameter updated"); let _ = writeln!(socket, "PID parameter updated");