Compare commits

..

2 Commits

Author SHA1 Message Date
Max
f13ab9deb9 Automatically execute the process again on a panic 2023-02-08 22:33:09 +01:00
Max
1ad9e64340 First fastly increment the first digit, then the second 2023-02-08 22:32:27 +01:00
3 changed files with 41 additions and 4 deletions

11
Cargo.lock generated
View File

@ -205,6 +205,7 @@ name = "ev3dev-pid-linefollow"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ev3dev-lang-rust", "ev3dev-lang-rust",
"exec",
"image", "image",
"imageproc", "imageproc",
"itertools", "itertools",
@ -215,6 +216,16 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "exec"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "886b70328cba8871bfc025858e1de4be16b1d5088f2ba50b57816f4210672615"
dependencies = [
"errno",
"libc",
]
[[package]] [[package]]
name = "exr" name = "exr"
version = "1.5.2" version = "1.5.2"

View File

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

View File

@ -8,19 +8,30 @@ use display::{
use ev3dev_lang_rust::{ use ev3dev_lang_rust::{
motors::LargeMotor, sensors::ColorSensor, Device, Ev3Error, Ev3Result, Screen, motors::LargeMotor, sensors::ColorSensor, Device, Ev3Error, Ev3Result, Screen,
}; };
use exec::execvp;
use itertools::Itertools;
use pid::Pid; use pid::Pid;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rusttype::Font; use rusttype::Font;
use std::{ use std::{
env,
error::Error, error::Error,
iter, iter, panic,
thread::sleep, thread::sleep,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use thiserror::Error; use thiserror::Error;
fn main() { fn main() {
while try_main().is_err() { // This loop just restarts the program in case of an error.
// Such error may occur when a motor or sensor got disconnected.
let args = env::args().collect_vec();
panic::set_hook(Box::new(move |_| {
let err = execvp(&args[0], &args[1..]);
println!("Couldn't restart the program: {err}");
}));
while let Err(e) = try_main() {
println!("Error: {e:?}");
sleep(Duration::from_millis(10)); sleep(Duration::from_millis(10));
} }
} }
@ -89,10 +100,24 @@ fn select_values(
let mut k_d = thread_rng().gen_range(0.01..=10.0); let mut k_d = thread_rng().gen_range(0.01..=10.0);
let mut v = thread_rng().gen_range(1..=100); let mut v = thread_rng().gen_range(1..=100);
let mut selected = Kp; let mut selected = Kp;
let mut times_pressed_up = 0;
let mut times_pressed_down = 0;
draw_settings(screen, k_p, k_i, k_d, v, selected, font); draw_settings(screen, k_p, k_i, k_d, v, selected, font);
loop { loop {
buttons.process(); buttons.process();
if buttons.is_up_pressed() {
times_pressed_up = 0;
}
if buttons.is_down_pressed() {
times_pressed_down = 0;
}
if buttons.is_up() {
times_pressed_up += 1;
}
if buttons.is_down() {
times_pressed_down += 1;
}
if buttons.is_enter_pressed() { if buttons.is_enter_pressed() {
break; break;
} }
@ -130,7 +155,7 @@ fn select_values(
}; };
if buttons.is_up() { if buttons.is_up() {
*param = (*param *param = (*param
+ if buttons.is_up_pressed() { + if times_pressed_up > 10 {
0.01f32 0.01f32
} else { } else {
0.1f32 0.1f32
@ -139,7 +164,7 @@ fn select_values(
} }
if buttons.is_down() { if buttons.is_down() {
*param = (*param *param = (*param
- if buttons.is_down_pressed() { - if times_pressed_down > 10 {
0.01f32 0.01f32
} else { } else {
0.1f32 0.1f32