Skip to content

Commit

Permalink
Merge branch 'master' into nav
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSprenger committed Sep 14, 2024
2 parents edabe7e + 0251afa commit 6bea4be
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 109 deletions.
12 changes: 12 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Base image for toolchain download
FROM mcr.microsoft.com/devcontainers/rust:latest as toolchain

RUN mkdir -p /toolchain && \
curl -L "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz" \
| tar --strip-components=1 -xJ -C /toolchain

# Final image
FROM mcr.microsoft.com/devcontainers/rust:latest

# Copy the toolchain from the previous stage
COPY --from=toolchain /toolchain /toolchain

ENV PATH="${PATH}:/toolchain/bin"
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get upgrade -y && apt-get install -y cmake pkg-config libusb-1.0-0-dev libftdi1-dev libudev-dev libssl-dev

Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ jobs:
with:
command: make
args: test-host
- uses: actions-rs/cargo@v1
name: "Generate Typescript bindings"
with:
command: make
args: generate-ts-bindings
- uses: actions/upload-artifact@v3
name: "Upload Binding Artifacts"
with:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
components: clippy
toolchain: stable
target: thumbv7em-none-eabihf
- name: cargofmt
run: cargo fmt --check
- uses: actions/cache@v3
with:
path: |
Expand All @@ -31,3 +33,4 @@ jobs:
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/.vscode/.cortex-debug.registers.state.json
/.vscode/.cortex-debug.peripherals.state.json
/.vscode/settings.json
/.vscode/settings.json
/toolchain
8 changes: 1 addition & 7 deletions Cargo.lock

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

7 changes: 0 additions & 7 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true
[config]
default_to_workspace = false

# -----------------------
# Typescript Bindings
# -----------------------
[tasks.generate-ts-bindings]
command = "cargo"
args = ["test", "-p", "messages", "--no-default-features", "--features", "ts", "--target", "${CARGO_MAKE_RUST_TARGET_TRIPLE}"]

# -----------------------
# Host Testing
# -----------------------
Expand Down
66 changes: 41 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# HYDRA   [![Build Status]][actions] [![docs-badge]][docs]
*HYper Dynamic Rocketry Avionics*

***HY**per **D**ynamic **R**ocketry **A**vionics*

[Build Status]: https://github.com/uorocketry/hydra/actions/workflows/build.yml/badge.svg
[actions]: https://github.com/uorocketry/hydra/actions?query=branch%3Amaster
Expand All @@ -12,46 +13,60 @@

## Getting Started

1. Install Rust: https://www.rust-lang.org/tools/install
2. Build: `cargo build`
3. Install probe-run: `cargo install --git https://github.com/uorocketry/probe-run`
- `probe-run` currently requires a patch to flash our chip, so please use the above version while the patch is upstreamed
4. Install cargo-make: `cargo install cargo-make`
4. Flash: `cargo run --bin main`
5. Run tests: `cargo make test-host` or `cargo make test-device`
> If you are in a DevContainer skip to step 5.
- Install Rust: https://www.rust-lang.org/tools/install
- Install necessary build tools:
- cargo-make: `cargo install cargo-make`
- probe-rs: `cargo install probe-rs --features cli`
- Install the [ARM GNU Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads) (last tested with 13.2) and have it available in your PATH
- Arch Linux: `sudo pacman -S arm-none-eabi-gcc`
- Alpine/Debian/Ubuntu: <https://pkgs.org/download/gcc-arm-none-eabi>
- MacOS: `brew install arm-none-eabi-gcc`
- Build: `cargo build`
- In case it fails, try `cargo build --release`
- Run tests:
- In the host machine: `cargo make test-host`
- In the device: `cargo make test-device`
- Flash on hardware: `cargo run --bin main`

