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",
"image",
"imageproc",
"itertools",
"paste",
"pid",
"rand 0.8.5",

View File

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

View File

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

View File

@ -151,16 +151,6 @@ fn select_values(
(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(
screen: &mut Screen,
sensor: &ColorSensor,
@ -201,7 +191,7 @@ fn follow_line(
(k_p, k_i, k_d): (f32, f32, f32),
v: i32,
) -> Result<Duration, ProgramError> {
let mut controller = Pid::new(setpoint, f32::INFINITY);
let mut controller = Pid::new(setpoint, 200.0);
controller
.p(k_p, f32::INFINITY)
.i(k_i, f32::INFINITY)
@ -217,9 +207,10 @@ fn follow_line(
while !(buttons.is_enter_pressed() || matches!(state, LineFollowState::Finish(_))) {
let color = sensor.get_rgb()?;
let measurement = gray(color);
let controll = controller.next_control_output(measurement).output;
left.set_duty_cycle_sp((v + (controll as i32)).clamp(-100, 100))?;
right.set_duty_cycle_sp((v - (controll as i32)).clamp(-100, 100))?;
#[allow(clippy::cast_possible_truncation)]
let controll = controller.next_control_output(measurement).output as i32;
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);
match state {
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 {
(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 {