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

create recovery timer mechanism #101

Merged
merged 12 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP: Timer Update
  • Loading branch information
NoahSprenger committed May 4, 2024
commit 777e0eccf798aecd284bfdb0b5b90798d988aeca
11 changes: 6 additions & 5 deletions boards/recovery/src/data_manager.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::app::recovery_counter_tick;

Check failure on line 1 in boards/recovery/src/data_manager.rs

View workflow job for this annotation

GitHub Actions / All

unresolved import `crate::app`

Check failure on line 1 in boards/recovery/src/data_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `crate::app`

error[E0432]: unresolved import `crate::app` --> boards/recovery/src/data_manager.rs:1:12 | 1 | use crate::app::recovery_counter_tick; | ^^^ could not find `app` in the crate root
use crate::app::{fire_drogue, fire_main};

Check failure on line 2 in boards/recovery/src/data_manager.rs

View workflow job for this annotation

GitHub Actions / All

unresolved import `crate::app`

Check failure on line 2 in boards/recovery/src/data_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `crate::app`

error[E0432]: unresolved import `crate::app` --> boards/recovery/src/data_manager.rs:2:12 | 2 | use crate::app::{fire_drogue, fire_main}; | ^^^ could not find `app` in the crate root
use crate::state_machine::RocketStates;
use common_arm::{spawn, HydraError};
use defmt::info;
use heapless::HistoryBuffer;
use messages::sensor::{Air, EkfNav1, EkfNav2, EkfQuat, GpsVel, Imu1, Imu2, UtcTime};
use messages::Message;
use crate::app::recovery_counter_tick;

const MAIN_HEIGHT: f32 = 876.0; // meters ASL
const HEIGHT_MIN: f32 = 600.0; // meters ASL
const RECOVERY_DATA_POINTS: u8 = 8; // number of barometric altitude readings held by the recovery
// algorithm

