Hardfault when trying to read ADC sensor on pixhawk 4

Hi guys,

I connected a sensor to one ADC line, and I m trying to read the value in one of the rover control px4 app.
I have a pixhawk 4.

Here is my code:
In RoverPositionControl.hpp

private:
....
int		_adc_report_sub{-1};		/**< adc report subscription */

orb_advert_t *mavlink_log_pub;//to pusblish critical alert to qgc
int		_adc_report_sub{-1};		/**< adc report subscription */
adc_report_s			_adc_report {};	/**< adc report status */

struct {
.......
	float leak_v; /**< Voltage threshold to detect leak on adc 3v3 */
} _parameters{};			/**< local copies of interesting parameters */

struct {
	.....
	param_t leak_v;
} _parameter_handles{};		/**< handles for interesting parameters */

	void		adc_report_poll();

In RoverPositionControl.cpp
In the constructor
_parameter_handles.leak_v = param_find(“LEAK_VOLT”);

 void RoverControl::adc_report_poll()
    {
    /* check if there is a new message */
    bool updated;
    orb_check(_adc_report_sub, &updated);

    if (updated) {
    	orb_copy(ORB_ID(adc_report), _adc_report_sub, &_adc_report);
    }
 }

In the RoverPositionControl::run()

adc_report_poll();//polling the adc to check on 3v input
float thresholdLeakVoltage = _parameters.leak_v;  

while (!should_exit()) { 
....
adc_report_poll()
const unsigned adc_chans = sizeof(_adc_report.channel_id) / sizeof(_adc_report.channel_id[0]);
		for (unsigned i = 0; i < adc_chans; i++) 
		{
			if (_adc_report.channel_id[i] == 4 ) //3v3 adc in
			{
				if (_adc_report.channel_value[i] > thresholdLeakVoltage) 
				{
						printf("ALERT");
						mavlink_log_critical(mavlink_log_pub, "ALERT ");
				}
			}
		}
....
}

It crahes as soon as I exceed the threshold set above, but not systematically

Here is the error I have:

