Set the EXT for HC-SR04

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

Moderator: RoccoMarco

giannir1988
Posts: 6
Joined: Tue Sep 15, 2015 6:06 pm

Set the EXT for HC-SR04

Post by giannir1988 »

Dear Giovanni,
we have a problem with the operation of a sensor HC-SR04.

This is our code:

Code: Select all

/*
    ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"
#include "chprintf.h"

BaseSequentialStream* chp = (BaseSequentialStream*) &SD2;

static event_source_t HCSR04;
static event_listener_t myel;

/*-----------------------------
 * EVENTO CHE VIENE INVOCATO
 ------------------------------*/
static void extcb1(EXTDriver *extp, expchannel_t channel) {
  (void)extp;
  (void)channel;
  chSysLockFromISR();
  chEvtBroadcastFlagsI(&HCSR04, 0);
  chSysUnlockFromISR();
}

/*-----------------------------
 * STRUTTURA DI CONFIGURAZIONE
 ------------------------------*/
static const EXTConfig extcfg1 = {
  {
    {EXT_CH_MODE_DISABLED, NULL}, //0
    {EXT_CH_MODE_DISABLED, NULL}, //1
    {EXT_CH_MODE_DISABLED, NULL}, //2
    {EXT_CH_MODE_DISABLED, NULL}, //3
    {EXT_CH_MODE_DISABLED, NULL}, //4
    {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOB, extcb1}, //5
    {EXT_CH_MODE_DISABLED, NULL}, //6
    {EXT_CH_MODE_DISABLED, NULL}, //7
    {EXT_CH_MODE_DISABLED, NULL}, //8
    {EXT_CH_MODE_DISABLED, NULL}, //9
    {EXT_CH_MODE_DISABLED, NULL}, //10
    {EXT_CH_MODE_DISABLED, NULL}, //11
    {EXT_CH_MODE_DISABLED, NULL}, //12
    {EXT_CH_MODE_DISABLED, NULL}, //13
    {EXT_CH_MODE_DISABLED, NULL}, //14
    {EXT_CH_MODE_DISABLED, NULL}, //15
    {EXT_CH_MODE_DISABLED, NULL}, //16
    {EXT_CH_MODE_DISABLED, NULL}, //17
    {EXT_CH_MODE_DISABLED, NULL}, //18
    {EXT_CH_MODE_DISABLED, NULL}, //19
    {EXT_CH_MODE_DISABLED, NULL}, //20
    {EXT_CH_MODE_DISABLED, NULL}, //21
    {EXT_CH_MODE_DISABLED, NULL}  //22
  }
};

/*
 * LED blinker thread, times are in milliseconds.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (true) {
    palClearPad(GPIOA, GPIOA_LED_GREEN);
    chThdSleepMilliseconds(500);
    palSetPad(GPIOA, GPIOA_LED_GREEN);
    chThdSleepMilliseconds(500);
  }
}

/*
 * Application entry point.
 */

/*-------------------------------
 * DEFINIZIONE DEI PARAMETRI
 * ------------------------------ */

//pin TRIGGER : (D2)
#define T1_PORT GPIOA
#define T1_PIN GPIOA_PIN10
//pin ECO : (D4)
#define E1_PORT GPIOB
#define E1_PIN GPIOB_PIN5

/*-------------------------------
 * PROCEDURE PERSONALI
 --------------------------------*/

//PROCEDURA DI INIZIALIZZAZIONE
void init_pin(void){

  //Definizione delle modalità

  palSetPadMode(T1_PORT, T1_PIN, PAL_MODE_OUTPUT_PUSHPULL |
                                 PAL_STM32_OSPEED_HIGHEST);
  palSetPadMode(E1_PORT, E1_PIN, PAL_MODE_INPUT_PULLDOWN |
                                 PAL_STM32_OSPEED_HIGHEST);

  chEvtObjectInit(&HCSR04);
  chEvtRegister(&HCSR04, &myel, 0);

}

//PROCEDURA PER LA LETTURA DEL TEMPO DI PERCORRENZA
float read_time_distance(void){

  //definizione delle variabii
    systime_t delta_t = 0;
  //abbassa il pin
    palClearPad(T1_PORT,T1_PIN);
    chThdSleepMicroseconds(2);
  //registra il tempo di inizio
    systime_t start = chVTGetSystemTimeX();
  //lancia l'ext
    extStart(&EXTD1, &extcfg1);
  //Lancia il trigger
    palSetPad(T1_PORT,T1_PIN);
    chThdSleepMicroseconds(10);
    palClearPad(T1_PORT,T1_PIN);
 
  //registra la maschera dell'evento
    eventmask_t evento = myel.el_events;
  //legge il tempo
    if(chEvtWaitAnyTimeout(evento, MS2ST(1000000000))){
      //rileva il tempo di esecuzione
      delta_t = chVTGetSystemTimeX() - start;
    }
    else{
      delta_t = 0;
    }


    //delta_t = chVTGetSystemTime() - start;
  //disabilita l'ext
    //extChannelDisableI(&EXTD1, &extcfg1);
    extStop(&EXTD1);
  //resituisce il tempo
    return (float)delta_t;
}

