-
Notifications
You must be signed in to change notification settings - Fork 1
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
SNS - Levitation Current Sensor #71
Open
Aux1r
wants to merge
12
commits into
main
Choose a base branch
from
kacper/hype-92-implement-current-sensor-for-levitation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f52dbc2
Create branch for levitation current sensor
Aux1r 0193d53
created levitation implementation
Aux1r 9a3ecf2
comment changes + tasks
Aux1r 4028279
add spawner, rejig structure
Aux1r dcc4447
Merge branch 'main' into kacper/hype-92-implement-current-sensor-for-…
Aux1r 6d9b3b9
fix comments + unnecessary import
Aux1r aa32ca7
rename file + remove abbrev
Aux1r 3cbb83c
Merge branch 'main' into kacper/hype-92-implement-current-sensor-for-…
Aux1r efea3cc
new_with_bounds
Aux1r bdfced2
Merge branch 'main' into kacper/hype-92-implement-current-sensor-for-…
Aux1r 62b542c
Merge branch 'main' into kacper/hype-92-implement-current-sensor-for-…
Aux1r fa5fbdb
some changes i dont remember
Aux1r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use embassy_executor::Spawner; | ||
use embassy_stm32::adc::Adc; | ||
use embassy_sync::{ | ||
blocking_mutex::{ | ||
raw::{CriticalSectionRawMutex, NoopRawMutex}, | ||
Mutex, | ||
}, | ||
watch::Watch, | ||
}; | ||
use embassy_time::{Duration, Timer}; | ||
use hyped_boards_stm32l432kc::tasks::current_levitation::read_current_levitation; | ||
use hyped_sensors::{current_levitation::CurrentLevitation, SensorValueRange::*}; | ||
use {defmt_rtt as _, panic_probe as _}; | ||
|
||
static CURRENT_LEVITATION_READING: Watch<CriticalSectionRawMutex, SensorValueRange<f32>, 1> = | ||
Watch::new(); | ||
|
||
#[embassy_executor::main] | ||
async fn main(spawner: Spawner) -> { | ||
let p = embassy_stm32::init(Default::default()); | ||
let adc = Adc::new(p.ADC1, Delay); | ||
|
||
// Create a sender to pass to the current levitation reading task, and a receiver for reading the values back. | ||
let current_levitation_reading_sender = CURRENT_LEVITATION_READING.sender(); | ||
let mut current_levitation_reading_receiver = CURRENT_LEVITATION_READING.receiver().unwrap(); | ||
|
||
spawner | ||
.spawn(read_current_levitation(current_levitation_reading_sender)) | ||
.unwrap(); | ||
|
||
// Every 100ms we read for the latest value from the current levitation sensor. | ||
loop { | ||
match current_levitation_reading_receiver.try_changed() { | ||
Some(reading) => match reading { | ||
Safe(value) => { | ||
defmt::info!("Current: {} A (safe)", value) | ||
} | ||
Warning(value) => { | ||
defmt::warn!("Current: {} A (warning)", value) | ||
} | ||
Critical(value) => { | ||
defmt::error!("Current: {} A (critical)", value) | ||
} | ||
None => (), | ||
} | ||
} | ||
Timer::after(Duration::from_millis(100)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod current_levitation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use embassy_stm32::adc::Adc; | ||
use embassy_time::Delay; | ||
use hyped_sensors::{current_levitation::CurrentLevitation, SensorValueRange::*}; | ||
use defmt_rtt as _; | ||
use embassy_sync::{ | ||
blocking_mutex::{ | ||
raw::{CriticalSectionRawMutex, NoopRawMutex}, | ||
Mutex, | ||
}, | ||
watch::Sender, | ||
}; | ||
|
||
|
||
/// Test task that reads the current and sends it with the Watch Sender | ||
#[embassy_executor::task] | ||
pub async fn read_current_levitation( | ||
sender: Sender<'static, CriticalSectionRawMutex, SensorValueRange<f32>, 1>, | ||
) -> ! { | ||
let p = embassy_stm32::init(Default::default()); | ||
let adc = Adc::new(p.ADC1, Delay); | ||
|
||
let mut current_levitation_sensor = CurrentLevitation::new(&mut adc); | ||
|
||
loop { | ||
sender.send(current_levitation_sensor.read()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::SensorValueRange; | ||
use hyped_adc::HypedAdc; | ||
|
||
/// current_levitation implements the logic to read current from the ACHS-7121 current sensor using the | ||
/// Hyped ADC trait. | ||
/// | ||
/// Data sheet: https://docs.broadcom.com/doc/ACHS-712x-DS | ||
pub struct CurrentLevitation<T: HypedAdc> { | ||
adc: T, | ||
calculate_bounds: fn(f32) -> SensorValueRange<f32>, | ||
} | ||
|
||
impl<T: HypedAdc> CurrentLevitation<T> { | ||
/// Create a new instance of the Current Levitation sensor | ||
pub fn new(adc: T) -> CurrentLevitation<T> { | ||
Self::new_with_bounds(adc, default_calculate_bounds) | ||
} | ||
|
||
pub fn new_with_bounds( | ||
adc: T, | ||
calculate_bounds: fn(f32) -> SensorValueRange<f32>, | ||
) -> CurrentLevitation<T> { | ||
CurrentLevitation { | ||
adc, | ||
calculate_bounds, | ||
} | ||
} | ||
/// The ACHS-7121 has a current range of +- 10 A, sensitivity of 185 mV/A. | ||
/// Assuming we're supplying 5V to the sensor, our off-set is 2.5V in the output reading - note that this offset is given | ||
/// by the supply voltage divided by 2, so if you change the supply voltage, you'll have to change the offset that we subtract | ||
/// in the read function accordingly. | ||
pub fn read(&mut self) -> SensorValueRange<f32> { | ||
let current = self.adc.read_value() as f32; | ||
(self.calculate_bounds)((current - OFFSET) / SENSITIVITY) | ||
} | ||
} | ||
|
||
pub fn default_calculate_bounds(value: f32) -> SensorValueRange<f32> { | ||
if value <= MIN_AMPS || value >= MAX_AMPS { | ||
SensorValueRange::Critical(value) | ||
} else if value <= WARN_AMPS_LOW || value >= WARN_AMPS_HIGH { | ||
SensorValueRange::Warning(value) | ||
} else { | ||
SensorValueRange::Safe(value) | ||
} | ||
} | ||
|
||
const OFFSET: f32 = 2.5; | ||
const SENSITIVITY: f32 = 0.185; | ||
const MIN_AMPS: f32 = 10.0; | ||
const MAX_AMPS: f32 = -10.0; | ||
const WARN_AMPS_LOW: f32 = -8.0; | ||
const WARN_AMPS_HIGH: f32 = 8.0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![no_std] | ||
|
||
pub mod current_levitation; | ||
pub mod keyence; | ||
pub mod temperature; | ||
pub mod time_of_flight; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to convert the value read on ADC into volts first (currently will be 0-4096)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that something that can be done as part of the adc implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getting the resolution of the ADC is being added in #82 if that's what you mean?
I guess we could also add functionality to map it to a given range (e.g. 0-3.3) too