Attemp to integrate sonar (ultrasonic sensor)

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

doughboy wrote:tinygps only supports 1 ultrasonic sensor (at least last time I checked). atmega328 is a better option. not really worth saving $2 to deal with hassles of programming the attiny chip. or one can just use a promini (about $10 on ebay) and just solder the sensor lines to the board (if you build the board yourself, you will be spending close to $10 already anyway).

------------
Go Giants!


My BAD, it is the I2C Sonar implementation that supports more than one.

viewtopic.php?f=7&t=1282&hilit=sonar+i2c

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by doughboy »

mahowik wrote:
penpen77 wrote:The only erratics values was above 4/5m where tilting correction gave more wobbly readings.

Code: Select all

#ifdef SONAR_TILT_CORRECTION
          float temp = cos(float(angle[1])/10) * cos(float(angle[0])/10);           
       temp = max(temp+0.05, 0.707);
            temp = constrain(temp,0.707,1.0);
            temp = (float)sonarAlt * temp;
            if(temp<sonarAlt) sonarAlt = temp;
     #endif


It doesn't make sense to use tilt correction for sonar because raw data from sonar independent from inclinations in range of 15-25 degrees (depends on sonar ... see doc). If inclination more than sonar visibility you should catch this and switch to baro...

thx-
Alex


BTW, I pointed out elsewhere in this thread that the above sonar tilt correction code is incorrect, as cosine function requires radians and not degrees. Perhaps that is why the code is not working for you (besides it being in the wrong place, which I also pointed out).


------------
Go Giants!

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

this code is not longer used, it has been corrected

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by doughboy »

where is the corrected code?

perhaps I may be missing it, but the only code I know of is the one in magnetron branch. everything else is just a snippet here and a snippet there.
I think it is a good idea to actually check in the official code in the branch so others can get and test it and we are all referring to the same code.

------------
Go Giants!

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by crazyal »

doughboy wrote:where is the corrected code?

viewtopic.php?f=7&t=1033&start=140#p25760

what I am missing, wasn't mahowik going to post his sonar code ?

mahowik
Posts: 332
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

mahowik wrote:
penpen77 wrote:tried on concrete floor, with hcsr 04 and srf08, raw reading wasn't at all the same.... may be at 10cm of the floor with ground multipath reflection, but at 1m and more, raw values aren't the same at all. And with tilting correction, readings became stable and precise even with tilt more than 30°/45°

Ok, i will re-check this, but remember exactly and we agreed with other developers (from russian forum) that is independent from inclination... You can see that sonar has a conus-emitter which generates divergent beam... this why it independent for some angle range...

Yes, I re-tested it (on parquet... about 1m) yesterday and now I'm sure on 100% that is independent from inclination... Probably this because your sonar not in the center of the copter and when you are tilt this you also changes the altitude...

doughboy wrote:BTW, I pointed out elsewhere in this thread that the above sonar tilt correction code is incorrect, as cosine function requires radians and not degrees. Perhaps that is why the code is not working for you (besides it being in the wrong place, which I also pointed out).

I'm not talking about that compensation code correct or not correct... I'm not using it in my implementation because it's doesn't make sense at all...

crazyal wrote:what I am missing, wasn't mahowik going to post his sonar code ?

soon soon... ;)
viewtopic.php?f=7&t=1033&start=140#p25761

thx-
Alex
Last edited by mahowik on Fri Oct 26, 2012 6:25 pm, edited 1 time in total.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by doughboy »

mahowik wrote:I'm not talking about that compensation code correct or not correct... I'm not using it in my implementation because it's doesn't make sense at all...



the trigonometry part makes sense, if you tilt it at an angle, taking cosine on both axes gives you the distance perpendicular to ground.
you are assuming the sound works like light reflection where it will bounce off to a different angle but not back to the source.
or you care to explain why it does not make sense?

------------
Go Giants!

mahowik
Posts: 332
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

doughboy wrote:or you care to explain why it does not make sense?


As I wrote before... You can see that sonar has a conus-emitter which generates divergent beam... this why it independent for some angle range...
and just check raw data form sonar and you will see ;)

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by doughboy »

ok, you lost me there with "conus-emitter". I don't know what that is.
but yes, I checked the raw data and it works fine, just as with penpen77, and the arducopter folks.
so it seems you are alone, perhaps you got a bad sonar.

