Automatically execute the process again on a panic

This commit is contained in:
Max 2023-02-08 22:33:09 +01:00
parent 1ad9e64340
commit f13ab9deb9
3 changed files with 25 additions and 2 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));
} }
} }