/*-------------------------------
 * VARIABILI GLOBALI
 --------------------------------*/
  float tempo;


  int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the serial driver 2 using the driver default configuration.
   */
  sdStart(&SD2, NULL);

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state.
   */

  /*--------------------------------------
   * FUNZIONI DA ESEGUIRE LA PRIMA VOLTA
   ---------------------------------------*/

  init_pin();

  while (true) {
    /*-------------------------------------
     * LOOP
     --------------------------------------*/

      //legge la distanza
      tempo = read_time_distance();
      //fa il break
      chprintf(chp, "%f\n\r", tempo);
      chThdSleepMilliseconds(250);
  }
}


we have problem: the system return on serial output always the same value (about 480).

How we can resolve?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Set the EXT for HC-SR04

Post by Giovanni »

"Invalid type argoument of '->' (have event_listern_t)"

It is event_listener_t.

Giovanni
afranzese
Posts: 2
Joined: Mon Sep 14, 2015 2:01 pm

Re: Set the EXT for HC-SR04

Post by afranzese »

Same problem with the use of EXT. i've checked also the example in testhal but it is not so simple to understand what's wrong.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Set the EXT for HC-SR04

Post by Giovanni »

giannir1988 wrote:we have problem: the system return on serial output always the same value (about 480).


Not enough info for a suggestion. Also, "return on serial output" is not clear to me.

Please make more specific questions.

Giovanni
giannir1988
Posts: 6
Joined: Tue Sep 15, 2015 6:06 pm

Re: Set the EXT for HC-SR04

Post by giannir1988 »

Giovanni wrote:
giannir1988 wrote:we have problem: the system return on serial output always the same value (about 480).


Not enough info for a suggestion. Also, "return on serial output" is not clear to me.

Please make more specific questions.

Giovanni


we use the serial comunication for display the value of delta variable on Terminal of Eclipse.

we determ the value of the time between the lounch of the trigger and the receve of eco with "read_time_distance" and we print this value with this iscrtuction " chprintf(chp, "%f\n\r", tempo);"

Code: Select all

  while (true) {
    /*-------------------------------------
     * LOOP
     --------------------------------------*/

      //legge la distanza
      tempo = read_time_distance();
      //fa il break
      chprintf(chp, "%f\n\r", tempo);
      chThdSleepMilliseconds(500);
  }


but in terminal we see only 460.000 value or also about it.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Set the EXT for HC-SR04

Post by Giovanni »

You seem to rely on microsecond-class time resolution, is your system tick set to 1MHz? the default is 10kHz. What you are reading is probably an approximation.

Giovanni
giannir1988
Posts: 6
Joined: Tue Sep 15, 2015 6:06 pm

Re: Set the EXT for HC-SR04

Post by giannir1988 »

Giovanni wrote:You seem to rely on microsecond-class time resolution, is your system tick set to 1MHz? the default is 10kHz. What you are reading is probably an approximation.

Giovanni



yes, we use this value :

Code: Select all

#define CH_CFG_ST_FREQUENCY                 1000000

1Mhz.

If we use the default value we see the number 6.0000 or other value in near.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Set the EXT for HC-SR04

Post by Giovanni »

Then there some other problem that is not immediately apparent to me. I can suggest to keep the code simple and add things one by one, that code looks very convoluted for the simple thing you are trying to accomplish.

If you need to measure an interval then you just need the system time.

1) Generate the pulse.
2) Read time.
3) Wait for the raising front (polling, not EXT).
4) Read time again and make the difference.

Once the simple thing works then you may try a more complex approach.

Giovanni
giannir1988
Posts: 6
Joined: Tue Sep 15, 2015 6:06 pm

Re: Set the EXT for HC-SR04

Post by giannir1988 »

Giovanni wrote:Then there some other problem that is not immediately apparent to me. I can suggest to keep the code simple and add things one by one, that code looks very convoluted for the simple thing you are trying to accomplish.

If you need to measure an interval then you just need the system time.

1) Generate the pulse.
2) Read time.
3) Wait for the raising front (polling, not EXT).
4) Read time again and make the difference.

Once the simple thing works then you may try a more complex approach.

Giovanni


ok, we try with polling system, thanks!
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Set the EXT for HC-SR04

Post by alex31 »

I have implemented a driver (chibios 2.6) for the HC-SR04 based on PWM for pulse generation and ICU for response measure,
it works well without hacking CH_FREQUENCY, and with minimal cpu usage.
Can post the source if interested.

Alexandre
Post Reply