help using ultrasonic ranging module with STM32 nucleoF401RE

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

blob84
Posts: 3
Joined: Wed May 25, 2016 4:39 pm

help using ultrasonic ranging module with STM32 nucleoF401RE

Postby blob84 » Thu Jun 30, 2016 3:32 pm

Hello I have a problem to run this module, it works like this:
(1) Using IO trigger for at least 10us high level signal,
(2) The Module automatically sends eight 40 kHz and detect whether there is a
pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is
the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound (340M/S) / 2

I'm interested only to know the differents beetween start and end time.

this is the code,
in the main function:
-the first 3 lines send high level signal at 10us
-the first while loop read if there is an high level signal
-the second while loop read if the singnal is gone down
It doesn't work. What is the problem?

Code: Select all

#include "ch.h"
#include "hal.h"
#include "pwm.h"
#include "pwm_lld.h"
#include "chvt.h"

void delay(int n) {
    chThdSleepMilliseconds(n);
}
void blink1(void) {
   palSetPad(GPIOA, GPIOA_LED_GREEN);
   delay(1000);
   palClearPad(GPIOA, GPIOA_LED_GREEN);
}
int main(void) {
    //GPIOA_PIN8 (PA8) trigger, GPIOA_PIN9 (PA9) echo
     palSetPad(GPIOA, GPIOA_PIN8);
     delay(100);
     palClearPad(GPIOA, GPIOA_PIN8);
     //ST2MS converts from ticks to milliseconds and chVTGetSystemTime Returns the number of system ticks since the chSysInit() invocation
     while (!palReadPad(GPIOA, GPIOA_PIN9)) ;
     int start = ST2MS(chVTGetSystemTime());
     while (palReadPad(GPIOA, GPIOA_PIN9)) ;
     int end = ST2MS(chVTGetSystemTime());
     int time = end-start;
     if(time < 38000)
          blink1();
     else break;
      delay(1500);
}
 

Thanks for your help.

jscott
Posts: 129
Joined: Tue Jul 03, 2012 3:50 pm
Location: Middle Georgia, USA
Contact:

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby jscott » Thu Jun 30, 2016 4:13 pm

You have a delay problem...

Here you have a delay function that delays for n miliseconds

Code: Select all

void delay(int n) {
    chThdSleepMilliseconds(n);
}




Here you call the delay function with a value of 100. This is going to delay 100 miliseconds
not 10 microseconds.

Code: Select all

    //GPIOA_PIN8 (PA8) trigger, GPIOA_PIN9 (PA9) echo
     palSetPad(GPIOA, GPIOA_PIN8);
     delay(100);
     palClearPad(GPIOA, GPIOA_PIN8);


To delay for 10uS you are probably going to need to use a HAL function to do a polled delay.
I don't remember the name of the function off the top of my head.
Also, you are wasting time using a function called delay in this case.
Just put the Chibios delay call in your main(). With a 10uS delay the function call and
return might become a noticeable percentage of the delay.

Sleep functions will only delay to the nearest system tick after the delay.
They are not a high precision timer, and are not suitable for this type of usage.


You can also set up a timer to perform a one-shot pulse output. This would keep
your delay from pausing all other processing while waiting for it to expire. You can try
searching this forum and see if there are any examples. I think it would have to be a
hardware timer not a virtual timer.

Good Luck.
-John

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby Giovanni » Thu Jun 30, 2016 4:39 pm

Hi,

For small delays you can use chSysPolledDelayX(rtcnt_t cycles). This function is not available on Cortex-M0, you can use the GPT driver gptPolledDelay(GPTDriver *gptp, gptcnt_t interval) there.

Note that your delay will be heavily affected by ISRs execution, don't expect any accuracy at that time scale. Consider the delay a guaranteed minimum.

Giovanni

blob84
Posts: 3
Joined: Wed May 25, 2016 4:39 pm

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby blob84 » Fri Jul 01, 2016 9:08 am

Thank you for the reply.
I have used the osalSysPolledDelayX who call the chSysPolledDelayX function because I have a cortex-m4 but the problem is the first while loop, it never stop, meaning that there are no high level signal back.

Code: Select all

while (!palReadPad(GPIOA, GPIOA_PIN9)) ;

This can happen because the module is not working or because it doesn't send the ultrasonic.
How can i test that the module is powered on?

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby Giovanni » Fri Jul 01, 2016 11:16 am

Hi,

This is outside the scope of ChibiOS, I don't necessarily know that module :)

Perhaps an oscilloscope or logic analyser?

Giovanni

blob84
Posts: 3
Joined: Wed May 25, 2016 4:39 pm

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby blob84 » Fri Jul 01, 2016 1:41 pm

I added the following lines of code:

Code: Select all

palSetPadMode(GPIOA, GPIOA_PIN8, PAL_MODE_OUTPUT_PUSHPULL);
       palSetPadMode(GPIOA, GPIOA_PIN9, PAL_MODE_INPUT);

to let the pins act as input and output, reading from the documentation.
To exclude other problems, do you think that the code i wrote is good, using the chibios functions in the right way?

Thank you.

User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times
Contact:

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby Giovanni » Fri Jul 01, 2016 1:48 pm

That's the correct way to program pins.

Giovanni

User avatar
tfAteba
Posts: 526
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 80 times
Been thanked: 46 times

Re: help using ultrasonic ranging module with STM32 nucleoF401RE

Postby tfAteba » Mon Jul 04, 2016 11:55 am

Hi Blob84,

Did you solved your problem?

What is the reference of your ultrasonic sensor module?

Recently, I made a small program to measure distance with the famous HCSR04 and a Nucléof401re.

- To generate the Trigger signal, I used the following function in a thread every 500ms: chThdSleepMicroseconds(10);

- To read the echo, I use a different way: To get the High level of the echo signal, I use the ICU (Input Capture Unit).
By configuring it, icu gave me the time that the echo signal is HIGH, I think that what you need? After that, in another thread, I was then able to calculate the distance and print it to the serial driver.

If you are interested, here is a link to my source code: https://github.com/tateba/ST_NUCLEO_F40 ... ter/main.c

Feel free to ask me any questions if necessaire.

I hope it will help you.

regards,

Theo


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 5 guests