Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating and Integrating pathfinding #28

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lunabotics/lunabot/src/apps/sim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::str;
use std::ops::Deref;
use std::{
cmp::Ordering,
collections::VecDeque,
Expand All @@ -14,6 +15,7 @@ use crossbeam::atomic::AtomicCell;
use gputter::{init_gputter_blocking, wgpu::hal::auxil::db};
use lunabot_ai::{run_ai, Action, Input};
use nalgebra::{Isometry3, UnitQuaternion, UnitVector3, Vector2, Vector3};
use pathfinding::Pathfinder;
use serde::{Deserialize, Serialize};
use urobotics::tokio;
use urobotics::{
Expand Down Expand Up @@ -287,6 +289,20 @@ impl Application for LunasimbotApp {
lunasim_stdin2.write(bytes);
}));

// Add callback for pathfinding
const CELL_COUNT: usize = 64 * 128;
let mut height_map: Arc<Mutex<[f32; CELL_COUNT]>> =
Arc::new(Mutex::new([0f32; CELL_COUNT]));
let mut height_map_copy = height_map.clone();
heightmap_callbacks.add_dyn_fn_mut(Box::new(move |heights| {
let mut map_arr = *height_map_copy.lock().unwrap();
for i in 0..heights.len() {
map_arr[i] = heights[i];
}
}));
let mut finder = Pathfinder::new(Vector2::new(32.0, 16.0), 0.0625);
let gradient_map: &[f32] = &[0f32; CELL_COUNT]; // Replace this with gradient map callback

let lunabot_stage = Arc::new(AtomicCell::new(LunabotStage::SoftStop));

let (packet_builder, mut from_lunabase_rx, mut connected) = create_packet_builder(
Expand Down Expand Up @@ -344,8 +360,14 @@ impl Application for LunasimbotApp {
lunasim_stdin.write(bytes);
}
Action::CalculatePath { from, to, mut into } => {
into.push(from);
into.push(to);
finder.append_path(
from,
to,
height_map.lock().unwrap().deref(),
gradient_map,
1.0,
&mut into,
);
inputs.push(Input::PathCalculated(into));
}
Action::WaitUntil(deadline) => {
Expand Down
31 changes: 9 additions & 22 deletions lunabotics/pathfinding/src/astar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,16 @@ pub(crate) fn astar(
mut start: Vector2<f64>,
mut goal: Vector2<f64>,
map_dimension: Vector2<f64>,
offset: Vector2<f64>,
step_size: f64,
mut is_safe: impl FnMut(Vector2<f64>, Vector2<f64>) -> bool,
) -> Vec<Vector2<f64>> {
let startf = start;
let goalf = goal;
let max_index = Vector2::new(
(map_dimension.x as f64 / step_size).round() as u32,
(map_dimension.y as f64 / step_size).round() as u32,
(map_dimension.x / step_size).round() as u32,
(map_dimension.y / step_size).round() as u32,
);

start -= offset;
if start.x < 0.0 {
start.x = 0.0;
}
if start.y < 0.0 {
start.y = 0.0;
}
let mut start = Vector2::new(
(start.x / step_size).round() as u32,
(start.y / step_size).round() as u32,
Expand All @@ -61,13 +53,6 @@ pub(crate) fn astar(
start.y = max_index.y;
}

goal -= offset;
if goal.x < 0.0 {
goal.x = 0.0;
}
if goal.y < 0.0 {
goal.y = 0.0;
}
let mut goal = Vector2::new(
(goal.x / step_size).round() as u32,
(goal.y / step_size).round() as u32,
Expand Down Expand Up @@ -110,10 +95,7 @@ pub(crate) fn astar(
let node_parent = parents.get(&node).unwrap();
let mut successors = heapless::Vec::<_, 8>::new();
let mut try_add = |next: Vector2<u32>, successor_parent: Parent, cost: usize| {
if is_safe(
step_size * node.cast() + offset,
step_size * next.cast() + offset,
) {
if is_safe(step_size * node.cast(), step_size * next.cast()) {
successors.push((next, successor_parent, cost)).unwrap();
}
};
Expand Down Expand Up @@ -177,6 +159,11 @@ pub(crate) fn astar(
}
}

// End here if pathfinding was not successful
if !parents.contains_key(&goal) {
return vec![startf];
}

let mut path = vec![goalf; best_cost_so_far.length];

for i in (1..best_cost_so_far.length - 1).rev() {
Expand All @@ -196,7 +183,7 @@ pub(crate) fn astar(
};
}
traverse!();
path[i] = step_size * best_so_far.cast() + offset;
path[i] = step_size * best_so_far.cast();

#[cfg(debug_assertions)]
{
Expand Down
Loading
Loading