mahowik
Posts: 332
Joined: Sun Apr 10, 2011 6:26 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by mahowik »

doughboy wrote:perhaps you got a bad sonar.

It seems all my five sonars are broken... :)
Last time: just check raw data from sonar but during the inclination make sure that sonar (but not copter) at the same altitude ;)

p.s. pls. look to the directional diagram (beam pattern) http://jaktek.com/wp-content/uploads/20 ... C-SR04.pdf
It helps to understand why...

jy0933
Posts: 180
Joined: Wed Jun 27, 2012 4:24 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jy0933 »

just a quick quick question.. is the sonar code included in the latest dev yet? it seems there is only i2c sonar.. not the serial ones...

tbx

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

there isn't (at last from what i know) any implementation for serial ultrasonic sensor. Most of serial/rs232/ttl sensor are from maxbotix and there aren't cheap....

for devantech serial, they all have i2c capabilities too if i'm right

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

for "tilting issue"

http://www.robot-electronics.co.uk/htm/sonar_faq.htm
http://www.robot-electronics.co.uk/htm/srf02tech.htm

by the way, it's just a tiny piece of code which is not driver dependant nor feature dependant, so, a #define with some blabla for saying "try, may not work" and that's all. No need to argue, let's looking for feature improvement

jy0933
Posts: 180
Joined: Wed Jun 27, 2012 4:24 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jy0933 »

penpen77 wrote:there isn't (at last from what i know) any implementation for serial ultrasonic sensor. Most of serial/rs232/ttl sensor are from maxbotix and there aren't cheap....

for devantech serial, they all have i2c capabilities too if i'm right


ok.. so probably i have some misunderstanding about type of sonar now..

mine has trig/echo/vcc/gnd.. same as the one used in first post

but mine is HC-SR04
http://www.aliexpress.com/item/HC-SR04- ... 54103.html

thx

crazyal
Posts: 84
Joined: Tue Sep 04, 2012 11:25 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by crazyal »

If anyone is interested I took penpen77 sonar code and implemented it into baseflight on my github branch. https://github.com/luggi/baseflight/tree/sonar
I got rid of the fade part and update Barohome continously when sonar is in range.
Any ideas what flaws this implementation could have ? I couldn't test it outside yet, due to bad weather and snow.

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

updated code to r1240 with sr04/dypme007/echo-trigger generic driver

be carefull, this is my current config.h, so check each define to adapt to your config

if you want to merge change, take care of crappy piece of code added to "emulate" Baro mode presence without baro
Attachments
MultiWii_r1240_sonar.rar
(122.12 KiB) Downloaded 2211 times
Last edited by penpen77 on Mon Nov 05, 2012 10:41 am, edited 1 time in total.

User avatar
KasparsL
Posts: 75
Joined: Wed May 16, 2012 3:31 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by KasparsL »

Why the HC-SR04 sonar is not a option in official dev. versions? It is quite a pain to try to integrate it every time! :x

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

Great work. I've tested the code today and it works very very fine. When I activate the sonar the quad holds the altitude in a perfect way, as a hummingbird. Thanks too much.

Really, somebody should consider including it in next release of Multiwii.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

rortega wrote:Great work. I've tested the code today and it works very very fine. When I activate the sonar the quad holds the altitude in a perfect way, as a hummingbird. Thanks too much.

Really, somebody should consider including it in next release of Multiwii.


Great news, what is the maximum altitude it will hold?

Thank you.

vpb
Posts: 231
Joined: Mon Jul 23, 2012 4:09 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by vpb »

rortega wrote:When I activate the sonar the quad holds the altitude in a perfect way, as a hummingbird. Thanks too much.


Can you share a clip?

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

Here you are the video: http://youtu.be/4fwnxL15yCQ
Sorry about the quality, duration, ... Its all I can record with my smartfone while it is on the ground ...

jy0933
Posts: 180
Joined: Wed Jun 27, 2012 4:24 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jy0933 »

KasparsL wrote:Why the HC-SR04 sonar is not a option in official dev. versions? It is quite a pain to try to integrate it every time! :x


same question here... since sr04 is much cheaper and easier to get... why isnt it in the official fork?

