Sonar HC-SR04 Support now implemented in MW2.4

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
schnupperm
Posts: 31
Joined: Tue Jun 25, 2013 1:41 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by schnupperm »

FengShuiDrone wrote: I am wondering why my debug goes to zero when I go higher than 25 or 30 cm. Any ideas on what could be going on? :D

Try to power the sensor from the BEC and not from the FC. The HC-SR04 seems to be power hungry and needs 5V to get good readings. (look here: viewtopic.php?f=7&t=1033&start=250#p52591)

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:33 am, edited 1 time in total.

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:33 am, edited 1 time in total.

Maine_Guy
Posts: 27
Joined: Fri Mar 06, 2015 7:07 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Maine_Guy »

HK sells a nice capacitor that can fix the noise / current draw of the SONAR. Plugs into a spare port and just acts as a buffer for the the 'ping', I assume.

Maine_Guy
Posts: 27
Joined: Fri Mar 06, 2015 7:07 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Maine_Guy »

Not sure if anyone can answer this,

on the Leonardo - I am seeing a conflict in using (in def.h)

Code: Select all

#define SONAR_GEP_EchoPin_PCINT_vect     PCINT0_vect  // PCINT0-7 belog PCINT0_vect


PCINT0_vect is also used as an interrupt for the RX

This is a bit confusing, I assume the SONAR cannot share the interrupt vector as the RX handler? Any other options?

Code: Select all

#define RX_PC_INTERRUPT              PCINT0_vect 


-j

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

Sorry for the late reply; I got no reminder Mail of new answers..

@FengShuiDrone: Glad it works. I Power my hcsr04 with 5Volts from linear BEC. I get a Range up to 2.30 Meters. Maybe a capacitor will help you to smooth out the Switching BEC noise

jaysonragasa wrote:
ItsOnlyMe wrote:Hi Guys,
I finally got the Sonar working on my MultiWii Pro / MTK GPS Controller on PIN 9 and 10. (Pin 11/12 is not available on this board)


What board did you define on config.h?
Does your board has BARO?


Like FengShuiDrone wrote above:

Code: Select all

#define FFIMUv2 // second version of 9DOF+baro board from Jussi, with HMC5883

Beside this I enabled

Code: Select all

#define USE_MSP_WP
#define GPS_SERIAL 2
#define GPS_BAUD   57600
#define NMEA
#define INIT_MTK_GPS


anything other defaults to your sketch from post #1
Last edited by ItsOnlyMe on Mon Apr 20, 2015 6:34 pm, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

FengShuiDrone wrote:I am wondering why my debug goes to zero when I go higher than 25 or 30 cm. Any ideas on what could be going on?

Looks like the same thing I found out. If you go above Sonar range, ALT drops to 0 :?

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:34 am, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

-do you hear a ticking sound from the Sensor?
-Is Mic and Speaker clean and not bent?
-can you power the board with some stabilised 5V source

I just sent you a testcode for the sensor via PM

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:34 am, edited 1 time in total.

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:35 am, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

Hmm, sounds like it's dead. But for $4.00 it's not a big loss :)
Have you tried to fly the Quad? if i switch to ALT hold and get out of sonar range it shoots up with full throttle :roll:

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:36 am, edited 1 time in total.

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:41 am, edited 1 time in total.

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:42 am, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

Sorry I have no Idea why your Sonar shows only a few Centimeters.
But in your video I see the following: As your sonar drops to 0 debug3 also shows 0. I think dbg3 is the actual height of the copter after all calculations.
So if the copter thinks he dropped to 0 Meters he opens full throttle to gain height.

Maybe jaysonragasa can write a few words, to clarify this

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:42 am, edited 2 times in total.

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:42 am, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

I'm just curious if you get more out of it after you convert to feet. If it "hears" only about 40 cm it should just read up to 1 feet (just my thought)
I tested mine a while ago with another project. Up to 2.5 - 3 Meters it was accurate, after that i got more and more random numbers.
this aren't precision instruments for around $4.00 :D :D

