-
Notifications
You must be signed in to change notification settings - Fork 173
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
Ultrasonic Sensor HC-SR04 Example #320
Conversation
@@ -0,0 +1,51 @@ | |||
use esp_idf_hal::delay::FreeRtos; |
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.
Don't import esp_idf_hal
. Use esp_idf_svc::hal
instead.
// Initialize logging and necessary peripherals | ||
esp_idf_sys::link_patches(); | ||
esp_idf_svc::log::EspLogger::initialize_default(); | ||
info!("Hello, world!"); |
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.
We probably don't need that line?
let peripherals = Peripherals::take().unwrap(); | ||
|
||
// Configure pins for trigger and echo | ||
let mut trigger_pin = PinDriver::output(peripherals.pins.gpio4).expect("Error configuring trigger pin"); |
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.
What can be the configuration error here? Simply .unwrap()
|
||
// Configure pins for trigger and echo | ||
let mut trigger_pin = PinDriver::output(peripherals.pins.gpio4).expect("Error configuring trigger pin"); | ||
let echo_pin = PinDriver::input(peripherals.pins.gpio5).expect("Error configuring echo pin"); |
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.
Ditto.
|
||
loop { | ||
// Send a 10us pulse to the trigger pin to start the measurement | ||
trigger_pin.set_high().expect("Error: Unable to set trigger pin high"); |
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.
Ditto. These methods are actually infallible. See #140
// Send a 10us pulse to the trigger pin to start the measurement | ||
trigger_pin.set_high().expect("Error: Unable to set trigger pin high"); | ||
FreeRtos::delay_us(10); | ||
trigger_pin.set_low().expect("Error: Unable to set trigger pin low"); |
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.
Ditto.
|
||
// Measure the duration of the echo pulse (in microseconds) | ||
let start_time = EspSystemTime {}.now().as_micros(); | ||
while echo_pin.is_high() {} |
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 are busy looping. Isn't this triggering the TWDT? An even more interesting example might be an async one, as it would avoid the busy looping, and the waits there are expressed trivially.
// Measure the duration of the echo pulse (in microseconds) | ||
let start_time = EspSystemTime {}.now().as_micros(); | ||
while echo_pin.is_high() {} | ||
let end_time = EspSystemTime {}.now().as_micros(); |
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.
Ditto.
You also need to run |
use esp_idf_hal::gpio::*; | ||
use esp_idf_hal::peripherals::Peripherals; | ||
use log::*; | ||
use esp_idf_svc::systime::EspSystemTime; |
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.
It is not possible to use the esp-idf-svc
crate inside the esp-idf-hal
crate. Circular dependency.
|
||
fn main() { | ||
// Initialize logging and necessary peripherals | ||
esp_idf_sys::link_patches(); |
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.
esp_idf_hal::sys
fn main() { | ||
// Initialize logging and necessary peripherals | ||
esp_idf_sys::link_patches(); | ||
esp_idf_svc::log::EspLogger::initialize_default(); |
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.
Not possible to use esp-idf-svc
here so it is not possible to use regular info!
, warn!
etc, logging. Use println!
instead.
This does not seem to go anywhere. Closing. Feel free to open a new PR if you plan to finish it. |
No description provided.