excalibur
Posts: 4
Joined: Sat Nov 24, 2012 9:57 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by excalibur »

Hello,

is it possible to get information about how i can use it with my CRIUS AIO V1.0. The official Trigger Output should be PIN9 i think und the Echo Input PIN10. If i simply change them in def.h nothing happens and in debug mode there are no informations about measering. Must there been additional changes?

Code: Select all

#if defined(SONAR_GENERIC_ECHOPULSE)
  #define SONAR_GEP_TriggerPin             12   <<< PIN9
  #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   <<<< PIN10
  #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


best regards
Christian

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

i'm using a crius for my test, the default def.h I left in the archive is made for crius pinout

excalibur
Posts: 4
Joined: Sat Nov 24, 2012 9:57 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by excalibur »

Hmm, but why doesn't that works for me? Is it right, that in debug-mode Debug1 should show the altitude?

Do you also use the connections marked here as trigger and echo?

Image

Image

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

A new and better video that shows how it flies in ANGLE+SONAR/BARO+MAG mode: https://www.youtube.com/watch?v=eiv7_8GAN20

And this other only with acro mode: https://www.youtube.com/watch?v=J0Mz-SKEU-4

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

Excalibur, I reply to you in the public thread, so it will be usefull for others:

Here you are how it's defined in def.h file:

Code: Select all

#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


So, yo only need to connect Triger to signal pin 12, and Echo to signal pin 11, exactly the pins with same numbers as you see on the board. Also you need to connect vcc and gnd wires to the vcc pin and gnd pin 12 or 11, any of them is the same.

The image linked inside the thread of the forum is not correct, it use other pins. If you use the code above as it is defined, you must use pins 11 and 12 of your board. Thats all.

Don't forget to define generic sonar in config.h:

Code: Select all