FengShuiDrone wrote:Mine doesn't do the full throttle thing that you describe. Also, Jayson states earlier in this thread that debug 1 is the sonar
debug 2, barometer
debug 3 and 4 will only activates once it is armed and switched sonar mode.
Debug 3 is the estimated height between baro and sonar and debug 4 is throttle.

Funny thing with mine is debug 3 comes on if it is armed or not and debug 4 never shows anything even when armed and throttled up a little. :roll: :shock: :o :?


You are right. stange, very strange. maybe the port change confuses the processor or there is a glitch somewhere in the Software. :?
In the next days I will redownload the sketch from post #1 and start from scratch

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:43 am, edited 1 time in total.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

I tested in cm. But why would you convert to feet? 13 ft ~ 4 m ( at least my calculator says :D )

The Sensor itself knows only time, not distance. It sends out a ping and waits for the echo.
Formula is distance = elapsed time / speed of sound. (very simplified, pls. someone correct this :ugeek: )
For speed of sound you can use m / ft / yd / mls / ?? but it must fit the rest of your code.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

So today I:
did a EEPROM clear,
downloaded the Sketch from post #1,
and made this changes:

Code: Select all

#define FFIMUv2         // second version of 9DOF+baro board from Jussi, with HMC5883       <- confirmed by Alex
#define MINTHROTTLE 1064 // special ESC (simonk)
#define ALLOW_ARM_DISARM_VIA_TX_YAW
#define EXTENDED_AUX_STATES

and disabled

Code: Select all

//#define GYRO_LPF_42HZ

Beside this I changed trigg/echo to pin 9/10.

Still no joy, same behavior than before :o
It holds the height very well up to 2 meters, over that it shoots up, and the ALT readout in my OSD drops to 0
for the next test I only disabled the Sonar

Code: Select all

//#define SONAR_GENERIC_ECHOPULSE 

and reloaded the code:
I got a ALT reading in my OSD and it holds the height like it should.

I'm stuck at this point :(

ste23s
Posts: 2
Joined: Mon May 04, 2015 11:34 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ste23s »

Hi I don't find the line indicated in this pice of code:

Sensors.cpp

Code: Select all

// ************************************************************************************************************
// I2C Sonar SRF08
// ************************************************************************************************************
// first contribution from guru_florida (02-25-2012)
//
#if defined(SRF02) || defined(SRF08) || defined(SRF10) || defined(SRC235)
   .
   .
   .
   .
   .
#else
   void Sonar_init() {}
   void Sonar_update() {}
#endif


I have this code in my Sensors.cpp file:

Code: Select all

// ************************************************************************************************************
// I2C Sonar SRF08
// ************************************************************************************************************
// first contribution from guru_florida (02-25-2012)
//
#if defined(SRF02) || defined(SRF08) || defined(SRF10) || defined(SRC235)

// the default address for any new sensor found on the bus
// the code will move new sonars to the next available sonar address in range of F0-FE so that another
// sonar sensor can be added again.
// Thus, add only 1 sonar sensor at a time, poweroff, then wire the next, power on, wait for flashing light and repeat
#if !defined(SRF08_DEFAULT_ADDRESS)
  #define SRF08_DEFAULT_ADDRESS (0xE0>>1)
#endif

#if !defined(SRF08_RANGE_WAIT)
  #define SRF08_RANGE_WAIT      70000      // delay between Ping and Range Read commands (65ms is safe in any case)
#endif

#if !defined(SRF08_RANGE_SLEEP)
  #define SRF08_RANGE_SLEEP     5000       // sleep this long before starting another Ping
#endif

#if !defined(SRF08_SENSOR_FIRST)
  #define SRF08_SENSOR_FIRST    (0xF0>>1)  // the first sensor i2c address (after it has been moved)
#endif

#if !defined(SRF08_MAX_SENSORS)
  #define SRF08_MAX_SENSORS     4          // maximum number of sensors we'll allow (can go up to 8)
#endif


.
.
.


I don't understand where i have to put this code:

Code: Select all

