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
asti
Posts: 12
Joined: Thu Apr 21, 2016 8:56 pm

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

Post by asti »

I have quick questions:
1. In which unit is the sonarAlt value? (cm,inch,something else)
2. Does the echo pin has to be an interrupt pin?
Your code for reading the sonar value did not work for me (it was a constant), but with the code from http://www.instructables.com/id/Non-blocking-Ultrasonic-Sensor-for-Arduino/ I get good values.

Thanks for your work!

asti
Posts: 12
Joined: Thu Apr 21, 2016 8:56 pm

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

Post by asti »

To answer my own question: the unit is in cm.

After understanding your code, I have an issue with it.
I don't understand why you do add the flight mode and box for sonar. What your code does is using the sonar value to improve the estimated altitude. This is an addition/improvement to the baro mode. Then you add the sonar flight mode and box which does in most cases what the baro mode does (lots of #if BARO | SONAR). However, the code line that actually applies the calculated baro pid values to the motor is not executed if only the sonar flight mode is activated (in MultiWii.cpp rcCommand[THROTTLE] = initialThrottleHold + BaroPID;). Thus, to have alt hold with sonar sensor one must activate baro mode as well. And so the sonar mode is first redundant and second does not have an effect in your implementation.

ArduBtSonarWii2022
Posts: 1
Joined: Fri Oct 28, 2022 3:12 am

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

Post by ArduBtSonarWii2022 »

Hi everyone, I find this post very interesting. I am trying to use an Arduino Nano board to run multiwii as flight controller. I have bmp085 barometer and Hc SR04 sonar. I made a few changes to the config.h file to reflect my board and barometer and uploaded the code.

In the GUI, I see 0.00 in ALT all the time, debug1 is constantly at 1, and debug2 changes reading when I move the quad up and down (also even without moving). This is similar to what dryflyer mentioned in a post. I am using trigger pin D12 and Echo pin D2 (this is a hardware interrupt pin on Atmega328p board). Since I am using an arduino nano board, the pins already get power, so I do not have lipo battery connected.

When I comment out the generic sonar in config.h file, I am able to see changing values in ALT, appears the barometer bmp085 is working. When I comment out BMP085, nothing in ALT, debug1 stays at 1 and the other debugs show 0. I tested the hc sr04 unit and confirmed that the sensor is functioning.

Though the discussion is a few years old, hope someone can advise on why the hc sr04 is not working in this code. Thanks a lot!

Post Reply