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)]
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<Command, Error>> {
Ok((input, Ok(Command::Pwm { width, total })))
}
fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<PidParameter, Error>> {
let (input, parameter_f) =
alt((value(PidParameter::KP, tag("kp")),
value(PidParameter::KP, tag("ki"))
fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
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<Command, Error>> {
@ -169,10 +179,7 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
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)

View File

@ -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");