Attemp to integrate sonar (ultrasonic sensor)

Wipo
Posts: 22
Joined: Mon Jan 20, 2014 9:33 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Wipo »

what pins can we use for the HC-SR04 on nanoboards with atmega32u4?

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by QuadBow »

Many people ask how to fit the cheap SRF-04 onto their promini or promicro board.
In my view, this doesn't make sense, since you problably wish to connect a GPS device via an I2C-GPS-NAV module.
The I2C-GPS-NAV software supports the SRF-04 sensor and there are already boards on the market with a break-out for the relevant pins.
So, it is better to amke use of this functionality.

Wipo
Posts: 22
Joined: Mon Jan 20, 2014 9:33 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Wipo »

Like many others I don't use the gps option so I need to connect it directly to the FC board.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by QuadBow »

Wipo wrote:Like many others I don't use the gps option so I need to connect it directly to the FC board.

In this case you should have one PCINT pin free on your board and another digital pin free.

rob222
Posts: 3
Joined: Tue Nov 26, 2013 12:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rob222 »

you have a full code arduino for HC-SR04 please for my quadcopter ?thanks

robinson01
Posts: 1
Joined: Tue Jun 03, 2014 12:52 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by robinson01 »

Hi!

Is there any way to use US-020 sensor for altitude hold? I chose this because of its bigger range (up to 700cm). It has the same operation method like the sr04, but I cannot find any way to make it work, and I cannot write the code myself, because I am really beginner at programming.

Could anyone help?

Thank you!

PeeBee
Posts: 7
Joined: Thu Jun 12, 2014 8:12 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by PeeBee »

I have a 32u4 based MicroWii (Hextronik) and a SRF-04. Is it possible to get Alt-hold to use this sensor directly from the MicroWii?

I have experience in coding the Arduino Leonardo, so can do some coding if that necessary.

Any suggestions would be appreciated... :)

dorty
Posts: 5
Joined: Thu May 01, 2014 1:53 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by dorty »

Just for completion ...
i read the thread :-) and there are persons having problems with to low ranges ... 0~50cm
i tested the sr04 a few months ago and its needing power :-)
some on-board-voltage-regulators are providing to less power to supply it .. for example the usb-port of my notebook doesnt deliver enough power for the echo-signal. when i use a 1A - USB PowerPlug it works perfectly at ~5,5m. I also noticed that my Crius AIOP from HK is having to less power on-board if i am connecting directly to the BEC everything is fine .. for example i am unable to connect a bluetooth-module to the rs232-port cause power is to low *ggg*

well, if you are having trouble .. try to get a power-source directly from bec for your sr04 ...

robertha
Posts: 16
Joined: Sun Mar 17, 2013 10:12 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by robertha »

Agree - I did the same finding, believe the SRF04 power surge during the ultrasoninc burst is quite high. I have also great results attaching capacitor on SRF04 power lines (+5V, GND) directly (parallel with supply wires). I did use this capacitor 3300uF 16V.

http://www.hobbyking.com/hobbyking/stor ... =capacitor

pihlerm
Posts: 23
Joined: Tue Dec 09, 2014 6:40 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by pihlerm »

Just wanted to say that I used code from kataventos's post.
I have a cheap HC-SR04.

Had to re-arrange the code a bit for MWI 2.3 but it works fine.

There is a big improvement in triggering the sonar RARELY. I settled for 10Hz.
I get no spikes and other artifacts as I had with a tighter (original) loop .. this is probably due to ringing, interference, lack of power etc.
I will test it outside tomorrow, but today inside I get a very smooth and noiseless signal up to 2.55m.
Tilt does not introduce errors until critical angle, when it jumps to either the distance from the wall or out of range.

Will progress towards autolanding. I'm planning to mix it with image processing (on external platform) later to land on target.

Just wanted to report it.

FengShuiDrone
Posts: 234
Joined: Wed Dec 24, 2014 1:20 am
Location: ......

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by FengShuiDrone »

Vallekano wrote:
Steeze McQueen wrote:
Vallekano wrote:I use D8 and D12

Thank you for getting the HC-SR04 working! I think I'm being a bit thick today, plus this is my first arduino project so I'm still learning. My question is, what pins are you using off the HC-SR04? I see a Vcc, Trig, Echo, and GND. How do those correspond to pins D8 and D12 on the arduino? Also, and you may not know this, but I'm using a seeeduino mega board, not a pro mini. In that case, how do the pins D8 and D12 on a pro mini correspond to pins on the arduino mega?


