mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 16:51:16 +08:00
fix(uavcan): initialize the node with the node ID of uavcan start (#29084)
* fix(uavcan): stop the node on an invalid node ID instead of exiting With an invalid UAVCAN_NODE_ID, the first run of the node called exit() from the work queue of the CAN drivers, which stopped all of its work items. The node now reports the error and stops. "uavcan start" already refuses an invalid node ID, so this only happens when the parameter changes before the node runs. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * fix(uavcan): initialize the node with the node ID and bit rate of uavcan start Run() read UAVCAN_NODE_ID and UAVCAN_BITRATE again, after uavcan start had read and validated them, and stopped the node when the node ID had become invalid in between. A node stopped that way could no longer be torn down: uavcan stop schedules Run(), which returned before the teardown, so the destructor gave up after 50 ms and freed the node while _instance still pointed to it. The node now keeps the node ID and bit rate that uavcan start validated, and initializes with them, so Run() has no error path left. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * fix(uavcan): pass the node ID and bit rate to the node constructor start() set them after publishing the new node in _instance. After uavcan stop, the bus event callback stays registered on the CAN driver and schedules _instance from the interrupt, so on the next uavcan start a frame arriving in between could run Run() with an invalid node ID and the default bit rate, leaving the node passive until it is restarted. As const members set by the constructor, they are in place before the node is published, and can->init() takes _bitrate directly. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * fix(uavcan): reject the node IDs DroneCAN reserves for maintenance tools uavcan start accepted node IDs 126 and 127, which UAVCAN_NODE_ID excludes. Now that Run() no longer checks the ID again, this check is the only one, so it matches the parameter's range. Assisted-by: Claude:claude-opus-5-5 --------- Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
1 parent
1bf4627dcd
commit
997ba57dea
2 files changed
+10
-19
No files matched your search
@@ -78,9 +78,11 @@ UavcanNode *UavcanNode::_instance;
|
||||
|
||||
static UavcanNode::CanInitHelper *can = nullptr;
|
||||
|
||||
UavcanNode::UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &system_clock) :
|
||||
UavcanNode::UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &system_clock, uavcan::NodeID node_id, uint32_t bitrate) :
|
||||
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::uavcan),
|
||||
ModuleParams(nullptr),
|
||||
_node_id(node_id),
|
||||
_bitrate(bitrate),
|
||||
_node(can_driver, system_clock, _pool_allocator),
|
||||
#if defined(CONFIG_UAVCAN_ARMING_CONTROLLER)
|
||||
_arming_status_controller(_node),
|
||||
@@ -442,7 +444,7 @@ UavcanNode::start(uavcan::NodeID node_id, uint32_t bitrate)
|
||||
/*
|
||||
* Node init
|
||||
*/
|
||||
_instance = new UavcanNode(can->driver, UAVCAN_DRIVER::SystemClock::instance());
|
||||
_instance = new UavcanNode(can->driver, UAVCAN_DRIVER::SystemClock::instance(), node_id, bitrate);
|
||||
|
||||
if (_instance == nullptr) {
|
||||
PX4_ERR("Out of memory");
|
||||
@@ -703,31 +705,18 @@ void
|
||||
UavcanNode::Run()
|
||||
{
|
||||
if (!_node_init) {
|
||||
// Node ID
|
||||
int32_t node_id = 1;
|
||||
(void)param_get(param_find("UAVCAN_NODE_ID"), &node_id);
|
||||
|
||||
if (node_id < 0 || node_id > uavcan::NodeID::Max || !uavcan::NodeID(node_id).isUnicast()) {
|
||||
PX4_ERR("Invalid Node ID %" PRId32, node_id);
|
||||
::exit(1);
|
||||
}
|
||||
|
||||
// CAN bitrate
|
||||
int32_t bitrate = 1000000;
|
||||
(void)param_get(param_find("UAVCAN_BITRATE"), &bitrate);
|
||||
|
||||
/*
|
||||
* CAN driver init
|
||||
* Note that we instantiate and initialize CanInitHelper only once, because the STM32's bxCAN driver
|
||||
* shipped with libuavcan does not support deinitialization.
|
||||
*/
|
||||
const int can_init_res = can->init(bitrate);
|
||||
const int can_init_res = can->init(_bitrate);
|
||||
|
||||
if (can_init_res < 0) {
|
||||
PX4_ERR("CAN driver init failed %i", can_init_res);
|
||||
|
||||
} else {
|
||||
_instance->init(node_id, can->driver.updateEvent());
|
||||
_instance->init(_node_id, can->driver.updateEvent());
|
||||
|
||||
_node_init = true;
|
||||
}
|
||||
@@ -1621,7 +1610,7 @@ extern "C" __EXPORT int uavcan_main(int argc, char *argv[])
|
||||
int32_t node_id = 1;
|
||||
(void)param_get(param_find("UAVCAN_NODE_ID"), &node_id);
|
||||
|
||||
if (node_id < 0 || node_id > uavcan::NodeID::Max || !uavcan::NodeID(node_id).isUnicast()) {
|
||||
if (node_id < 0 || node_id > uavcan::NodeID::MaxRecommendedForRegularNodes || !uavcan::NodeID(node_id).isUnicast()) {
|
||||
PX4_ERR("Invalid Node ID %" PRId32, node_id);
|
||||
::exit(1);
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ public:
|
||||
typedef UAVCAN_DRIVER::CanInitHelper<RxQueueLenPerIface> CanInitHelper;
|
||||
enum eServerAction : int {None, Start, Stop, CheckFW, Busy};
|
||||
|
||||
UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &system_clock);
|
||||
UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &system_clock, uavcan::NodeID node_id, uint32_t bitrate);
|
||||
|
||||
virtual ~UavcanNode();
|
||||
|
||||
@@ -268,6 +268,8 @@ private:
|
||||
uavcan_node::Allocator _pool_allocator;
|
||||
|
||||
bool _node_init{false};
|
||||
const uavcan::NodeID _node_id; ///< node ID, validated by uavcan_main()
|
||||
const uint32_t _bitrate; ///< CAN bit rate in bit/s
|
||||
uavcan::Node<> _node; ///< library instance
|
||||
pthread_mutex_t _node_mutex;
|
||||
|
||||
|
||||
Reference in new issue
Block a user