Hi everyone,

Does anyone have experience with configuring their own sensors onto the Pixhawk (I have an IRIS)? I purchased a gas sensor that I'd like to wire to it, does anyone know of any good resources to help me get started? I've downloaded the PX4 development environment from the Pixhawk developers website and have gone through some of the "getting started" instructions on there, I have been able to install the firmware through Eclipse as opposed to the Mission Planner. I also purchased 3-position and 5-position connectors for the analog inputs.

I was also wondering if there is a way to view the telemetry log data on Mission Planner during flight? If I did add this sensor, would data be saved through the telemetry logs or dataflash logs?

I appreciate whatever help I can get, thank you!


Views: 14495

Reply to This

Replies to This Discussion

Hi Kate,

This is not exactly what you want but it may give you some ideas. It is an example of using an external Arduino (Nano) to collect data from a number of sensors and communicate the data back to the APM 2.5 via i2c where it is logged (dataflash).


I have not done a lot with the telemetry logs but I suspect that the easiest option would be to substitute your data for some existing parameter that is passed over the telemetry link.

I will have a go at compiling this example with the Pixhawk and report back

I had a bit of a play on the Pixhawk and managed to view live data from the ADC pins (13 in example) via telemetry. I simply read the adc pin and assigned the value to sonar_alt. I also altered the mavlink code to transmit sonar_alt even when it is disabled in the parameters. I could view the value live in MP as sonarrange and it is also available as mavlink_rangefinder_t.distance in the tlog. I note that the same message records the sonarvoltage parameter as well so you could potentially pass two separate readings by overriding this message.

GCS_Mavlink.pde (starting at line 486 in current master):

static void NOINLINE send_rangefinder(mavlink_channel_t chan)
// exit immediately if sonar is disabled
if (!g.sonar_enabled) {
// return; commented out this line to force message to be sent
mavlink_msg_rangefinder_send(chan, sonar_alt * 0.01f, 0); // you can scale the parameters as you need. The 3rd param is supposed to be sonar voltage (not used in this code and is 0) and could potentially be another variable


AP_HAL::AnalogSource *test_adc;

void userhook_init()

test_adc = hal.analogin->channel(13);


void userhook_MediumLoop()
// put your 10Hz code here

Don't forget to uncomment USERHOOK_INIT and USERHOOK_MEDIUMLOOP in APM_Config.h

Wow, this is great. Can't thank you enough for this.

Just to clarify, you did this with an Arduino communicating between the sensor and Pixhawk? Do you know if there is a way to eliminate using the Arduino?

Thank you!

Sorry to be unclear. The second post was referring to using the Pixhawk only with a sensor connected to pin 13 on the ADC3.3 port. The output is the voltage present on this pin. The code in the first post was not used - only those changes listed.

Got it. Once I get my connectors in the mail, I'll start testing. Thank you so much for your help!!


Where is sonar_alt defined?

ArduCopter.pde line 571 (current master):

static int16_t sonar_alt;

I chose sonar_alt as sonar is not so commonly used. Obviously if you attempted to use this code  with SONAR_ENABLE=1 then there would be problems. I think a better approach would be to declare a separate variable and then substitute this in the send_rangefinder() mavlink function. This would ensure that no other code was relying  on sonar_alt being any particular value when  SONAR_ENABLE=0.

Hi James, 

Firstly, I must say that this simple example is so helpful. I would greatly appreciate if you could answer a couple of my questions. 

1. I understand your GCS_Mavlink.pde code, however could you explain what your UserCode.pde does?

2. Noob question, How do you make these changes in the code? (Where can these files be accessed?) 

3. What baud rare is the bata being sent back and recorder? can it be changed?

Thank you in advance :)

- Rai Gohalwar

Hi Raibrinder, I am glad that you find this helpful. 

1. UserCode.pde is a a place set aside in the Ardupilot code for users to add their code to extend functionality. To enable this you have to uncomment the calls to both the init code and loop code in APM_config.h. In this example in the init code I simply declare an instance of AnalogSource named test_adc setup to look at pin 13. The loop code is called at 10Hz by the system and here I read the voltage on pin 13 and assign it to sonar_alt. 

2. See http://dev.ardupilot.com/

3. The data is transmitted via Mavlink and you can view it live at approx 2hz by default. You can alter the rate at which this data is requested by MP in the 'Planner' config under 'Config/Tuning'. Obviously you need to be mindful of the total bandwidth available. 50Hz is the max telemetry rate and this will be fine while connected via USB at 1.5Mbps but at the much lower rate used by the radios, would prove problematic. In this example to make things simple, I have overridden the use of the sonar_alt variable so it also gets written to the dataflash log in the CTUN entry at 10Hz. If you follow the first example in this thread, you will see how to create your own custom dataflash log entry which will allow you to log at up to 100Hz (using userhook_FastLoop)

You are welcome :)

Any idea if this could be done on an APM?

Hi James, 

I have finally managed to make the changes to the arducopter files and have the necessary tools to build the firmware. However, I didn't understand what you meant by "Don't forget to uncomment USERHOOK_INIT and USERHOOK_MEDIUMLOOP in APM_Config.h

I see the file APM_Config.h, what change do i make.

Thanks for the fast responses brother.

- Rai Gohalwar


In the file APM_Config.h:

Line 52:

// Put your custom code into the UserCode.pde with function names matching those listed below and ensure the appropriate #define below is uncommented below

//#define USERHOOK_INIT userhook_init(); // for code to be run once at startup

Line 56:

//#define USERHOOK_MEDIUMLOOP userhook_MediumLoop(); // for code to be run at 10hz

These lines are prefixed with "//" which causes the compiler to ignore them as comments.

You need to remove the "//" from the beginning of these two lines so the compiler includes them as code and calls the two functions at the appropriate time.

Reply to Discussion


© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service