Sorry for the delay.

VCC -> 5V
GND -> GND
ECHO -> D8
TRIGGER -> D12


if i have time this afternoon or tomorrow i'll record a video of MultiWiiconf

If I wanted to use the HC-SR04 with my Multiwii Pro 2 board from RTF quads, what would be the correct pins to use? Is it even possible to do it with this board? I already have the sensor and I can copy and paste code, I just don't know what pins to hook it up to.

robertha
Posts: 16
Joined: Sun Mar 17, 2013 10:12 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by robertha »

pihlerm wrote:Just wanted to say that I used code from kataventos's post.
I have a cheap HC-SR04.

Had to re-arrange the code a bit for MWI 2.3 but it works fine.

There is a big improvement in triggering the sonar RARELY. I settled for 10Hz.
I get no spikes and other artifacts as I had with a tighter (original) loop .. this is probably due to ringing, interference, lack of power etc.
I will test it outside tomorrow, but today inside I get a very smooth and noiseless signal up to 2.55m.
Tilt does not introduce errors until critical angle, when it jumps to either the distance from the wall or out of range.

Will progress towards autolanding. I'm planning to mix it with image processing (on external platform) later to land on target.

Just wanted to report it.


Could you post your changes for MWII 2.3 ? I am also trying to implement KV mod (have already working sonar on I2C GPS - but just as indication), but as I am not familliar with MWI deeply I have problem where to put changes as 2.3 differs a lot from KV 2.2.

robertha
Posts: 16
Joined: Sun Mar 17, 2013 10:12 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by robertha »

robertha wrote:Agree - I did the same finding, believe the SRF04 power surge during the ultrasoninc burst is quite high. I have also great results attaching capacitor on SRF04 power lines (+5V, GND) directly (parallel with supply wires). I did use this capacitor 3300uF 16V.

http://www.hobbyking.com/hobbyking/stor ... =capacitor


I attached few pictures to demonstrate capacitor influence at same conditions - real height from sensor to ceiling about 1.85m

- 6763 configuration
- 6764 result when cap connected
- 6766 result when cap disconnected
Attachments
result without cap
result without cap
result with cap
result with cap
configuration
configuration

pihlerm
Posts: 23
Joined: Tue Dec 09, 2014 6:40 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by pihlerm »

robertha wrote: Could you post your changes for MWII 2.3 ? I am also trying to implement KV mod (have already working sonar on I2C GPS - but just as indication), but as I am not familliar with MWI deeply I have problem where to put changes as 2.3 differs a lot from KV 2.2.



Hi,

sorry for the delay,

