uavcan module cleanup

- move most orb to uORB::Publication and uORB::Subscription
 - update legacy message handling (warn to PX4_INFO, PX4_WARN, PX4_ERR)
 - add perf counters
 - sensors/mag support newer `uavcan::equipment::ahrs::MagneticFieldStrength2` message
 - sensors/gps support `uavcan::equipment::gnss::Auxiliary` for hdop and vdop
 - sensors delete obsolete ioctl and read methods
 - use PublicationMulti for actuator_outputs and esc_reports (to coexist with other output modules)
 - add GNSS parameter metadata (parameters_injected.xml)
This commit is contained in:
Daniel Agar
2019-10-28 19:57:50 -04:00
committed by GitHub
parent 47dd312b57
commit 375fc4a75c
22 changed files with 705 additions and 677 deletions
+145 -149
View File
@@ -60,11 +60,9 @@
#include <uORB/topics/vehicle_command.h>
#include <uORB/topics/vehicle_command_ack.h>
#include <uORB/topics/uavcan_parameter_request.h>
#include <uORB/topics/uavcan_parameter_value.h>
#include <v2.0/common/mavlink.h>
/**
* @file uavcan_servers.cpp
*
@@ -78,8 +76,8 @@
* UavcanNode
*/
UavcanServers *UavcanServers::_instance;
UavcanServers::UavcanServers(uavcan::INode &main_node) :
_subnode_thread(-1),
_vdriver(NumIfaces, UAVCAN_DRIVER::SystemClock::instance(), main_node.getAllocator(), VirtualIfaceBlockAllocationQuota),
_subnode(_vdriver, UAVCAN_DRIVER::SystemClock::instance(), main_node.getAllocator()),
_main_node(main_node),
@@ -96,7 +94,6 @@ UavcanServers::UavcanServers(uavcan::INode &main_node) :
_enumeration_client(_subnode),
_enumeration_getset_client(_subnode),
_enumeration_save_client(_subnode)
{
}
@@ -109,17 +106,18 @@ UavcanServers::~UavcanServers()
_main_node.getDispatcher().removeRxFrameListener();
}
int UavcanServers::stop()
int
UavcanServers::stop()
{
UavcanServers *server = instance();
if (server == nullptr) {
warnx("Already stopped");
PX4_INFO("Already stopped");
return -1;
}
if (server->_subnode_thread) {
warnx("stopping fw srv thread...");
PX4_INFO("stopping fw srv thread...");
server->_subnode_thread_should_exit = true;
(void)pthread_join(server->_subnode_thread, NULL);
}
@@ -132,10 +130,11 @@ int UavcanServers::stop()
return 0;
}
int UavcanServers::start(uavcan::INode &main_node)
int
UavcanServers::start(uavcan::INode &main_node)
{
if (_instance != nullptr) {
warnx("Already started");
PX4_INFO("Already started");
return -1;
}
@@ -145,14 +144,14 @@ int UavcanServers::start(uavcan::INode &main_node)
_instance = new UavcanServers(main_node);
if (_instance == nullptr) {
warnx("Out of memory");
PX4_ERR("Out of memory");
return -2;
}
int rv = _instance->init();
int rv = _instance->init();
if (rv < 0) {
warnx("Node init failed: %d", rv);
PX4_ERR("Node init failed: %d", rv);
delete _instance;
_instance = nullptr;
return rv;
@@ -170,7 +169,7 @@ int UavcanServers::start(uavcan::INode &main_node)
param.sched_priority = Priority;
if (pthread_attr_setschedparam(&tattr, &param)) {
warnx("setting sched params failed");
PX4_ERR("setting sched params failed");
}
static auto run_trampoline = [](void *) {return UavcanServers::_instance->run(_instance);};
@@ -179,7 +178,7 @@ int UavcanServers::start(uavcan::INode &main_node)
if (rv != 0) {
rv = -rv;
warnx("pthread_create() failed: %d", rv);
PX4_ERR("pthread_create() failed: %d", rv);
delete _instance;
_instance = nullptr;
}
@@ -187,7 +186,8 @@ int UavcanServers::start(uavcan::INode &main_node)
return rv;
}
int UavcanServers::init()
int
UavcanServers::init()
{
errno = 0;
@@ -195,11 +195,10 @@ int UavcanServers::init()
* Initialize the mutex.
* giving it its path
*/
int ret = Lock::init(_subnode_mutex);
if (ret < 0) {
warnx("Lock init: %d", errno);
PX4_ERR("Lock init: %d", errno);
return ret;
}
@@ -208,7 +207,6 @@ int UavcanServers::init()
_subnode.setNodeID(_main_node.getNodeID());
_main_node.getDispatcher().installRxFrameListener(&_vdriver);
/*
* Initialize the fw version checker.
* giving it its path
@@ -216,7 +214,7 @@ int UavcanServers::init()
ret = _fw_version_checker.createFwPaths(UAVCAN_FIRMWARE_PATH);
if (ret < 0) {
warnx("FirmwareVersionChecker init: %d, errno: %d", ret, errno);
PX4_ERR("FirmwareVersionChecker init: %d, errno: %d", ret, errno);
return ret;
}
@@ -225,7 +223,7 @@ int UavcanServers::init()
ret = _fw_server.start();
if (ret < 0) {
warnx("BasicFileServer init: %d, errno: %d", ret, errno);
PX4_ERR("BasicFileServer init: %d, errno: %d", ret, errno);
return ret;
}
@@ -234,7 +232,7 @@ int UavcanServers::init()
ret = _storage_backend.init(UAVCAN_NODE_DB_PATH);
if (ret < 0) {
warnx("FileStorageBackend init: %d, errno: %d", ret, errno);
PX4_ERR("FileStorageBackend init: %d, errno: %d", ret, errno);
return ret;
}
@@ -243,7 +241,7 @@ int UavcanServers::init()
ret = _tracer.init(UAVCAN_LOG_FILE);
if (ret < 0) {
warnx("FileEventTracer init: %d, errno: %d", ret, errno);
PX4_ERR("FileEventTracer init: %d, errno: %d", ret, errno);
return ret;
}
@@ -255,34 +253,32 @@ int UavcanServers::init()
ret = _server_instance.init(hwver.unique_id);
if (ret < 0) {
warnx("CentralizedServer init: %d", ret);
PX4_ERR("CentralizedServer init: %d", ret);
return ret;
}
/* Start node info retriever to fetch node info from new nodes */
ret = _node_info_retriever.start();
if (ret < 0) {
warnx("NodeInfoRetriever init: %d", ret);
PX4_ERR("NodeInfoRetriever init: %d", ret);
return ret;
}
/* Start the fw version checker */
ret = _fw_upgrade_trigger.start(_node_info_retriever, _fw_version_checker.getFirmwarePath());
if (ret < 0) {
warnx("FirmwareUpdateTrigger init: %d", ret);
PX4_ERR("FirmwareUpdateTrigger init: %d", ret);
return ret;
}
/* Start the Node */
return 0;
}
pthread_addr_t UavcanServers::run(pthread_addr_t)
pthread_addr_t
UavcanServers::run(pthread_addr_t)
{
prctl(PR_SET_NAME, "uavcan fw srv", 0);
@@ -296,9 +292,9 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
/* the subscribe call needs to happen in the same thread,
* so not in the constructor */
int cmd_sub = orb_subscribe(ORB_ID(vehicle_command));
int param_request_sub = orb_subscribe(ORB_ID(uavcan_parameter_request));
int armed_sub = orb_subscribe(ORB_ID(actuator_armed));
uORB::Subscription armed_sub{ORB_ID(actuator_armed)};
uORB::Subscription vcmd_sub{ORB_ID(vehicle_command)};
uORB::Subscription param_request_sub{ORB_ID(uavcan_parameter_request)};
/* Set up shared service clients */
_param_getset_client.setCallback(GetSetCallback(this, &UavcanServers::cb_getset));
@@ -326,16 +322,13 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
const int spin_res = _subnode.spin(uavcan::MonotonicDuration::fromMSec(10));
if (spin_res < 0) {
warnx("node spin error %i", spin_res);
PX4_ERR("node spin error %i", spin_res);
}
// Check for parameter requests (get/set/list)
bool param_request_ready;
orb_check(param_request_sub, &param_request_ready);
if (param_request_ready && !_param_list_in_progress && !_param_in_progress && !_count_in_progress) {
struct uavcan_parameter_request_s request;
orb_copy(ORB_ID(uavcan_parameter_request), param_request_sub, &request);
if (param_request_sub.updated() && !_param_list_in_progress && !_param_in_progress && !_count_in_progress) {
uavcan_parameter_request_s request{};
param_request_sub.copy(&request);
if (_param_counts[request.node_id]) {
/*
@@ -355,7 +348,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
int call_res = _param_getset_client.call(request.node_id, req);
if (call_res < 0) {
warnx("UAVCAN command bridge: couldn't send GetSet: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send GetSet: %d", call_res);
} else {
_param_in_progress = true;
@@ -388,7 +381,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
int call_res = _param_getset_client.call(request.node_id, req);
if (call_res < 0) {
warnx("UAVCAN command bridge: couldn't send GetSet: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send GetSet: %d", call_res);
} else {
_param_in_progress = true;
@@ -402,7 +395,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
_param_list_node_id = request.node_id;
_param_list_all_nodes = false;
warnx("UAVCAN command bridge: starting component-specific param list");
PX4_INFO("UAVCAN command bridge: starting component-specific param list");
}
} else if (request.node_id == MAV_COMP_ID_ALL) {
@@ -416,7 +409,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
_param_list_node_id = get_next_active_node_id(0);
_param_list_all_nodes = true;
warnx("UAVCAN command bridge: starting global param list with node %hhu", _param_list_node_id);
PX4_INFO("UAVCAN command bridge: starting global param list with node %hhu", _param_list_node_id);
if (_param_counts[_param_list_node_id] == 0) {
param_count(_param_list_node_id);
@@ -435,7 +428,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
// Handle parameter listing index/node ID advancement
if (_param_list_in_progress && !_param_in_progress && !_count_in_progress) {
if (_param_index >= _param_counts[_param_list_node_id]) {
warnx("UAVCAN command bridge: completed param list for node %hhu", _param_list_node_id);
PX4_INFO("UAVCAN command bridge: completed param list for node %hhu", _param_list_node_id);
// Reached the end of the current node's parameter set.
_param_list_in_progress = false;
@@ -457,7 +450,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
// Keep on listing.
_param_index = 0;
_param_list_in_progress = true;
warnx("UAVCAN command bridge: started param list for node %hhu", _param_list_node_id);
PX4_INFO("UAVCAN command bridge: started param list for node %hhu", _param_list_node_id);
}
}
}
@@ -474,7 +467,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
if (call_res < 0) {
_param_list_in_progress = false;
warnx("UAVCAN command bridge: couldn't send param list GetSet: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send param list GetSet: %d", call_res);
} else {
_param_in_progress = true;
@@ -482,12 +475,9 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
}
// Check for ESC enumeration commands
bool cmd_ready;
orb_check(cmd_sub, &cmd_ready);
if (cmd_ready && !_cmd_in_progress) {
struct vehicle_command_s cmd;
orb_copy(ORB_ID(vehicle_command), cmd_sub, &cmd);
if (vcmd_sub.updated() && !_cmd_in_progress) {
vehicle_command_s cmd{};
vcmd_sub.copy(&cmd);
uint8_t cmd_ack_result = vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED;
@@ -496,7 +486,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
int node_id = static_cast<int>(cmd.param2 + 0.5f);
int call_res;
warnx("UAVCAN command bridge: received UAVCAN command ID %d, node ID %d", command_id, node_id);
PX4_INFO("UAVCAN command bridge: received UAVCAN command ID %d, node ID %d", command_id, node_id);
switch (command_id) {
case 0:
@@ -512,7 +502,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
call_res = _enumeration_client.call(get_next_active_node_id(0), req);
if (call_res < 0) {
warnx("UAVCAN ESC enumeration: couldn't send initial Begin request: %d", call_res);
PX4_ERR("UAVCAN ESC enumeration: couldn't send initial Begin request: %d", call_res);
beep(BeepFrequencyError);
cmd_ack_result = vehicle_command_ack_s::VEHICLE_RESULT_FAILED;
@@ -524,7 +514,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
}
default: {
warnx("UAVCAN command bridge: unknown command ID %d", command_id);
PX4_ERR("UAVCAN command bridge: unknown command ID %d", command_id);
cmd_ack_result = vehicle_command_ack_s::VEHICLE_RESULT_UNSUPPORTED;
break;
}
@@ -533,7 +523,7 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
} else if (cmd.command == vehicle_command_s::VEHICLE_CMD_PREFLIGHT_STORAGE) {
int command_id = static_cast<int>(cmd.param1 + 0.5f);
warnx("UAVCAN command bridge: received storage command ID %d", command_id);
PX4_INFO("UAVCAN command bridge: received storage command ID %d", command_id);
switch (command_id) {
case 1: {
@@ -576,15 +566,12 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
// Shut down once armed
// TODO (elsewhere): start up again once disarmed?
bool updated;
orb_check(armed_sub, &updated);
if (updated) {
struct actuator_armed_s armed;
orb_copy(ORB_ID(actuator_armed), armed_sub, &armed);
if (armed_sub.updated()) {
actuator_armed_s armed{};
armed_sub.copy(&armed);
if (armed.armed && !(armed.lockdown || armed.manual_lockdown)) {
warnx("UAVCAN command bridge: system armed, exiting now.");
PX4_INFO("UAVCAN command bridge: system armed, exiting now.");
break;
}
}
@@ -592,11 +579,12 @@ pthread_addr_t UavcanServers::run(pthread_addr_t)
_subnode_thread_should_exit = false;
warnx("exiting");
PX4_INFO("exiting");
return (pthread_addr_t) 0;
}
void UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result)
void
UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result)
{
if (_count_in_progress) {
/*
@@ -622,14 +610,14 @@ void UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::
if (call_res < 0) {
_count_in_progress = false;
_count_index = 0;
warnx("UAVCAN command bridge: couldn't send GetSet during param count: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send GetSet during param count: %d", call_res);
beep(BeepFrequencyError);
}
} else {
_count_in_progress = false;
_count_index = 0;
warnx("UAVCAN command bridge: completed param count for node %hhu: %hhu", node_id, _param_counts[node_id]);
PX4_INFO("UAVCAN command bridge: completed param count for node %hhu: %hhu", node_id, _param_counts[node_id]);
beep(BeepFrequencyGenericIndication);
}
@@ -637,7 +625,7 @@ void UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::
_param_counts[node_id] = 0;
_count_in_progress = false;
_count_index = 0;
warnx("UAVCAN command bridge: GetSet error during param count");
PX4_ERR("UAVCAN command bridge: GetSet error during param count");
}
} else {
@@ -669,15 +657,10 @@ void UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::
response.int_value = param.value.to<uavcan::protocol::param::Value::Tag::boolean_value>();
}
if (_param_response_pub == nullptr) {
_param_response_pub = orb_advertise(ORB_ID(uavcan_parameter_value), &response);
} else {
orb_publish(ORB_ID(uavcan_parameter_value), _param_response_pub, &response);
}
_param_response_pub.publish(response);
} else {
warnx("UAVCAN command bridge: GetSet error");
PX4_ERR("UAVCAN command bridge: GetSet error");
}
_param_in_progress = false;
@@ -685,38 +668,41 @@ void UavcanServers::cb_getset(const uavcan::ServiceCallResult<uavcan::protocol::
}
}
void UavcanServers::param_count(uavcan::NodeID node_id)
void
UavcanServers::param_count(uavcan::NodeID node_id)
{
uavcan::protocol::param::GetSet::Request req;
req.index = 0;
int call_res = _param_getset_client.call(node_id, req);
if (call_res < 0) {
warnx("UAVCAN command bridge: couldn't start parameter count: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't start parameter count: %d", call_res);
} else {
_count_in_progress = true;
_count_index = 0;
warnx("UAVCAN command bridge: starting param count");
PX4_INFO("UAVCAN command bridge: starting param count");
}
}
void UavcanServers::param_opcode(uavcan::NodeID node_id)
void
UavcanServers::param_opcode(uavcan::NodeID node_id)
{
uavcan::protocol::param::ExecuteOpcode::Request opcode_req;
opcode_req.opcode = _param_save_opcode;
int call_res = _param_opcode_client.call(node_id, opcode_req);
if (call_res < 0) {
warnx("UAVCAN command bridge: couldn't send ExecuteOpcode: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send ExecuteOpcode: %d", call_res);
} else {
_cmd_in_progress = true;
warnx("UAVCAN command bridge: sent ExecuteOpcode");
PX4_INFO("UAVCAN command bridge: sent ExecuteOpcode");
}
}
void UavcanServers::cb_opcode(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result)
void
UavcanServers::cb_opcode(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result)
{
bool success = result.isSuccessful();
uint8_t node_id = result.getCallID().server_node_id.get();
@@ -725,23 +711,23 @@ void UavcanServers::cb_opcode(const uavcan::ServiceCallResult<uavcan::protocol::
_cmd_in_progress = false;
if (!result.isSuccessful()) {
warnx("UAVCAN command bridge: save request for node %hhu timed out.", node_id);
PX4_ERR("UAVCAN command bridge: save request for node %hhu timed out.", node_id);
} else if (!result.getResponse().ok) {
warnx("UAVCAN command bridge: save request for node %hhu rejected.", node_id);
PX4_ERR("UAVCAN command bridge: save request for node %hhu rejected.", node_id);
} else {
warnx("UAVCAN command bridge: save request for node %hhu completed OK, restarting.", node_id);
PX4_INFO("UAVCAN command bridge: save request for node %hhu completed OK, restarting.", node_id);
uavcan::protocol::RestartNode::Request restart_req;
restart_req.magic_number = restart_req.MAGIC_NUMBER;
int call_res = _param_restartnode_client.call(node_id, restart_req);
if (call_res < 0) {
warnx("UAVCAN command bridge: couldn't send RestartNode: %d", call_res);
PX4_ERR("UAVCAN command bridge: couldn't send RestartNode: %d", call_res);
} else {
warnx("UAVCAN command bridge: sent RestartNode");
PX4_ERR("UAVCAN command bridge: sent RestartNode");
_cmd_in_progress = true;
}
}
@@ -761,7 +747,8 @@ void UavcanServers::cb_opcode(const uavcan::ServiceCallResult<uavcan::protocol::
}
}
void UavcanServers::cb_restart(const uavcan::ServiceCallResult<uavcan::protocol::RestartNode> &result)
void
UavcanServers::cb_restart(const uavcan::ServiceCallResult<uavcan::protocol::RestartNode> &result)
{
bool success = result.isSuccessful();
uint8_t node_id = result.getCallID().server_node_id.get();
@@ -770,12 +757,13 @@ void UavcanServers::cb_restart(const uavcan::ServiceCallResult<uavcan::protocol:
_cmd_in_progress = false;
if (success) {
warnx("UAVCAN command bridge: restart request for node %hhu completed OK.", node_id);
PX4_INFO("UAVCAN command bridge: restart request for node %hhu completed OK.", node_id);
// Clear the dirty flag
clear_node_params_dirty(node_id);
} else {
warnx("UAVCAN command bridge: restart request for node %hhu failed.", node_id);
PX4_ERR("UAVCAN command bridge: restart request for node %hhu failed.", node_id);
}
// Get the next dirty node ID and send the same command to it
@@ -786,7 +774,8 @@ void UavcanServers::cb_restart(const uavcan::ServiceCallResult<uavcan::protocol:
}
}
uint8_t UavcanServers::get_next_active_node_id(uint8_t base)
uint8_t
UavcanServers::get_next_active_node_id(uint8_t base)
{
base++;
@@ -796,7 +785,8 @@ uint8_t UavcanServers::get_next_active_node_id(uint8_t base)
return base;
}
uint8_t UavcanServers::get_next_dirty_node_id(uint8_t base)
uint8_t
UavcanServers::get_next_dirty_node_id(uint8_t base)
{
base++;
@@ -805,7 +795,8 @@ uint8_t UavcanServers::get_next_dirty_node_id(uint8_t base)
return base;
}
void UavcanServers::beep(float frequency)
void
UavcanServers::beep(float frequency)
{
uavcan::equipment::indication::BeepCommand cmd;
cmd.frequency = frequency;
@@ -813,20 +804,21 @@ void UavcanServers::beep(float frequency)
(void)_beep_pub.broadcast(cmd);
}
void UavcanServers::cb_enumeration_begin(const uavcan::ServiceCallResult<uavcan::protocol::enumeration::Begin> &result)
void
UavcanServers::cb_enumeration_begin(const uavcan::ServiceCallResult<uavcan::protocol::enumeration::Begin> &result)
{
uint8_t next_id = get_next_active_node_id(result.getCallID().server_node_id.get());
if (!result.isSuccessful()) {
warnx("UAVCAN ESC enumeration: begin request for node %hhu timed out.", result.getCallID().server_node_id.get());
PX4_ERR("UAVCAN ESC enumeration: begin request for node %hhu timed out.", result.getCallID().server_node_id.get());
} else if (result.getResponse().error) {
warnx("UAVCAN ESC enumeration: begin request for node %hhu rejected: %hhu", result.getCallID().server_node_id.get(),
result.getResponse().error);
PX4_ERR("UAVCAN ESC enumeration: begin request for node %hhu rejected: %hhu", result.getCallID().server_node_id.get(),
result.getResponse().error);
} else {
_esc_count++;
warnx("UAVCAN ESC enumeration: begin request for node %hhu completed OK.", result.getCallID().server_node_id.get());
PX4_INFO("UAVCAN ESC enumeration: begin request for node %hhu completed OK.", result.getCallID().server_node_id.get());
}
if (next_id < 128) {
@@ -840,22 +832,23 @@ void UavcanServers::cb_enumeration_begin(const uavcan::ServiceCallResult<uavcan:
int call_res = _enumeration_client.call(next_id, req);
if (call_res < 0) {
warnx("UAVCAN ESC enumeration: couldn't send Begin request: %d", call_res);
PX4_ERR("UAVCAN ESC enumeration: couldn't send Begin request: %d", call_res);
} else {
warnx("UAVCAN ESC enumeration: sent Begin request");
PX4_INFO("UAVCAN ESC enumeration: sent Begin request");
}
} else {
warnx("UAVCAN ESC enumeration: begun enumeration on all nodes.");
PX4_INFO("UAVCAN ESC enumeration: begun enumeration on all nodes.");
}
}
void UavcanServers::cb_enumeration_indication(const
uavcan::ReceivedDataStructure<uavcan::protocol::enumeration::Indication> &msg)
void
UavcanServers::cb_enumeration_indication(const uavcan::ReceivedDataStructure<uavcan::protocol::enumeration::Indication>
&msg)
{
// Called whenever an ESC thinks it has received user input.
warnx("UAVCAN ESC enumeration: got indication");
PX4_INFO("UAVCAN ESC enumeration: got indication");
if (!_esc_enumeration_active) {
// Ignore any messages received when we're not expecting them
@@ -867,33 +860,33 @@ void UavcanServers::cb_enumeration_indication(const
for (; i < _esc_enumeration_index; i++) {
if (_esc_enumeration_ids[i] == msg.getSrcNodeID().get()) {
warnx("UAVCAN ESC enumeration: already enumerated ESC ID %hhu as index %d, ignored", _esc_enumeration_ids[i], i);
PX4_INFO("UAVCAN ESC enumeration: already enumerated ESC ID %hhu as index %d, ignored", _esc_enumeration_ids[i], i);
return;
}
}
uavcan::protocol::param::GetSet::Request req;
req.name =
msg.parameter_name; // 'esc_index' or something alike, the name is not standardized
req.name = msg.parameter_name; // 'esc_index' or something alike, the name is not standardized
req.value.to<uavcan::protocol::param::Value::Tag::integer_value>() = i;
int call_res = _enumeration_getset_client.call(msg.getSrcNodeID(), req);
if (call_res < 0) {
warnx("UAVCAN ESC enumeration: couldn't send GetSet: %d", call_res);
PX4_ERR("UAVCAN ESC enumeration: couldn't send GetSet: %d", call_res);
} else {
warnx("UAVCAN ESC enumeration: sent GetSet to node %hhu (index %d)", _esc_enumeration_ids[i], i);
PX4_INFO("UAVCAN ESC enumeration: sent GetSet to node %hhu (index %d)", _esc_enumeration_ids[i], i);
}
}
void UavcanServers::cb_enumeration_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result)
void
UavcanServers::cb_enumeration_getset(const uavcan::ServiceCallResult<uavcan::protocol::param::GetSet> &result)
{
if (!result.isSuccessful()) {
warnx("UAVCAN ESC enumeration: save request for node %hhu timed out.", result.getCallID().server_node_id.get());
PX4_ERR("UAVCAN ESC enumeration: save request for node %hhu timed out.", result.getCallID().server_node_id.get());
} else {
warnx("UAVCAN ESC enumeration: save request for node %hhu completed OK.", result.getCallID().server_node_id.get());
PX4_INFO("UAVCAN ESC enumeration: save request for node %hhu completed OK.", result.getCallID().server_node_id.get());
uavcan::protocol::param::GetSet::Response resp = result.getResponse();
uint8_t esc_index = (uint8_t)resp.value.to<uavcan::protocol::param::Value::Tag::integer_value>();
@@ -907,41 +900,43 @@ void UavcanServers::cb_enumeration_getset(const uavcan::ServiceCallResult<uavcan
int call_res = _enumeration_save_client.call(result.getCallID().server_node_id, opcode_req);
if (call_res < 0) {
warnx("UAVCAN ESC enumeration: couldn't send ExecuteOpcode: %d", call_res);
PX4_ERR("UAVCAN ESC enumeration: couldn't send ExecuteOpcode: %d", call_res);
} else {
warnx("UAVCAN ESC enumeration: sent ExecuteOpcode to node %hhu (index %hhu)", _esc_enumeration_ids[esc_index],
esc_index);
PX4_INFO("UAVCAN ESC enumeration: sent ExecuteOpcode to node %hhu (index %hhu)", _esc_enumeration_ids[esc_index],
esc_index);
}
}
}
void UavcanServers::cb_enumeration_save(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result)
void
UavcanServers::cb_enumeration_save(const uavcan::ServiceCallResult<uavcan::protocol::param::ExecuteOpcode> &result)
{
const bool this_is_the_last_one =
_esc_enumeration_index >= uavcan::equipment::esc::RawCommand::FieldTypes::cmd::MaxSize - 1 ||
_esc_enumeration_index >= _esc_count;
(_esc_enumeration_index >= uavcan::equipment::esc::RawCommand::FieldTypes::cmd::MaxSize - 1) ||
(_esc_enumeration_index >= _esc_count);
if (!result.isSuccessful()) {
warnx("UAVCAN ESC enumeration: save request for node %hhu timed out.", result.getCallID().server_node_id.get());
PX4_ERR("UAVCAN ESC enumeration: save request for node %hhu timed out.", result.getCallID().server_node_id.get());
beep(BeepFrequencyError);
} else if (!result.getResponse().ok) {
warnx("UAVCAN ESC enumeration: save request for node %hhu rejected", result.getCallID().server_node_id.get());
PX4_ERR("UAVCAN ESC enumeration: save request for node %hhu rejected", result.getCallID().server_node_id.get());
beep(BeepFrequencyError);
} else {
warnx("UAVCAN ESC enumeration: save request for node %hhu completed OK.", result.getCallID().server_node_id.get());
PX4_INFO("UAVCAN ESC enumeration: save request for node %hhu completed OK.", result.getCallID().server_node_id.get());
beep(this_is_the_last_one ? BeepFrequencySuccess : BeepFrequencyGenericIndication);
}
warnx("UAVCAN ESC enumeration: completed %hhu of %hhu", _esc_enumeration_index, _esc_count);
PX4_INFO("UAVCAN ESC enumeration: completed %hhu of %hhu", _esc_enumeration_index, _esc_count);
if (this_is_the_last_one) {
_esc_enumeration_active = false;
// Tell all ESCs to stop enumerating
uavcan::protocol::enumeration::Begin::Request req;
// TODO: Incorrect implementation; the parameter name field should be left empty.
// Leaving it as-is to avoid breaking compatibility with non-compliant nodes.
req.parameter_name = "esc_index";
@@ -949,15 +944,16 @@ void UavcanServers::cb_enumeration_save(const uavcan::ServiceCallResult<uavcan::
int call_res = _enumeration_client.call(get_next_active_node_id(0), req);
if (call_res < 0) {
warnx("UAVCAN ESC enumeration: couldn't send Begin request to stop enumeration: %d", call_res);
PX4_ERR("UAVCAN ESC enumeration: couldn't send Begin request to stop enumeration: %d", call_res);
} else {
warnx("UAVCAN ESC enumeration: sent Begin request to stop enumeration");
PX4_INFO("UAVCAN ESC enumeration: sent Begin request to stop enumeration");
}
}
}
void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_path)
void
UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_path)
{
/*
Copy the ROMFS firmware directory to the appropriate location on SD, without
@@ -1025,14 +1021,14 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
size_t srcpath_dev_len = romfs_path_len + 1 + dev_dirname_len;
if (srcpath_dev_len > maxlen) {
warnx("dev: srcpath '%s/%s' too long", romfs_path, dev_dirent->d_name);
PX4_WARN("dev: srcpath '%s/%s' too long", romfs_path, dev_dirent->d_name);
continue;
}
size_t dstpath_dev_len = sd_path_len + 1 + dev_dirname_len;
if (dstpath_dev_len > maxlen) {
warnx("dev: dstpath '%s/%s' too long", sd_path, dev_dirent->d_name);
PX4_WARN("dev: dstpath '%s/%s' too long", sd_path, dev_dirent->d_name);
continue;
}
@@ -1044,7 +1040,7 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
rv = mkdir(dstpath, S_IRWXU | S_IRWXG | S_IRWXO);
if (rv != 0) {
warnx("dev: couldn't create '%s'", dstpath);
PX4_ERR("dev: couldn't create '%s'", dstpath);
continue;
}
}
@@ -1056,7 +1052,7 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
DIR *const dev_dir = opendir(srcpath);
if (!dev_dir) {
warnx("dev: couldn't open '%s'", srcpath);
PX4_ERR("dev: couldn't open '%s'", srcpath);
continue;
}
@@ -1074,14 +1070,14 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
size_t srcpath_ver_len = srcpath_dev_len + 1 + ver_dirname_len;
if (srcpath_ver_len > maxlen) {
warnx("ver: srcpath '%s/%s' too long", srcpath, ver_dirent->d_name);
PX4_ERR("ver: srcpath '%s/%s' too long", srcpath, ver_dirent->d_name);
continue;
}
size_t dstpath_ver_len = dstpath_dev_len + 1 + ver_dirname_len;
if (dstpath_ver_len > maxlen) {
warnx("ver: dstpath '%s/%s' too long", dstpath, ver_dirent->d_name);
PX4_ERR("ver: dstpath '%s/%s' too long", dstpath, ver_dirent->d_name);
continue;
}
@@ -1093,7 +1089,7 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
rv = mkdir(dstpath, S_IRWXU | S_IRWXG | S_IRWXO);
if (rv != 0) {
warnx("ver: couldn't create '%s'", dstpath);
PX4_ERR("ver: couldn't create '%s'", dstpath);
continue;
}
}
@@ -1107,7 +1103,7 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
DIR *const src_ver_dir = opendir(srcpath);
if (!src_ver_dir) {
warnx("ver: couldn't open '%s'", srcpath);
PX4_ERR("ver: couldn't open '%s'", srcpath);
continue;
}
@@ -1129,7 +1125,7 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
DIR *const dst_ver_dir = opendir(dstpath);
if (!dst_ver_dir) {
warnx("unlink: couldn't open '%s'", dstpath);
PX4_ERR("unlink: couldn't open '%s'", dstpath);
} else {
struct dirent *fw_dirent = NULL;
@@ -1153,14 +1149,14 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
if (dstpath_fw_len > maxlen) {
// sizeof(prefix) includes trailing NUL, cancelling out the +1 for the path separator
warnx("unlink: path '%s/%s' too long", dstpath, fw_dirent->d_name);
PX4_ERR("unlink: path '%s/%s' too long", dstpath, fw_dirent->d_name);
} else {
// File name starts with "_", delete it.
dstpath[dstpath_ver_len] = '/';
memcpy(&dstpath[dstpath_ver_len + 1], fw_dirent->d_name, dst_fw_len + 1);
unlink(dstpath);
warnx("unlink: removed '%s'", dstpath);
PX4_ERR("unlink: removed '%s'", dstpath);
}
} else {
@@ -1178,10 +1174,10 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
size_t dstpath_fw_len = dstpath_ver_len + fw_len;
if (srcpath_fw_len > maxlen) {
warnx("copy: srcpath '%s/%s' too long", srcpath, src_fw_dirent->d_name);
PX4_ERR("copy: srcpath '%s/%s' too long", srcpath, src_fw_dirent->d_name);
} else if (dstpath_fw_len > maxlen) {
warnx("copy: dstpath '%s/%s' too long", dstpath, src_fw_dirent->d_name);
PX4_ERR("copy: dstpath '%s/%s' too long", dstpath, src_fw_dirent->d_name);
} else {
// All OK, make the paths and copy the file
@@ -1194,10 +1190,10 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
rv = copyFw(dstpath, srcpath);
if (rv != 0) {
warnx("copy: '%s' -> '%s' failed: %d", srcpath, dstpath, rv);
PX4_ERR("copy: '%s' -> '%s' failed: %d", srcpath, dstpath, rv);
} else {
warnx("copy: '%s' -> '%s' succeeded", srcpath, dstpath);
PX4_INFO("copy: '%s' -> '%s' succeeded", srcpath, dstpath);
}
}
}
@@ -1211,24 +1207,24 @@ void UavcanServers::unpackFwFromROMFS(const char *sd_path, const char *romfs_pat
(void)closedir(romfs_dir);
}
int UavcanServers::copyFw(const char *dst, const char *src)
int
UavcanServers::copyFw(const char *dst, const char *src)
{
int rv = 0;
int dfd, sfd;
uint8_t buffer[512];
uint8_t buffer[512] {};
dfd = open(dst, O_WRONLY | O_CREAT, 0666);
int dfd = open(dst, O_WRONLY | O_CREAT, 0666);
if (dfd < 0) {
warnx("copyFw: couldn't open dst");
PX4_ERR("copyFw: couldn't open dst");
return -errno;
}
sfd = open(src, O_RDONLY, 0);
int sfd = open(src, O_RDONLY, 0);
if (sfd < 0) {
(void)close(dfd);
warnx("copyFw: couldn't open src");
PX4_ERR("copyFw: couldn't open src");
return -errno;
}
@@ -1238,7 +1234,7 @@ int UavcanServers::copyFw(const char *dst, const char *src)
size = read(sfd, buffer, sizeof(buffer));
if (size < 0) {
warnx("copyFw: couldn't read");
PX4_ERR("copyFw: couldn't read");
rv = -errno;
} else if (size > 0) {
@@ -1251,7 +1247,7 @@ int UavcanServers::copyFw(const char *dst, const char *src)
written = write(dfd, &buffer[total_written], remaining);
if (written < 0) {
warnx("copyFw: couldn't write");
PX4_ERR("copyFw: couldn't write");
rv = -errno;
} else {