Skip to content

Commit

Permalink
use explicit heapless sizes (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur authored Apr 5, 2023
1 parent 55c3263 commit a4832dc
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 34 deletions.
21 changes: 20 additions & 1 deletion blinky/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use stm32f7xx_hal::{pac, prelude::*};

use blinky::init_heap;

const TICK_TIMER_HZ: u32 = 1_000_000;
const RUN_COMMANDS_COUNT: usize = 6;
const START_COMMANDS_COUNT: usize = 0;
const STOP_COMMANDS_COUNT: usize = 0;
const LEDS_COUNT: usize = 4;
const AXES_COUNT: usize = 2;
const SPINDLES_COUNT: usize = 2;
const ACTIVE_COMMANDS_COUNT: usize = 1;

pub fn get_run_commands<const TIMER_HZ: u32>() -> [Command<'static, TIMER_HZ>; 6] {
[
Command::Led(
Expand Down Expand Up @@ -83,7 +92,17 @@ fn main() -> ! {
let user_button_timer = super_timer.sub();
let mut user_button = SwitchDevice::new_active_high(user_button_pin, user_button_timer);

let mut robot_builder = RobotBuilder::new();
let mut robot_builder: RobotBuilder<
'_,
TICK_TIMER_HZ,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
LEDS_COUNT,
AXES_COUNT,
SPINDLES_COUNT,
ACTIVE_COMMANDS_COUNT,
> = RobotBuilder::new();

let green_led_pin = gpiob.pb0.into_push_pull_output();
let green_led_timer = super_timer.sub();
Expand Down
20 changes: 19 additions & 1 deletion gridbot-tahi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ use gridbot_tahi::{
init_heap,
};

const RUN_COMMANDS_COUNT: usize = 32;
const START_COMMANDS_COUNT: usize = 4;
const STOP_COMMANDS_COUNT: usize = 4;
const LEDS_COUNT: usize = 4;
const AXES_COUNT: usize = 4;
const SPINDLES_COUNT: usize = 4;
const ACTIVE_COMMANDS_COUNT: usize = 8;

const TICK_TIMER_MAX: u32 = u32::MAX;
const TICK_TIMER_HZ: u32 = 1_000_000;
type TickTimerDevice = Counter<pac::TIM5, TICK_TIMER_HZ>;
Expand Down Expand Up @@ -103,7 +111,17 @@ fn main() -> ! {
let mut user_button: UserButton =
SwitchDevice::new_active_high(user_button_pin, user_button_timer);

let mut robot_builder: RobotBuilder<TICK_TIMER_HZ> = RobotBuilder::new();
let mut robot_builder: RobotBuilder<
'_,
TICK_TIMER_HZ,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
LEDS_COUNT,
AXES_COUNT,
SPINDLES_COUNT,
ACTIVE_COMMANDS_COUNT,
> = RobotBuilder::new();

let green_led_pin: GreenLedPin = gpiob.pb0.into_push_pull_output();
let green_led_timer: GreenLedTimer = super_timer.sub();
Expand Down
116 changes: 103 additions & 13 deletions robokit/src/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,43 @@ use crate::error::Error;
use crate::runner::{Command, Runner};
use crate::scheduler::Scheduler;

pub struct RobotBuilder<'a, const LED_TIMER_HZ: u32> {
run_commands: Vec<Command<'a, LED_TIMER_HZ>, 32>,
start_commands: Vec<Command<'a, LED_TIMER_HZ>, 4>,
stop_commands: Vec<Command<'a, LED_TIMER_HZ>, 4>,
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, 16>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, 16>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, 16>,
pub struct RobotBuilder<
'a,
const LED_TIMER_HZ: u32,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMANDS_COUNT: usize,
> {
run_commands: Vec<Command<'a, LED_TIMER_HZ>, RUN_COMMANDS_COUNT>,
start_commands: Vec<Command<'a, LED_TIMER_HZ>, START_COMMANDS_COUNT>,
stop_commands: Vec<Command<'a, LED_TIMER_HZ>, STOP_COMMANDS_COUNT>,
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, LEDS_COUNT>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, AXES_COUNT>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, SPINDLES_COUNT>,
}

pub struct Robot<'a, const LED_TIMER_HZ: u32> {
scheduler: Scheduler<Command<'a, LED_TIMER_HZ>, Runner<'a, LED_TIMER_HZ>>,
pub struct Robot<
'a,
const LED_TIMER_HZ: u32,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMANDS_COUNT: usize,
> {
scheduler: Scheduler<
Command<'a, LED_TIMER_HZ>,
Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMANDS_COUNT>,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
>,
}

#[derive(Copy, Clone, Debug)]
Expand All @@ -33,7 +59,29 @@ pub enum RobotBuilderError {
TooManyStopCommands,
}

impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> {
impl<
'a,
const LED_TIMER_HZ: u32,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMANDS_COUNT: usize,
>
RobotBuilder<
'a,
LED_TIMER_HZ,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
LEDS_COUNT,
AXES_COUNT,
SPINDLES_COUNT,
ACTIVE_COMMANDS_COUNT,
>
{
pub fn new() -> Self {
Self {
run_commands: Vec::new(),
Expand Down Expand Up @@ -111,7 +159,19 @@ impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> {
Ok(())
}

pub fn build(self) -> Robot<'a, LED_TIMER_HZ> {
pub fn build(
self,
) -> Robot<
'a,
LED_TIMER_HZ,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
LEDS_COUNT,
AXES_COUNT,
SPINDLES_COUNT,
ACTIVE_COMMANDS_COUNT,
> {
let runner = Runner::new(self.leds, self.axes, self.spindles);
let scheduler = Scheduler::new(
runner,
Expand All @@ -123,8 +183,38 @@ impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> {
}
}

impl<'a, const LED_TIMER_HZ: u32> Robot<'a, LED_TIMER_HZ> {
pub fn new(scheduler: Scheduler<Command<'a, LED_TIMER_HZ>, Runner<'a, LED_TIMER_HZ>>) -> Self {
impl<
'a,
const LED_TIMER_HZ: u32,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMANDS_COUNT: usize,
>
Robot<
'a,
LED_TIMER_HZ,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
LEDS_COUNT,
AXES_COUNT,
SPINDLES_COUNT,
ACTIVE_COMMANDS_COUNT,
>
{
pub fn new(
scheduler: Scheduler<
Command<'a, LED_TIMER_HZ>,
Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMANDS_COUNT>,
RUN_COMMANDS_COUNT,
START_COMMANDS_COUNT,
STOP_COMMANDS_COUNT,
>,
) -> Self {
Self { scheduler }
}

Expand Down
44 changes: 34 additions & 10 deletions robokit/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,33 @@ pub enum RunnerAction<Command> {
Reset,
}

pub struct Runner<'a, const LED_TIMER_HZ: u32> {
active_commands: Deque<Command<'a, LED_TIMER_HZ>, 8>,
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, 16>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, 16>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, 16>,
pub struct Runner<
'a,
const LED_TIMER_HZ: u32,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMMANDS_COUNT: usize,
> {
active_commands: Deque<Command<'a, LED_TIMER_HZ>, ACTIVE_COMMMANDS_COUNT>,
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, LEDS_COUNT>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, AXES_COUNT>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, SPINDLES_COUNT>,
}

impl<'a, const LED_TIMER_HZ: u32> Runner<'a, LED_TIMER_HZ> {
impl<
'a,
const LED_TIMER_HZ: u32,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMMANDS_COUNT: usize,
> Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMMANDS_COUNT>
{
pub fn new(
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, 16>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, 16>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, 16>,
leds: FnvIndexMap<&'a str, BoxActuator<LedAction<LED_TIMER_HZ>>, LEDS_COUNT>,
axes: FnvIndexMap<&'a str, BoxActuator<AxisAction>, AXES_COUNT>,
spindles: FnvIndexMap<&'a str, BoxActuator<SpindleAction>, SPINDLES_COUNT>,
) -> Self {
Self {
active_commands: Deque::new(),
Expand All @@ -50,7 +65,16 @@ pub enum RunnerError<'a> {
}
*/

impl<'a, const LED_TIMER_HZ: u32> Actuator for Runner<'a, LED_TIMER_HZ> {
impl<
'a,
const LED_TIMER_HZ: u32,
const LEDS_COUNT: usize,
const AXES_COUNT: usize,
const SPINDLES_COUNT: usize,
const ACTIVE_COMMMANDS_COUNT: usize,
> Actuator
for Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMMANDS_COUNT>
{
type Action = RunnerAction<Command<'a, LED_TIMER_HZ>>;
type Error = BoxError;

Expand Down
29 changes: 20 additions & 9 deletions robokit/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,39 @@ pub enum SchedulerState {
StopLoop,
}

pub struct Scheduler<Command, Runner>
where
pub struct Scheduler<
Command,
Runner,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
> where
Runner: Actuator<Action = RunnerAction<Command>>,
{
runner: Runner,
state: SchedulerState,
run_commands: Vec<Command, 32>,
start_commands: Vec<Command, 4>,
stop_commands: Vec<Command, 4>,
run_commands: Vec<Command, RUN_COMMANDS_COUNT>,
start_commands: Vec<Command, START_COMMANDS_COUNT>,
stop_commands: Vec<Command, STOP_COMMANDS_COUNT>,
}

impl<Command, Runner> Scheduler<Command, Runner>
impl<
Command,
Runner,
const RUN_COMMANDS_COUNT: usize,
const START_COMMANDS_COUNT: usize,
const STOP_COMMANDS_COUNT: usize,
> Scheduler<Command, Runner, RUN_COMMANDS_COUNT, START_COMMANDS_COUNT, STOP_COMMANDS_COUNT>
where
Command: Copy + Clone + Debug + Format,
Runner: Actuator<Action = RunnerAction<Command>>,
Runner::Error: 'static,
{
pub fn new(
runner: Runner,
run_commands: Vec<Command, 32>,
start_commands: Vec<Command, 4>,
stop_commands: Vec<Command, 4>,
run_commands: Vec<Command, RUN_COMMANDS_COUNT>,
start_commands: Vec<Command, START_COMMANDS_COUNT>,
stop_commands: Vec<Command, STOP_COMMANDS_COUNT>,
) -> Self {
Self {
runner,
Expand Down

0 comments on commit a4832dc

Please sign in to comment.