Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 1   [ 4 posts ]
Author Message
 Post subject: Ping Sensor HC-SR04
PostPosted: Tue Mar 05, 2013 5:16 pm 

Joined: Tue Mar 05, 2013 5:08 pm
Posts: 3
Hi,

I'm doing some experiments with your raspberry to arduino bridge.

On Arduino I'm able to read the range of the ping Sensor using the function "pulseIn"
On Rasperry I'm missing this function, so I wonder how solve the issue.
I need to get the time between setting Triggerpin "LOW-HIGH-LOW" and receving the echo on the echopin.
echopin will get HIGH, when receiving the echo. But how to get the time in microseconds?

This kind of code will do the job on arduino:
Code:
  const int pingTrig = 2;
  const int pingEcho = 3;

  pinMode(pingTrig, OUTPUT);
  pinMode(pingEcho, INPUT);

  long duration, cm;

  digitalWrite(pingTrig,LOW);
  delayMicroseconds(50);
  digitalWrite(pingTrig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(pingTrig,LOW);

  duration = pulseIn(pingEcho, HIGH);

  cm = duration /2 /29.1;  //range in cm


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Ping Sensor HC-SR04
PostPosted: Wed Mar 06, 2013 9:25 am 

Joined: Tue Mar 05, 2013 5:08 pm
Posts: 3
I now wrote a class, which should be able to get results from the ping sensor, but it seems not working at the moment, I did not get correct values.
Is there somebody out there, have a look into the code, and tell what I'm doing wrong.
I've tested with attachinterrupt method also as waiting for change of state at the echopin:
hc_sr04.hpp
Code:
#ifndef ping_hpp
#define ping_hpp
#include <sys/time.h>

class HC_SR04
{
   public:
      HC_SR04();
      HC_SR04(int, int);
      int Ping();
      int Entfernung(){ return cm;}
      timeval start, end;
      long duration, cm;
   private:
      int TrigPin;
      int EchoPin;

      void Messung();  // soll aufgerufen werden, wenn ein Interrupt kommt,
                           // also ein Signal am Echopin anliegt

};

#endif


hc_sr04.cpp
Code:
#include "hc_sr04.hpp"
#include "../../Libs/arduPi.h"
#include <sys/time.h>
#include <iostream>
#include "error.hpp"

HC_SR04 :: HC_SR04(int trig, int echo)
{
   TrigPin = trig;
   EchoPin = echo;

   pinMode(TrigPin, OUTPUT);
   pinMode(EchoPin, INPUT);

   digitalWrite(TrigPin,LOW);
   //attachInterrupt(EchoPin,Messung,RISING);

}


/*
void HC_SR04 :: Messung()
{
   gettimeofday(&end, 0);
   duration = end.tv_usec - start.tv_usec;
   cm = duration /2 /29.1;
}
*/
int HC_SR04 :: Ping()
{
   boolean echo = false;

   long duration, cm;
   try
   {
      digitalWrite(TrigPin,LOW);
      delayMicroseconds(50);
      digitalWrite(TrigPin,HIGH);
      delayMicroseconds(200);
      gettimeofday(&start, 0);
      digitalWrite(TrigPin,LOW);
      while(digitalRead(EchoPin)==1){}
      gettimeofday(&end, 0);

      duration = end.tv_usec - start.tv_usec;
   }
   catch(Error &e)
   {
      e.message();
      return -4;
   }

   std::cout << end.tv_usec << " - " <<start.tv_usec << std::endl;

   cm = duration /2 /29.1;
   //messe entfernung
   std::cout << duration << " in cm " << cm << std::endl;
   return cm;
}


Top
 Profile  
 
 Post subject: [SOLVED] Re: Ping Sensor HC-SR04
PostPosted: Wed Mar 06, 2013 3:00 pm 

Joined: Tue Mar 05, 2013 5:08 pm
Posts: 3
for anyone how is interessted in, I've finished the class, and it's working well right now.

Code:
#ifndef ping_hpp
#define ping_hpp
#include <sys/time.h>

class HC_SR04
{
   public:
      HC_SR04();
      HC_SR04(int, int);
      int pulseIn(int, int, int);
      int Ping();
      timeval start, end,wait;
   private:
      int TrigPin;
      int EchoPin;
      long duration, cm;
};
#endif


Code:
/*
   HC_SR 04 Klasse von Georg Trümper
   georg.truemper (AT) gmail.com

   06.03.2013
*/


#include "hc_sr04.hpp"
#include "../../Libs/arduPi.h"
#include <sys/time.h>
#include <iostream>
#include "error.hpp"

HC_SR04 :: HC_SR04(int trig, int echo)
{
   TrigPin = trig;
   EchoPin = echo;

   pinMode(TrigPin, OUTPUT);
   pinMode(EchoPin, INPUT);

   digitalWrite(TrigPin,LOW);
}


int HC_SR04 :: pulseIn(int pin, int level, int timeout)
{
   struct timeval tn, t0, t1;

   long micros;

   gettimeofday(&t0, NULL);

   micros = 0;

   while (digitalRead(pin) != level)
   {
      gettimeofday(&tn, NULL);

      if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
      micros += (tn.tv_usec - t0.tv_usec);

      if (micros > timeout) return 0;
   }

   gettimeofday(&t1, NULL);

   while (digitalRead(pin) == level)
   {
      gettimeofday(&tn, NULL);

      if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
      micros = micros + (tn.tv_usec - t0.tv_usec);

      if (micros > timeout) return 0;
   }

   if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
   micros = micros + (tn.tv_usec - t1.tv_usec);

   return micros;
}

int HC_SR04 :: Ping()
{
   long duration, cm;
   try
   {
         digitalWrite(TrigPin,LOW);
         delayMicroseconds(200);
         digitalWrite(TrigPin,HIGH);
         delayMicroseconds(10);
         digitalWrite(TrigPin,LOW);
         delayMicroseconds(60);
         duration = pulseIn(EchoPin,HIGH,100000);
   }
   catch(Error &e)
   {
      e.message();
      return -4;
   }

   cm = duration /2 /29.1;

   return cm;

}


the trigger pin is connected direktly to the Arduino-Pi Bridge, the Echo Pin is connected using
potential divider 330Ohm to 470 Ohm.


Top
 Profile  
 
 Post subject: Re: Ping Sensor HC-SR04
PostPosted: Fri Mar 08, 2013 8:52 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hello minimi,

Congratulations!! You are making a good job!!

Thanks for your contribution to the Cooking Hacks community.

Regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 4 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.