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

Ultrasonic Sensor HC-SR04 Example #320

Closed
wants to merge 1 commit into from

Conversation

Zacchaeus-Oluwole
Copy link

No description provided.

@@ -0,0 +1,51 @@
use esp_idf_hal::delay::FreeRtos;
Copy link
Collaborator

@ivmarkov ivmarkov Oct 10, 2023

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!");
Copy link
Collaborator

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");
Copy link
Collaborator

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");
Copy link
Collaborator

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");
Copy link
Collaborator

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");
Copy link
Collaborator

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() {}
Copy link
Collaborator

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();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto.

@ivmarkov
Copy link
Collaborator

You also need to run cargo fmt

use esp_idf_hal::gpio::*;
use esp_idf_hal::peripherals::Peripherals;
use log::*;
use esp_idf_svc::systime::EspSystemTime;
Copy link
Collaborator

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();
Copy link
Collaborator

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();
Copy link
Collaborator

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.

@ivmarkov
Copy link
Collaborator

This does not seem to go anywhere. Closing. Feel free to open a new PR if you plan to finish it.

@ivmarkov ivmarkov closed this Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants