Skip to content

Commit

Permalink
change beep frequency when GPS fix is obtained (#73)
Browse files Browse the repository at this point in the history
* feat(cansat-stm32f4): add buzzer support #59

* style: make cargo-fmt happy

* feat(cansat-stm32f4): beep every three seconds

* feat!(cansat-core): add nmea parser

* feat(cansat-stm32f4): when fix is obtained, change beep frequency

* refactor!: use fork of nmea with implemented serialize trait

* fix: use nmea fork from git repo

* refactor: use refactored NmeaGga in read_measurement task

* refactor: make clippy happy

* feat: respect defmt optionality, impl Debug for nmea Error

* style: delete commented code

* chore: use original nmea repository

* chore: enable GGA statement

* style: make clippy happy
  • Loading branch information
0xf4lc0n authored Aug 28, 2023
1 parent 0106c44 commit 0ef3e64
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 14 deletions.
111 changes: 109 additions & 2 deletions Cargo.lock

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

12 changes: 9 additions & 3 deletions crates/cansat-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "cansat-core"
version = "0.1.0"
edition = "2021"

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

Expand All @@ -18,9 +18,15 @@ heapless-bytes = "0.3.0"
defmt = { version = "0.3.4", optional = true }
mpu6050 = { version = "0.1.0"}

[dependencies.nmea]
git = "https://github.com/AeroRust/nmea"
default-features = false
features = ["serde", "GGA"]

[dependencies.derive_more]
version = "0.99.17"
default-features = false
features = [
"add", "mul",
"add",
"mul",
]
version = "0.99.17"
1 change: 1 addition & 0 deletions crates/cansat-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![no_std]

pub mod measurements;
pub mod nmea;
pub mod quantity;

pub use measurements::Measurements;
Expand Down
7 changes: 4 additions & 3 deletions crates/cansat-core/src/measurements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::nmea::NmeaGga;
use crate::quantity::{Distance, Pressure, Temperature};
use accelerometer::vector;
use heapless_bytes::Bytes;

use serde::Serialize;

#[derive(Default, serde::Serialize)]
Expand All @@ -14,7 +15,7 @@ pub struct Measurements {
#[serde(serialize_with = "option_distance_meters")]
pub altitude: Option<Distance>,

pub nmea: Option<Bytes<256>>,
pub nmea: Option<NmeaGga>,

#[serde(serialize_with = "option_vector_f32x3")]
pub acceleration: Option<vector::F32x3>,
Expand Down Expand Up @@ -76,7 +77,7 @@ impl defmt::Format for Measurements {
OrError(&self.temperature.map(Celsius)),
OrError(&self.pressure.map(HectoPascals)),
OrError(&self.altitude.map(Meters)),
OrError(&self.nmea.as_ref().map(|v| Ascii(v))),
OrError(&self.nmea),
OrError(&self.acceleration.map(Vector3)),
OrError(&self.gyro.map(Vector3)),
OrError(&self.rollpitch.map(Vector2)),
Expand Down
52 changes: 52 additions & 0 deletions crates/cansat-core/src/nmea.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use nmea::sentences::{FixType, GgaData};
use nmea::ParseResult;
use serde::Serialize;

#[cfg(feature = "defmt")]
use defmt::Format;

#[cfg_attr(feature = "defmt", derive(Format))]
#[derive(Debug)]
pub enum Error<'a> {
ParsingFailed(#[cfg_attr(feature = "defmt", defmt(Debug2Format))] nmea::Error<'a>),
InvalidCommand,
}

#[derive(Serialize, Debug)]
pub struct NmeaGga(GgaData);

impl NmeaGga {
pub fn try_new(bytes: &[u8]) -> Result<NmeaGga, Error> {
let ParseResult::GGA(gga) = nmea::parse_bytes(bytes).map_err(Error::ParsingFailed)? else {
return Err(Error::InvalidCommand)
};

Ok(Self(gga))
}

pub fn get_fix(&self) -> bool {
self.0
.fix_type
.map(|ft| FixType::Invalid != ft)
.unwrap_or(false)
}
}

#[cfg(feature = "defmt")]
impl Format for NmeaGga {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(
fmt,
"fix_time: {:?}, fix_type: {:?}, latitude: {:?},
fix_satelites: {:?}, hdop: {:?}, altitude: {:?},
geoid_separation: {:?}",
self.0.fix_time.as_ref().map(defmt::Debug2Format),
self.0.fix_type.as_ref().map(defmt::Debug2Format),
self.0.latitude,
self.0.fix_satellites,
self.0.hdop,
self.0.altitude,
self.0.geoid_separation
);
}
}
5 changes: 3 additions & 2 deletions crates/cansat-stm32f4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod app {

#[shared]
pub struct Shared {
pub is_fixed: bool,
pub gps: Gps,
pub csv_record: Vec<u8, 512>,
}
Expand All @@ -53,7 +54,7 @@ mod app {
startup::init(ctx)
}

#[idle(local = [delay, sd_logger, tracker, i2c1_devices], shared = [gps, csv_record])]
#[idle(local = [delay, sd_logger, tracker, i2c1_devices], shared = [gps, csv_record, is_fixed])]
fn idle(ctx: idle::Context) -> ! {
tasks::idle(ctx)
}
Expand All @@ -67,7 +68,7 @@ mod app {

#[task(local = [led], priority = 1)]
async fn blink(ctx: blink::Context);
#[task(local = [buzzer], priority = 1)]
#[task(local = [buzzer], shared = [is_fixed], priority = 1)]
async fn buzz(ctx: buzz::Context);
}
}
1 change: 1 addition & 0 deletions crates/cansat-stm32f4/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn init(ctx: app::init::Context) -> (app::Shared, app::Local) {
let shared = app::Shared {
gps: cansat.gps,
csv_record: Vec::new(),
is_fixed: false,
};
let local = app::Local {
delay: cansat.delay,
Expand Down
34 changes: 30 additions & 4 deletions crates/cansat-stm32f4/src/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{app, error::Error, startup::LoraError};
use accelerometer::vector;
use cansat_core::{
nmea::NmeaGga,
quantity::{Pressure, Temperature},
Measurements,
};
Expand Down Expand Up @@ -71,7 +72,20 @@ fn read_measurements(ctx: &mut app::idle::Context) -> Measurements {
if let Some(mut nmea) = gps.lock(|gps| gps.last_nmea()) {
let clrf_len = 2;
nmea.truncate(nmea.len().saturating_sub(clrf_len));
data.nmea = Some(nmea.into());
let nmea_gga = NmeaGga::try_new(&nmea);

match nmea_gga {
Ok(gga) => {
ctx.shared.is_fixed.lock(|f: &mut bool| *f = gga.get_fix());
data.nmea = Some(gga);
}
Err(e) => {
defmt::error!(
"Could not read NMEA GGA command: {}",
defmt::Debug2Format(&e)
);
}
}
}

if let Some(mpu) = &mut i2c1_devices.mpu {
Expand Down Expand Up @@ -150,11 +164,23 @@ pub async fn blink(ctx: app::blink::Context<'_>) {
}

/// Toggle buzzer every second
pub async fn buzz(ctx: app::buzz::Context<'_>) {
pub async fn buzz(mut ctx: app::buzz::Context<'_>) {
let buzzer = ctx.local.buzzer;
let mut is_fixed = false;

loop {
buzzer.toggle();
defmt::debug!("Buzz");
Systick::delay(3.secs()).await;

ctx.shared.is_fixed.lock(|f| {
is_fixed = *f;
});

if is_fixed {
defmt::debug!("Buzz with GPS fix");
Systick::delay(1.secs()).await;
} else {
defmt::debug!("Buzz without GPS fix");
Systick::delay(3.secs()).await;
}
}
}

0 comments on commit 0ef3e64

Please sign in to comment.