Sonar sensors installation and coding into Pixhawk collision avoidiance

Hello People
I am doing a studies and want to use sonar sensors for distance measurement
and operate a servo which will open as a function of distance. The Quad is equipped with a pixhawk Autopilot
How would you go about incorporating it into the pixhawk firmware, am a newbie.
kind regards

Best way is to start study firmware program code and setup your own development environment for software development. Take a look at our Developer documentation:

Thanks a million jpkh
I will do that, but it seems you guys are way advance, do you
think I can comprehend at that level now, the arduino I can code
but has not done anything on the pixhawk.
Kindest regards

Coding Arduino or Pixhawk aint that much different. Both of them are using language based on C-language. You just need to understand coding methods used on current codebase and how different devices are talking to each other via HAL layers.

Sure it will take awhile as our code is rather high-level, our Devs are really good on programming and optimizing things :slight_smile:

It also depends what you plan to do. There are already some sonar code inside so you might want to look that.

Thanks a lot JPKH, for your encouragement, I will look into that
and thanks for letting me know that it is not too intimidating.
Kindest regards

I am looking to do the same thing. I am an experienced C/C++ developer but I think I need more information on the existing framework. More details below with an initial question.

I am using PixHawk with the MAXBOTIX MB1242 I2C sonar sensors. I have one sensor at 0x70 that is working correctly for ALT-HOLD and other flight modes. I have added four additional sensors at 0x71-0x74.

Question: RangeFinder object seems to detect multiple sensors and choose one as primary for existing functionality. Should I use RangeFinder to retrieve the distances from my other sensors? If not, can you provide some direction on what method should I use to retrieve the distances?


Greetings David,
I am trying to do that but don’t know how to link that to the
pixhawk to do an alt hold or to change sense or path of motion.

To retrieve a distance from the sensor, you trigger the sensor with a
before that you want to precede that with a LOW PULSE so you would be
starting clean, with HIGH PULSE,
so like 1- LOW PULSE delay(2);
2- HIGH PULSE delay(5);
3- LOW PULSE delay(5);

then using the same pin you read the signal which is a high pulse whose
duration is the time in microseconds from sending
the ping to receiving its echo from the object you are measuring. You
divide by two because the ping traveled to the object
and it’s echo traveled back again. to convert to inches you divide by
74, to convert to cm you divide by 29
so its like this
inches = duration/2/74, Note this might be different for your sensor
check your data sheet for the conversion factor,sensitivity to
temp and its effect on speed of sound.
cm = duration/2/29;
so this gives you distance from the object to the sensor.

Now you have four sensors,
Sensor_1 determines if there is enough clearance in front
Sensor_2 determines if there is enough clearance in bottom
Sensor_3 determines if there is enough clearance in Right Side
Sensor_4 determines if there is enough clearance in Left side

except if you are using all sensors as redundant sensors, that is like a
or to compare if the readings are correct. Does it make sense to you?

My question to you is how do you incorporate this into pixhawk, I dont
know yet its lingo.

Thanks for the reply. I am however using I2C to retrieve the distance from the sensors. I have written an arduino application to do this and it works well. I too am trying to learn the ardupilot architecture so that I implement this consistent with the rest of the application. This is the difficult part. It looks like there is a decent design in place but I need some basic information in the documentation to go along with the code. Currently, I am just following the code around to the light comes on in my head!

So far, I have created another library called AP_CollisionAvoidance that is a copy of AP_RangeFinder. My main problem is understanding what layer is reading the values form RangeFinder so I can see how they are using the library. I like the decoupled architecture, but just need information on how the layers interact a little better.

This is all just to read the sensors using the correct architecture. Not sure what I am going to do once I get the values! I am planning on integrating with POS_HOLD and calling existing methods to correct drone path.

hi guys, was anyone able to successfully implement mulitple I2C connected sonars onto Arducopter with Pixhawk? I watched a tutorial on how to do this with arduino - seemed simple enough - but with pixhawk for a quad obstacle avoidance purpose, has anyone had success?

Greetings have you been able to accomplish the objective of using 12C to retrieve distance from your sensors. I am curious how did you accomplish that. I crashed mine, and almost on the verge of doing maiden flight on a new one I just finished.
kindest regards