ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

PX4: add support for Sonar

Open rmackay9 opened this issue 13 years ago • 25 comments

rmackay9 avatar May 07 '13 05:05 rmackay9

Hi, Tridge and rmackay9. Related to APM 2.0 board, and in order to help automatic landings on wing fixed planes, specially on the last phase of the approach, I would like to use MaxBotix sonar, mantaining the telemetry, of course. I know that sonar is for copters and telemetry is for planes, mainly, but I need both, if possible. Best regards, Frederico Fernandes

frednfernandes avatar May 19 '13 18:05 frednfernandes

Can I assume sonar is still not added to pixhawk/px4 yet?

JamesxL avatar Feb 26 '14 00:02 JamesxL

James, That's right. It's still not working for Pixhawk/px4 on AC. I think arduplane works but not many plane users use sonar anyway.

rmackay9 avatar Feb 26 '14 00:02 rmackay9

thanks Randy, We have a competition coming up. It will be quite helpful to have sonar(than going back to mega). I wonder if by any chance, if there is one, I can test the alpha code for sonar on px4/pixhawk.

JamesxL avatar Feb 26 '14 00:02 JamesxL

I just have so many things on my plate at the moment I really can't make any promises except that it'll be working for AC3.2. There are other devs here and there and I'm happy to give advice to any code who wants to take on the challenge of getting it working.

rmackay9 avatar Feb 26 '14 01:02 rmackay9

we can give it a shot if we know where to look into.

JamesxL avatar Feb 26 '14 01:02 JamesxL

James, I think the issue may just be that the analog pin is always defaulting to zero which is not a valid pin on the Pixhawk. Adding a line like below to the APM_Config.h might work but of course "0" would need to be replaced by the correct number. #define CONFIG_SONAR_SOURCE_ANALOG_PIN 0

