diff --git a/src/drivers/uavcan/uavcan_main.cpp b/src/drivers/uavcan/uavcan_main.cpp index 65aa197218b..778902de0f8 100644 --- a/src/drivers/uavcan/uavcan_main.cpp +++ b/src/drivers/uavcan/uavcan_main.cpp @@ -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); } diff --git a/src/drivers/uavcan/uavcan_main.hpp b/src/drivers/uavcan/uavcan_main.hpp index d4c9cc6c299..027931e734b 100644 --- a/src/drivers/uavcan/uavcan_main.hpp +++ b/src/drivers/uavcan/uavcan_main.hpp @@ -216,7 +216,7 @@ public: typedef UAVCAN_DRIVER::CanInitHelper 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;