Skip to content

Commit

Permalink
fix: better handling of vertical lines
Browse files Browse the repository at this point in the history
Handles vertical lines better. Now the robot will actually turn the direction of the vertical line(instead of just upwards).
Also adds a temporary fix for #1.
  • Loading branch information
DakshG07 committed Sep 4, 2023
1 parent 3c6323d commit 2b4692b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
47 changes: 39 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
use egui::Pos2;
use egui_extras::RetainedImage;
// Uncomment this section to get access to the console_log macro
// Use console_log to print things to console. println macro doesn't work
// here, so you'll need it.
/*use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);
// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}
// */
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct PathyApp {
Expand Down Expand Up @@ -185,17 +211,22 @@ impl PathyApp {
let mut distance: f32 =
// f32::sqrt((pos.x - prev.x).powi(2) + (pos.x - prev.x).powi(2)).round() as i32;
cx / angle.to_radians().cos();
if (-1.0..=1.0).contains(&angle) {
// It's basically straight
angle = 0.0;
}
let mut complex_angle = ComplexAngle::new(-angle.round() as i32, pos.x >= prev.x);
if cx == 0.0 {
// Things get buggy with 1/0, manual override
if cy.is_sign_positive() {
complex_angle.angle = 90;
} else {
complex_angle.angle = -90;
}
angle = 90.0;
distance = cy;
}
let complex_angle = ComplexAngle::new(-angle.round() as i32, pos.x >= prev.x);
let turn: i32 = prev_angle.calculate_turn(&complex_angle);
let mut turn: i32 = prev_angle.calculate_turn(&complex_angle);
if (-1..=1).contains(&turn) {
// It's basically straight
turn = 0;
}
prev = *pos;
prev_angle = complex_angle;
(angle.round() as i32, distance.round() as i32, turn)
Expand Down Expand Up @@ -240,7 +271,7 @@ impl PathyApp {
// avoid unneccessary turns
result.push_str(
format!(
"\nchassis.set_turn_pid({}, TURN_SPEED)\nchassis.wait_drive();",
"\nchassis.set_turn_pid({}, TURN_SPEED);\nchassis.wait_drive();",
angle
)
.as_str(),
Expand All @@ -249,7 +280,7 @@ impl PathyApp {
}
Process::Drive(distance) => result.push_str(
format!(
"\nchassis.set_drive_pid({}, DRIVE_SPEED)\nchassis.wait_drive();",
"\nchassis.set_drive_pid({}, DRIVE_SPEED);\nchassis.wait_drive();",
distance
)
.as_str(),
Expand Down
30 changes: 2 additions & 28 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,6 @@ use crate::app::*;
use eframe::egui::Visuals;
use egui::{pos2, Color32, Pos2, Rect, Sense, Stroke, Vec2};
use egui_extras::RetainedImage;
// Uncomment this section to get access to the console_log macro
// Use console_log to print things to console. println macro doesn't work
// here, so you'll need it.
/*use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);
// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}
// */
/// We derive Deserialize/Serialize so we can persist app state on shutdown.

impl eframe::App for PathyApp {
Expand Down Expand Up @@ -149,7 +122,8 @@ impl eframe::App for PathyApp {
*processed = Vec::new();
*result = None;
}
if ui.button("Preprocess").clicked() {
// Both an "optimization" and a hacky workaround (see https://github.com/750W/pathy/issues/1)
if processed.is_empty() && ui.button("Preprocess").clicked() {
*processed = Self::preprocess(path, (*scale, aspecty), (*width, *height));
}
// Order here is important, as the ui button is only rendered if the first
Expand Down

0 comments on commit 2b4692b

Please sign in to comment.