/* 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)
      #define SONAR_GENERIC_MAX_RANGE 450


I use 450 cm as max range because this is the value in the specs from the supplier where i bought it.

Sorry for not sending an image of my quad, but I have to disassemble many parts to do a good photo, but if you do what I say it will work nice.

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

Perhaps, if you upload megapirates code to your board you must use "the official pins 9 and 10". But for the code of this thread, use 11 and 12.

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

you can use others pin, but in this case, you'll have to change the other parameters/defs SONAR_GEP_TriggerPin_xxx (bit shifting, interrupt, etc...). The DEFINE list in GENERIC_ECHO_PULSE is made to make this easy: refere to arduino 2560 pinout: 12 is PB6 (so shifting 6), 11 is pb5, pcint5 so pcint0-7 range, so PCINT0/PCIE0 flag. Adapt that with your pin selection (but without overriding pin/io/interrupt used by multiwii "core")

vpb
Posts: 231
Joined: Mon Jul 23, 2012 4:09 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by vpb »

Has someone used more than 1 sonar to implement object avoidance test?

excalibur
Posts: 4
Joined: Sat Nov 24, 2012 9:57 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by excalibur »

Thanks to all, it's working now. Thought that i have to use PIN 9 and 10 in any case. Next step is to get code for autolanding by sonar.

P.S.: Where can i get more information about all the definitions that are used to define the pinouts, so that i would be able to change or add pin's? Are there describtions or tutorials?

Code: Select all

  #define SONAR_GEP_TriggerPin_PIN_HIGH    PORTB |= 1<<6;
  #define SONAR_GEP_TriggerPin_PIN_LOW     PORTB &= ~(1<<6);
  #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

excalibur
Posts: 4
Joined: Sat Nov 24, 2012 9:57 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by excalibur »

Goog morning,

back from testing and several problems detected. First of all, when i arm my copter it goes some times directly in failsafe mode as if there is not enough "power" left for the rest of the board. Sometimes it works. Is it possible that gps and sonar together need to much current?
Next prob was, that as i switched on level+bara it suddenly pitches backward. All qyros, acc and mag were calibrated but he still wants to drift in one direction backwards.

best regards Christian

Paisley
Posts: 3
Joined: Thu Sep 20, 2012 3:55 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Paisley »

Sorry for asking, but I can't find it in the thread... does there exist a working I²C-sonarcode? I really don't like the idea of connecting the sonar directly to the controller and want to use I²C to do so...

eXc
Posts: 8
Joined: Mon Oct 08, 2012 6:51 am
Location: Hamm/Germany

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by eXc »

Hello,

so i have tested a while today because of the goog wheather. But the sonar function isn't realy working with my crius. In the MWC i can see the debugged values but the activated baro function doesn't work.
Do i have to activated angle or horizon as well? Is it normal, that when i have activated baro and then change my throttle stick, that the baro button in MWC blinks?

best regards
Christian

jy0933
Posts: 180
Joined: Wed Jun 27, 2012 4:24 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jy0933 »

GLB gets some TTL serial sonar now...
http://www.goodluckbuy.com/uart-ttl-ser ... dule-.html


i wonder except the connection.. what other differences are between ttl/i2c/ trig-echo?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

jy0933 wrote:GLB gets some TTL serial sonar now...
http://www.goodluckbuy.com/uart-ttl-ser ... dule-.html


i wonder except the connection.. what other differences are between ttl/i2c/ trig-echo?


In other to get this one to work, a serial port or Softserial is required.

kataventos
Posts: 702
Joined: Sun Aug 28, 2011 8:14 pm
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by kataventos »

Hi,

I ordered HC-SR04... this one http://www.goodluckbuy.com/ultrasonic-w ... -sr04.html

Since we are using trigger and echo, almost any ultrasonic sensor with this will do the job, or not?


Cheers,
KV

EDIT: Yes... I saw now that some are already using it! Thanks.

rortega
Posts: 34
Joined: Sat Aug 11, 2012 7:34 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by rortega »

My last video testing POSHOLD and RTH with GPS and serial sonar HC-SR04 integrated in r12340 version.

http://www.youtube.com/watch?v=Jmue9jEYwc8

:D :D :D

simone76
Posts: 1
Joined: Fri Sep 07, 2012 11:19 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by simone76 »

penpen77 wrote:if you want to merge change, take care of crappy piece of code added to "emulate" Baro mode presence without baro


can you point this workaround you added?
tnx

Lithium_Bob
Posts: 1
Joined: Fri Feb 01, 2013 7:17 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by Lithium_Bob »

Hi all. I have been trying without success to get my Crius AIOP V1.1 board going with sonar. I am new to editing code so don't know very much about it. I'm using the HC-SR04 from Hobby King. I'm flying a quad in X mode. Can somebody post a copy of their working sketch please. Thanks in advance.

Lithium Bob.

elriri
Posts: 2
Joined: Thu Feb 07, 2013 11:19 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by elriri »

Hi all. I have been trying without success to get my Crius AIOP V1.1 board going with sonar. I am new to editing code so don't know very much about it. I'm using the HC-SR04 from Hobby King. I'm flying a quad in X mode. Can somebody post a copy of their working sketch please. Thanks in advance.

Lithium Bob.


Same problem for me !

maccoa
Posts: 1
Joined: Sun Feb 17, 2013 1:38 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by maccoa »

hello all. sorry (google translation).

very good topic! I tested "MultiWii_21_1177_sonar" arduino mega 2560 on a HC-SR04 sonar. it works very well.

Now I would like to install it on my FC with the latest version of install multiwii.

I test:
Excalibur, I reply to you in the public thread, so it will be usefull for others:

Here you are how it's defined in def.h file:

CODE: SELECT ALL
#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


So, yo only need to connect Triger to signal pin 12, and Echo to signal pin 11, exactly the pins with same numbers as you see on the board. Also you need to connect vcc and gnd wires to the vcc pin and gnd pin 12 or 11, any of them is the same.

The image linked inside the thread of the forum is not correct, it use other pins. If you use the code above as it is defined, you must use pins 11 and 12 of your board. Thats all.

Don't forget to define generic sonar in config.h:

CODE: SELECT ALL
/* 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)
#define SONAR_GENERIC_MAX_RANGE 450


I use 450 cm as max range because this is the value in the specs from the supplier where i bought it.

Sorry for not sending an image of my quad, but I have to disassemble many parts to do a good photo, but if you do what I say it will work nice.


but it does not work.

someone could explain to me as how to integrate it into the dev version of MultiWii version of r1342 (2013/02/14)

I tried to compare the two versions to see changes, but I admit that it is beyond my compétances

merci :D

jy0933
Posts: 180
Joined: Wed Jun 27, 2012 4:24 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by jy0933 »

when using a no baro board... how do i enable alt hold?

it seems if i turn on baro... it is not really working


thanks

kataventos
Posts: 702
Joined: Sun Aug 28, 2011 8:14 pm
Contact:

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by kataventos »

Hi,

I would too like to have this running on latest developments, is it so much to ask for some organization here?
What about to answer to people requests? I too have an extremely expensive HC SR04 :mrgreen: and I would like to use it sometime.

Even with latest great developments on alt code I think that this continues to be the best and safe/accurate way to auto land. Besides this, I just don´t get why it is not available (HC SR04) on the MW code (even for monitoring proposes) and using dedicated/or other pins for MEGA Boards using trigger and echo, since it is proved to work fine with older MW versions.

I sincerely hope that some of the developers that have done a good job on this in past can answer me and to other users questions, at least to say that no more work should be done on this, then questions will stop or someone can continue with developments! OK?

Tank you.
Cheers,
KV

basti
Posts: 4
Joined: Tue Mar 12, 2013 10:03 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by basti »

I'm just getting into multiwii/multicopters, it's been a bit of a steep learning curve :D

I'm using a hobbyking multiwii board on my tricopter, which has an atmega328p. On this board pin 11 is already used for one of the esc's so I was wondering what would have to be done to this code to use a different pin as the echo?

Code: Select all

#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


I have plenty of pins free as I'm using ppm in.

Ok figured it out after a bit of trial and mostly error :D this is the code segment I used in def.h:

Code: Select all

#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<<4);
  #define SONAR_GEP_TriggerPin_PIN_LOW     PORTB &= ~(1<<4);
  #define SONAR_GEP_EchoPin                8 // using pin 8 instead of 11
  #define SONAR_GEP_EchoPin_PINMODE_IN     pinMode(SONAR_GEP_EchoPin,INPUT);
  #define SONAR_GEP_EchoPin_PCINT          PCINT0  //pin 8 is PCINT0 instead of pin 11 which is 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 PCINT0 - 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


That's in the code from this post viewtopic.php?f=7&t=1033&start=160#p26227

I'm using this board http://www.hobbyking.com/hobbyking/stor ... _Port.html which has pin8 unused as well as spare breakout pads/holes so I soldered a wire from pin8 to one of those.
I also added in some code to make the P term 5x more sensitive when using the sonar for a much more responsive and precise altitude hold indoors. Works like a treat now, with my not so good flying skills I can now hover in my kitchen!

One thing that tripped me up for a few hours was that the sonar didn't work well at all when powering the board from usb - readings all over the place. Fine when a flight battery is connected though so that's good.

Thanks to everyone contributing to this, it's awesome to be able to go in and chop'n'change stuff!

BeringBullet
Posts: 27
Joined: Tue Nov 13, 2012 10:02 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by BeringBullet »

is this a dead topic, I have a HC SR04 would love to use if for more accorded alt data but I see a lot of question and no replays?

basti
Posts: 4
Joined: Tue Mar 12, 2013 10:03 am

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by basti »

What are you wanting to know? I'm fairly new to this stuff so I might not be able to help but I can try :D Mine's all going now, I made some changes to the code and I'm pretty happy with it.

shadow29
Posts: 1
Joined: Sat Apr 06, 2013 6:11 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by shadow29 »

Does anyone have the code for multiwii 2.2? I would be grateful for entire code

penpen77
Posts: 73
Joined: Tue Jan 24, 2012 10:45 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by penpen77 »

Yes, this is a (kind of) dead topic since none of questions about request for feature has been answered.... For now, this is just a fusion/stand alone alt hold usinh baro+sonar or sonar alone

I've join the sketch i'm using, building and tuning are as described before in the thread. Beware of config.h which isn't clean

For any update of incoming version or "nightlybuild", the sonar code (generic driver or alt hold part) isn't intrusive, so a simple diff is enough to merge both sonar code and newly mwc.
Attachments
MultiWii.rar
(132.51 KiB) Downloaded 741 times

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Attemp to integrate sonar (ultrasonic sensor)

Post by copterrichie »

I think this is sad because I have seen some of the videos posted with the sonar working. I can not promise when, but I will try it on one of my copters.

Post Reply