For more detailed instructions on flashing, debugging, and more, please see [the wiki](https://avwiki.uorocketry.ca/en/Avionics/HYDRA/Software).

## Windows Users

### Setting up with Docker
1. Install Docker: https://docs.docker.com/desktop/install/windows-install/
2. Install VS Code: https://code.visualstudio.com/download
3. From VS Code, install the "Dev Containers" extension
4. press `ctrl` + `shift` + `p`, and search for `Dev Containers: Open Folder in Container`

- Install [Docker](https://docs.docker.com/desktop/install/windows-install/)
- Install [VS Code](https://code.visualstudio.com/download)
- From VS Code, install the "Dev Containers" extension
- press `ctrl` + `shift` + `p`, and search for `Dev Containers: Open Folder in Container`

### Setting up with WSL
1. Enable WSL: https://learn.microsoft.com/en-us/windows/wsl/install
2. Install a linux distro from the Microsoft Store
4. Install ARM GNU Toolchain: https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads
- NOTE: You may find this in your distro's package managerm, but make sure it is up to date.

- Enable WSL: https://learn.microsoft.com/en-us/windows/wsl/install
- Install a linux distro from the Microsoft Store
- Install the [ARM GNU Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads)
- NOTE: You may find this in your distro's package manager, but ensure it is version 13.2.
- Ubuntu has an outdated version in its repositories. If using Ubuntu, download it manually from the link above
3. Follow the rest of the instructions in [Getting Started](#-getting-started)
- Follow the rest of the instructions in [Getting Started](#-getting-started)

### Flashing

After plugging in J-Link, it will likely show up as unknown.

1. Install Zadig: https://zadig.akeo.ie/
2. From Zadig, select J-Link as the device and WinUSB as the driver
3. click Install Driver

If using WSL or Docker with a WSL backend (you probably are), you need to tell Windows to share J-Link with WSL.
1. From WSL, install linux-tools-generic
1. on Ubuntu: `sudo apt install linux-tools-generic`
2. Install usbipd-win: https://github.com/dorssel/usbipd-win/releases
3. Open command prompt/powershell with admin privileges and run `usbipd list`
4. Make note of the entry called J-Link and run `usbipd bind --busid <busid>`
5. Next, run `usbipd attach --wsl --busid <busid>`
6. You can now follow the flashing instructions in [Getting Started](#-getting-started)

- From WSL, install linux-tools-generic
- on Ubuntu: `sudo apt install linux-tools-generic`
- Install usbipd-win: https://github.com/dorssel/usbipd-win/releases
- Open command prompt/powershell with admin privileges and run `usbipd list`
- Make note of the entry called J-Link and run `usbipd bind --busid <busid>`
- Next, run `usbipd attach --wsl --busid <busid>`
- You can now follow the flashing instructions in [Getting Started](#getting-started)

## Documentation

Expand All @@ -60,6 +75,7 @@ Run `cargo doc --open` to build and open the documentation. Most documentation f
Documentation is also automatically built and deployed to https://hydra-docs.uorocketry.ca/common_arm

## Project Structure

The project is structured in a way to allow reuse of the code across various boards and other projects.

- `boards`: Folder containing each individual board's binary crates. Any code in those crates should only contain logic specific to the board.
Expand All @@ -72,4 +88,4 @@ The project is structured in a way to allow reuse of the code across various boa

This project is licensed under GPLv3-only.

We please ask for any derivatives of this work to be kept open-source, even if such derivative is only for internal use.
We please ask for any derivatives of this work to be kept open-source, even if such derivative is only for internal use.
1 change: 1 addition & 0 deletions boards/beacon/src/communication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Interface the SPI-based CAN device here.
3 changes: 1 addition & 2 deletions boards/camera/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl CanDevice0 {
can.filters_standard()
.push(Filter::Classic {
action: Action::StoreFifo0,
filter: ecan::StandardId::new(messages::sender::Sender::SensorBoard.into())
.unwrap(),
filter: ecan::StandardId::new(messages::node::Node::SensorBoard.into()).unwrap(),
mask: ecan::StandardId::ZERO,
})
.unwrap_or_else(|_| panic!("Sensor filter"));
Expand Down
6 changes: 3 additions & 3 deletions boards/camera/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use messages::sender::{Sender, Sender::CameraBoard};
use messages::node::{Node, Node::CameraBoard};
// -------
// Sender ID
// Node ID
// -------
pub static _COM_ID: Sender = CameraBoard;
pub static _COM_ID: Node = CameraBoard;
5 changes: 2 additions & 3 deletions boards/recovery/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ impl CanDevice0 {
can.filters_standard()
.push(Filter::Classic {
action: Action::StoreFifo0,
filter: ecan::StandardId::new(messages::sender::Sender::SensorBoard.into())
.unwrap(),
filter: ecan::StandardId::new(messages::node::Node::SensorBoard.into()).unwrap(),
mask: ecan::StandardId::ZERO,
})
.unwrap_or_else(|_| panic!("Sensor Board filter"));
Expand All @@ -122,7 +121,7 @@ impl CanDevice0 {
can.filters_standard()
.push(Filter::Classic {
action: Action::StoreFifo1,
filter: ecan::StandardId::new(messages::sender::Sender::CommunicationBoard.into())
filter: ecan::StandardId::new(messages::node::Node::CommunicationBoard.into())
.unwrap(),
mask: ecan::StandardId::ZERO,
})
Expand Down
11 changes: 7 additions & 4 deletions boards/recovery/src/data_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct DataManager {
pub gps_vel: Option<GpsVel>,
pub historical_barometer_altitude: HistoryBuffer<(f32, u32), DATA_POINTS>, // (alt, timestamp)
pub current_state: Option<RocketStates>,
// each tick represents a minute that passed
pub recovery_counter: u8,
}

impl DataManager {
Expand All @@ -37,6 +39,7 @@ impl DataManager {
gps_vel: None,
historical_barometer_altitude,
current_state: None,
recovery_counter: 0,
}
}
/// Returns true if the rocket is descending
Expand Down Expand Up @@ -123,8 +126,8 @@ impl DataManager {
None => false,
}
}
pub fn is_landed(&self) -> bool {
if self.historical_barometer_altitude.len() < 8 {
pub fn is_landed(&mut self) -> bool {
if self.historical_barometer_altitude.len() < RECOVERY_DATA_POINTS.into() {
return false;
}
let mut buf = self.historical_barometer_altitude.oldest_ordered();
Expand All @@ -140,13 +143,13 @@ impl DataManager {
avg_sum += (i.0 - prev.0) / time_diff;
prev = i;
}
match avg_sum / 7.0 {
match avg_sum / (RECOVERY_DATA_POINTS as f32 - 1.0) {
// inclusive range
x if (-0.5..=0.5).contains(&x) => {
return true;
}
_ => {
// continue
self.recovery_counter = 0;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion boards/recovery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ mod app {
can0,
can_command_manager,
gpio,
recovery_timer,
},
Local {
led_green,
Expand Down Expand Up @@ -382,7 +383,7 @@ mod app {

/// Runs the state machine.
/// This takes control of the shared resources.
#[task(priority = 3, local = [state_machine], shared = [can0, gpio, data_manager, &em])]
#[task(priority = 3, local = [state_machine], shared = [can0, gpio, data_manager, &em, recovery_timer])]
fn run_sm(mut cx: run_sm::Context) {
cx.local.state_machine.run(&mut StateMachineContext {
shared_resources: &mut cx.shared,
Expand Down
11 changes: 8 additions & 3 deletions boards/recovery/src/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::communication::CanDevice0;
use crate::data_manager::DataManager;
use crate::gpio_manager::GPIOManager;
use crate::state_machine::states::*;
use atsamd_hal::timer::TimerCounter2;
pub use black_magic::*;
use core::fmt::Debug;
use defmt::Format;
Expand All @@ -17,6 +18,7 @@ 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_recovery_timer(&mut self, f: &dyn Fn(&mut TimerCounter2));
}

impl<'a> StateMachineSharedResources for crate::app::__rtic_internal_run_smSharedResources<'a> {
Expand All @@ -29,6 +31,9 @@ impl<'a> StateMachineSharedResources for crate::app::__rtic_internal_run_smShare
fn lock_gpio(&mut self, fun: &dyn Fn(&mut GPIOManager)) {
self.gpio.lock(fun)
}
fn lock_recovery_timer(&mut self, fun: &dyn Fn(&mut TimerCounter2)) {
self.recovery_timer.lock(fun)
}
}

pub struct StateMachineContext<'a, 'b> {
Expand Down Expand Up @@ -103,9 +108,9 @@ impl From<state::StateData> for RocketStates {
}
}
// Linter: an implementation of From is preferred since it gives you Into<_> for free where the reverse isn't true
impl Into<state::StateData> for RocketStates {
fn into(self) -> state::StateData {
match self {
impl From<RocketStates> for state::StateData {
fn from(val: RocketStates) -> Self {
match val {
RocketStates::Initializing(_) => state::StateData::Initializing,
RocketStates::WaitForTakeoff(_) => state::StateData::WaitForTakeoff,
RocketStates::Ascent(_) => state::StateData::Ascent,
Expand Down
10 changes: 10 additions & 0 deletions boards/recovery/src/state_machine/states/terminal_descent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::state_machine::{
RocketStates, State, StateMachineContext, TransitionInto, WaitForRecovery,
};
use crate::{no_transition, transition};
use atsamd_hal::prelude::_embedded_hal_timer_CountDown;
use atsamd_hal::timer_traits::InterruptDrivenTimer;
use common_arm::spawn;
use defmt::{write, Format, Formatter};
use rtic::mutex::Mutex;
Expand All @@ -17,6 +19,14 @@ impl State for TerminalDescent {
spawn!(fire_main)?;
Ok(())
});
context.shared_resources.recovery_timer.lock(|timer| {
timer.enable_interrupt();
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();
timer.start(timer_duration);
});
}
fn step(&mut self, context: &mut StateMachineContext) -> Option<RocketStates> {
context.shared_resources.data_manager.lock(|data| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::RTC;
use common_arm::spawn;
use defmt::{write, Format, Formatter};
use messages::command::{Command, PowerDown, RadioRate, RadioRateChange};
use messages::sender::Sender::SensorBoard;
use messages::node::Node::SensorBoard;
use messages::Message;

#[derive(Debug, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions boards/recovery/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use messages::sender::Sender;
use messages::sender::Sender::RecoveryBoard;
use messages::node::Node;
use messages::node::Node::RecoveryBoard;

// -------
// Sender ID
// Node ID
// -------
pub static COM_ID: Sender = RecoveryBoard;
pub static COM_ID: Node = RecoveryBoard;
1 change: 0 additions & 1 deletion libraries/messages/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions libraries/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name = "messages"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_more = "0.99.17"
serde = { workspace = true }
Expand Down
Loading

0 comments on commit 6bea4be

Please sign in to comment.