PX4 / PX4-Autopilot

PX4 Autopilot Software
https://px4.io
BSD 3-Clause "New" or "Revised" License
8.44k stars 13.48k forks source link

module added issue which may cause by the software architecture #16587

Open kevinuav opened 3 years ago

kevinuav commented 3 years ago

I have added a module which can communicate with a device via uart4 at 10hz. My module initials the uart4 in CA::init(). Then set the work item with ScheduleOnInterval(100000, 0); And the CA::run() read/write in a super while .The device receives the message queried by the autopilot. Then it replies the message I need. Every period is about 70ms. However it usually make the whole system crash. Why? Is this a system architecture issue or just my fault? How could I fix it? Thank you!

CA.hpp

class CA : public ModuleBase, public ModuleParams,public px4::ScheduledWorkItem { public: CA(); ~CA() override = default;

    /** @see ModuleBase */
    static int task_spawn(int argc, char *argv[]);

    /** @see ModuleBase */
    static int custom_command(int argc, char *argv[]);

    /** @see ModuleBase */
    static int print_usage(const char *reason = nullptr);

    bool init();

private:

    void Run() override;

    perf_counter_t  _loop_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")};
    perf_counter_t  _loop_interval_perf{perf_alloc(PC_INTERVAL, MODULE_NAME": interval")};
    ...........

CA.cpp

CA::CA() : ModuleParams(nullptr), ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::UART4) { }

bool CA::init() { _uart4 = open("/dev/ttyS3", O_RDWR | O_NONBLOCK | O_NOCTTY);

    set_opt(_uart4, _Baudrate, 8, 'N', 1);

    ScheduleOnInterval(100000, 0);

    return true;

}

void CA::Run() {

    if (should_exit()) {
            ScheduleClear();
            exit_and_cleanup();
            return;
    }

    perf_begin(_loop_perf);
    perf_count(_loop_interval_perf);

    _uart4 = open("/dev/ttyS3", O_RDWR | O_NONBLOCK | O_NOCTTY);

while(1){ … SEND AND RECEICE MESSAGE VIA SERIAL AT ABOUT 10HZ; … px4_usleep(100000); ……. }

The crash message comes from nsh system:

up_hardfault: Hard Fault: up_hardfault: IRQ: 3 regs: 0x2001fafc up_hardfault: BASEPRI: 000000f0 PRIMASK: 00000000 IPSR: 00000003 CONTROL: 00000000 up_hardfault: CFAULTS: 00008200 HFAULTS: 40000000 DFAULTS: 00000000 BFAULTADDR: 0003f90a AFAULTS: 00000000 up_hardfault: PANIC!!! Hard fault: 40000000 up_assert: Assertion failed at file:armv7-m/up_hardfault.c line: 148 task: wq:UART4 up_registerdump: R0: 2003c728 2003c7b8 20025eac 0003f8d0 2003c728 2003c7b8 428c0000 42ba0000 up_registerdump: R8: 00000000 2001fc30 00000000 2001fc7c 00000000 2001fbd0 08070ddd 08028e50 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 2001fcb0 20021398 20021400 00000574 08009c2d 000000f0 00000000 up_stackdump: 200213a0: 2001fc7c 00000000 2001fbd0 08070ddd 08028e50 200213c0 08009769 00000003 up_stackdump: 200213c0: 2001fc7c 08009771 40000000 00000000 0003f90a 00000000 20021400 0800b53f up_stackdump: 200213e0: 000000f0 08008371 000000f0 2001fafc 2003c7b8 428c0000 42ba0000 08008237 up_dumpstate: sp: 2001fbd0 up_dumpstate: User stack: up_dumpstate: base: 2001fcb0 up_dumpstate: size: 00000574 up_dumpstate: used: 00000360 up_stackdump: 2001fbc0: 38606f95 38606f9b 00000010 08015a8d 00000000 2003c670 000186a0 08070ddd up_stackdump: 2001fbe0: 08070f69 2003c688 00000000 2001fc20 00000080 081382cb 20007a60 2001fc20 up_stackdump: 2001fc00: 20000890 2002b330 20000894 00000000 00000000 00000000 00000000 0813840b up_stackdump: 2001fc20: 20007a60 000000f0 20009048 20009048 08010001 00000000 00000000 00000000 up_stackdump: 2001fc40: 00000000 081a0b1c 20009048 00000000 00000001 00000000 00000000 00000000 up_stackdump: 2001fc60: 00000000 0001ffff 00000000 00000000 00000000 00000000 00000000 0800c900 up_stackdump: 2001fc80: 000000ff 2001f5b0 2000f680 2007fb90 2007fbb4 0801d327 00000000 00000000 up_stackdump: 2001fca0: 00000000 00000000 00000000 00000000 deadbeef 00000000 00000180 80000580 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=1016 of 1612 up_taskdump: init: PID=3 Stack Used=1976 of 2924 up_taskdump: wq:manager: PID=4 Stack Used=416 of 1252 up_taskdump: wq:SPI1: PID=133 Stack Used=1688 of 2332 up_taskdump: wq:I2C3: PID=138 Stack Used=880 of 1468 up_taskdump: wq:SPI4: PID=140 Stack Used=860 of 2332 up_taskdump: gps: PID=270 Stack Used=920 of 1676 up_taskdump: navigator: PID=399 Stack Used=976 of 1764 up_taskdump: wq:lp_default: PID=16 Stack Used=816 of 1700 up_taskdump: wq:hp_default: PID=18 Stack Used=1096 of 1900 up_taskdump: dataman: PID=20 Stack Used=760 of 1204 up_taskdump: wq:nav_and_controllers: PID=214 Stack Used=5296 of 7196 up_taskdump: wq:rate_ctrl: PID=215 Stack Used=1120 of 1660 up_taskdump: commander: PID=220 Stack Used=1824 of 3212 up_taskdump: commander_low_prio: PID=221 Stack Used=952 of 2996 up_taskdump: wq:I2C1: PID=30 Stack Used=960 of 1468 up_taskdump: mavlink_if0: PID=228 Stack Used=1896 of 2572 up_taskdump: logger: PID=424 Stack Used=2168 of 3644 up_taskdump: log_writer_file: PID=428 Stack Used=368 of 1164 up_taskdump: mavlink_rcv_if0: PID=429 Stack Used=2824 of 4076 up_taskdump: mavlink_if1: PID=303 Stack Used=1848 of 2484 up_taskdump: mavlink_rcv_if1: PID=304 Stack Used=2704 of 4076 up_taskdump: wq:UART4: PID=314 Stack Used=864 of 1396 up_taskdump: wq:attitude_ctrl: PID=382 Stack Used=1236 of 1668 [boot] Rev 0x0 : Ver 0x6 V560

[boot] Fault Log info File No 4 Length 3177 flags:0x01 state:0

[boot] Fault Logged on 2021-01-19-07:37:42 - Valid

..............

kevinuav commented 3 years ago

Hey,Guys. Finally. I know why my nuttx system usually crashes while my module is running. Since I want the serial reads/writes at 10hz. I use the function usleep(100000) at my workqueue in the super while(1). The hard fault never occurs after that I deleted the usleep function. Why the usleep makes the workqueue unstable?