Skip to content

radar: Support scanning bot orientations #62

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

Merged
merged 3 commits into from
Apr 12, 2025
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
1 change: 1 addition & 0 deletions app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 5 additions & 16 deletions app/crates/kartoffel/src/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cmd, rdi, wri, MEM_ARM};
use crate::*;

/// Returns whether the arm is ready and [`arm_stab()`] can be invoked.
///
Expand All @@ -15,12 +15,11 @@ use crate::{cmd, rdi, wri, MEM_ARM};
/// // run, forrest, run!
/// }
/// ```
#[inline(always)]
pub fn is_arm_ready() -> bool {
rdi(MEM_ARM, 0) == 1
}

/// Waits for the arm to become ready.
/// Waits until the arm is ready.
///
/// See also: [`is_arm_ready()`].
///
Expand All @@ -32,7 +31,6 @@ pub fn is_arm_ready() -> bool {
/// arm_wait();
/// arm_stab();
/// ```
#[inline(always)]
pub fn arm_wait() {
while !is_arm_ready() {
//
Expand All @@ -46,9 +44,7 @@ pub fn arm_wait() {
///
/// # Cooldown
///
/// ```text
/// 60_000 +- 15% ticks (~930 ms)
/// ```
/// ~60k ticks (~930 ms)
///
/// # Example
///
Expand All @@ -58,7 +54,6 @@ pub fn arm_wait() {
/// arm_wait();
/// arm_stab();
/// ```
#[inline(always)]
pub fn arm_stab() {
wri(MEM_ARM, 0, cmd(0x01, 0x00, 0x00, 0x00));
}
Expand All @@ -75,9 +70,7 @@ pub fn arm_stab() {
///
/// # Cooldown
///
/// ```text
/// 60_000 +- 15% ticks (~930 ms)
/// ```
/// ~60k ticks (~930 ms)
///
/// # Example
///
Expand All @@ -87,7 +80,6 @@ pub fn arm_stab() {
/// arm_wait();
/// arm_pick();
/// ```
#[inline(always)]
pub fn arm_pick() {
wri(MEM_ARM, 0, cmd(0x02, 0x00, 0x00, 0x00));
}
Expand All @@ -104,9 +96,7 @@ pub fn arm_pick() {
///
/// # Cooldown
///
/// ```text
/// 60_000 +- 15% ticks (~930 ms)
/// ```
/// ~60k ticks (~930 ms)
///
/// # Example
///
Expand All @@ -119,7 +109,6 @@ pub fn arm_pick() {
/// arm_wait();
/// arm_drop(0); // drops whatever got picked before
/// ```
#[inline(always)]
pub fn arm_drop(idx: u8) {
wri(MEM_ARM, 0, cmd(0x03, idx, 0x00, 0x00));
}
3 changes: 1 addition & 2 deletions app/crates/kartoffel/src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{rdi, MEM_BATTERY};
use crate::*;

/// Returns the remaining battery energy.
///
/// Since battery is not simulated at the moment, this function doesn't come
/// useful.
#[doc(hidden)]
#[inline(always)]
pub fn battery_energy() -> u32 {
rdi(MEM_BATTERY, 0)
}
2 changes: 1 addition & 1 deletion app/crates/kartoffel/src/compass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{rdi, MEM_COMPASS};
use crate::*;

/// Returns which direction the bot was facing at the time of the latest
/// measurement, and then removes that measurement from the compass.
Expand Down
9 changes: 3 additions & 6 deletions app/crates/kartoffel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! # kartoffel
//!
//! This crate provides building blocks for implementing a bot for the
//! [kartoffels](https://kartoffels.pwy.io) game - see:
//! This crate provides building blocks for implementing a kartoffel bot:
//!
//! <https://github.com/patryk27/kartoffel>
//! - <https://github.com/patryk27/kartoffel>
//! - <https://github.com/patryk27/kartoffels>

#![no_std]

Expand Down Expand Up @@ -37,19 +37,16 @@ const MEM_ARM: *mut u32 = MEM.wrapping_byte_add(4 * 1024);
const MEM_RADAR: *mut u32 = MEM.wrapping_byte_add(5 * 1024);
const MEM_COMPASS: *mut u32 = MEM.wrapping_byte_add(6 * 1024);

#[inline(always)]
fn rdi(ptr: *mut u32, off: usize) -> u32 {
unsafe { ptr::read_volatile(ptr.wrapping_add(off)) }
}

#[inline(always)]
fn wri(ptr: *mut u32, off: usize, val: u32) {
unsafe {
ptr::write_volatile(ptr.wrapping_add(off), val);
}
}

#[inline(always)]
fn cmd(cmd: u8, arg0: u8, arg1: u8, arg2: u8) -> u32 {
u32::from_le_bytes([cmd, arg0, arg1, arg2])
}
Expand Down
36 changes: 11 additions & 25 deletions app/crates/kartoffel/src/motor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::{cmd, rdi, wri, MEM_MOTOR};
use crate::*;

/// Returns whether the motor is ready and [`motor_pulse()`] can be invoked.
/// Returns whether the motor is ready and [`motor_pulse()`] etc. can be
/// invoked.
///
/// See also: [`motor_wait()`].
#[inline(always)]
pub fn is_motor_ready() -> bool {
rdi(MEM_MOTOR, 0) == 1
}

/// Waits for the motor to become ready.
/// Waits until the motor is ready.
///
/// See also: [`is_motor_ready()`].
///
Expand All @@ -24,7 +24,6 @@ pub fn is_motor_ready() -> bool {
/// motor_step_fw();
/// }
/// ```
#[inline(always)]
pub fn motor_wait() {
while !is_motor_ready() {
//
Expand All @@ -40,10 +39,10 @@ pub fn motor_wait() {
/// - `(-1, 1)` - bot turns left (counterclockwise),
/// - `(1, -1)` - bot turns right (clockwise).
///
/// Other values will cause the CPU to crash.
/// Other values will cause the firmware to crash.
///
/// Note that this is a low-level function - for convenience you'll most likely
/// want to use one of:
/// This is a low-level function - for convenience you'll most likely/ want to
/// use one of:
///
/// - [`motor_step_fw()`],
/// - [`motor_step_bw()`],
Expand All @@ -58,7 +57,6 @@ pub fn motor_wait() {
/// - [`motor_step_bw()`],
/// - [`motor_turn_left()`],
/// - [`motor_turn_right()`],
#[inline(always)]
pub fn motor_pulse(left: i8, right: i8) {
wri(MEM_MOTOR, 0, cmd(0x01, left as u8, right as u8, 0x00));
}
Expand All @@ -69,9 +67,7 @@ pub fn motor_pulse(left: i8, right: i8) {
///
/// # Cooldown
///
/// ```text
/// 20_000 +- 15% ticks (~310 ms)
/// ```
/// ~20k ticks (~310 ms)
///
/// # Example
///
Expand All @@ -81,7 +77,6 @@ pub fn motor_pulse(left: i8, right: i8) {
/// motor_wait();
/// motor_step_fw();
/// ```
#[inline(always)]
pub fn motor_step_fw() {
motor_pulse(1, 1);
}
Expand All @@ -92,9 +87,7 @@ pub fn motor_step_fw() {
///
/// # Cooldown
///
/// ```text
/// 30_000 +- 15% ticks (~468 ms)
/// ```
/// ~30k ticks (~468 ms)
///
/// # Example
///
Expand All @@ -104,7 +97,6 @@ pub fn motor_step_fw() {
/// motor_wait();
/// motor_step_bw();
/// ```
#[inline(always)]
pub fn motor_step_bw() {
motor_pulse(-1, -1);
}
Expand All @@ -115,9 +107,7 @@ pub fn motor_step_bw() {
///
/// # Cooldown
///
/// ```text
/// 25_000 +- 15% ticks (~390 ms)
/// ```
/// ~25k ticks (~390 ms)
///
/// # Example
///
Expand All @@ -127,7 +117,6 @@ pub fn motor_step_bw() {
/// motor_wait();
/// motor_turn_left();
/// ```
#[inline(always)]
pub fn motor_turn_left() {
motor_pulse(-1, 1);
}
Expand All @@ -138,9 +127,7 @@ pub fn motor_turn_left() {
///
/// # Cooldown
///
/// ```text
/// 25_000 +- 15% ticks (~390 ms)
/// ```
/// ~25k ticks (~390 ms)
///
/// # Example
///
Expand All @@ -150,7 +137,6 @@ pub fn motor_turn_left() {
/// motor_wait();
/// motor_turn_right();
/// ```
#[inline(always)]
pub fn motor_turn_right() {
motor_pulse(1, -1);
}
2 changes: 1 addition & 1 deletion app/crates/kartoffel/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Serial;
use crate::*;
use core::fmt::Write;
use core::panic::PanicInfo;

Expand Down
Loading