I am using pin 12 for trigger and pin 11 for echo. Changing pins requires modification to def.h (but NOT only #define SONAR_GEP_EchoPin 11 also masks for interrups etc.)
Currenty I have disabled any control code, I am only sending sonar height via telemetry.

Thank you for posting data regarding capacitor; I will try adding one since my real flight readings are somewhat unstable and most importantly above certain height the reading is not >400cm as it should be but erratic and low.
Perhaps the code needs some reworking as well - but I am working on other issues currety, will come back to sonar in a month or so.



config.h, somewhere around 196

Code: Select all

  /* Sonar */ // for visualization purpose currently - no control code behind
      //#define SRF02 // use the Devantech SRF i2c sensors
      //#define SRF08
      //#define SRF10
      //#define SRF23

--- ADD
     /* Generic sonar: hc-sr04, srf04, dyp-me007, all generic sonar with echo/pulse pin
         default pulse is PH6/12, echo is PB4/11
      */
      #define SONAR_GENERIC_ECHOPULSE
      #define SONAR_GENERIC_SCALE 58 //scale for ranging conversion (hcsr04 is 58)
      #define SONAR_GENERIC_MAX_RANGE 500 //cm (could be more)

   /************************* Sonar alt hold / precision / ground collision keeper *******/
    #define SONAR_MAX_HOLD 400 //cm, kind of error delimiter, for now to avoid rocket climbing, only usefull if no baro
   
    //if using baro + sonar       
    #define SONAR_BARO_FUSION_LC 200 //cm, baro/sonar readings fusion, low cut, below = full sonar
    #define SONAR_BARO_FUSION_HC 400 //cm, baro/sonar readings fusion, high cut, above = full baro
    #define SONAR_BARO_FUSION_RATIO 0.0 //0.0-1.0,  baro/sonar readings fusion, amount of each sensor value, 0 = proportionnel between LC and HC
    #define SONAR_BARO_LPF_LC 0.9f
    #define SONAR_BARO_LPF_HC 0.9f
    #define SONAR_UPDATE_INTERVAL 100000



def.h around 1590

Code: Select all

/**************************************************************************************/
/***************              Sensor Type definitions              ********************/
/**************************************************************************************/

#if defined(SONAR_GENERIC_ECHOPULSE)
  #define SONAR_GEP_TriggerPin             12
  #define SONAR_GEP_TriggerPin_PINMODE_OUT pinMode(SONAR_GEP_TriggerPin,OUTPUT);
  #define SONAR_GEP_TriggerPin_PIN_HIGH    PORTB |= 1<<6;
  #define SONAR_GEP_TriggerPin_PIN_LOW     PORTB &= ~(1<<6);
  #define SONAR_GEP_EchoPin                11
  #define SONAR_GEP_EchoPin_PINMODE_IN     pinMode(SONAR_GEP_EchoPin,INPUT);
  #define SONAR_GEP_EchoPin_PCINT          PCINT5
  #define SONAR_GEP_EchoPin_PCICR          PCICR |= (1<<PCIE0); // PCINT 0-7 belong to PCIE0
  #define SONAR_GEP_EchoPin_PCMSK          PCMSK0 = (1<<SONAR_GEP_EchoPin_PCINT); // Mask Pin PCINT5 - all other PIns PCINT0-7 are not allowed to create interrupts!
  #define SONAR_GEP_EchoPin_PCINT_vect     PCINT0_vect  // PCINT0-7 belog PCINT0_vect
  #define SONAR_GEP_EchoPin_PIN            PINB  // PCINT0-7 belong to PINB
#endif


def.h around 1650

Code: Select all

#if defined(SRF02) || defined(SRF08) || defined(SRF10) || defined(SRC235) || defined(TINY_GPS_SONAR) || defined(SONAR_GENERIC_ECHOPULSE) 
  #define SONAR 1
#else
  #define SONAR 0
#endif



sensors.cpp around 1783

Code: Select all

sonarAlt = srf08_ctx.range[0]; //tmp
}

#elif defined(SONAR_GENERIC_ECHOPULSE)

volatile unsigned long SONAR_GEP_startTime = 0;
volatile unsigned long SONAR_GEP_echoTime = 0;
volatile static int32_t  tempSonarAlt=0;
static int32_t sonarPreviousT;


void Sonar_init()
{
  SONAR_GEP_EchoPin_PCICR;
  SONAR_GEP_EchoPin_PCMSK;
  SONAR_GEP_EchoPin_PINMODE_IN;
  SONAR_GEP_TriggerPin_PINMODE_OUT;
  sonarPreviousT=micros();
  Sonar_update();
}

ISR(SONAR_GEP_EchoPin_PCINT_vect) {
  if (SONAR_GEP_EchoPin_PIN & (1<<SONAR_GEP_EchoPin_PCINT)) {
     SONAR_GEP_startTime = micros();
  }
  else {
    SONAR_GEP_echoTime = micros() - SONAR_GEP_startTime;
    if (SONAR_GEP_echoTime <= SONAR_GENERIC_MAX_RANGE*SONAR_GENERIC_SCALE)                                 
      tempSonarAlt = SONAR_GEP_echoTime / SONAR_GENERIC_SCALE;
    else
      tempSonarAlt = -1;
  }
}

void Sonar_update()
{
  sonarAlt=1+tempSonarAlt;

  int32_t currentT = micros();
  int32_t dTime;
  dTime = currentT - sonarPreviousT;
  if (dTime < SONAR_UPDATE_INTERVAL) return;
  sonarPreviousT = currentT;

    SONAR_GEP_TriggerPin_PIN_LOW;
    delayMicroseconds(2);
    SONAR_GEP_TriggerPin_PIN_HIGH;
    delayMicroseconds(10);
    SONAR_GEP_TriggerPin_PIN_LOW;
}

#else

User avatar
jaysonragasa
Posts: 53
Joined: Wed Jan 28, 2015 6:40 am
Location: Philippines
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jaysonragasa »

Sonar HC-SR04 Support now implemented in MW2.4
viewtopic.php?f=8&t=6282&p=62603#p62603

rob222
Posts: 3
Joined: Tue Nov 26, 2013 12:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rob222 »

you have a code ready for my quad please ?

Post Reply