safety and safety button: refactoring #19413

This commit is contained in:
Igor Misic
2022-04-05 07:57:37 +02:00
committed by Beat Küng
parent 1f17a1470a
commit 80aef942cd
20 changed files with 446 additions and 128 deletions
+1
View File
@@ -52,6 +52,7 @@ px4_add_module(
lm_fit.cpp
mag_calibration.cpp
rc_calibration.cpp
Safety.cpp
state_machine_helper.cpp
worker_thread.cpp
DEPENDS
+6
View File
@@ -784,6 +784,10 @@ transition_result_t Commander::disarm(arm_disarm_reason_t calling_reason, bool f
events::send<events::px4::enums::arm_disarm_reason_t>(events::ID("commander_disarmed_by"), events::Log::Info,
"Disarmed by {1}", calling_reason);
if (_param_com_force_safety.get()) {
_safety_handler.enableSafety();
}
_status_changed = true;
} else if (arming_res == TRANSITION_DENIED) {
@@ -2196,6 +2200,8 @@ Commander::run()
}
}
_safety_handler.safetyButtonHandler();
/* update safety topic */
const bool safety_updated = _safety_sub.updated();
+4
View File
@@ -36,6 +36,7 @@
/* Helper classes */
#include "Arming/PreFlightCheck/PreFlightCheck.hpp"
#include "failure_detector/FailureDetector.hpp"
#include "Safety.hpp"
#include "state_machine_helper.h"
#include "worker_thread.hpp"
@@ -234,6 +235,7 @@ private:
(ParamInt<px4::params::COM_OBL_RC_ACT>) _param_com_obl_rc_act,
(ParamInt<px4::params::COM_PREARM_MODE>) _param_com_prearm_mode,
(ParamBool<px4::params::COM_FORCE_SAFETY>) _param_com_force_safety,
(ParamBool<px4::params::COM_MOT_TEST_EN>) _param_com_mot_test_en,
(ParamFloat<px4::params::COM_KILL_DISARM>) _param_com_kill_disarm,
@@ -399,6 +401,8 @@ private:
vehicle_status_s _status{};
vehicle_status_flags_s _status_flags{};
Safety _safety_handler{};
WorkerThread _worker_thread;
// Subscriptions
+80
View File
@@ -0,0 +1,80 @@
/****************************************************************************
*
* Copyright (c) 2022 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file Safety.cpp
*/
#include "Safety.hpp"
#include <circuit_breaker/circuit_breaker.h>
using namespace time_literals;
Safety::Safety()
{
/*
* Safety can be turned off with the CBRK_IO_SAFETY parameter.
*/
_safety_disabled = circuit_breaker_enabled("CBRK_IO_SAFETY", CBRK_IO_SAFETY_KEY);
}
void Safety::safetyButtonHandler()
{
if (_safety_disabled) {
_safety.safety_switch_available = true;
_safety.safety_off = true;
} else {
button_event_s button_event;
while (_safety_button_sub.update(&button_event)) {
_safety.safety_switch_available = true;
_safety.safety_off |= button_event.triggered; // triggered safety button activates safety off
}
}
// publish immediately on change, otherwise at 1 Hz for logging
if ((hrt_elapsed_time(&_safety.timestamp) >= 1_s) ||
(_safety.safety_off != _previous_safety_off)) {
_safety.timestamp = hrt_absolute_time();
_safety_pub.publish(_safety);
}
_previous_safety_off = _safety.safety_off;
}
void Safety::enableSafety()
{
_safety.safety_off = false;
}
+64
View File
@@ -0,0 +1,64 @@
/****************************************************************************
*
* Copyright (c) 2022 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file Safety.hpp
*/
#pragma once
#include <uORB/SubscriptionMultiArray.hpp>
#include <uORB/Publication.hpp>
#include <uORB/topics/button_event.h>
#include <uORB/topics/safety.h>
class Safety
{
public:
Safety();
~Safety() = default;
void safetyButtonHandler();
void enableSafety();
private:
uORB::Subscription _safety_button_sub{ORB_ID::safety_button};
uORB::Publication<safety_s> _safety_pub{ORB_ID(safety)};
safety_s _safety{};
bool _safety_disabled{false};
bool _previous_safety_off{false};
};
+10
View File
@@ -978,6 +978,16 @@ PARAM_DEFINE_INT32(COM_ARM_CHK_ESCS, 0);
*/
PARAM_DEFINE_INT32(COM_PREARM_MODE, 0);
/**
* Enable force safety
*
* Force safety when the vehicle disarms. Not supported when safety button used over PX4IO board.
*
* @boolean
* @group Commander
*/
PARAM_DEFINE_INT32(COM_FORCE_SAFETY, 0);
/**
* Enable Motor Testing
*