feat(v6s): enable safety switch on PWM10 output

This commit is contained in:
mahima-yoga committed 2026-11-25 13:05:51 +01:00
1 parent ff37963842
commit 575fd28f0a
5 files changed
+104 -2

No files matched your search

+1
View File
@@ -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
@@ -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
@@ -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, \
+71 -2
View File
@@ -34,6 +34,11 @@
#include <board_config.h>
#include "SafetyButton.hpp"
#ifdef BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL
#include <px4_arch/io_timer.h>
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;
@@ -36,9 +36,11 @@
#include <float.h>
#include <drivers/drv_hrt.h>
#include <px4_platform_common/events.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <button/ButtonPublisher.hpp>
#include <systemlib/mavlink_log.h>
#include <uORB/topics/vehicle_status.h>
class SafetyButton : public ModuleBase, public px4::ScheduledWorkItem
@@ -73,6 +75,15 @@ private:
uint8_t _blink_counter{0};
bool _button_prev_sate{false}; ///< Previous state of the HW button
// Switch has to be seen inactive before it is accepted (BOARD_SAFETY_BUTTON_FITTED_COUNT)
bool _input_accepted{true};
uint8_t _fitted_counter{0};
uint8_t _not_fitted_counter{0};
orb_advert_t _mavlink_log_pub{nullptr};
// Timer channel of the input pin is reserved (BOARD_SAFETY_BUTTON_IO_TIMER_CHANNEL)
bool _timer_channel_reserved{false};
// Pairing request
hrt_abstime _pairing_start{0};
int _pairing_button_counter{0};