From 575fd28f0ab24518da4a72dd8a39a5c2a20ee15f Mon Sep 17 00:00:00 2001 From: mahima-yoga Date: Wed, 25 Nov 2026 13:05:51 +0100 Subject: [PATCH] feat(v6s): enable safety switch on PWM10 output --- boards/auterion/fmu-v6s/default.px4board | 1 + .../auterion/fmu-v6s/init/rc.board_defaults | 6 ++ boards/auterion/fmu-v6s/src/board_config.h | 15 ++++ src/drivers/safety_button/SafetyButton.cpp | 73 ++++++++++++++++++- src/drivers/safety_button/SafetyButton.hpp | 11 +++ 5 files changed, 104 insertions(+), 2 deletions(-) diff --git a/boards/auterion/fmu-v6s/default.px4board b/boards/auterion/fmu-v6s/default.px4board index fc722c22749..f896f0a41b3 100644 --- a/boards/auterion/fmu-v6s/default.px4board +++ b/boards/auterion/fmu-v6s/default.px4board @@ -99,3 +99,4 @@ CONFIG_WQ_TTY_STACKSIZE=2500 CONFIG_MODULES_INTERNAL_COMBUSTION_ENGINE_CONTROL=y CONFIG_DRIVERS_RPM_CAPTURE=y +CONFIG_DRIVERS_SAFETY_BUTTON=y diff --git a/boards/auterion/fmu-v6s/init/rc.board_defaults b/boards/auterion/fmu-v6s/init/rc.board_defaults index 28a7801ebb0..5490c25a1f2 100644 --- a/boards/auterion/fmu-v6s/init/rc.board_defaults +++ b/boards/auterion/fmu-v6s/init/rc.board_defaults @@ -43,6 +43,12 @@ netman update_default -i eth0 # Start a second NSH connected to the debug port nshterm /dev/ttyS3 & +# Safety plug on PWM10, only needed when COM_SAFETY_MODE requires a physical switch +if param compare COM_SAFETY_MODE 1 2 +then + safety_button start +fi + # Start the time_persistor to cyclically store the RTC in FRAM time_persistor start diff --git a/boards/auterion/fmu-v6s/src/board_config.h b/boards/auterion/fmu-v6s/src/board_config.h index c57d61c225b..6d08f0ed7e3 100644 --- a/boards/auterion/fmu-v6s/src/board_config.h +++ b/boards/auterion/fmu-v6s/src/board_config.h @@ -157,9 +157,24 @@ #define BOARD_INDICATE_EXTERNAL_LOCKOUT_STATE(enabled) px4_arch_configgpio((enabled) ? GPIO_ARMED : GPIO_ARMED_INIT) #define BOARD_GET_EXTERNAL_LOCKOUT_STATE() px4_arch_gpioread(GPIO_ARMED) +/* Safety plug input on FMU_CAP1 (PWM10, PC7, timer channel index 9) + * The pin reads low when nothing is connected on Skynode S. + * Pulling the plug lets the pin go low, which turns safety off (active low). + * Note: a wire that breaks after boot also reads as pulled. + * safety_button reserves the timer channel so that pwm_out leaves the pin alone. + * While it runs, PWM10 can not be used as PWM output, PPS or RPM input. + */ +#define GPIO_SAFETY_SWITCH_IN /* PC7 */ (GPIO_INPUT|GPIO_PULLDOWN|GPIO_PORTC|GPIO_PIN7) +#define BOARD_SAFETY_BUTTON() (!px4_arch_gpioread(GPIO_SAFETY_SWITCH_IN)) +#define BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL 9 +/* The plug has to be seen fitted for 3 seconds (at 30 Hz) before pulling it is accepted, + * so that booting without a plug does not turn safety off. */ +#define BOARD_SAFETY_BUTTON_FITTED_COUNT 90 + /* All pins to initialize on boot */ #define PX4_GPIO_INIT_LIST { \ GPIO_ARMED_INIT, \ + GPIO_SAFETY_SWITCH_IN, \ PX4_ADC_GPIO, \ GPIO_CAN1_TX, \ GPIO_CAN1_RX, \ diff --git a/src/drivers/safety_button/SafetyButton.cpp b/src/drivers/safety_button/SafetyButton.cpp index ffea5de2afb..fec02b23101 100644 --- a/src/drivers/safety_button/SafetyButton.cpp +++ b/src/drivers/safety_button/SafetyButton.cpp @@ -34,6 +34,11 @@ #include #include "SafetyButton.hpp" +#ifdef BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL +#include +static_assert(BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL < MAX_TIMER_IO_CHANNELS, "safety input timer channel out of range"); +#endif + // BOARD_SAFETY_BUTTON() is true while the safety button is pressed, and BOARD_SAFETY_LED(on_true) turns the safety // LED on and off: by default, the GPIO_BTN_SAFETY input, high while pressed, and the active low GPIO_LED_SAFETY #if !defined(BOARD_SAFETY_BUTTON) && defined(GPIO_BTN_SAFETY) @@ -52,7 +57,24 @@ using namespace time_literals; ModuleBase::Descriptor SafetyButton::desc{task_spawn, custom_command, print_usage}; -static constexpr uint8_t CYCLE_COUNT{30}; /* safety switch must be held for 1 second to activate */ +// Number of consecutive 30 Hz samples the safety switch must be active to turn safety off. +// Boards can override the default of 1 second, e.g. for a switch that is not operated by hand. +#ifndef BOARD_SAFETY_BUTTON_CYCLE_COUNT +#define BOARD_SAFETY_BUTTON_CYCLE_COUNT 30 +#endif + +static constexpr uint8_t CYCLE_COUNT{BOARD_SAFETY_BUTTON_CYCLE_COUNT}; + +// Number of consecutive 30 Hz samples the safety switch must be inactive before it is accepted as an input. +// Boards with a pull-out safety plug set this, so that a plug that is missing at boot does not turn safety off. +// Such boards also get no pairing request, as anything that toggles the input would trigger it. +// The default of 0 accepts the switch right away. +#ifndef BOARD_SAFETY_BUTTON_FITTED_COUNT +#define BOARD_SAFETY_BUTTON_FITTED_COUNT 0 +#endif + +static constexpr uint8_t FITTED_COUNT{BOARD_SAFETY_BUTTON_FITTED_COUNT}; +static constexpr uint8_t NOT_FITTED_WARN_COUNT{150}; /* warn if still not fitted 5 seconds after start */ // Define the various LED flash sequences for each system state. enum class LED_PATTERN : uint16_t { @@ -67,16 +89,47 @@ SafetyButton::SafetyButton() : ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default) { _has_px4io = PX4_MFT_HW_SUPPORTED(PX4_MFT_PX4IO); + _input_accepted = (FITTED_COUNT == 0); } SafetyButton::~SafetyButton() { ScheduleClear(); + +#ifdef BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL + + if (_timer_channel_reserved) { + io_timer_unallocate_channel(BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL); + } + +#endif } void SafetyButton::CheckSafetyRequest(bool button_pressed) { + if (!_input_accepted) { + /* The switch has to be seen inactive first, e.g. a safety plug has to be fitted */ + if (!button_pressed) { + if (++_fitted_counter >= FITTED_COUNT) { + _input_accepted = true; + } + + } else { + _fitted_counter = 0; + + if (_not_fitted_counter < NOT_FITTED_WARN_COUNT) { + if (++_not_fitted_counter == NOT_FITTED_WARN_COUNT) { + mavlink_log_critical(&_mavlink_log_pub, "Safety plug not fitted, safety stays on\t"); + events::send(events::ID("safety_button_plug_not_fitted"), events::Log::Critical, + "Safety plug not fitted, safety stays on"); + } + } + } + + return; + } + /* Keep button pressed for one second to turn off safety */ if (button_pressed) { @@ -181,7 +234,11 @@ SafetyButton::Run() CheckSafetyRequest(button_pressed); } - CheckPairingRequest(button_pressed); + // Pressing 3 times to pair only makes sense for a hand operated button, not for a plug + if (FITTED_COUNT == 0) { + CheckPairingRequest(button_pressed); + } + _button_prev_sate = button_pressed; } @@ -211,6 +268,18 @@ SafetyButton::task_spawn(int argc, char *argv[]) int SafetyButton::Start() { +#ifdef BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL + // Reserve the timer channel of the input pin, so that pwm_out does not turn it into an output + int ret = io_timer_allocate_channel(BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL, IOTimerChanMode_Other); + + if (ret != 0) { + PX4_ERR("safety input timer channel %d is in use (%i)", BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL, ret); + return ret; + } + + _timer_channel_reserved = true; +#endif + ScheduleOnInterval(33_ms); // run at 30 Hz return PX4_OK; diff --git a/src/drivers/safety_button/SafetyButton.hpp b/src/drivers/safety_button/SafetyButton.hpp index b6e76088c59..0e3aa844cda 100644 --- a/src/drivers/safety_button/SafetyButton.hpp +++ b/src/drivers/safety_button/SafetyButton.hpp @@ -36,9 +36,11 @@ #include #include +#include #include #include #include