pub struct DataManager {
pub air: Option<Air>,
Expand All @@ -35,12 +37,12 @@
gps_vel: None,
historical_barometer_altitude,
current_state: None,
recovery_counter: 0
recovery_counter: 0,
}
}
/// Returns true if the rocket is descending
pub fn is_falling(&self) -> bool {
if self.historical_barometer_altitude.len() < 8 {
if (self.historical_barometer_altitude.len() as u8) < RECOVERY_DATA_POINTS {
return false;
}
let mut buf = self.historical_barometer_altitude.oldest_ordered();
Expand All @@ -60,7 +62,7 @@
avg_sum += slope;
prev = i;
}
match avg_sum / 7.0 {
match avg_sum / (RECOVERY_DATA_POINTS as f32 - 1.0) {
// 7 because we have 8 points.
// exclusive range
x if !(-100.0..=-5.0).contains(&x) => {
Expand Down Expand Up @@ -106,7 +108,6 @@
if self.recovery_counter >= 15 {
return true;
}
spawn!(recovery_counter_tick);
}
_ => {
self.recovery_counter = 0;
Expand Down
12 changes: 7 additions & 5 deletions boards/recovery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
mod types;

use atsamd_hal as hal;
use atsamd_hal::clock::v2::pclk::Pclk;

Check warning on line 11 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `atsamd_hal::clock::v2::pclk::Pclk`

warning: unused import: `atsamd_hal::clock::v2::pclk::Pclk` --> boards/recovery/src/main.rs:11:5 | 11 | use atsamd_hal::clock::v2::pclk::Pclk; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use atsamd_hal::clock::v2::Source;

Check warning on line 12 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `atsamd_hal::clock::v2::Source`

warning: unused import: `atsamd_hal::clock::v2::Source` --> boards/recovery/src/main.rs:12:5 | 12 | use atsamd_hal::clock::v2::Source; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use atsamd_hal::dmac::DmaController;

Check warning on line 13 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `atsamd_hal::dmac::DmaController`

warning: unused import: `atsamd_hal::dmac::DmaController` --> boards/recovery/src/main.rs:13:5 | 13 | use atsamd_hal::dmac::DmaController; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use common_arm::hinfo;

Check warning on line 14 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `common_arm::hinfo`

warning: unused import: `common_arm::hinfo` --> boards/recovery/src/main.rs:14:5 | 14 | use common_arm::hinfo; | ^^^^^^^^^^^^^^^^^
use common_arm::mcan;
use common_arm::*;

Check warning on line 16 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `common_arm::*`

warning: unused import: `common_arm::*` --> boards/recovery/src/main.rs:16:5 | 16 | use common_arm::*; | ^^^^^^^^^^^^^
use communication::Capacities;

Check warning on line 17 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `communication::Capacities`

warning: unused import: `communication::Capacities` --> boards/recovery/src/main.rs:17:5 | 17 | use communication::Capacities; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use data_manager::DataManager;

Check warning on line 18 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `data_manager::DataManager`

warning: unused import: `data_manager::DataManager` --> boards/recovery/src/main.rs:18:5 | 18 | use data_manager::DataManager; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use gpio_manager::GPIOManager;

Check warning on line 19 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `gpio_manager::GPIOManager`

warning: unused import: `gpio_manager::GPIOManager` --> boards/recovery/src/main.rs:19:5 | 19 | use gpio_manager::GPIOManager; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use hal::gpio::{Pin, Pins, PushPullOutput, PB16, PB17};

Check warning on line 20 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `PB16`, `PB17`, `Pin`, `Pins`, `PushPullOutput`

warning: unused imports: `PB16`, `PB17`, `Pin`, `Pins`, `PushPullOutput` --> boards/recovery/src/main.rs:20:17 | 20 | use hal::gpio::{Pin, Pins, PushPullOutput, PB16, PB17}; | ^^^ ^^^^ ^^^^^^^^^^^^^^ ^^^^ ^^^^
use hal::prelude::*;

Check warning on line 21 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `hal::prelude::*`

warning: unused import: `hal::prelude::*` --> boards/recovery/src/main.rs:21:5 | 21 | use hal::prelude::*; | ^^^^^^^^^^^^^^^
use hal::timer::TimerCounter2;

Check warning on line 22 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `hal::timer::TimerCounter2`

warning: unused import: `hal::timer::TimerCounter2` --> boards/recovery/src/main.rs:22:5 | 22 | use hal::timer::TimerCounter2; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use mcan::messageram::SharedMemory;

Check warning on line 23 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `mcan::messageram::SharedMemory`

warning: unused import: `mcan::messageram::SharedMemory` --> boards/recovery/src/main.rs:23:5 | 23 | use mcan::messageram::SharedMemory; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use messages::*;

Check warning on line 24 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `messages::*`

warning: unused import: `messages::*` --> boards/recovery/src/main.rs:24:5 | 24 | use messages::*; | ^^^^^^^^^^^
use state_machine::{StateMachine, StateMachineContext};

Check warning on line 25 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `StateMachineContext`, `StateMachine`

warning: unused imports: `StateMachineContext`, `StateMachine` --> boards/recovery/src/main.rs:25:21 | 25 | use state_machine::{StateMachine, StateMachineContext}; | ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
use systick_monotonic::*;

Check warning on line 26 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `systick_monotonic::*`

warning: unused import: `systick_monotonic::*` --> boards/recovery/src/main.rs:26:5 | 26 | use systick_monotonic::*; | ^^^^^^^^^^^^^^^^^^^^
use types::COM_ID;

Check warning on line 27 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `types::COM_ID`

warning: unused import: `types::COM_ID` --> boards/recovery/src/main.rs:27:5 | 27 | use types::COM_ID; | ^^^^^^^^^^^^^
use hal::timer::TimerCounter2;

/// Custom panic handler.
/// Reset the system if a panic occurs.
Expand All @@ -43,14 +43,14 @@
data_manager: DataManager,
can0: communication::CanDevice0,
gpio: GPIOManager,
recovery_timer: TimerCounter2,
}

#[local]
struct Local {
led_green: Pin<PB16, PushPullOutput>,
led_red: Pin<PB17, PushPullOutput>,
state_machine: StateMachine,
recovery_timer: TimerCounter2,
}

#[monotonic(binds = SysTick, default = true)]
Expand Down Expand Up @@ -109,7 +109,8 @@
/* Recovery Timer config */
let (pclk_tc2tc3, gclk0) = Pclk::enable(tokens.pclks.tc2_tc3, gclk0);
let timerclk: hal::clock::v1::Tc2Tc3Clock = pclk_tc2tc3.into();
let mut recovery_timer = hal::timer::TimerCounter2::tc2_(&timerclk, peripherals.TC2, &mut mclk);
let mut recovery_timer =
hal::timer::TimerCounter2::tc2_(&timerclk, peripherals.TC2, &mut mclk);
recovery_timer.enable_interrupt();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to enable interrupts here, just create the timer object.


NoahSprenger marked this conversation as resolved.
Show resolved Hide resolved
/* Spawn tasks */
Expand All @@ -128,12 +129,12 @@
data_manager: DataManager::new(),
can0,
gpio,
recovery_timer,
},
Local {
led_green,
led_red,
state_machine,
recovery_timer,
},
init::Monotonics(mono),
)
Expand All @@ -147,14 +148,15 @@

// handle recovery counter
// start a counter
#[task(local = [recovery_timer])]

Check failure on line 151 in boards/recovery/src/main.rs

View workflow job for this annotation

GitHub Actions / All

this local resource has NOT been declared
fn recovery_counter_tick(cx: recovery_counter_tick::Context) {
let timer = cx.local.recovery_timer;
// only start timer if it is not already running
if timer.wait().is_ok() {
let duration_mins = atsamd_hal::fugit::MinutesDurationU32::minutes(1);
// timer requires specific duration format
let timer_duration: atsamd_hal::fugit::Duration<u32, 1, 1000000000> = duration_mins.convert();
let timer_duration: atsamd_hal::fugit::Duration<u32, 1, 1000000000> =
duration_mins.convert();
timer.enable_interrupt(); // clear interrupt?
timer.start(timer_duration);
}
Expand Down
4 changes: 4 additions & 0 deletions boards/recovery/src/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
use defmt::Format;
use enum_dispatch::enum_dispatch;
use messages::state;
use rtic::Mutex;

Check warning on line 13 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `rtic::Mutex`

warning: unused import: `rtic::Mutex` --> boards/recovery/src/state_machine/mod.rs:13:5 | 13 | use rtic::Mutex; | ^^^^^^^^^^^
pub use states::Initializing;

pub trait StateMachineSharedResources {
fn lock_can(&mut self, f: &dyn Fn(&mut CanDevice0));
fn lock_data_manager(&mut self, f: &dyn Fn(&mut DataManager));
fn lock_gpio(&mut self, f: &dyn Fn(&mut GPIOManager));
fn lock_timer(&mut self, f: &dyn Fn(&mut TimerCounter2));

Check failure on line 20 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / All

cannot find type `TimerCounter2` in this scope

Check failure on line 20 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find type `TimerCounter2` in this scope

error[E0412]: cannot find type `TimerCounter2` in this scope --> boards/recovery/src/state_machine/mod.rs:20:46 | 20 | fn lock_timer(&mut self, f: &dyn Fn(&mut TimerCounter2)); | ^^^^^^^^^^^^^ not found in this scope | help: consider importing one of these items | 4 + use atsamd_hal::timer::TimerCounter2; | 4 + use crate::TimerCounter2; |
}

impl<'a> StateMachineSharedResources for crate::app::__rtic_internal_run_smSharedResources<'a> {

Check failure on line 23 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / All

failed to resolve: could not find `app` in the crate root

Check failure on line 23 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

failed to resolve: could not find `app` in the crate root

error[E0433]: failed to resolve: could not find `app` in the crate root --> boards/recovery/src/state_machine/mod.rs:23:49 | 23 | impl<'a> StateMachineSharedResources for crate::app::__rtic_internal_run_smSharedResources<'a> { | ^^^ could not find `app` in the crate root
fn lock_can(&mut self, fun: &dyn Fn(&mut CanDevice0)) {
self.can0.lock(fun)
}
Expand All @@ -29,10 +30,13 @@
fn lock_gpio(&mut self, fun: &dyn Fn(&mut GPIOManager)) {
self.gpio.lock(fun)
}
fn lock_timer(&mut self, fun: &dyn Fn(&mut TimerCounter2)) {

Check failure on line 33 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find type `TimerCounter2` in this scope

error[E0412]: cannot find type `TimerCounter2` in this scope --> boards/recovery/src/state_machine/mod.rs:33:48 | 33 | fn lock_timer(&mut self, fun: &dyn Fn(&mut TimerCounter2)) { | ^^^^^^^^^^^^^ not found in this scope | help: consider importing one of these items | 4 + use atsamd_hal::timer::TimerCounter2; | 4 + use crate::TimerCounter2; |
self.recovery_timer.lock(fun)
}
}

pub struct StateMachineContext<'a, 'b> {
pub shared_resources: &'b mut crate::app::__rtic_internal_run_smSharedResources<'a>,

Check failure on line 39 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / All

failed to resolve: could not find `app` in the crate root

Check failure on line 39 in boards/recovery/src/state_machine/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

failed to resolve: could not find `app` in the crate root

error[E0433]: failed to resolve: could not find `app` in the crate root --> boards/recovery/src/state_machine/mod.rs:39:42 | 39 | pub shared_resources: &'b mut crate::app::__rtic_internal_run_smSharedResources<'a>, | ^^^ could not find `app` in the crate root
}
pub struct StateMachine {
state: RocketStates,
Expand Down
3 changes: 3 additions & 0 deletions boards/recovery/src/state_machine/states/terminal_descent.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::Descent;
use crate::app::fire_main;

Check failure on line 2 in boards/recovery/src/state_machine/states/terminal_descent.rs

View workflow job for this annotation

GitHub Actions / All

unresolved import `crate::app`

Check failure on line 2 in boards/recovery/src/state_machine/states/terminal_descent.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `crate::app`

error[E0432]: unresolved import `crate::app` --> boards/recovery/src/state_machine/states/terminal_descent.rs:2:12 | 2 | use crate::app::fire_main; | ^^^ could not find `app` in the crate root
use crate::state_machine::{
RocketStates, State, StateMachineContext, TransitionInto, WaitForRecovery,
};
use crate::{no_transition, transition};
use common_arm::spawn;
use defmt::{write, Format, Formatter};
use rtic::mutex::Mutex;

Check warning on line 9 in boards/recovery/src/state_machine/states/terminal_descent.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `rtic::mutex::Mutex`

warning: unused import: `rtic::mutex::Mutex` --> boards/recovery/src/state_machine/states/terminal_descent.rs:9:5 | 9 | use rtic::mutex::Mutex; | ^^^^^^^^^^^^^^^^^^

#[derive(Debug, Clone)]
pub struct TerminalDescent {}
Expand All @@ -17,6 +17,9 @@
spawn!(fire_main)?;
Ok(())
});
context.shared_resources.timer.lock(|timer| {
timer.start();
});
}
fn step(&mut self, context: &mut StateMachineContext) -> Option<RocketStates> {
context.shared_resources.data_manager.lock(|data| {
Expand Down
3 changes: 3 additions & 0 deletions boards/recovery/src/state_machine/states/wait_for_recovery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::TerminalDescent;
use crate::app::monotonics;

Check failure on line 2 in boards/recovery/src/state_machine/states/wait_for_recovery.rs

View workflow job for this annotation

GitHub Actions / All

unresolved import `crate::app`

Check failure on line 2 in boards/recovery/src/state_machine/states/wait_for_recovery.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `crate::app`

error[E0432]: unresolved import `crate::app` --> boards/recovery/src/state_machine/states/wait_for_recovery.rs:2:12 | 2 | use crate::app::monotonics; | ^^^ could not find `app` in the crate root
use crate::no_transition;
use crate::state_machine::{RocketStates, State, StateMachineContext, TransitionInto};
use crate::types::COM_ID;
Expand All @@ -7,7 +7,7 @@
use messages::command::{Command, PowerDown, RadioRate, RadioRateChange};
use messages::sender::Sender::SensorBoard;
use messages::Message;
use rtic::mutex::Mutex;

Check warning on line 10 in boards/recovery/src/state_machine/states/wait_for_recovery.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `rtic::mutex::Mutex`

warning: unused import: `rtic::mutex::Mutex` --> boards/recovery/src/state_machine/states/wait_for_recovery.rs:10:5 | 10 | use rtic::mutex::Mutex; | ^^^^^^^^^^^^^^^^^^

#[derive(Debug, Clone)]
pub struct WaitForRecovery {}
Expand All @@ -32,6 +32,9 @@
})
});
}
context.shared_resources.timer.lock(|timer| {
timer.disable_interrupts();
})
}
fn step(&mut self, _context: &mut StateMachineContext) -> Option<RocketStates> {
no_transition!() // this is our final resting place. We should also powerdown this board.
Expand Down
Loading