#if defined(SONAR_GENERIC_ECHOPULSE)
// ************************************************************************************************************
// Generic Sonar Support
// ************************************************************************************************************
volatile unsigned long SONAR_GEP_startTime = 0;
volatile unsigned long SONAR_GEP_echoTime = 0;
volatile static int32_t  tempSonarAlt = 0;

void Sonar_init() {
   SONAR_GEP_EchoPin_PCICR;
   SONAR_GEP_EchoPin_PCMSK;
   SONAR_GEP_EchoPin_PINMODE_IN;
   SONAR_GEP_TriggerPin_PINMODE_OUT;
}

uint8_t Sonar_update() {
   sonarAlt = 1 + tempSonarAlt;
   SONAR_GEP_TriggerPin_PIN_LOW;
   delayMicroseconds(2);
   SONAR_GEP_TriggerPin_PIN_HIGH;
   delayMicroseconds(10);
   SONAR_GEP_TriggerPin_PIN_LOW;

   return sonarAlt;
}

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;
   }
}
#else
void Sonar_init() {}
uint8_t Sonar_update() {}
#endif


someone can help me? realy i don't understand! thank you!

ps: i'm using multiwii 2.4

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by jaysonragasa »

I uploaded the whole source code on my very first post. Check that out.

Maine_Guy
Posts: 27
Joined: Fri Mar 06, 2015 7:07 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Maine_Guy »

An update - getting the Leonardo / HK (ATmega32U4) board to share an interrupt vector is not trivial. I gave up.

What board do people recommend to allow for sonar use?


Maine_Guy wrote:Not sure if anyone can answer this,

on the Leonardo - I am seeing a conflict in using (in def.h)

Code: Select all

#define SONAR_GEP_EchoPin_PCINT_vect     PCINT0_vect  // PCINT0-7 belog PCINT0_vect


PCINT0_vect is also used as an interrupt for the RX

This is a bit confusing, I assume the SONAR cannot share the interrupt vector as the RX handler? Any other options?

Code: Select all

#define RX_PC_INTERRUPT              PCINT0_vect 


-j
Last edited by Maine_Guy on Wed May 27, 2015 5:58 pm, edited 1 time in total.

ste23s
Posts: 2
Joined: Mon May 04, 2015 11:34 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ste23s »