WARN [load_mon] wq:hp_default low on stack! (284 bytes left)
INFO [ecl/EKF] EKF aligned, (pressure height, IMU buf: 22, OBS buf: 14)
WAup_hardfault: Hard Fault:
up_hardfault: IRQ: 3 regs: 0x20030824
up_hardfault: BASEPRI: 000000f0 PRIMASK: 00000000 IPSR: 00000003 CONTROL: 00000000
up_hardfault: CFAULTS: 00008200 HFAULTS: 40000000 DFAULTS: 00000000 BFAULTADDR: 0da6ff00 AFAULTS: 00000000
up_hardfault: PANIC!!! Hard fault: 40000000
up_assert: Assertion failed at file:armv7-m/up_hardfault.c line: 148 task: rover_pos_ctrl
up_registerdump: R0: 00000013 081583de 20030908 081583df 0da6ff00 081583cb 20030f78 081583cb
up_registerdump: R8: 43c80000 20030f40 00000001 08140b64 080214ef 200308f8 0801409b 08129614
up_registerdump: xPSR: 61000000 BASEPRI: 000000f0 CONTROL: 00000000
up_registerdump: EXC_RETURN: ffffffe9
up_dumpstate: sp: 20021398
up_dumpstate: IRQ stack:
up_dumpstate: base: 20021400
up_dumpstate: size: 00000200
up_dumpstate: used: 00000158
up_stackdump: 20021380: 00000000 20030a78 20021398 20021400 0000068c 08009b09 000000f0 00000000
up_stackdump: 200213a0: 08140b64 080214ef 200308f8 0801409b 08129614 200213c0 08009645 00000003
up_stackdump: 200213c0: 08140b64 0800964d 40000000 00000000 0da6ff00 00000000 20021400 0800b327
up_stackdump: 200213e0: 000000f0 08008371 000000f0 20030824 081583cb 20030f78 081583cb 08008237
up_dumpstate: sp: 200308f8
up_dumpstate: User stack:
up_dumpstate: base: 20030a78
up_dumpstate: size: 0000068c
up_dumpstate: used: 000003f8
up_stackdump: 200308e0: 3eaaada5 3a83126f 3f800000 00000000 80000010 08129615 00000000 20030954
up_stackdump: 20030900: 00a5ee4f 00000000 52454c41 4f502054 42495353 4c20454c 004b4145 0801730d
up_stackdump: 20030920: 0000000e 2002e074 00000050 081995f0 20030f60 0001731d 080295f0 20030aa0
up_stackdump: 20030940: 20030f6a 20030aa0 20030f6a 080864f1 081583cb 200308b0 20004cf0 00000000
up_stackdump: 20030960: 00000038 200311b0 00000000 3d8d7ebc 3cbb8d4b 3de4f6b7 00000000 00000000
up_stackdump: 20030980: 00000000 00000000 00000000 00000000 3d8d7ebc 3cbb8d4b 3de4f6b7 00000000
up_stackdump: 200309a0: 3f7f6fc3 3d0a9e62 3c5ac19a 3d63137d 3f7e55cb bde0b9bd 3cf90399 3de46d5b
up_stackdump: 200309c0: 3f7dd70b bd87481f bcbb8919 3d8d5871 3f7f5285 000001c7 0000000c 0813e59d
up_stackdump: 200309e0: 00000000 00000004 00000001 00000000 00000000 2002dfd4 00000006 00000001
up_stackdump: 20030a00: 08158518 00000000 2002dff4 00000009 08150101 081a44e4 00000000 2002e024
up_stackdump: 20030a20: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 20026fe4
up_stackdump: 20030a40: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 08085e15
up_stackdump: 20030a60: 08085df5 2002dd90 00000000 0800c2f5 00000000 00000000 deadbeef 20030a84
up_taskdump: Idle Task: PID=0 Stack Used=0 of 0
up_taskdump: hpwork: PID=1 Stack Used=344 of 1260
up_taskdump: lpwork: PID=2 Stack Used=604 of 1516
up_taskdump: init: PID=3 Stack Used=2032 of 2604
up_taskdump: wq:manager: PID=4 Stack Used=384 of 1252
up_taskdump: sensors: PID=197 Stack Used=1456 of 1964
up_taskdump: wq:att_pos_ctrl: PID=198 Stack Used=4768 of 6596
up_taskdump: wq:I2C3: PID=167 Stack Used=820 of 1244
up_taskdump: mavlink_rcv_if1: PID=392 Stack Used=2520 of 3916
up_taskdump: wq:SPI4: PID=169 Stack Used=600 of 1396
up_taskdump: commander: PID=202 Stack Used=1944 of 3212
up_taskdump: wq:rate_ctrl: PID=203 Stack Used=984 of 1596
up_taskdump: commander_low_prio: PID=204 Stack Used=560 of 2996
up_taskdump: rover_pos_ctrl: PID=623 Stack Used=1016 of 1676
up_taskdump: px4io: PID=432 Stack Used=1048 of 1484
up_taskdump: uavcan: PID=209 Stack Used=1624 of 2380
up_taskdump: log_writer_file: PID=722 Stack Used=368 of 1164
up_taskdump: dataman: PID=19 Stack Used=752 of 1180
up_taskdump: wq:SPI1: PID=148 Stack Used=1040 of 1396
up_taskdump: wq:lp_default: PID=22 Stack Used=992 of 1700
up_taskdump: navigator: PID=632 Stack Used=896 of 1764
up_taskdump: mavlink_if0: PID=217 Stack Used=1760 of 2572
up_taskdump: wq:I2C1: PID=26 Stack Used=864 of 1244
up_taskdump: mavlink_rcv_if0: PID=219 Stack Used=2616 of 3916
up_taskdump: logger: PID=700 Stack Used=1240 of 3644
up_taskdump: gps: PID=317 Stack Used=1144 of 1620
up_taskdump: wq:hp_default: PID=30 Stack Used=1216 of 1500

Thanks

Hi
You need to initialize mavlink_log_pub:
orb_advert_t *mavlink_log_pub{nullptr};

Thanks,
i’ll try today.
Out of curiosity, could you explain me what it changes if I initialize as a nullptr?

@bkueng, the error is gone now, but the warning never appears on qgc as before.
EDIT:
It works.

Any idea?