This wiki page has some information on the ADC (which should work) and output pins (which won't work). I'm really not sure what the pin value should be though. http://copter.ardupilot.com/wiki/common-pixhawk-overview/ Maybe looking at the arduplane software might help although they use a different sonar library. The plan is eventually for AC to move to use the same library as plane.

rmackay9 avatar Feb 26 '14 01:02 rmackay9

would it be possible that it was defined here and caused the problem

https://github.com/diydrones/ardupilot/blob/master/ArduCopter/config.h

#elif CONFIG_SONAR_SOURCE == SONAR_SOURCE_ANALOG_PIN

ifndef CONFIG_SONAR_SOURCE_ANALOG_PIN

define CONFIG_SONAR_SOURCE_ANALOG_PIN 0

endif

therefore the pin is initialed to pin 0

also, I seem to saw commit that enables digi_output pins (50-55) to GPIO to use as ADC as well?

JamesxL avatar Feb 26 '14 02:02 JamesxL

James, Yes, that could be the issue. It's hardcoding the pin number to 0 when it needs to be something else for PX4/Pixhawk. The #define in the APM_Config.h that I mentioned in my previous comment will do the same thing as changing the #define in config.h.

rmackay9 avatar Feb 26 '14 03:02 rmackay9

I understand that putting it over in apm_config will do a private fix. I was thinking what if putting CONFIG_SONAR_SOURCE_ANALOG_PIN as a parameter to store in eeprom and made it possible to change. Maybe it will solve the problem? ( I am probably wrong since I have not dug into the code yet. )

thank you

JamesxL avatar Feb 26 '14 03:02 JamesxL

Yes, that would work too. I think the sonar library that ArduPlane uses allows setting of the pin through a parameter

rmackay9 avatar Feb 26 '14 05:02 rmackay9

I tested changing the analog pin hardcoded in 3.1.1 but it did not work for me. That seems to be not the only thing that must be done to activate sonar on pixhawk. http://ardupilot.com/forum/viewtopic.php?f=21&t=5733

Gregmaan avatar Feb 26 '14 07:02 Gregmaan

I have some problem with toolchain that my build with unmod code does not run on pixhawk. before I get it working correctly @Gregmaan can you try putting these two line in apm_config.h and see if it work.

#define CONFIG_SONAR_SOURCE SONAR_SOURCE_ANALOG_PIN //force source be analog pin #define SONAR_SOURCE_ANALOG_PIN 15 //force pin to 15(airspeed pin)

JamesxL avatar Feb 26 '14 20:02 JamesxL

Tested it with latest code: It still does not work on Console (test/sonar)

Gregmaan avatar Feb 27 '14 12:02 Gregmaan

Randy, just for clarification. current code(3.1.2) has sonar working on APM2.5 right? (just to make sure it is specific to pixhawk hw.)

JamesxL avatar Feb 28 '14 00:02 JamesxL

James, yes.

rmackay9 avatar Feb 28 '14 00:02 rmackay9

I got mine working today. Checked with "test" "sonar" in CLI mode.

sonarrange and sonarvoltage are not being updated though mavlink as far as I can tell??? Thus Mission planner doesn't display these values, just 0. Would love to get this working..*

APM_Config.h

... #define CONFIG_SONAR_SOURCE_ANALOG_PIN SONAR_SOURCE_ANALOG_PIN #define PIXHAWK_SONAR_K_FACTOR 2.79 //Needed to have sonar scale properly.. Something on pixhawk ADC code scales this off. Adjust to scale properly #define SONAR_SOURCE_ANALOG_PIN 15 //Pixhawk ADC 6.6 Center pin on DF13-3 plug

sensors.pde

static int16_t read_sonar(void) { ... int16_t temp_alt = sonar->read(); temp_alt = temp_alt / PIXHAWK_SONAR_K_FACTOR; //Added K Factor because pixhawk does something weird to ADC pins and scales off.. ?? .... .... }

ghost avatar Mar 16 '14 01:03 ghost

Fantastic. I totally missed cli sonar test. Will take a look at it tomorrow and see if I can figure out scaling.

Have you tried if it actually work as function yet? On Mar 15, 2014 8:53 PM, "Supergoldfish" [email protected] wrote:

I got mine working today. Checked with "test" "sonar" in CLI mode.

sonarrange and sonarvoltage are not being updated though mavlink as far as I can tell??? Thus Mission planner doesn't display these values, just 0. Would love to get this working..* APM_Config.h

... #define CONFIG_SONAR_SOURCE_ANALOG_PIN SONAR_SOURCE_ANALOG_PIN #define PIXHAWK_SONAR_K_FACTOR 2.79 //Needed to have sonar scale properly.. Something on pixhawk ADC code scales this off. Adjust to scale properly #define SONAR_SOURCE_ANALOG_PIN 15 //Pixhawk ADC 6.6 Center pin on DF13-3 plug sensors.pde

static int16_t read_sonar(void) { ... int16_t temp_alt = sonar->read(); temp_alt = temp_alt / PIXHAWK_SONAR_K_FACTOR; //Added K Factor because pixhawk does something weird to ADC pins and scales off.. ?? .... .... }

Reply to this email directly or view it on GitHubhttps://github.com/diydrones/ardupilot/issues/253#issuecomment-37745498 .

JamesxL avatar Mar 16 '14 01:03 JamesxL

Supergoldfish, I used your snippet to get my sonar working in the CLI, but the scaling seems to have no effect. I can return a static value or a defined value in the read_sonar function and it has no effect on the sonar range. My min distance is 55cm +- 5cm.

steveomg avatar May 14 '14 00:05 steveomg

steveomg> Just for clarification; your CLI test report isn't changing from ~55cm reguardless of returning a static value from read_sonar()? that's odd.. Check your datalog under CTUN. Any changes happening there? (I think you will have to arm to start datalogging).

if you wan't to give it a try, I just updated my ardupilot\sonar2 branch to current master with a much better analog sonar alternative that you can tweek with mission planner. Check the commit notes for tips. (Pretty much makes it same as plane/rover)

Or you can cherry pick the commits into your current working fork.

The above worked for me, but I have abandoned Sonar as it is rubbish for anything other than hard concrete or smooth surfaces. Cant track on grass worth beans. (at least I couldn't) Waiting for the new Lidar to become available to start playing with.

Or hang tight, I believe the DEV team will have this fixed up rather shortly.

ghost avatar May 14 '14 02:05 ghost