jaysonragasa thank you for the answer! I have one more think to ask you. I have this board (http://www.hobbyking.com/hobbyking/stor ... _V2_0.html)
What i have to change for integrate your software into my bord? What did you change over the sonar? If i'll upload that multiwii and i'll chang the usual line (that specify the model of the board) in config.h, my quadcpter will fly normaly? :?

Obviously, then i have also to set the configuration of the quadcopter with the GUI!

Ph4nt0m
Posts: 2
Joined: Wed May 20, 2015 9:18 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Ph4nt0m »

Can anybody tell me how connect the HC-SR04 sonar by a serial port and how use it?

diego9313
Posts: 1
Joined: Wed May 27, 2015 10:17 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by diego9313 »

Can anybody show a video of the performance of the sonar flight mode?

Thanks in advance.

Maine_Guy
Posts: 27
Joined: Fri Mar 06, 2015 7:07 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Maine_Guy »

Finally got this working!! Had to upgrade my board to a HK version of the AIOP. I even get the SONAR readings back at the transmitter via telemetry now (thru hijacked RPM telemetry output).

I had issues getting the SONAR board powered. Thru the USB port - it would not work. The only way to get it working was to directly power the 'output pins' on the AIOP via a stable SBEC. I also added a nice capacitor on the output pins to allow for a buffer for current draw.

Thanks for your coding efforts on this. I learned a ton along the way - and am so glad to have a working SONAR.

When I get it all flying again, I'll post a video of my airplane. Somewhat unconventional - a terrain-hugger.

-j

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

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by FengShuiDrone »

.
Last edited by FengShuiDrone on Fri Aug 07, 2015 1:43 am, edited 1 time in total.

Maine_Guy
Posts: 27
Joined: Fri Mar 06, 2015 7:07 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Maine_Guy »

Trying to piece together how this code works a bit more. When I output the SONAR altitude in my taranis display, the numbers seem off, although the altitude in MultiWiiconf seems accurate.

in the code that calls the sonar I see this - this does not seem to match my understanding of turning the cycle time into a distance reading,

in Sonar_update()

Code: Select all

tempSonarAlt = SONAR_GEP_echoTime / SONAR_GENERIC_SCALE;


this variable is then passed to:


Code: Select all

sonarAlt = 1 + tempSonarAlt;


why do this? ^^^


The manual for my SONAR says:
(high level time * sound velocity (340M/S) / 2.


Although I guess a reasonable shortcut is to just divide by 58.. still, why add 1 later?



Any insight would be appreciated.

kasiviswanadh
Posts: 2
Joined: Thu Jun 11, 2015 5:21 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by kasiviswanadh »

hi jayson ,i made small changes to your code to work in atmega328(crius se 2.5)

Code: Select all

#pragma region GENERIC SONAR SUPPORT
#if defined(SONAR_GENERIC_ECHOPULSE)
#define SONAR_GEP_TriggerPin             SONAR_GENERIC_TRIGGER_PIN
#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                SONAR_GENERIC_ECHO_PIN
#define SONAR_GEP_EchoPin_PINMODE_IN     pinMode(8,INPUT);
#define SONAR_GEP_EchoPin_PCINT          PCINT0
#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
#pragma endregion



changed pb6 to pb4 and pcint5 to pcint0, im getting sonar readings in debug1 but altitude not getting locked.
help me

manuel.roccon
Posts: 2
Joined: Tue Jun 23, 2015 10:41 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by manuel.roccon »

Hi,
iv'è a Multiwii pro board buy from hobbyking and i buy HC-SR04 sonar and download sonar generic fw in this post.

I've modify in the config.h

#define SONAR_GENERIC_TRIGGER_PIN 9 // motor 12
#define SONAR_GENERIC_ECHO_PIN 10 // motor 11

and in the def.h i edit this entry

#define SONAR_GEP_TriggerPin_PIN_HIGH PORTH |= 1<<6; // WAS: PORTB |= 1<<6;
#define SONAR_GEP_TriggerPin_PIN_LOW PORTH &= ~(1<<6); // WAS: PORTB &= ~(1<<6);
#define SONAR_GEP_EchoPin_PCINT PCINT4 // WAS: PCINT5

In the MultiwiiConfig i read the debug value and the sonar value seems ok, but if i active only angle + sonar the sonar not hold the altitudine, i've try in about 1 m.

Someone have any ideas or is in my same situation or if possible get me the right edit version of firmware for this board?

Thanks

Manuel

User avatar
wetzel
Posts: 4
Joined: Mon Jun 29, 2015 8:11 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by wetzel »

I've modify in the config.h

#define SONAR_GENERIC_TRIGGER_PIN 9 // motor 12
#define SONAR_GENERIC_ECHO_PIN 10 // motor 11

and in the def.h i edit this entry

#define SONAR_GEP_TriggerPin_PIN_HIGH PORTH |= 1<<6; // WAS: PORTB |= 1<<6;
#define SONAR_GEP_TriggerPin_PIN_LOW PORTH &= ~(1<<6); // WAS: PORTB &= ~(1<<6);
#define SONAR_GEP_EchoPin_PCINT PCINT4 // WAS: PCINT5


That code is working on my HK Multiwii Pro an my second SR-04.
I figured out that my first Sonar works only if the Motors are turned off.
If the Motors are running the SR04 measure only between 0-50cm.
Above 50cm i get jumping values. So i bought a new Sr-04.

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

Hi wetzel,
Glad this code works for you.
Have you tested ALT Hold above sonar range?

User avatar
wetzel
Posts: 4
Joined: Mon Jun 29, 2015 8:11 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by wetzel »

Have you tested ALT Hold above sonar range?


Hey ItsOnlyMe,

no i haven't tested it.
But i will and post the result ;)
Above 400cm the Code uses only the Baro,i think it should work.

User avatar
wetzel
Posts: 4
Joined: Mon Jun 29, 2015 8:11 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by wetzel »

ItsOnlyMe wrote:Hi wetzel,
Glad this code works for you.
Have you tested ALT Hold above sonar range?


Did you solve the Problem with the Full Throttle issue up to 4xx cm?
There is might be a logical issue in the Code. I can't see anything that is preventing from the situation, that Sonar Value is 0 an the Copter is up to 401 CM.

In Case we use BARO && Sonar and the "ALT_TO_HOLD" is set to 3.80m and the Copter raises up to 4.XX because of anything, ..., the sonarAlt will drop to ZERO. That is waht i see on my Sonar Values...
If that happens, the Code will Calculate wrong Values because the first IF clause becomes true.

OR DID I MISS SOMETHING?

Code: Select all

if (sonarAlt < SONAR_BARO_FUSION_LC) {
      alt.EstAlt = alt.EstAlt * SONAR_BARO_LPF_LC + (BaroHome + sonarAlt) * (1 - SONAR_BARO_LPF_LC); // additional LPF to reduce baro noise (faster
}


alt.EstAlt = 3.80 * 0,9 + (0+0)*01 = firstloop( 34cm) second lop (0,3cm)...0cm

best regards

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

OR DID I MISS SOMETHING?

I'm no programmer but I think your finding in the code is correct.
at least it is consistent with my experience. (watch my videos at the end of page #1)

I am very curious about your test-results. :!:

i tried to set

Code: Select all

#define SONAR_GENERIC_MAX_RANGE 100

down to only 1m but for the heck of it I can't sort out the full throttle thingy.
So I'm using it with Sonar disabled for now. :roll:

ItsOnlyMe
Posts: 18
Joined: Sat Apr 18, 2015 4:25 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ItsOnlyMe »

ItsOnlyMe wrote:I am very curious about your test-results. :!:

Any news here?

User avatar
wetzel
Posts: 4
Joined: Mon Jun 29, 2015 8:11 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by wetzel »

Yes i got the Same issue like you.
HC-SR04 isn't able to detect distance between 0-2 cm.
So i made a small change in The Code, and i will test it at weekend.

The Change did something like they trying to do here

Code: Select all

   // limit sonar altitude 
   /*if (sonarAlt > SONAR_MAX_HOLD) {   
   sonarAlt = SONAR_MAX_HOLD;
   }*/


I can't fly at the moment because of bad weather

in imu.cpp

Code: Select all

#elif SONAR && BARO
   if (sonarAlt < SONAR_BARO_FUSION_LC) {


Code: Select all

#elif SONAR && BARO
        //SonarAlt Value have to be between 2 cm - and SONAR_BARO_FUSION_LC for Sonar only.
   if (sonarAlt < SONAR_BARO_FUSION_LC && sonarAlt>1) {

ravid824
Posts: 26
Joined: Wed Jul 29, 2015 3:59 am
Location: California

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ravid824 »

Hi Jason
Thank you for sharing the code. Great work.
I managed to compile the code base on the instructions you provided.
However, later I compared the instructions to the download code you shared and I can see few differences.
Can you please help and direct to what is correct?
**************************************
**************************************
config.h > Line 1206
In forum link Instructions:
#pragma region GENERIC SONAR
/* Generic sonar: hc-sr04, srf04, dyp-me007, all generic sonar with echo/pulse pin
default pulse is PH6/12, echo is PB4/11 */
Reference code:
Missing: #pragma region GENERIC SONAR. It is important?

**************************************
**************************************
Multiwii.cpp > Setup > line 715
In forum link Instructions:
#if SONAR
calibratingS = 10;
#endif
Reference code: value is 200

**************************************
**************************************
Multiwii.cpp > line 1370
In forum link Instructions:
#if (BARO || SONAR) && (!defined(SUPPRESS_BARO_ALTHOLD)) //in Ref code #if BARO && (!defined(SUPPRESS_BARO_ALTHOLD))???
//Smooth alt change routine , for slow auto and aerophoto modes (in general solution from alexmos). It's slowly increase/decrease
//altitude proportional to stick movement (+/-100 throttle gives about +/-50 cm in 1 second with cycle time about 3-4ms)
if (f.BARO_MODE || f.SONAR_MODE) {
static uint8_t isAltHoldChanged = 0;
static int16_t AltHoldCorr = 0;
Reference code:
#if BARO && (!defined(SUPPRESS_BARO_ALTHOLD))
/* Smooth alt change routine , for slow auto and aerophoto modes (in general solution from alexmos). It's slowly increase/decrease
* altitude proportional to stick movement (+/-100 throttle gives about +/-50 cm in 1 second with cycle time about 3-4ms)
*/
if (f.BARO_MODE) {
static uint8_t isAltHoldChanged = 0;
static int16_t AltHoldCorr = 0;

************************************
Multiwii.cpp > Original 2.4 code has the following lines
if (rcOptions[BOXARM] == 0) f.OK_TO_ARM = 1;
#if !defined(GPS_LED_INDICATOR)
if (f.ANGLE_MODE || f.HORIZON_MODE) {STABLEPIN_ON;} else {STABLEPIN_OFF;}
#endif
Reference code:
These lines were eliminated, why?

**************************************
**************************************
IMU.cpp > line 295
In Forum link Instructions:
#if BARO | SONAR
static int32_t BaroHome = 0;
#endif
Reference code:
#if BARO || SONAR
static int32_t BaroHome = 0;

**************************************
**************************************
Types.h > line 141
Forum link Instructions:
#if BARO || GPS || SONAR // SONAR update In reference code #if BARO || GPS
uint8_t THROTTLE_IGNORED : 1; // If it is 1 then ignore throttle stick movements in baro mode;
#endif
Reference code:
#if BARO || GPS
uint8_t THROTTLE_IGNORED : 1; // If it is 1 then ignore throttle stick movements in baro mode;
#endif
**************************************
**************************************

mabaka
Posts: 21
Joined: Thu Oct 17, 2013 12:03 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4 -> optimi

Post by mabaka »

Hi, jaysonragasa.

Impressive work! I've implemented it on a Crius AIO and expect to test in field shortly.

I observe that in your ISR for the echo pulse you use 32 bits arithmetic. The problem is in the division. If you inspect the resultant assembly code ('avr-objdump -dS sensors.cpp.o') you get the following:

Code: Select all

         tempSonarAlt = SONAR_GEP_echoTime / SONAR_GENERIC_SCALE;
  86:   60 91 00 00    lds   r22, 0x0000
  8a:   70 91 00 00    lds   r23, 0x0000
  8e:   80 91 00 00    lds   r24, 0x0000
  92:   90 91 00 00    lds   r25, 0x0000
  96:   2a e3          ldi   r18, 0x3A   ; 58
  98:   30 e0          ldi   r19, 0x00   ; 0
  9a:   40 e0          ldi   r20, 0x00   ; 0
  9c:   50 e0          ldi   r21, 0x00   ; 0
  9e:   0e 94 00 00    call   0   ; 0x0 <__vector_9>
  a2:   c9 01          movw   r24, r18
  a4:   da 01          movw   r26, r20
  a6:   00 c0          rjmp   .+0         ; 0xa8 <__vector_9+0xa8>
      else


There appears, as expected, a 'call' to a 32 bits division in a library. Probably it is reentrant, but is going to consume time (and intrrupts are not enabled).

There is a simple workaround: move the division away from the ISR, to SonarUpdate():

Code: Select all

void Sonar_update() {
   sonarAlt = 1 + tempSonarAlt / SONAR_GENERIC_SCALE;   // moved 32 bits division here away from ISR below
   SONAR_GEP_TriggerPin_PIN_LOW;
   delayMicroseconds(2);
   SONAR_GEP_TriggerPin_PIN_HIGH;
   delayMicroseconds(10);
   SONAR_GEP_TriggerPin_PIN_LOW;
}

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;   // avoid 32 bits division in ISR
      else
         tempSonarAlt = -1;
   }
}

omnibusor
Posts: 2
Joined: Thu Aug 13, 2015 4:45 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by omnibusor »

I put your whole code in Crius AIO PRO

just changed Crius_se -> Crius AIO PRO

When I saw your video above, I saw changing debug[3]

Did you just put your code

debug[0] = sonarAlt;
debug[1] = BaroAlt;
debug[2] = alt.EstAlt;
debug[3] = rcCommand[THROTTLE]; //just I added in your code

But it is definitely wrong

as your video, before I turn on Sonar, rcCommand[THROTTLE] must not be changed

But mine is changing

omnibusor
Posts: 2
Joined: Thu Aug 13, 2015 4:45 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by omnibusor »

I can not see anything about debug[3]

The only thing about debug in your code
debug[0] = sonarAlt; // raw sonar altitude
debug[1] = BaroAlt; // barometer altitude
debug[2] = alt.EstAlt;

So, I put debug[3]= rcCommand[THROTTLE];

in your code and it is not working

debug 3 and 4 will only activates once it is armed and switched sonar mode

and I connected battery also,

but debug 4 is still working even it is disarmed and switched off sonar mode

and debug 4 = rcCommand[THROTTLE] doesn't properly work

User avatar
Shadow-Master
Posts: 2
Joined: Thu Aug 13, 2015 6:39 pm
Location: Tenerife, Canary Islands, Spain

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by Shadow-Master »

Hi guys could do with some help here im trying to upload the sketch from post 1 and im getting this error not sure what to do


Sensors.cpp.o: In function `__vector_3':
C:\Users\Chris\AppData\Local\Temp\build4712706684594701496.tmp/Sensors.cpp:1538: multiple definition of `__vector_3'
RX.cpp.o:C:\Users\Chris\AppData\Local\Temp\build4712706684594701496.tmp/RX.cpp:204: first defined here
collect2.exe: error: ld returned 1 exit status
Error compiling.

virus989898
Posts: 1
Joined: Sun Aug 16, 2015 1:18 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by virus989898 »

Hi!
wetzel wrote:Yes i got the Same issue like you.
So i made a small change in The Code, and i will test it at weekend.
[/code]

How went the tests? I've got the same issue, up to 1 meter working sonar with anti vibration foam. Above that height I have to disable sonar because of random numbers.
Is your new sonar with code fixes working well? Was it worth to buy a new one?

iDaniel
Posts: 11
Joined: Mon Dec 15, 2014 9:51 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by iDaniel »

Hi,
i would like to test some of your code with the SRf08 Sensor. I have used the existing code for the srf08 and your code for activating and deactivating the sonar. It works in the gui, but when i turn on my radio the sensor don´t meassure and an i2c error is shown, but the i2c GY-521 Gyro works...i turn of the radio an everything is fine again :D
I use an Arduino uno, GY-521 Gyro, SRF08, ext. Pullups and an FRSky X8R with normal wiring, nor serial sum ppm.
Does anybody know why this happens or had had the same problem?
thanks
Daniel

iDaniel
Posts: 11
Joined: Mon Dec 15, 2014 9:51 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by iDaniel »

Ok i have tested now the same code, same frsky receiver, same sensor but another arduino(pro mini) and GY-88 Sensorboard. I works without i2c errors ... so i don´t know why, because of the other arduino or the sensorboard... but i did not want to use the uno anyway on my copter. So i won´t figure out why anymore :D

ameliacv
Posts: 2
Joined: Mon Mar 21, 2016 6:52 am

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by ameliacv »

i'm using multiwii Crius AIOP V2.0,I want to use more than one sensor, I tried using pins 10 and 9, 8 and 7 but can not work. please help
Attachments
multiwiikuuu.PNG
(246.12 KiB) Not downloaded yet

thalespi
Posts: 1
Joined: Fri Jul 15, 2016 8:09 pm

Re: Sonar HC-SR04 Support now implemented in MW2.4

Post by thalespi »

I am trying to activate the sonar on a MultiWii Pro board, FFIMUv2.
I have checked everything and all I get when debugging is 1 as the value on GUI.
Could someone help me ?

Edit:
Changed to 9 and 10 pins and it worked.
I was trying with 7 and 8.
It was supossed to work too, right ?

Post Reply