Fix clippy warnings

This commit is contained in:
Max Känner 2023-01-29 15:39:39 +01:00
parent a4ac3b9c9c
commit f730c0baf1
4 changed files with 49 additions and 83 deletions

1
Cargo.lock generated
View File

@ -207,6 +207,7 @@ dependencies = [
"ev3dev-lang-rust", "ev3dev-lang-rust",
"image", "image",
"imageproc", "imageproc",
"itertools",
"paste", "paste",
"pid", "pid",
"rand 0.8.5", "rand 0.8.5",

View File

@ -12,6 +12,7 @@ paste = "1.0"
pid = "4.0" pid = "4.0"
thiserror = "1.0" thiserror = "1.0"
rand = "0.8" rand = "0.8"
itertools = "0.10"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -6,6 +6,7 @@ use imageproc::{
drawing::{draw_line_segment_mut, draw_polygon_mut, draw_text_mut, text_size}, drawing::{draw_line_segment_mut, draw_polygon_mut, draw_text_mut, text_size},
point::Point, point::Point,
}; };
use itertools::Itertools;
use rusttype::{Font, Scale}; use rusttype::{Font, Scale};
const BACKGROUND: Rgb<u8> = Rgb([255; 3]); const BACKGROUND: Rgb<u8> = Rgb([255; 3]);
@ -20,24 +21,22 @@ pub enum Parameter {
Speed, Speed,
} }
#[allow(clippy::similar_names)]
pub fn draw_calibration(screen: &mut Screen, parameter: &str, value: impl Display, font: &Font) { pub fn draw_calibration(screen: &mut Screen, parameter: &str, value: impl Display, font: &Font) {
#[allow(clippy::cast_precision_loss)]
let smal_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0); let smal_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0);
#[allow(clippy::cast_precision_loss)]
let big_scale = Scale::uniform(screen.yres() as f32 / 2.0 - 8.0); let big_scale = Scale::uniform(screen.yres() as f32 / 2.0 - 8.0);
let center = screen.xres() as i32 / 2; let xres = i32::try_from(screen.xres()).expect("range checked");
let yres = i32::try_from(screen.yres()).expect("range checked");
let center = xres / 2;
clear(screen); clear(screen);
draw_centered_text(screen, center, 4, smal_scale, font, "kalibrieren:"); draw_centered_text(screen, center, 4, smal_scale, font, "kalibrieren:");
draw_centered_text(screen, center, yres / 4 + 4, smal_scale, font, parameter);
draw_centered_text( draw_centered_text(
screen, screen,
center, center,
screen.yres() as i32 / 4 + 4, yres / 2 + 4,
smal_scale,
font,
parameter,
);
draw_centered_text(
screen,
center,
screen.yres() as i32 / 2 + 4,
big_scale, big_scale,
font, font,
format!("{value:.2}").as_str(), format!("{value:.2}").as_str(),
@ -54,11 +53,12 @@ pub fn draw_settings(
selected: Parameter, selected: Parameter,
font: &Font, font: &Font,
) { ) {
#[allow(clippy::cast_precision_loss)]
let text_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0); let text_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0);
clear(screen); clear(screen);
draw_centered_text( draw_centered_text(
screen, screen,
screen.xres() as i32 / 2, i32::try_from(screen.xres()).expect("range checked") / 2,
4, 4,
text_scale, text_scale,
font, font,
@ -77,8 +77,9 @@ pub fn draw_driving(screen: &mut Screen, font: &Font) {
clear(screen); clear(screen);
draw_centered_text( draw_centered_text(
screen, screen,
screen.xres() as i32 / 2, i32::try_from(screen.xres()).expect("range checked") / 2,
4, 4,
#[allow(clippy::cast_precision_loss)]
Scale::uniform(screen.yres() as f32 / 2.0 - 8.0), Scale::uniform(screen.yres() as f32 / 2.0 - 8.0),
font, font,
"Fahrt", "Fahrt",
@ -86,21 +87,18 @@ pub fn draw_driving(screen: &mut Screen, font: &Font) {
screen.update(); screen.update();
} }
#[allow(clippy::similar_names)]
pub fn draw_finished(screen: &mut Screen, time: Duration, font: &Font) { pub fn draw_finished(screen: &mut Screen, time: Duration, font: &Font) {
clear(screen); clear(screen);
#[allow(clippy::cast_precision_loss)]
let text_scale = Scale::uniform(screen.yres() as f32 / 2.0 - 8.0); let text_scale = Scale::uniform(screen.yres() as f32 / 2.0 - 8.0);
let xres = i32::try_from(screen.xres()).expect("range checked");
let yres = i32::try_from(screen.yres()).expect("range checked");
draw_centered_text(screen, xres / 2, 4, text_scale, font, "Zeit:");
draw_centered_text( draw_centered_text(
screen, screen,
screen.xres() as i32 / 2, xres / 2,
4, yres / 2 + 4,
text_scale,
font,
"Zeit:",
);
draw_centered_text(
screen,
screen.xres() as i32 / 2,
screen.yres() as i32 / 2 + 4,
text_scale, text_scale,
font, font,
format!("{:.2}s", time.as_secs_f32()).as_str(), format!("{:.2}s", time.as_secs_f32()).as_str(),
@ -108,32 +106,11 @@ pub fn draw_finished(screen: &mut Screen, time: Duration, font: &Font) {
screen.update(); screen.update();
} }
pub fn draw_cycles(screen: &mut Screen, cycles: u32, font: &Font) {
clear(screen);
let text_scale = Scale::uniform(screen.yres() as f32 / 2.0 - 8.0);
draw_centered_text(
screen,
screen.xres() as i32 / 2,
4,
text_scale,
font,
"Cycles:",
);
draw_centered_text(
screen,
screen.xres() as i32 / 2,
screen.yres() as i32 / 2 + 4,
text_scale,
font,
format!("{cycles}").as_str(),
);
screen.update();
}
pub fn font() -> Font<'static> { pub fn font() -> Font<'static> {
Font::try_from_bytes(FONT_DATA).unwrap() Font::try_from_bytes(FONT_DATA).unwrap()
} }
#[allow(clippy::similar_names)]
fn draw_setting( fn draw_setting(
screen: &mut Screen, screen: &mut Screen,
value: impl Display, value: impl Display,
@ -142,50 +119,44 @@ fn draw_setting(
name: &str, name: &str,
font: &Font, font: &Font,
) { ) {
#[allow(clippy::cast_precision_loss)]
let text_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0); let text_scale = Scale::uniform(screen.yres() as f32 / 4.0 - 8.0);
#[allow(clippy::cast_precision_loss)]
let value_scale = Scale { let value_scale = Scale {
x: screen.xres() as f32 / 10.0, x: screen.xres() as f32 / 10.0,
y: screen.yres() as f32 / 4.0 - 8.0, y: screen.yres() as f32 / 4.0 - 8.0,
}; };
let x = screen.xres() as i32 / 8 + screen.xres() as i32 * i32::from(index) / 4; let xres = i32::try_from(screen.xres()).expect("range checked");
let yres = i32::try_from(screen.yres()).expect("range checked");
let x = xres / 8 + xres * i32::from(index) / 4;
draw_centered_text(screen, x, yres / 4 + 4, text_scale, font, name);
draw_centered_text( draw_centered_text(
screen, screen,
x, x,
screen.yres() as i32 / 4 + 4, yres * 5 / 8 + 4,
text_scale,
font,
name,
);
draw_centered_text(
screen,
x,
screen.yres() as i32 * 5 / 8 + 4,
value_scale, value_scale,
font, font,
format!("{value:.2}").as_str(), format!("{value:.2}").as_str(),
); );
let top_triangle = [ let top_triangle = [
Point::new(x, screen.yres() as i32 / 2), Point::new(x, yres / 2),
Point::new(x - 10, screen.yres() as i32 / 2 + 10), Point::new(x - 10, yres / 2 + 10),
Point::new(x + 10, screen.yres() as i32 / 2 + 10), Point::new(x + 10, yres / 2 + 10),
]; ];
let bottom_triangle = [ let bottom_triangle = [
Point::new(x, screen.yres() as i32), Point::new(x, yres),
Point::new(x - 10, screen.yres() as i32 - 10), Point::new(x - 10, yres - 10),
Point::new(x + 10, screen.yres() as i32 - 10), Point::new(x + 10, yres - 10),
]; ];
if selected { if selected {
draw_polygon_mut(&mut screen.image, &top_triangle, FOREGROUND); draw_polygon_mut(&mut screen.image, &top_triangle, FOREGROUND);
draw_polygon_mut(&mut screen.image, &bottom_triangle, FOREGROUND); draw_polygon_mut(&mut screen.image, &bottom_triangle, FOREGROUND);
} else { } else {
for (start, end) in [ for (start, end) in top_triangle
(top_triangle[0], top_triangle[1]), .into_iter()
(top_triangle[1], top_triangle[2]), .circular_tuple_windows()
(top_triangle[2], top_triangle[0]), .chain(bottom_triangle.into_iter().circular_tuple_windows())
(bottom_triangle[0], bottom_triangle[1]), {
(bottom_triangle[1], bottom_triangle[2]),
(bottom_triangle[2], bottom_triangle[0]),
] {
draw_line_segment_mut( draw_line_segment_mut(
&mut screen.image, &mut screen.image,
point2tuple(start), point2tuple(start),

View File

@ -151,16 +151,6 @@ fn select_values(
(k_p, k_i, k_d, v) (k_p, k_i, k_d, v)
} }
fn increase(parameter: &mut f32) {
let change = 0.01;
*parameter = (*parameter + change).min(99.99);
}
fn decrease(parameter: &mut f32) {
let change = 0.01;
*parameter = (*parameter - change).max(0.0);
}
fn calibrate_gray( fn calibrate_gray(
screen: &mut Screen, screen: &mut Screen,
sensor: &ColorSensor, sensor: &ColorSensor,
@ -201,7 +191,7 @@ fn follow_line(
(k_p, k_i, k_d): (f32, f32, f32), (k_p, k_i, k_d): (f32, f32, f32),
v: i32, v: i32,
) -> Result<Duration, ProgramError> { ) -> Result<Duration, ProgramError> {
let mut controller = Pid::new(setpoint, f32::INFINITY); let mut controller = Pid::new(setpoint, 200.0);
controller controller
.p(k_p, f32::INFINITY) .p(k_p, f32::INFINITY)
.i(k_i, f32::INFINITY) .i(k_i, f32::INFINITY)
@ -217,9 +207,10 @@ fn follow_line(
while !(buttons.is_enter_pressed() || matches!(state, LineFollowState::Finish(_))) { while !(buttons.is_enter_pressed() || matches!(state, LineFollowState::Finish(_))) {
let color = sensor.get_rgb()?; let color = sensor.get_rgb()?;
let measurement = gray(color); let measurement = gray(color);
let controll = controller.next_control_output(measurement).output; #[allow(clippy::cast_possible_truncation)]
left.set_duty_cycle_sp((v + (controll as i32)).clamp(-100, 100))?; let controll = controller.next_control_output(measurement).output as i32;
right.set_duty_cycle_sp((v - (controll as i32)).clamp(-100, 100))?; left.set_duty_cycle_sp((v + controll).clamp(-100, 100))?;
right.set_duty_cycle_sp((v - controll).clamp(-100, 100))?;
let red = is_red(color); let red = is_red(color);
match state { match state {
LineFollowState::PreStart if red => state = LineFollowState::Start(Instant::now()), LineFollowState::PreStart if red => state = LineFollowState::Start(Instant::now()),
@ -247,7 +238,9 @@ fn sleep_until(time: Instant) {
} }
fn gray((r, g, b): (i32, i32, i32)) -> f32 { fn gray((r, g, b): (i32, i32, i32)) -> f32 {
(r + g + b) as f32 / 3.0 #[allow(clippy::cast_precision_loss)]
let sum = (r + g + b) as f32;
sum / 3.0
} }
const fn is_red((r, g, b): (i32, i32, i32)) -> bool { const fn is_red((r, g, b): (i32, i32, i32)) -> bool {