Automatically execute the process again on a panic
This commit is contained in:
parent
1ad9e64340
commit
f13ab9deb9
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -205,6 +205,7 @@ name = "ev3dev-pid-linefollow"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ev3dev-lang-rust",
|
||||
"exec",
|
||||
"image",
|
||||
"imageproc",
|
||||
"itertools",
|
||||
@ -215,6 +216,16 @@ dependencies = [
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exec"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "886b70328cba8871bfc025858e1de4be16b1d5088f2ba50b57816f4210672615"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exr"
|
||||
version = "1.5.2"
|
||||
|
@ -13,6 +13,7 @@ pid = "4.0"
|
||||
thiserror = "1.0"
|
||||
rand = "0.8"
|
||||
itertools = "0.10"
|
||||
exec = "0.3"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
15
src/main.rs
15
src/main.rs
@ -8,19 +8,30 @@ use display::{
|
||||
use ev3dev_lang_rust::{
|
||||
motors::LargeMotor, sensors::ColorSensor, Device, Ev3Error, Ev3Result, Screen,
|
||||
};
|
||||
use exec::execvp;
|
||||
use itertools::Itertools;
|
||||
use pid::Pid;
|
||||
use rand::{thread_rng, Rng};
|
||||
use rusttype::Font;
|
||||
use std::{
|
||||
env,
|
||||
error::Error,
|
||||
iter,
|
||||
iter, panic,
|
||||
thread::sleep,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user