2019-05-26 22:46:41 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
AP_Periph can support
|
|
|
|
*/
|
2021-08-26 05:46:51 -03:00
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2022-02-21 00:40:40 -04:00
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
2019-05-26 22:46:41 -03:00
|
|
|
#include "AP_Periph.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <drivers/stm32/canard_stm32.h>
|
2019-10-18 21:28:09 -03:00
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2019-11-06 02:56:13 -04:00
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
2020-01-16 08:16:28 -04:00
|
|
|
#include <AP_Common/AP_FWVersion.h>
|
2021-11-11 07:14:19 -04:00
|
|
|
#include <dronecan_msgs.h>
|
2020-09-12 16:00:22 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2022-02-21 00:40:40 -04:00
|
|
|
#include <hal.h>
|
2020-07-31 10:37:18 -03:00
|
|
|
#include <AP_HAL_ChibiOS/CANIface.h>
|
2020-09-12 16:00:22 -03:00
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/stm32_util.h>
|
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/watchdog.h>
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
#include <AP_HAL_SITL/CANSocketIface.h>
|
|
|
|
#endif
|
|
|
|
|
2022-08-17 21:29:00 -03:00
|
|
|
#define IFACE_ALL ((1U<<(HAL_NUM_CAN_IFACES+1U))-1U)
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
#include "i2c.h"
|
2019-10-18 21:28:09 -03:00
|
|
|
#include <utility>
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2021-04-30 22:36:42 -03:00
|
|
|
#if HAL_NUM_CAN_IFACES >= 2
|
|
|
|
#include <AP_CANManager/AP_CANSensor.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-29 08:49:13 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
extern const HAL_SITL &hal;
|
|
|
|
#else
|
2019-05-26 22:46:41 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2022-04-29 08:49:13 -03:00
|
|
|
#endif
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
extern AP_Periph_FW periph;
|
|
|
|
|
2021-09-02 07:27:09 -03:00
|
|
|
#ifndef HAL_PERIPH_LOOP_DELAY_US
|
|
|
|
// delay between can loop updates. This needs to be longer on F4
|
|
|
|
#if defined(STM32H7)
|
|
|
|
#define HAL_PERIPH_LOOP_DELAY_US 64
|
|
|
|
#else
|
2022-04-15 00:31:29 -03:00
|
|
|
#define HAL_PERIPH_LOOP_DELAY_US 1024
|
2021-09-02 07:27:09 -03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-07-23 15:50:34 -03:00
|
|
|
#define DEBUG_PKTS 0
|
2021-07-16 13:16:24 -03:00
|
|
|
|
2022-10-13 15:23:33 -03:00
|
|
|
#ifndef HAL_PERIPH_SUPPORT_LONG_CAN_PRINTF
|
|
|
|
// When enabled, can_printf() strings longer than the droneCAN max text length (90 chars)
|
|
|
|
// are split into multiple packets instead of truncating the string. This is
|
|
|
|
// especially helpful with HAL_GCS_ENABLED where libraries use the mavlink
|
|
|
|
// send_text() method where we support strings up to 256 chars by splitting them
|
|
|
|
// up into multiple 50 char mavlink packets.
|
|
|
|
#define HAL_PERIPH_SUPPORT_LONG_CAN_PRINTF (BOARD_FLASH_SIZE >= 1024)
|
|
|
|
#endif
|
|
|
|
|
2021-07-11 12:18:30 -03:00
|
|
|
static struct instance_t {
|
|
|
|
uint8_t index;
|
2022-08-16 03:27:57 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2022-10-07 00:19:55 -03:00
|
|
|
AP_HAL::CANIface* iface;
|
2022-08-16 03:27:57 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
HALSITL::CANIface* iface;
|
|
|
|
#endif
|
|
|
|
} instances[HAL_NUM_CAN_IFACES];
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-06-22 01:52:18 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS && defined(HAL_GPIO_PIN_TERMCAN1) && (HAL_NUM_CAN_IFACES >= 2)
|
2021-07-16 13:16:24 -03:00
|
|
|
static ioline_t can_term_lines[] = {
|
|
|
|
HAL_GPIO_PIN_TERMCAN1
|
|
|
|
|
|
|
|
#if HAL_NUM_CAN_IFACES > 2
|
|
|
|
#ifdef HAL_GPIO_PIN_TERMCAN2
|
|
|
|
,HAL_GPIO_PIN_TERMCAN2
|
|
|
|
#else
|
|
|
|
#error "Only one Can Terminator defined with over two CAN Ifaces"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_NUM_CAN_IFACES > 2
|
|
|
|
#ifdef HAL_GPIO_PIN_TERMCAN3
|
|
|
|
,HAL_GPIO_PIN_TERMCAN3
|
|
|
|
#else
|
|
|
|
#error "Only two Can Terminator defined with three CAN Ifaces"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS && defined(HAL_GPIO_PIN_TERMCAN1)
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
#ifndef CAN_APP_NODE_NAME
|
2023-08-25 04:35:25 -03:00
|
|
|
#define CAN_APP_NODE_NAME "org.ardupilot." CHIBIOS_BOARD_NAME
|
2019-05-26 22:46:41 -03:00
|
|
|
#endif
|
|
|
|
|
2021-07-11 12:18:30 -03:00
|
|
|
#ifndef HAL_CAN_DEFAULT_NODE_ID
|
|
|
|
#define HAL_CAN_DEFAULT_NODE_ID CANARD_BROADCAST_NODE_ID
|
|
|
|
#endif
|
|
|
|
uint8_t PreferredNodeID = HAL_CAN_DEFAULT_NODE_ID;
|
|
|
|
|
2023-03-05 17:58:43 -04:00
|
|
|
#ifndef AP_PERIPH_PROBE_CONTINUOUS
|
|
|
|
#define AP_PERIPH_PROBE_CONTINUOUS 0
|
2019-11-06 05:09:04 -04:00
|
|
|
#endif
|
|
|
|
|
2021-04-30 22:36:42 -03:00
|
|
|
#ifndef AP_PERIPH_ENFORCE_AT_LEAST_ONE_PORT_IS_UAVCAN_1MHz
|
|
|
|
#define AP_PERIPH_ENFORCE_AT_LEAST_ONE_PORT_IS_UAVCAN_1MHz 1
|
|
|
|
#endif
|
|
|
|
|
2020-09-12 16:00:22 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2021-04-30 22:36:42 -03:00
|
|
|
ChibiOS::CANIface* AP_Periph_FW::can_iface_periph[HAL_NUM_CAN_IFACES];
|
2020-09-12 16:00:22 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2021-04-30 22:36:42 -03:00
|
|
|
HALSITL::CANIface* AP_Periph_FW::can_iface_periph[HAL_NUM_CAN_IFACES];
|
2020-09-12 16:00:22 -03:00
|
|
|
#endif
|
2021-04-30 22:36:42 -03:00
|
|
|
|
2023-03-10 20:46:34 -04:00
|
|
|
#if AP_CAN_SLCAN_ENABLED
|
2022-10-07 00:19:55 -03:00
|
|
|
SLCAN::CANIface AP_Periph_FW::slcan_interface;
|
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Node status variables
|
|
|
|
*/
|
|
|
|
static uavcan_protocol_NodeStatus node_status;
|
2023-07-07 04:36:52 -03:00
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
|
|
|
static dronecan_protocol_Stats protocol_stats;
|
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
/**
|
2019-11-18 04:22:43 -04:00
|
|
|
* Returns a pseudo random integer in a given range
|
2019-05-26 22:46:41 -03:00
|
|
|
*/
|
2019-11-18 04:22:43 -04:00
|
|
|
static uint16_t get_random_range(uint16_t range)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2019-11-18 04:22:43 -04:00
|
|
|
return get_random16() % range;
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
get cpu unique ID
|
|
|
|
*/
|
|
|
|
static void readUniqueID(uint8_t* out_uid)
|
|
|
|
{
|
2021-09-29 15:34:32 -03:00
|
|
|
uint8_t len = sizeof(uavcan_protocol_dynamic_node_id_Allocation::unique_id.data);
|
2019-05-26 22:46:41 -03:00
|
|
|
memset(out_uid, 0, len);
|
|
|
|
hal.util->get_system_id_unformatted(out_uid, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle a GET_NODE_INFO request
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_get_node_info(CanardInstance* canard_instance,
|
|
|
|
CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_GETNODEINFO_RESPONSE_MAX_SIZE] {};
|
|
|
|
uavcan_protocol_GetNodeInfoResponse pkt {};
|
|
|
|
|
2023-08-21 22:41:18 -03:00
|
|
|
node_status.uptime_sec = AP_HAL::millis() / 1000U;
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
pkt.status = node_status;
|
2019-10-24 22:05:30 -03:00
|
|
|
pkt.software_version.major = AP::fwversion().major;
|
|
|
|
pkt.software_version.minor = AP::fwversion().minor;
|
2019-10-20 23:35:01 -03:00
|
|
|
pkt.software_version.optional_field_flags = UAVCAN_PROTOCOL_SOFTWAREVERSION_OPTIONAL_FIELD_FLAG_VCS_COMMIT | UAVCAN_PROTOCOL_SOFTWAREVERSION_OPTIONAL_FIELD_FLAG_IMAGE_CRC;
|
|
|
|
pkt.software_version.vcs_commit = app_descriptor.git_hash;
|
|
|
|
uint32_t *crc = (uint32_t *)&pkt.software_version.image_crc;
|
|
|
|
crc[0] = app_descriptor.image_crc1;
|
|
|
|
crc[1] = app_descriptor.image_crc2;
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
readUniqueID(pkt.hardware_version.unique_id);
|
|
|
|
|
2019-10-24 08:14:29 -03:00
|
|
|
// use hw major/minor for APJ_BOARD_ID so we know what fw is
|
|
|
|
// compatible with this hardware
|
|
|
|
pkt.hardware_version.major = APJ_BOARD_ID >> 8;
|
|
|
|
pkt.hardware_version.minor = APJ_BOARD_ID & 0xFF;
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
if (g.serial_number > 0) {
|
|
|
|
hal.util->snprintf((char*)pkt.name.data, sizeof(pkt.name.data), "%s(%u)", CAN_APP_NODE_NAME, (unsigned)g.serial_number);
|
2020-12-17 01:46:31 -04:00
|
|
|
} else {
|
2021-09-29 15:34:32 -03:00
|
|
|
hal.util->snprintf((char*)pkt.name.data, sizeof(pkt.name.data), "%s", CAN_APP_NODE_NAME);
|
2020-12-17 01:46:31 -04:00
|
|
|
}
|
2021-09-29 15:34:32 -03:00
|
|
|
pkt.name.len = strnlen((char*)pkt.name.data, sizeof(pkt.name.data));
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = uavcan_protocol_GetNodeInfoResponse_encode(&pkt, buffer, !canfdout());
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-07-31 07:39:12 -03:00
|
|
|
const int16_t resp_res = canardRequestOrRespond(canard_instance,
|
2019-05-26 22:46:41 -03:00
|
|
|
transfer->source_node_id,
|
|
|
|
UAVCAN_PROTOCOL_GETNODEINFO_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_GETNODEINFO_ID,
|
|
|
|
&transfer->transfer_id,
|
|
|
|
transfer->priority,
|
|
|
|
CanardResponse,
|
|
|
|
&buffer[0],
|
2021-05-17 14:26:34 -03:00
|
|
|
total_size
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
, IFACE_ALL
|
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-08-26 06:53:59 -03:00
|
|
|
, canfdout()
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
|
|
|
);
|
2019-05-26 22:46:41 -03:00
|
|
|
if (resp_res <= 0) {
|
|
|
|
printf("Could not respond to GetNodeInfo: %d\n", resp_res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle parameter GetSet request
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_param_getset(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2019-10-20 19:11:04 -03:00
|
|
|
// param fetch all can take a long time, so pat watchdog
|
|
|
|
stm32_watchdog_pat();
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
uavcan_protocol_param_GetSetRequest req;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (uavcan_protocol_param_GetSetRequest_decode(transfer, &req)) {
|
2019-05-26 22:46:41 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uavcan_protocol_param_GetSetResponse pkt {};
|
|
|
|
|
|
|
|
AP_Param *vp;
|
|
|
|
enum ap_var_type ptype;
|
|
|
|
|
2019-10-23 04:56:51 -03:00
|
|
|
if (req.name.len != 0 && req.name.len > AP_MAX_NAME_SIZE) {
|
2019-05-26 22:46:41 -03:00
|
|
|
vp = nullptr;
|
2019-10-23 04:56:51 -03:00
|
|
|
} else if (req.name.len != 0 && req.name.len <= AP_MAX_NAME_SIZE) {
|
2021-09-29 15:34:32 -03:00
|
|
|
memcpy((char *)pkt.name.data, (char *)req.name.data, req.name.len);
|
|
|
|
vp = AP_Param::find((char *)pkt.name.data, &ptype);
|
2019-05-26 22:46:41 -03:00
|
|
|
} else {
|
2021-01-21 06:55:55 -04:00
|
|
|
AP_Param::ParamToken token {};
|
2019-05-26 22:46:41 -03:00
|
|
|
vp = AP_Param::find_by_index(req.index, &ptype, &token);
|
|
|
|
if (vp != nullptr) {
|
2021-09-29 15:34:32 -03:00
|
|
|
vp->copy_name_token(token, (char *)pkt.name.data, AP_MAX_NAME_SIZE+1, true);
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vp != nullptr && req.name.len != 0 && req.value.union_tag != UAVCAN_PROTOCOL_PARAM_VALUE_EMPTY) {
|
|
|
|
// param set
|
|
|
|
switch (ptype) {
|
|
|
|
case AP_PARAM_INT8:
|
|
|
|
if (req.value.union_tag != UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
((AP_Int8 *)vp)->set_and_save_ifchanged(req.value.integer_value);
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT16:
|
|
|
|
if (req.value.union_tag != UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
((AP_Int16 *)vp)->set_and_save_ifchanged(req.value.integer_value);
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT32:
|
|
|
|
if (req.value.union_tag != UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
((AP_Int32 *)vp)->set_and_save_ifchanged(req.value.integer_value);
|
|
|
|
break;
|
|
|
|
case AP_PARAM_FLOAT:
|
|
|
|
if (req.value.union_tag != UAVCAN_PROTOCOL_PARAM_VALUE_REAL_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
((AP_Float *)vp)->set_and_save_ifchanged(req.value.real_value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vp != nullptr) {
|
|
|
|
switch (ptype) {
|
|
|
|
case AP_PARAM_INT8:
|
|
|
|
pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE;
|
|
|
|
pkt.value.integer_value = ((AP_Int8 *)vp)->get();
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT16:
|
|
|
|
pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE;
|
|
|
|
pkt.value.integer_value = ((AP_Int16 *)vp)->get();
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT32:
|
|
|
|
pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE;
|
|
|
|
pkt.value.integer_value = ((AP_Int32 *)vp)->get();
|
|
|
|
break;
|
|
|
|
case AP_PARAM_FLOAT:
|
|
|
|
pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_REAL_VALUE;
|
|
|
|
pkt.value.real_value = ((AP_Float *)vp)->get();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2021-09-29 15:34:32 -03:00
|
|
|
pkt.name.len = strnlen((char *)pkt.name.data, sizeof(pkt.name.data));
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
2020-11-11 02:33:12 -04:00
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_PARAM_GETSET_RESPONSE_MAX_SIZE] {};
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = uavcan_protocol_param_GetSetResponse_encode(&pkt, buffer, !canfdout());
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-07-31 07:39:12 -03:00
|
|
|
canardRequestOrRespond(canard_instance,
|
2019-05-26 22:46:41 -03:00
|
|
|
transfer->source_node_id,
|
|
|
|
UAVCAN_PROTOCOL_PARAM_GETSET_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_PARAM_GETSET_ID,
|
|
|
|
&transfer->transfer_id,
|
|
|
|
transfer->priority,
|
|
|
|
CanardResponse,
|
|
|
|
&buffer[0],
|
2021-05-17 14:26:34 -03:00
|
|
|
total_size
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
, IFACE_ALL
|
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-08-26 06:53:59 -03:00
|
|
|
,canfdout()
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
|
|
|
);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle parameter executeopcode request
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_param_executeopcode(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
uavcan_protocol_param_ExecuteOpcodeRequest req;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (uavcan_protocol_param_ExecuteOpcodeRequest_decode(transfer, &req)) {
|
2019-05-26 22:46:41 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req.opcode == UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_REQUEST_OPCODE_ERASE) {
|
|
|
|
StorageManager::erase();
|
|
|
|
AP_Param::erase_all();
|
|
|
|
AP_Param::load_all();
|
|
|
|
AP_Param::setup_sketch_defaults();
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_GPS
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&gps, gps.var_info);
|
2019-05-26 22:46:41 -03:00
|
|
|
#endif
|
2020-11-21 01:29:10 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_BATTERY
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&battery, battery_lib.var_info);
|
2020-11-21 01:29:10 -04:00
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_MAG
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&compass, compass.var_info);
|
2019-05-26 22:46:41 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_BARO
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&baro, baro.var_info);
|
2019-10-03 08:02:56 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_AIRSPEED
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&airspeed, airspeed.var_info);
|
2019-10-19 01:36:14 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_RANGEFINDER
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Param::setup_object_defaults(&rangefinder, rangefinder.var_info);
|
2019-05-26 22:46:41 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uavcan_protocol_param_ExecuteOpcodeResponse pkt {};
|
|
|
|
|
|
|
|
pkt.ok = true;
|
|
|
|
|
2020-11-11 02:33:12 -04:00
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_RESPONSE_MAX_SIZE] {};
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = uavcan_protocol_param_ExecuteOpcodeResponse_encode(&pkt, buffer, !canfdout());
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-07-31 07:39:12 -03:00
|
|
|
canardRequestOrRespond(canard_instance,
|
2019-05-26 22:46:41 -03:00
|
|
|
transfer->source_node_id,
|
|
|
|
UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_ID,
|
|
|
|
&transfer->transfer_id,
|
|
|
|
transfer->priority,
|
|
|
|
CanardResponse,
|
|
|
|
&buffer[0],
|
2021-05-17 14:26:34 -03:00
|
|
|
total_size
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
, IFACE_ALL
|
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-08-26 06:53:59 -03:00
|
|
|
,canfdout()
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
|
|
|
);
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_begin_firmware_update(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2019-10-21 00:47:06 -03:00
|
|
|
#if HAL_RAM_RESERVE_START >= 256
|
|
|
|
// setup information on firmware request at start of ram
|
|
|
|
struct app_bootloader_comms *comms = (struct app_bootloader_comms *)HAL_RAM0_START;
|
|
|
|
memset(comms, 0, sizeof(struct app_bootloader_comms));
|
|
|
|
comms->magic = APP_BOOTLOADER_COMMS_MAGIC;
|
|
|
|
|
2023-04-09 22:32:27 -03:00
|
|
|
uavcan_protocol_file_BeginFirmwareUpdateRequest req;
|
|
|
|
if (uavcan_protocol_file_BeginFirmwareUpdateRequest_decode(transfer, &req)) {
|
2019-10-21 00:47:06 -03:00
|
|
|
return;
|
|
|
|
}
|
2023-04-09 22:32:27 -03:00
|
|
|
|
|
|
|
comms->server_node_id = req.source_node_id;
|
2019-10-21 00:47:06 -03:00
|
|
|
if (comms->server_node_id == 0) {
|
|
|
|
comms->server_node_id = transfer->source_node_id;
|
|
|
|
}
|
2023-04-09 22:32:27 -03:00
|
|
|
memcpy(comms->path, req.image_file_remote_path.path.data, req.image_file_remote_path.path.len);
|
2023-07-31 07:39:12 -03:00
|
|
|
comms->my_node_id = canardGetLocalNodeID(canard_instance);
|
2019-10-21 00:47:06 -03:00
|
|
|
|
2020-11-11 02:33:12 -04:00
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_MAX_SIZE] {};
|
2019-10-21 00:47:06 -03:00
|
|
|
uavcan_protocol_file_BeginFirmwareUpdateResponse reply {};
|
|
|
|
reply.error = UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_ERROR_OK;
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
uint32_t total_size = uavcan_protocol_file_BeginFirmwareUpdateResponse_encode(&reply, buffer, !canfdout());
|
2023-07-31 07:39:12 -03:00
|
|
|
canardRequestOrRespond(canard_instance,
|
2019-10-21 00:47:06 -03:00
|
|
|
transfer->source_node_id,
|
|
|
|
UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID,
|
|
|
|
&transfer->transfer_id,
|
|
|
|
transfer->priority,
|
|
|
|
CanardResponse,
|
|
|
|
&buffer[0],
|
2021-05-17 14:26:34 -03:00
|
|
|
total_size
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
,IFACE_ALL
|
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-08-26 06:53:59 -03:00
|
|
|
,canfdout()
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
|
|
|
);
|
2019-10-21 00:47:06 -03:00
|
|
|
uint8_t count = 50;
|
|
|
|
while (count--) {
|
|
|
|
processTx();
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
// instant reboot, with backup register used to give bootloader
|
2019-10-21 00:47:06 -03:00
|
|
|
// the node_id
|
2023-08-26 06:53:59 -03:00
|
|
|
prepare_reboot();
|
2020-09-12 16:00:22 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2023-07-31 07:39:12 -03:00
|
|
|
set_fast_reboot((rtc_boot_magic)(RTC_BOOT_CANBL | canardGetLocalNodeID(canard_instance)));
|
2019-05-26 22:46:41 -03:00
|
|
|
NVIC_SystemReset();
|
2020-09-12 16:00:22 -03:00
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_allocation_response(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
// Rule C - updating the randomized time interval
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.send_next_node_id_allocation_request_at_ms =
|
2023-08-21 22:41:18 -03:00
|
|
|
AP_HAL::millis() + UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MIN_REQUEST_PERIOD_MS +
|
2019-11-18 04:22:43 -04:00
|
|
|
get_random_range(UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MAX_FOLLOWUP_DELAY_MS);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
if (transfer->source_node_id == CANARD_BROADCAST_NODE_ID)
|
|
|
|
{
|
|
|
|
printf("Allocation request from another allocatee\n");
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.node_id_allocation_unique_id_offset = 0;
|
2019-05-26 22:46:41 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copying the unique ID from the message
|
2021-09-29 15:34:32 -03:00
|
|
|
uavcan_protocol_dynamic_node_id_Allocation msg;
|
|
|
|
|
|
|
|
uavcan_protocol_dynamic_node_id_Allocation_decode(transfer, &msg);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
// Obtaining the local unique ID
|
2021-09-29 15:34:32 -03:00
|
|
|
uint8_t my_unique_id[sizeof(msg.unique_id.data)];
|
2019-05-26 22:46:41 -03:00
|
|
|
readUniqueID(my_unique_id);
|
|
|
|
|
|
|
|
// Matching the received UID against the local one
|
2021-09-29 15:34:32 -03:00
|
|
|
if (memcmp(msg.unique_id.data, my_unique_id, msg.unique_id.len) != 0) {
|
2019-05-26 22:46:41 -03:00
|
|
|
printf("Mismatching allocation response\n");
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.node_id_allocation_unique_id_offset = 0;
|
2019-05-26 22:46:41 -03:00
|
|
|
return; // No match, return
|
|
|
|
}
|
|
|
|
|
2021-09-29 15:34:32 -03:00
|
|
|
if (msg.unique_id.len < sizeof(msg.unique_id.data)) {
|
2019-05-26 22:46:41 -03:00
|
|
|
// The allocator has confirmed part of unique ID, switching to the next stage and updating the timeout.
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.node_id_allocation_unique_id_offset = msg.unique_id.len;
|
|
|
|
dronecan.send_next_node_id_allocation_request_at_ms -= UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MIN_REQUEST_PERIOD_MS;
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2021-09-29 15:34:32 -03:00
|
|
|
printf("Matching allocation response: %d\n", msg.unique_id.len);
|
2019-05-26 22:46:41 -03:00
|
|
|
} else {
|
|
|
|
// Allocation complete - copying the allocated node ID from the message
|
2023-07-31 07:39:12 -03:00
|
|
|
canardSetLocalNodeID(canard_instance, msg.node_id);
|
2022-08-16 03:27:57 -03:00
|
|
|
printf("IF%d Node ID allocated: %d\n", dronecan.dna_interface, msg.node_id);
|
2021-07-16 13:16:24 -03:00
|
|
|
|
|
|
|
#if defined(HAL_PERIPH_ENABLE_GPS) && (HAL_NUM_CAN_IFACES >= 2) && GPS_MOVING_BASELINE
|
2023-08-26 06:53:59 -03:00
|
|
|
if (g.gps_mb_only_can_port) {
|
2021-07-16 13:16:24 -03:00
|
|
|
// we need to assign the unallocated port to be used for Moving Baseline only
|
2023-08-26 06:53:59 -03:00
|
|
|
gps_mb_can_port = (dronecan.dna_interface+1)%HAL_NUM_CAN_IFACES;
|
2022-08-16 03:27:57 -03:00
|
|
|
if (canardGetLocalNodeID(&dronecan.canard) == CANARD_BROADCAST_NODE_ID) {
|
2021-07-16 13:16:24 -03:00
|
|
|
// copy node id from the primary iface
|
2022-08-16 03:27:57 -03:00
|
|
|
canardSetLocalNodeID(&dronecan.canard, msg.node_id);
|
2021-07-16 13:16:24 -03:00
|
|
|
#ifdef HAL_GPIO_PIN_TERMCAN1
|
|
|
|
// also terminate the line as we don't have any other device on this port
|
2023-08-26 06:53:59 -03:00
|
|
|
palWriteLine(can_term_lines[gps_mb_can_port], 1);
|
2021-07-16 13:16:24 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:36:15 -03:00
|
|
|
|
2020-12-12 05:08:45 -04:00
|
|
|
#if defined(HAL_GPIO_PIN_SAFE_LED) || defined(HAL_PERIPH_ENABLE_RC_OUT)
|
2019-08-31 01:30:15 -03:00
|
|
|
static uint8_t safety_state;
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle SafetyState
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_safety_state(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-08-31 01:30:15 -03:00
|
|
|
{
|
|
|
|
ardupilot_indication_SafetyState req;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (ardupilot_indication_SafetyState_decode(transfer, &req)) {
|
2019-08-31 01:30:15 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
safety_state = req.status;
|
2020-12-12 05:08:45 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
2023-08-26 06:53:59 -03:00
|
|
|
rcout_handle_safety_state(safety_state);
|
2020-12-12 05:08:45 -04:00
|
|
|
#endif
|
2019-08-31 01:30:15 -03:00
|
|
|
}
|
2019-10-18 21:28:09 -03:00
|
|
|
#endif // HAL_GPIO_PIN_SAFE_LED
|
2019-08-31 01:30:15 -03:00
|
|
|
|
2021-01-22 19:48:38 -04:00
|
|
|
/*
|
|
|
|
handle ArmingStatus
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_arming_status(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2021-01-22 19:48:38 -04:00
|
|
|
{
|
|
|
|
uavcan_equipment_safety_ArmingStatus req;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (uavcan_equipment_safety_ArmingStatus_decode(transfer, &req)) {
|
2021-01-22 19:48:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
hal.util->set_soft_armed(req.status == UAVCAN_EQUIPMENT_SAFETY_ARMINGSTATUS_STATUS_FULLY_ARMED);
|
|
|
|
}
|
|
|
|
|
2019-10-21 08:11:47 -03:00
|
|
|
|
|
|
|
|
2020-12-29 01:06:44 -04:00
|
|
|
#if defined(AP_PERIPH_HAVE_LED_WITHOUT_NOTIFY) || defined(HAL_PERIPH_ENABLE_NOTIFY)
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::set_rgb_led(uint8_t red, uint8_t green, uint8_t blue)
|
2019-10-18 21:28:09 -03:00
|
|
|
{
|
2020-12-29 01:06:44 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_NOTIFY
|
2023-08-26 06:53:59 -03:00
|
|
|
notify.handle_rgb(red, green, blue);
|
2021-09-17 11:33:17 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
2023-08-26 06:53:59 -03:00
|
|
|
rcout_has_new_data_to_update = true;
|
2021-09-17 11:33:17 -03:00
|
|
|
#endif // HAL_PERIPH_ENABLE_RC_OUT
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif // HAL_PERIPH_ENABLE_NOTIFY
|
|
|
|
|
|
|
|
#ifdef HAL_PERIPH_NEOPIXEL_COUNT_WITHOUT_NOTIFY
|
|
|
|
hal.rcout->set_serial_led_rgb_data(HAL_PERIPH_NEOPIXEL_CHAN_WITHOUT_NOTIFY, -1, red, green, blue);
|
|
|
|
hal.rcout->serial_led_send(HAL_PERIPH_NEOPIXEL_CHAN_WITHOUT_NOTIFY);
|
|
|
|
#endif // HAL_PERIPH_NEOPIXEL_COUNT_WITHOUT_NOTIFY
|
|
|
|
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_NCP5623_LED_WITHOUT_NOTIFY
|
2019-10-18 21:28:09 -03:00
|
|
|
{
|
|
|
|
const uint8_t i2c_address = 0x38;
|
|
|
|
static AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev;
|
|
|
|
if (!dev) {
|
|
|
|
dev = std::move(hal.i2c_mgr->get_device(0, i2c_address));
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(dev->get_semaphore());
|
|
|
|
dev->set_retries(0);
|
|
|
|
uint8_t v = 0x3f; // enable LED
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x40 | red >> 3; // red
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x60 | green >> 3; // green
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x80 | blue >> 3; // blue
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
}
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif // HAL_PERIPH_ENABLE_NCP5623_LED_WITHOUT_NOTIFY
|
|
|
|
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_NCP5623_BGR_LED_WITHOUT_NOTIFY
|
2020-12-11 23:19:28 -04:00
|
|
|
{
|
|
|
|
const uint8_t i2c_address = 0x38;
|
|
|
|
static AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev;
|
|
|
|
if (!dev) {
|
|
|
|
dev = std::move(hal.i2c_mgr->get_device(0, i2c_address));
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(dev->get_semaphore());
|
|
|
|
dev->set_retries(0);
|
|
|
|
uint8_t v = 0x3f; // enable LED
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x40 | blue >> 3; // blue
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x60 | green >> 3; // green
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
v = 0x80 | red >> 3; // red
|
|
|
|
dev->transfer(&v, 1, nullptr, 0);
|
|
|
|
}
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif // HAL_PERIPH_ENABLE_NCP5623_BGR_LED_WITHOUT_NOTIFY
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_TOSHIBA_LED_WITHOUT_NOTIFY
|
2020-12-28 17:49:31 -04:00
|
|
|
{
|
|
|
|
#define TOSHIBA_LED_PWM0 0x01 // pwm0 register
|
|
|
|
#define TOSHIBA_LED_ENABLE 0x04 // enable register
|
|
|
|
#define TOSHIBA_LED_I2C_ADDR 0x55 // default I2C bus address
|
|
|
|
|
|
|
|
static AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev_toshiba;
|
|
|
|
if (!dev_toshiba) {
|
|
|
|
dev_toshiba = std::move(hal.i2c_mgr->get_device(0, TOSHIBA_LED_I2C_ADDR));
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(dev_toshiba->get_semaphore());
|
|
|
|
dev_toshiba->set_retries(0); // use 0 because this is running on main thread.
|
|
|
|
|
|
|
|
// enable the led
|
|
|
|
dev_toshiba->write_register(TOSHIBA_LED_ENABLE, 0x03);
|
|
|
|
|
|
|
|
/* 4-bit for each color */
|
|
|
|
uint8_t val[4] = {
|
|
|
|
TOSHIBA_LED_PWM0,
|
|
|
|
(uint8_t)(blue >> 4),
|
|
|
|
(uint8_t)(green / 16),
|
|
|
|
(uint8_t)(red / 16)
|
|
|
|
};
|
|
|
|
dev_toshiba->transfer(val, sizeof(val), nullptr, 0);
|
|
|
|
}
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif // HAL_PERIPH_ENABLE_TOSHIBA_LED_WITHOUT_NOTIFY
|
2019-10-18 21:28:09 -03:00
|
|
|
}
|
|
|
|
|
2019-09-09 18:37:42 -03:00
|
|
|
/*
|
|
|
|
handle lightscommand
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_lightscommand(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2019-09-09 18:37:42 -03:00
|
|
|
{
|
|
|
|
uavcan_equipment_indication_LightsCommand req;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (uavcan_equipment_indication_LightsCommand_decode(transfer, &req)) {
|
2019-09-09 18:37:42 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<req.commands.len; i++) {
|
|
|
|
uavcan_equipment_indication_SingleLightCommand &cmd = req.commands.data[i];
|
2019-10-09 18:05:08 -03:00
|
|
|
// to get the right color proportions we scale the green so that is uses the
|
|
|
|
// same number of bits as red and blue
|
2022-08-17 21:29:00 -03:00
|
|
|
uint8_t red = cmd.color.red<<3U;
|
|
|
|
uint8_t green = (cmd.color.green>>1U)<<3U;
|
|
|
|
uint8_t blue = cmd.color.blue<<3U;
|
2020-12-29 01:06:44 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_NOTIFY
|
2023-08-26 06:53:59 -03:00
|
|
|
const int8_t brightness = notify.get_rgb_led_brightness_percent();
|
2020-12-29 01:06:44 -04:00
|
|
|
#elif defined(AP_PERIPH_HAVE_LED_WITHOUT_NOTIFY)
|
2023-08-26 06:53:59 -03:00
|
|
|
const int8_t brightness = g.led_brightness;
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif
|
|
|
|
if (brightness != 100 && brightness >= 0) {
|
|
|
|
const float scale = brightness * 0.01;
|
2019-10-10 00:18:59 -03:00
|
|
|
red = constrain_int16(red * scale, 0, 255);
|
|
|
|
green = constrain_int16(green * scale, 0, 255);
|
|
|
|
blue = constrain_int16(blue * scale, 0, 255);
|
|
|
|
}
|
2019-10-18 21:28:09 -03:00
|
|
|
set_rgb_led(red, green, blue);
|
2019-09-09 18:37:42 -03:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 01:06:44 -04:00
|
|
|
#endif // AP_PERIPH_HAVE_LED_WITHOUT_NOTIFY
|
2019-09-09 18:37:42 -03:00
|
|
|
|
2020-12-12 05:08:45 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_esc_rawcommand(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2020-12-12 05:08:45 -04:00
|
|
|
{
|
|
|
|
uavcan_equipment_esc_RawCommand cmd;
|
2021-09-29 15:34:32 -03:00
|
|
|
if (uavcan_equipment_esc_RawCommand_decode(transfer, &cmd)) {
|
2020-12-12 05:08:45 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-08-26 06:53:59 -03:00
|
|
|
rcout_esc(cmd.cmd.data, cmd.cmd.len);
|
2022-11-28 00:45:59 -04:00
|
|
|
|
|
|
|
// Update internal copy for disabling output to ESC when CAN packets are lost
|
2023-08-26 06:53:59 -03:00
|
|
|
last_esc_num_channels = cmd.cmd.len;
|
|
|
|
last_esc_raw_command_ms = AP_HAL::millis();
|
2020-12-12 05:08:45 -04:00
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_act_command(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2020-12-12 05:08:45 -04:00
|
|
|
{
|
2023-04-09 22:32:27 -03:00
|
|
|
uavcan_equipment_actuator_ArrayCommand cmd;
|
|
|
|
if (uavcan_equipment_actuator_ArrayCommand_decode(transfer, &cmd)) {
|
2020-12-12 05:08:45 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:32:27 -03:00
|
|
|
for (uint8_t i=0; i < cmd.commands.len; i++) {
|
|
|
|
const auto &c = cmd.commands.data[i];
|
|
|
|
switch (c.command_type) {
|
2022-09-12 00:27:22 -03:00
|
|
|
case UAVCAN_EQUIPMENT_ACTUATOR_COMMAND_COMMAND_TYPE_UNITLESS:
|
2023-08-26 06:53:59 -03:00
|
|
|
rcout_srv_unitless(c.actuator_id, c.command_value);
|
2022-09-12 00:27:22 -03:00
|
|
|
break;
|
|
|
|
case UAVCAN_EQUIPMENT_ACTUATOR_COMMAND_COMMAND_TYPE_PWM:
|
2023-08-26 06:53:59 -03:00
|
|
|
rcout_srv_PWM(c.actuator_id, c.command_value);
|
2022-09-12 00:27:22 -03:00
|
|
|
break;
|
2020-12-12 05:08:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAL_PERIPH_ENABLE_RC_OUT
|
|
|
|
|
2021-09-17 11:33:17 -03:00
|
|
|
#if defined(HAL_PERIPH_ENABLE_NOTIFY)
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::handle_notify_state(CanardInstance* canard_instance, CanardRxTransfer* transfer)
|
2021-09-17 11:33:17 -03:00
|
|
|
{
|
|
|
|
ardupilot_indication_NotifyState msg;
|
2021-09-30 03:21:34 -03:00
|
|
|
if (ardupilot_indication_NotifyState_decode(transfer, &msg)) {
|
2021-09-17 11:33:17 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msg.aux_data.len == 2 && msg.aux_data_type == ARDUPILOT_INDICATION_NOTIFYSTATE_VEHICLE_YAW_EARTH_CENTIDEGREES) {
|
|
|
|
uint16_t tmp = 0;
|
|
|
|
memcpy(&tmp, msg.aux_data.data, sizeof(tmp));
|
2023-08-26 06:53:59 -03:00
|
|
|
yaw_earth = radians((float)tmp * 0.01f);
|
2021-09-17 11:33:17 -03:00
|
|
|
}
|
2023-08-26 06:53:59 -03:00
|
|
|
vehicle_state = msg.vehicle_state;
|
|
|
|
last_vehicle_state = AP_HAL::millis();
|
2021-09-17 11:33:17 -03:00
|
|
|
}
|
|
|
|
#endif // HAL_PERIPH_ENABLE_NOTIFY
|
|
|
|
|
2019-10-18 21:28:09 -03:00
|
|
|
#ifdef HAL_GPIO_PIN_SAFE_LED
|
2019-08-31 01:30:15 -03:00
|
|
|
/*
|
|
|
|
update safety LED
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::can_safety_LED_update(void)
|
2019-08-31 01:30:15 -03:00
|
|
|
{
|
|
|
|
static uint32_t last_update_ms;
|
|
|
|
switch (safety_state) {
|
|
|
|
case ARDUPILOT_INDICATION_SAFETYSTATE_STATUS_SAFETY_OFF:
|
|
|
|
palWriteLine(HAL_GPIO_PIN_SAFE_LED, SAFE_LED_ON);
|
|
|
|
break;
|
|
|
|
case ARDUPILOT_INDICATION_SAFETYSTATE_STATUS_SAFETY_ON: {
|
2023-08-21 22:41:18 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2019-08-31 01:30:15 -03:00
|
|
|
if (now - last_update_ms > 100) {
|
|
|
|
last_update_ms = now;
|
|
|
|
static uint8_t led_counter;
|
|
|
|
const uint16_t led_pattern = 0x5500;
|
|
|
|
led_counter = (led_counter+1) % 16;
|
|
|
|
palWriteLine(HAL_GPIO_PIN_SAFE_LED, (led_pattern & (1U << led_counter))?!SAFE_LED_ON:SAFE_LED_ON);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
palWriteLine(HAL_GPIO_PIN_SAFE_LED, !SAFE_LED_ON);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAL_GPIO_PIN_SAFE_LED
|
|
|
|
|
2019-08-31 01:49:38 -03:00
|
|
|
|
|
|
|
#ifdef HAL_GPIO_PIN_SAFE_BUTTON
|
2020-01-12 22:37:52 -04:00
|
|
|
#ifndef HAL_SAFE_BUTTON_ON
|
|
|
|
#define HAL_SAFE_BUTTON_ON 1
|
|
|
|
#endif
|
2019-08-31 01:49:38 -03:00
|
|
|
/*
|
|
|
|
update safety button
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::can_safety_button_update(void)
|
2019-08-31 01:49:38 -03:00
|
|
|
{
|
|
|
|
static uint32_t last_update_ms;
|
|
|
|
static uint8_t counter;
|
2023-08-21 22:41:18 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2019-08-31 01:49:38 -03:00
|
|
|
// send at 10Hz when pressed
|
2020-01-12 22:37:52 -04:00
|
|
|
if (palReadLine(HAL_GPIO_PIN_SAFE_BUTTON) != HAL_SAFE_BUTTON_ON) {
|
2019-08-31 01:49:38 -03:00
|
|
|
counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (now - last_update_ms < 100) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (counter < 255) {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_update_ms = now;
|
|
|
|
ardupilot_indication_Button pkt {};
|
|
|
|
pkt.button = ARDUPILOT_INDICATION_BUTTON_BUTTON_SAFETY;
|
|
|
|
pkt.press_time = counter;
|
|
|
|
|
2020-11-11 02:33:12 -04:00
|
|
|
uint8_t buffer[ARDUPILOT_INDICATION_BUTTON_MAX_SIZE] {};
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = ardupilot_indication_Button_encode(&pkt, buffer, !canfdout());
|
2019-08-31 01:49:38 -03:00
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
canard_broadcast(ARDUPILOT_INDICATION_BUTTON_SIGNATURE,
|
2023-07-03 21:12:19 -03:00
|
|
|
ARDUPILOT_INDICATION_BUTTON_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
&buffer[0],
|
|
|
|
total_size);
|
2019-08-31 01:49:38 -03:00
|
|
|
}
|
|
|
|
#endif // HAL_GPIO_PIN_SAFE_BUTTON
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
/**
|
|
|
|
* This callback is invoked by the library when a new message or request or response is received.
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::onTransferReceived(CanardInstance* canard_instance,
|
|
|
|
CanardRxTransfer* transfer)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2019-10-20 23:35:01 -03:00
|
|
|
#ifdef HAL_GPIO_PIN_LED_CAN1
|
|
|
|
palToggleLine(HAL_GPIO_PIN_LED_CAN1);
|
|
|
|
#endif
|
|
|
|
|
2023-04-09 18:34:59 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
|
|
|
// enable tao for decoding when not on CANFD
|
|
|
|
transfer->tao = !transfer->canfd;
|
|
|
|
#endif
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
/*
|
|
|
|
* Dynamic node ID allocation protocol.
|
|
|
|
* Taking this branch only if we don't have a node ID, ignoring otherwise.
|
|
|
|
*/
|
2023-07-31 07:39:12 -03:00
|
|
|
if (canardGetLocalNodeID(canard_instance) == CANARD_BROADCAST_NODE_ID) {
|
2019-05-26 22:46:41 -03:00
|
|
|
if (transfer->transfer_type == CanardTransferTypeBroadcast &&
|
|
|
|
transfer->data_type_id == UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID) {
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_allocation_response(canard_instance, transfer);
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (transfer->data_type_id) {
|
|
|
|
case UAVCAN_PROTOCOL_GETNODEINFO_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_get_node_info(canard_instance, transfer);
|
2019-05-26 22:46:41 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_begin_firmware_update(canard_instance, transfer);
|
2019-05-26 22:46:41 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UAVCAN_PROTOCOL_RESTARTNODE_ID:
|
|
|
|
printf("RestartNode\n");
|
|
|
|
hal.scheduler->delay(10);
|
2023-08-26 06:53:59 -03:00
|
|
|
prepare_reboot();
|
2020-09-12 16:00:22 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2019-05-26 22:46:41 -03:00
|
|
|
NVIC_SystemReset();
|
2020-09-12 16:00:22 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
HAL_SITL::actually_reboot();
|
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UAVCAN_PROTOCOL_PARAM_GETSET_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_param_getset(canard_instance, transfer);
|
2019-05-26 22:46:41 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_param_executeopcode(canard_instance, transfer);
|
2019-05-26 22:46:41 -03:00
|
|
|
break;
|
2019-08-31 00:36:15 -03:00
|
|
|
|
2020-12-29 01:06:44 -04:00
|
|
|
#if defined(HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY) || defined (HAL_PERIPH_ENABLE_NOTIFY)
|
2019-08-31 00:36:15 -03:00
|
|
|
case UAVCAN_EQUIPMENT_INDICATION_BEEPCOMMAND_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_beep_command(canard_instance, transfer);
|
2019-08-31 00:36:15 -03:00
|
|
|
break;
|
|
|
|
#endif
|
2019-08-31 01:30:15 -03:00
|
|
|
|
2020-12-12 05:08:45 -04:00
|
|
|
#if defined(HAL_GPIO_PIN_SAFE_LED) || defined(HAL_PERIPH_ENABLE_RC_OUT)
|
2019-08-31 01:30:15 -03:00
|
|
|
case ARDUPILOT_INDICATION_SAFETYSTATE_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_safety_state(canard_instance, transfer);
|
2019-08-31 01:30:15 -03:00
|
|
|
break;
|
|
|
|
#endif
|
2019-09-09 18:37:42 -03:00
|
|
|
|
2021-01-22 19:48:38 -04:00
|
|
|
case UAVCAN_EQUIPMENT_SAFETY_ARMINGSTATUS_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_arming_status(canard_instance, transfer);
|
2021-01-22 19:48:38 -04:00
|
|
|
break;
|
|
|
|
|
2019-10-21 08:11:47 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_GPS
|
|
|
|
case UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_RTCMStream(canard_instance, transfer);
|
2019-10-21 08:11:47 -03:00
|
|
|
break;
|
2021-07-11 03:47:18 -03:00
|
|
|
|
|
|
|
#if GPS_MOVING_BASELINE
|
|
|
|
case ARDUPILOT_GNSS_MOVINGBASELINEDATA_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_MovingBaselineData(canard_instance, transfer);
|
2021-07-11 03:47:18 -03:00
|
|
|
break;
|
|
|
|
#endif
|
2023-06-26 06:55:54 -03:00
|
|
|
#endif // HAL_PERIPH_ENABLE_GPS
|
|
|
|
|
|
|
|
#if AP_UART_MONITOR_ENABLED
|
|
|
|
case UAVCAN_TUNNEL_TARGETTED_ID:
|
2023-08-26 06:53:59 -03:00
|
|
|
handle_tunnel_Targetted(canard_instance, transfer);
|
2023-06-26 06:55:54 -03:00
|
|
|
break;
|
2019-10-21 08:11:47 -03:00
|
|
|
#endif
|
2023-06-26 06:55:54 -03:00
|
|
|
|
2020-12-29 01:06:44 -04:00
|
|
|
#if defined(AP_PERIPH_HAVE_LED_WITHOUT_NOTIFY) || defined(HAL_PERIPH_ENABLE_NOTIFY)
|
2019-09-09 18:37:42 -03:00
|
|
|
case UAVCAN_EQUIPMENT_INDICATION_LIGHTSCOMMAND_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_lightscommand(canard_instance, transfer);
|
2019-09-09 18:37:42 -03:00
|
|
|
break;
|
|
|
|
#endif
|
2020-12-12 05:08:45 -04:00
|
|
|
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
|
|
|
case UAVCAN_EQUIPMENT_ESC_RAWCOMMAND_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_esc_rawcommand(canard_instance, transfer);
|
2020-12-12 05:08:45 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UAVCAN_EQUIPMENT_ACTUATOR_ARRAYCOMMAND_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_act_command(canard_instance, transfer);
|
2020-12-12 05:08:45 -04:00
|
|
|
break;
|
|
|
|
#endif
|
2021-09-17 11:33:17 -03:00
|
|
|
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_NOTIFY
|
|
|
|
case ARDUPILOT_INDICATION_NOTIFYSTATE_ID:
|
2023-07-31 07:39:12 -03:00
|
|
|
handle_notify_state(canard_instance, transfer);
|
2021-09-17 11:33:17 -03:00
|
|
|
break;
|
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
/**
|
|
|
|
* This callback is invoked by the library when a new message or request or response is received.
|
|
|
|
*/
|
|
|
|
static void onTransferReceived_trampoline(CanardInstance* canard_instance,
|
|
|
|
CanardRxTransfer* transfer)
|
|
|
|
{
|
|
|
|
AP_Periph_FW *fw = (AP_Periph_FW *)canard_instance->user_reference;
|
|
|
|
fw->onTransferReceived(canard_instance, transfer);
|
|
|
|
}
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This callback is invoked by the library when it detects beginning of a new transfer on the bus that can be received
|
|
|
|
* by the local node.
|
|
|
|
* If the callback returns true, the library will receive the transfer.
|
|
|
|
* If the callback returns false, the library will ignore the transfer.
|
|
|
|
* All transfers that are addressed to other nodes are always ignored.
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
bool AP_Periph_FW::shouldAcceptTransfer(const CanardInstance* canard_instance,
|
|
|
|
uint64_t* out_data_type_signature,
|
|
|
|
uint16_t data_type_id,
|
|
|
|
CanardTransferType transfer_type,
|
|
|
|
uint8_t source_node_id)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
(void)source_node_id;
|
|
|
|
|
2023-07-31 07:39:12 -03:00
|
|
|
if (canardGetLocalNodeID(canard_instance) == CANARD_BROADCAST_NODE_ID)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we're in the process of allocation of dynamic node ID, accept only relevant transfers.
|
|
|
|
*/
|
|
|
|
if ((transfer_type == CanardTransferTypeBroadcast) &&
|
|
|
|
(data_type_id == UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID))
|
|
|
|
{
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (data_type_id) {
|
|
|
|
case UAVCAN_PROTOCOL_GETNODEINFO_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_GETNODEINFO_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
case UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
case UAVCAN_PROTOCOL_RESTARTNODE_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_RESTARTNODE_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
case UAVCAN_PROTOCOL_PARAM_GETSET_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_PARAM_GETSET_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
case UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_SIGNATURE;
|
|
|
|
return true;
|
2020-12-29 01:06:44 -04:00
|
|
|
#if defined(HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY) || defined (HAL_PERIPH_ENABLE_NOTIFY)
|
2019-08-31 00:36:15 -03:00
|
|
|
case UAVCAN_EQUIPMENT_INDICATION_BEEPCOMMAND_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_INDICATION_BEEPCOMMAND_SIGNATURE;
|
|
|
|
return true;
|
2019-08-31 01:30:15 -03:00
|
|
|
#endif
|
2020-12-12 05:08:45 -04:00
|
|
|
#if defined(HAL_GPIO_PIN_SAFE_LED) || defined(HAL_PERIPH_ENABLE_RC_OUT)
|
2019-08-31 01:30:15 -03:00
|
|
|
case ARDUPILOT_INDICATION_SAFETYSTATE_ID:
|
|
|
|
*out_data_type_signature = ARDUPILOT_INDICATION_SAFETYSTATE_SIGNATURE;
|
|
|
|
return true;
|
2019-09-09 18:37:42 -03:00
|
|
|
#endif
|
2021-01-22 19:48:38 -04:00
|
|
|
case UAVCAN_EQUIPMENT_SAFETY_ARMINGSTATUS_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_SAFETY_ARMINGSTATUS_SIGNATURE;
|
|
|
|
return true;
|
2020-12-29 01:06:44 -04:00
|
|
|
#if defined(AP_PERIPH_HAVE_LED_WITHOUT_NOTIFY) || defined(HAL_PERIPH_ENABLE_NOTIFY)
|
2019-09-09 18:37:42 -03:00
|
|
|
case UAVCAN_EQUIPMENT_INDICATION_LIGHTSCOMMAND_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_INDICATION_LIGHTSCOMMAND_SIGNATURE;
|
|
|
|
return true;
|
2019-08-31 00:36:15 -03:00
|
|
|
#endif
|
2019-10-21 08:11:47 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_GPS
|
|
|
|
case UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_SIGNATURE;
|
|
|
|
return true;
|
2021-07-11 03:47:18 -03:00
|
|
|
|
|
|
|
#if GPS_MOVING_BASELINE
|
|
|
|
case ARDUPILOT_GNSS_MOVINGBASELINEDATA_ID:
|
|
|
|
*out_data_type_signature = ARDUPILOT_GNSS_MOVINGBASELINEDATA_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
#endif
|
2023-06-26 06:55:54 -03:00
|
|
|
#endif // HAL_PERIPH_ENABLE_GPS
|
|
|
|
|
|
|
|
#if AP_UART_MONITOR_ENABLED
|
|
|
|
case UAVCAN_TUNNEL_TARGETTED_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_TUNNEL_TARGETTED_SIGNATURE;
|
|
|
|
return true;
|
2020-12-12 05:08:45 -04:00
|
|
|
#endif
|
2023-06-26 06:55:54 -03:00
|
|
|
|
2020-12-12 05:08:45 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
|
|
|
case UAVCAN_EQUIPMENT_ESC_RAWCOMMAND_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_ESC_RAWCOMMAND_SIGNATURE;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case UAVCAN_EQUIPMENT_ACTUATOR_ARRAYCOMMAND_ID:
|
|
|
|
*out_data_type_signature = UAVCAN_EQUIPMENT_ACTUATOR_ARRAYCOMMAND_SIGNATURE;
|
|
|
|
return true;
|
2021-09-17 11:33:17 -03:00
|
|
|
#endif
|
|
|
|
#if defined(HAL_PERIPH_ENABLE_NOTIFY)
|
|
|
|
case ARDUPILOT_INDICATION_NOTIFYSTATE_ID:
|
|
|
|
*out_data_type_signature = ARDUPILOT_INDICATION_NOTIFYSTATE_SIGNATURE;
|
|
|
|
return true;
|
2019-10-21 08:11:47 -03:00
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
static bool shouldAcceptTransfer_trampoline(const CanardInstance* canard_instance,
|
|
|
|
uint64_t* out_data_type_signature,
|
|
|
|
uint16_t data_type_id,
|
|
|
|
CanardTransferType transfer_type,
|
|
|
|
uint8_t source_node_id)
|
2021-06-19 13:19:07 -03:00
|
|
|
{
|
2023-08-26 06:53:59 -03:00
|
|
|
AP_Periph_FW *fw = (AP_Periph_FW *)canard_instance->user_reference;
|
|
|
|
return fw->shouldAcceptTransfer(canard_instance, out_data_type_signature, data_type_id, transfer_type, source_node_id);
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::cleanup_stale_transactions(uint64_t ×tamp_usec)
|
|
|
|
{
|
|
|
|
canardCleanupStaleTransfers(&dronecan.canard, timestamp_usec);
|
|
|
|
}
|
2021-07-23 15:50:34 -03:00
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
uint8_t *AP_Periph_FW::get_tid_ptr(uint32_t transfer_desc)
|
2021-07-23 15:50:34 -03:00
|
|
|
{
|
|
|
|
// check head
|
2022-08-16 03:27:57 -03:00
|
|
|
if (!dronecan.tid_map_head) {
|
|
|
|
dronecan.tid_map_head = (dronecan_protocol_t::tid_map*)calloc(1, sizeof(dronecan_protocol_t::tid_map));
|
|
|
|
if (dronecan.tid_map_head == nullptr) {
|
2021-07-23 15:50:34 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.tid_map_head->transfer_desc = transfer_desc;
|
|
|
|
dronecan.tid_map_head->next = nullptr;
|
|
|
|
return &dronecan.tid_map_head->tid;
|
|
|
|
} else if (dronecan.tid_map_head->transfer_desc == transfer_desc) {
|
|
|
|
return &dronecan.tid_map_head->tid;
|
2021-07-23 15:50:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// search through the list for an existing entry
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan_protocol_t::tid_map *tid_map_ptr = dronecan.tid_map_head;
|
2021-07-23 15:50:34 -03:00
|
|
|
while(tid_map_ptr->next) {
|
|
|
|
tid_map_ptr = tid_map_ptr->next;
|
|
|
|
if (tid_map_ptr->transfer_desc == transfer_desc) {
|
|
|
|
return &tid_map_ptr->tid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new entry, if not found
|
2022-08-16 03:27:57 -03:00
|
|
|
tid_map_ptr->next = (dronecan_protocol_t::tid_map*)calloc(1, sizeof(dronecan_protocol_t::tid_map));
|
2021-07-23 15:50:34 -03:00
|
|
|
if (tid_map_ptr->next == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tid_map_ptr->next->transfer_desc = transfer_desc;
|
|
|
|
tid_map_ptr->next->next = nullptr;
|
|
|
|
return &tid_map_ptr->next->tid;
|
|
|
|
}
|
|
|
|
|
2023-07-07 04:36:52 -03:00
|
|
|
bool AP_Periph_FW::canard_broadcast(uint64_t data_type_signature,
|
2023-07-03 21:12:19 -03:00
|
|
|
uint16_t data_type_id,
|
|
|
|
uint8_t priority,
|
|
|
|
const void* payload,
|
|
|
|
uint16_t payload_len)
|
2021-06-19 13:19:07 -03:00
|
|
|
{
|
2022-08-16 03:27:57 -03:00
|
|
|
if (canardGetLocalNodeID(&dronecan.canard) == CANARD_BROADCAST_NODE_ID) {
|
2023-07-07 04:36:52 -03:00
|
|
|
return false;
|
2022-08-16 03:27:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *tid_ptr = get_tid_ptr(MAKE_TRANSFER_DESCRIPTOR(data_type_signature, data_type_id, 0, CANARD_BROADCAST_NODE_ID));
|
|
|
|
if (tid_ptr == nullptr) {
|
2023-07-07 04:36:52 -03:00
|
|
|
return false;
|
2022-08-16 03:27:57 -03:00
|
|
|
}
|
2023-07-07 04:36:52 -03:00
|
|
|
|
|
|
|
const int16_t res = canardBroadcast(&dronecan.canard,
|
2022-08-16 03:27:57 -03:00
|
|
|
data_type_signature,
|
|
|
|
data_type_id,
|
|
|
|
tid_ptr,
|
|
|
|
priority,
|
|
|
|
payload,
|
|
|
|
payload_len
|
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
, IFACE_ALL // send over all ifaces
|
2021-07-23 15:50:34 -03:00
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-08-26 06:53:59 -03:00
|
|
|
, canfdout()
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
2022-08-16 03:27:57 -03:00
|
|
|
);
|
|
|
|
|
2021-07-23 15:50:34 -03:00
|
|
|
#if DEBUG_PKTS
|
2022-08-16 03:27:57 -03:00
|
|
|
if (res < 0) {
|
|
|
|
can_printf("Tx error %d\n", res);
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
2022-08-16 03:27:57 -03:00
|
|
|
#endif
|
2023-07-07 04:36:52 -03:00
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
|
|
|
if (res <= 0) {
|
|
|
|
protocol_stats.tx_errors++;
|
|
|
|
} else {
|
|
|
|
protocol_stats.tx_frames += res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return res > 0;
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::processTx(void)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2022-08-16 03:27:57 -03:00
|
|
|
for (const CanardCANFrame* txf = NULL; (txf = canardPeekTxQueue(&dronecan.canard)) != NULL;) {
|
|
|
|
AP_HAL::CANFrame txmsg {};
|
|
|
|
txmsg.dlc = AP_HAL::CANFrame::dataLengthToDlc(txf->data_len);
|
|
|
|
memcpy(txmsg.data, txf->data, txf->data_len);
|
|
|
|
txmsg.id = (txf->id | AP_HAL::CANFrame::FlagEFF);
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2022-08-16 03:27:57 -03:00
|
|
|
txmsg.canfd = txf->canfd;
|
2021-05-17 14:26:34 -03:00
|
|
|
#endif
|
2022-08-16 03:27:57 -03:00
|
|
|
// push message with 1s timeout
|
|
|
|
bool sent = true;
|
2023-08-21 22:41:18 -03:00
|
|
|
const uint64_t deadline = AP_HAL::micros64() + 1000000;
|
2022-08-16 03:27:57 -03:00
|
|
|
// try sending to all interfaces
|
2023-07-31 07:39:12 -03:00
|
|
|
for (auto &_ins : instances) {
|
|
|
|
if (_ins.iface == NULL) {
|
2022-08-16 03:27:57 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if CANARD_MULTI_IFACE
|
2023-07-31 07:39:12 -03:00
|
|
|
if (!(txf->iface_mask & (1U<<_ins.index))) {
|
2022-08-16 03:27:57 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if HAL_NUM_CAN_IFACES >= 2
|
2023-08-26 06:53:59 -03:00
|
|
|
if (can_protocol_cached[_ins.index] != AP_CAN::Protocol::DroneCAN) {
|
2022-08-16 03:27:57 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2023-07-31 07:39:12 -03:00
|
|
|
if (_ins.iface->send(txmsg, deadline, 0) <= 0) {
|
2022-08-16 03:27:57 -03:00
|
|
|
sent = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sent) {
|
|
|
|
canardPopTxQueue(&dronecan.canard);
|
|
|
|
dronecan.tx_fail_count = 0;
|
|
|
|
} else {
|
|
|
|
// just exit and try again later. If we fail 8 times in a row
|
|
|
|
// then start discarding to prevent the pool filling up
|
|
|
|
if (dronecan.tx_fail_count < 8) {
|
|
|
|
dronecan.tx_fail_count++;
|
2019-05-26 22:46:41 -03:00
|
|
|
} else {
|
2023-07-07 04:36:52 -03:00
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
|
|
|
protocol_stats.tx_errors++;
|
|
|
|
#endif
|
2022-08-16 03:27:57 -03:00
|
|
|
canardPopTxQueue(&dronecan.canard);
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
2022-08-16 03:27:57 -03:00
|
|
|
break;
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 04:36:52 -03:00
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::update_rx_protocol_stats(int16_t res)
|
2023-07-07 04:36:52 -03:00
|
|
|
{
|
|
|
|
switch (res) {
|
|
|
|
case CANARD_OK:
|
|
|
|
protocol_stats.rx_frames++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_OUT_OF_MEMORY:
|
|
|
|
protocol_stats.rx_error_oom++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_INTERNAL:
|
|
|
|
protocol_stats.rx_error_internal++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_INCOMPATIBLE_PACKET:
|
|
|
|
protocol_stats.rx_ignored_not_wanted++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_WRONG_ADDRESS:
|
|
|
|
protocol_stats.rx_ignored_wrong_address++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_NOT_WANTED:
|
|
|
|
protocol_stats.rx_ignored_not_wanted++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_MISSED_START:
|
|
|
|
protocol_stats.rx_error_missed_start++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_WRONG_TOGGLE:
|
|
|
|
protocol_stats.rx_error_wrong_toggle++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_UNEXPECTED_TID:
|
|
|
|
protocol_stats.rx_ignored_unexpected_tid++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_SHORT_FRAME:
|
|
|
|
protocol_stats.rx_error_short_frame++;
|
|
|
|
break;
|
|
|
|
case -CANARD_ERROR_RX_BAD_CRC:
|
|
|
|
protocol_stats.rx_error_bad_crc++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// mark all other errors as internal
|
|
|
|
protocol_stats.rx_error_internal++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::processRx(void)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2020-07-31 10:37:18 -03:00
|
|
|
AP_HAL::CANFrame rxmsg;
|
2021-07-11 12:18:30 -03:00
|
|
|
for (auto &ins : instances) {
|
|
|
|
if (ins.iface == NULL) {
|
2021-06-19 13:19:07 -03:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-30 22:36:42 -03:00
|
|
|
#if HAL_NUM_CAN_IFACES >= 2
|
2023-08-26 06:53:59 -03:00
|
|
|
if (can_protocol_cached[ins.index] != AP_CAN::Protocol::DroneCAN) {
|
2021-06-19 13:19:07 -03:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-30 22:36:42 -03:00
|
|
|
#endif
|
2021-06-19 13:19:07 -03:00
|
|
|
while (true) {
|
2021-03-14 03:15:28 -03:00
|
|
|
bool read_select = true;
|
|
|
|
bool write_select = false;
|
2021-07-11 12:18:30 -03:00
|
|
|
ins.iface->select(read_select, write_select, nullptr, 0);
|
2021-06-19 13:19:07 -03:00
|
|
|
if (!read_select) { // No data pending
|
|
|
|
break;
|
2021-03-14 03:15:28 -03:00
|
|
|
}
|
|
|
|
CanardCANFrame rx_frame {};
|
|
|
|
|
|
|
|
//palToggleLine(HAL_GPIO_PIN_LED);
|
|
|
|
uint64_t timestamp;
|
|
|
|
AP_HAL::CANIface::CanIOFlags flags;
|
2022-08-16 03:27:57 -03:00
|
|
|
if (ins.iface->receive(rxmsg, timestamp, flags) <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2021-05-17 14:26:34 -03:00
|
|
|
rx_frame.data_len = AP_HAL::CANFrame::dlcToDataLength(rxmsg.dlc);
|
|
|
|
memcpy(rx_frame.data, rxmsg.data, rx_frame.data_len);
|
|
|
|
#if HAL_CANFD_SUPPORTED
|
|
|
|
rx_frame.canfd = rxmsg.canfd;
|
|
|
|
#endif
|
2021-03-14 03:15:28 -03:00
|
|
|
rx_frame.id = rxmsg.id;
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
|
|
|
rx_frame.iface_id = ins.index;
|
|
|
|
#endif
|
2023-07-07 04:36:52 -03:00
|
|
|
|
|
|
|
const int16_t res = canardHandleRxFrame(&dronecan.canard, &rx_frame, timestamp);
|
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
|
|
|
if (res == -CANARD_ERROR_RX_MISSED_START) {
|
|
|
|
// this might remaining frames from a message that we don't accept, so check
|
|
|
|
uint64_t dummy_signature;
|
|
|
|
if (shouldAcceptTransfer(&dronecan.canard,
|
|
|
|
&dummy_signature,
|
|
|
|
extractDataType(rx_frame.id),
|
|
|
|
extractTransferType(rx_frame.id),
|
|
|
|
1)) { // doesn't matter what we pass here
|
|
|
|
update_rx_protocol_stats(res);
|
|
|
|
} else {
|
|
|
|
protocol_stats.rx_ignored_not_wanted++;
|
2021-07-23 15:50:34 -03:00
|
|
|
}
|
2023-07-07 04:36:52 -03:00
|
|
|
} else {
|
|
|
|
update_rx_protocol_stats(res);
|
2021-07-23 15:50:34 -03:00
|
|
|
}
|
2023-07-07 04:36:52 -03:00
|
|
|
#else
|
|
|
|
(void)res;
|
2021-07-23 15:50:34 -03:00
|
|
|
#endif
|
2020-01-10 03:13:34 -04:00
|
|
|
}
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t AP_Periph_FW::pool_peak_percent()
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2022-08-16 03:27:57 -03:00
|
|
|
const CanardPoolAllocatorStatistics stats = canardGetPoolAllocatorStatistics(&dronecan.canard);
|
2019-05-26 22:46:41 -03:00
|
|
|
const uint16_t peak_percent = (uint16_t)(100U * stats.peak_usage_blocks / stats.capacity_blocks);
|
|
|
|
return peak_percent;
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::node_status_send(void)
|
2020-07-08 18:30:45 -03:00
|
|
|
{
|
2023-07-07 04:36:52 -03:00
|
|
|
{
|
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_NODESTATUS_MAX_SIZE];
|
|
|
|
node_status.uptime_sec = AP_HAL::millis() / 1000U;
|
2020-07-08 18:30:45 -03:00
|
|
|
|
2023-07-07 04:36:52 -03:00
|
|
|
node_status.vendor_specific_status_code = hal.util->available_memory();
|
2020-07-08 18:30:45 -03:00
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
uint32_t len = uavcan_protocol_NodeStatus_encode(&node_status, buffer, !canfdout());
|
2020-07-08 18:30:45 -03:00
|
|
|
|
2023-08-26 06:53:59 -03:00
|
|
|
canard_broadcast(UAVCAN_PROTOCOL_NODESTATUS_SIGNATURE,
|
2023-07-07 04:36:52 -03:00
|
|
|
UAVCAN_PROTOCOL_NODESTATUS_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
buffer,
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
#if HAL_ENABLE_SENDING_STATS
|
2023-08-26 06:53:59 -03:00
|
|
|
if (debug_option_is_set(AP_Periph_FW::DebugOptions::ENABLE_STATS)) {
|
2023-07-07 04:36:52 -03:00
|
|
|
{
|
|
|
|
uint8_t buffer[DRONECAN_PROTOCOL_STATS_MAX_SIZE];
|
2023-08-26 06:53:59 -03:00
|
|
|
uint32_t len = dronecan_protocol_Stats_encode(&protocol_stats, buffer, !canfdout());
|
|
|
|
canard_broadcast(DRONECAN_PROTOCOL_STATS_SIGNATURE,
|
2023-07-07 04:36:52 -03:00
|
|
|
DRONECAN_PROTOCOL_STATS_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOWEST,
|
|
|
|
buffer,
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
for (auto &ins : instances) {
|
|
|
|
uint8_t buffer[DRONECAN_PROTOCOL_CANSTATS_MAX_SIZE];
|
|
|
|
dronecan_protocol_CanStats can_stats;
|
|
|
|
const AP_HAL::CANIface::bus_stats_t *bus_stats = ins.iface->get_statistics();
|
|
|
|
if (bus_stats == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
can_stats.interface = ins.index;
|
|
|
|
can_stats.tx_requests = bus_stats->tx_requests;
|
|
|
|
can_stats.tx_rejected = bus_stats->tx_rejected;
|
|
|
|
can_stats.tx_overflow = bus_stats->tx_overflow;
|
|
|
|
can_stats.tx_success = bus_stats->tx_success;
|
|
|
|
can_stats.tx_timedout = bus_stats->tx_timedout;
|
|
|
|
can_stats.tx_abort = bus_stats->tx_abort;
|
|
|
|
can_stats.rx_received = bus_stats->rx_received;
|
|
|
|
can_stats.rx_overflow = bus_stats->rx_overflow;
|
|
|
|
can_stats.rx_errors = bus_stats->rx_errors;
|
|
|
|
can_stats.busoff_errors = bus_stats->num_busoff_err;
|
2023-08-26 06:53:59 -03:00
|
|
|
uint32_t len = dronecan_protocol_CanStats_encode(&can_stats, buffer, !canfdout());
|
|
|
|
canard_broadcast(DRONECAN_PROTOCOL_CANSTATS_SIGNATURE,
|
2023-07-07 04:36:52 -03:00
|
|
|
DRONECAN_PROTOCOL_CANSTATS_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOWEST,
|
2023-07-03 21:12:19 -03:00
|
|
|
buffer,
|
|
|
|
len);
|
2023-07-07 04:36:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-08 18:30:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
/**
|
|
|
|
* This function is called at 1 Hz rate from the main loop.
|
|
|
|
*/
|
2023-08-26 06:53:59 -03:00
|
|
|
void AP_Periph_FW::process1HzTasks(uint64_t timestamp_usec)
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Purging transfers that are no longer transmitted. This will occasionally free up some memory.
|
|
|
|
*/
|
2021-06-19 13:19:07 -03:00
|
|
|
cleanup_stale_transactions(timestamp_usec);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Printing the memory usage statistics.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The recommended way to establish the minimal size of the memory pool is to stress-test the application and
|
|
|
|
* record the worst case memory usage.
|
|
|
|
*/
|
2022-08-16 03:27:57 -03:00
|
|
|
|
|
|
|
if (pool_peak_percent() > 70) {
|
|
|
|
printf("WARNING: ENLARGE MEMORY POOL\n");
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transmitting the node status message periodically.
|
|
|
|
*/
|
2020-07-08 18:30:45 -03:00
|
|
|
node_status_send();
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2019-10-23 04:57:27 -03:00
|
|
|
#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT)
|
2023-08-26 06:53:59 -03:00
|
|
|
if (g.flash_bootloader.get()) {
|
|
|
|
const uint8_t flash_bl = g.flash_bootloader.get();
|
|
|
|
g.flash_bootloader.set_and_save_ifchanged(0);
|
2020-11-26 00:10:27 -04:00
|
|
|
if (flash_bl == 42) {
|
|
|
|
// magic developer value to test watchdog support with main loop lockup
|
|
|
|
while (true) {
|
|
|
|
can_printf("entering lockup\n");
|
|
|
|
hal.scheduler->delay(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flash_bl == 43) {
|
|
|
|
// magic developer value to test watchdog support with hard fault
|
|
|
|
can_printf("entering fault\n");
|
|
|
|
void *foo = (void*)0xE000ED38;
|
|
|
|
typedef void (*fptr)();
|
|
|
|
fptr gptr = (fptr) (void *) foo;
|
|
|
|
gptr();
|
|
|
|
}
|
2020-12-05 19:30:07 -04:00
|
|
|
EXPECT_DELAY_MS(2000);
|
2019-10-25 06:11:36 -03:00
|
|
|
hal.scheduler->delay(1000);
|
2019-10-24 23:15:56 -03:00
|
|
|
AP_HAL::Util::FlashBootloader res = hal.util->flash_bootloader();
|
|
|
|
switch (res) {
|
|
|
|
case AP_HAL::Util::FlashBootloader::OK:
|
2019-10-23 04:57:27 -03:00
|
|
|
can_printf("Flash bootloader OK\n");
|
2019-10-24 23:15:56 -03:00
|
|
|
break;
|
|
|
|
case AP_HAL::Util::FlashBootloader::NO_CHANGE:
|
|
|
|
can_printf("Bootloader unchanged\n");
|
|
|
|
break;
|
2022-09-02 19:46:07 -03:00
|
|
|
#if AP_SIGNED_FIRMWARE
|
|
|
|
case AP_HAL::Util::FlashBootloader::NOT_SIGNED:
|
|
|
|
can_printf("Bootloader not signed\n");
|
|
|
|
break;
|
|
|
|
#endif
|
2019-10-24 23:15:56 -03:00
|
|
|
default:
|
2019-10-23 04:57:27 -03:00
|
|
|
can_printf("Flash bootloader FAILED\n");
|
2019-10-24 23:15:56 -03:00
|
|
|
break;
|
2019-10-23 04:57:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-04-29 08:49:13 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
if (hal.run_in_maintenance_mode()) {
|
|
|
|
node_status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_MAINTENANCE;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
node_status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL;
|
|
|
|
}
|
2019-10-25 22:46:22 -03:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// test code for watchdog reset
|
2023-08-21 22:41:18 -03:00
|
|
|
if (AP_HAL::millis() > 15000) {
|
2019-10-25 22:46:22 -03:00
|
|
|
while (true) ;
|
|
|
|
}
|
|
|
|
#endif
|
2020-09-12 16:00:22 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2023-08-21 22:41:18 -03:00
|
|
|
if (AP_HAL::millis() > 30000) {
|
2019-10-25 22:46:22 -03:00
|
|
|
// use RTC to mark that we have been running fine for
|
|
|
|
// 30s. This is used along with watchdog resets to ensure the
|
|
|
|
// user has a chance to load a fixed firmware
|
|
|
|
set_fast_reboot(RTC_BOOT_FWOK);
|
|
|
|
}
|
2020-09-12 16:00:22 -03:00
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
wait for dynamic allocation of node ID
|
|
|
|
*/
|
2021-07-12 12:59:31 -03:00
|
|
|
bool AP_Periph_FW::no_iface_finished_dna = true;
|
2023-08-26 06:53:59 -03:00
|
|
|
|
|
|
|
bool AP_Periph_FW::can_do_dna()
|
2019-05-26 22:46:41 -03:00
|
|
|
{
|
2022-08-16 03:27:57 -03:00
|
|
|
if (canardGetLocalNodeID(&dronecan.canard) != CANARD_BROADCAST_NODE_ID) {
|
2021-07-12 12:59:31 -03:00
|
|
|
AP_Periph_FW::no_iface_finished_dna = false;
|
2021-06-19 13:19:07 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-21 22:41:18 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
2021-07-12 12:59:31 -03:00
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
static uint8_t node_id_allocation_transfer_id = 0;
|
2021-06-19 13:19:07 -03:00
|
|
|
|
2021-07-12 12:59:31 -03:00
|
|
|
if (AP_Periph_FW::no_iface_finished_dna) {
|
2022-08-16 03:27:57 -03:00
|
|
|
printf("Waiting for dynamic node ID allocation %x... (pool %u)\n", IFACE_ALL, pool_peak_percent());
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.send_next_node_id_allocation_request_at_ms =
|
2021-06-19 13:19:07 -03:00
|
|
|
now + UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MIN_REQUEST_PERIOD_MS +
|
|
|
|
get_random_range(UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MAX_FOLLOWUP_DELAY_MS);
|
|
|
|
|
|
|
|
// Structure of the request is documented in the DSDL definition
|
|
|
|
// See http://uavcan.org/Specification/6._Application_level_functions/#dynamic-node-id-allocation
|
|
|
|
uint8_t allocation_request[CANARD_CAN_FRAME_MAX_DATA_LEN - 1];
|
|
|
|
allocation_request[0] = (uint8_t)(PreferredNodeID << 1U);
|
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
if (dronecan.node_id_allocation_unique_id_offset == 0) {
|
2021-06-19 13:19:07 -03:00
|
|
|
allocation_request[0] |= 1; // First part of unique ID
|
2022-08-16 03:27:57 -03:00
|
|
|
// set interface to try
|
|
|
|
dronecan.dna_interface++;
|
|
|
|
dronecan.dna_interface %= HAL_NUM_CAN_IFACES;
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
2021-09-29 15:34:32 -03:00
|
|
|
uint8_t my_unique_id[sizeof(uavcan_protocol_dynamic_node_id_Allocation::unique_id.data)];
|
2021-06-19 13:19:07 -03:00
|
|
|
readUniqueID(my_unique_id);
|
|
|
|
|
|
|
|
static const uint8_t MaxLenOfUniqueIDInRequest = 6;
|
2022-08-16 03:27:57 -03:00
|
|
|
uint8_t uid_size = (uint8_t)(sizeof(uavcan_protocol_dynamic_node_id_Allocation::unique_id.data) - dronecan.node_id_allocation_unique_id_offset);
|
2021-06-19 13:19:07 -03:00
|
|
|
|
|
|
|
if (uid_size > MaxLenOfUniqueIDInRequest) {
|
|
|
|
uid_size = MaxLenOfUniqueIDInRequest;
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
memmove(&allocation_request[1], &my_unique_id[dronecan.node_id_allocation_unique_id_offset], uid_size);
|
2021-06-19 13:19:07 -03:00
|
|
|
|
|
|
|
// Broadcasting the request
|
2022-08-16 03:27:57 -03:00
|
|
|
const int16_t bcast_res = canardBroadcast(&dronecan.canard,
|
2021-06-19 13:19:07 -03:00
|
|
|
UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID,
|
|
|
|
&node_id_allocation_transfer_id,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
&allocation_request[0],
|
2021-05-17 14:26:34 -03:00
|
|
|
(uint16_t) (uid_size + 1)
|
2022-08-16 03:27:57 -03:00
|
|
|
#if CANARD_MULTI_IFACE
|
2022-08-17 21:29:00 -03:00
|
|
|
,(1U << dronecan.dna_interface)
|
2022-08-16 03:27:57 -03:00
|
|
|
#endif
|
2021-05-17 14:26:34 -03:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
2023-04-09 18:34:59 -03:00
|
|
|
// always send allocation request as non-FD
|
2021-05-17 14:26:34 -03:00
|
|
|
,false
|
|
|
|
#endif
|
|
|
|
);
|
2021-06-19 13:19:07 -03:00
|
|
|
if (bcast_res < 0) {
|
2022-08-16 03:27:57 -03:00
|
|
|
printf("Could not broadcast ID allocation req; error %d\n", bcast_res);
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preparing for timeout; if response is received, this value will be updated from the callback.
|
2022-08-16 03:27:57 -03:00
|
|
|
dronecan.node_id_allocation_unique_id_offset = 0;
|
2021-06-19 13:19:07 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
void AP_Periph_FW::can_start()
|
|
|
|
{
|
|
|
|
node_status.health = UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK;
|
|
|
|
node_status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_INITIALIZATION;
|
2023-08-21 22:41:18 -03:00
|
|
|
node_status.uptime_sec = AP_HAL::millis() / 1000U;
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2020-10-14 04:59:01 -03:00
|
|
|
if (g.can_node >= 0 && g.can_node < 128) {
|
|
|
|
PreferredNodeID = g.can_node;
|
|
|
|
}
|
|
|
|
|
2020-12-22 19:44:48 -04:00
|
|
|
#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT)
|
2023-08-26 06:53:59 -03:00
|
|
|
g.flash_bootloader.set_and_save_ifchanged(0);
|
2020-12-22 19:44:48 -04:00
|
|
|
#endif
|
2020-11-26 00:10:27 -04:00
|
|
|
|
2021-04-30 22:36:42 -03:00
|
|
|
#if AP_PERIPH_ENFORCE_AT_LEAST_ONE_PORT_IS_UAVCAN_1MHz && HAL_NUM_CAN_IFACES >= 2
|
|
|
|
bool has_uavcan_at_1MHz = false;
|
|
|
|
for (uint8_t i=0; i<HAL_NUM_CAN_IFACES; i++) {
|
2023-04-18 06:44:06 -03:00
|
|
|
if (g.can_protocol[i] == AP_CAN::Protocol::DroneCAN && g.can_baudrate[i] == 1000000) {
|
2021-04-30 22:36:42 -03:00
|
|
|
has_uavcan_at_1MHz = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_uavcan_at_1MHz) {
|
2023-04-18 06:44:06 -03:00
|
|
|
g.can_protocol[0].set_and_save(uint8_t(AP_CAN::Protocol::DroneCAN));
|
2021-04-30 22:36:42 -03:00
|
|
|
g.can_baudrate[0].set_and_save(1000000);
|
|
|
|
}
|
|
|
|
#endif // HAL_PERIPH_ENFORCE_AT_LEAST_ONE_PORT_IS_UAVCAN_1MHz
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<HAL_NUM_CAN_IFACES; i++) {
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
|
|
|
can_iface_periph[i] = new ChibiOS::CANIface();
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
can_iface_periph[i] = new HALSITL::CANIface();
|
|
|
|
#endif
|
2021-07-11 12:18:30 -03:00
|
|
|
instances[i].iface = can_iface_periph[i];
|
|
|
|
instances[i].index = i;
|
2021-04-30 22:36:42 -03:00
|
|
|
#if HAL_NUM_CAN_IFACES >= 2
|
|
|
|
can_protocol_cached[i] = g.can_protocol[i];
|
|
|
|
CANSensor::set_periph(i, can_protocol_cached[i], can_iface_periph[i]);
|
|
|
|
#endif
|
|
|
|
if (can_iface_periph[i] != nullptr) {
|
2022-02-23 04:21:20 -04:00
|
|
|
#if HAL_CANFD_SUPPORTED
|
|
|
|
can_iface_periph[i]->init(g.can_baudrate[i], g.can_fdbaudrate[i]*1000000U, AP_HAL::CANIface::NormalMode);
|
|
|
|
#else
|
2021-04-30 22:36:42 -03:00
|
|
|
can_iface_periph[i]->init(g.can_baudrate[i], AP_HAL::CANIface::NormalMode);
|
2022-02-23 04:21:20 -04:00
|
|
|
#endif
|
2021-04-30 22:36:42 -03:00
|
|
|
}
|
2022-08-16 03:27:57 -03:00
|
|
|
}
|
2022-10-07 00:19:55 -03:00
|
|
|
|
2023-03-10 20:46:34 -04:00
|
|
|
#if AP_CAN_SLCAN_ENABLED
|
2022-10-07 00:19:55 -03:00
|
|
|
const uint8_t slcan_selected_index = g.can_slcan_cport - 1;
|
|
|
|
if (slcan_selected_index < HAL_NUM_CAN_IFACES) {
|
|
|
|
slcan_interface.set_can_iface(can_iface_periph[slcan_selected_index]);
|
|
|
|
instances[slcan_selected_index].iface = (AP_HAL::CANIface*)&slcan_interface;
|
|
|
|
|
|
|
|
// ensure there's a serial port mapped to SLCAN
|
2023-08-26 06:53:59 -03:00
|
|
|
if (!serial_manager.have_serial(AP_SerialManager::SerialProtocol_SLCAN, 0)) {
|
|
|
|
serial_manager.set_protocol_and_baud(SERIALMANAGER_NUM_PORTS-1, AP_SerialManager::SerialProtocol_SLCAN, 1500000);
|
2022-10-07 00:19:55 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
canardInit(&dronecan.canard, (uint8_t *)dronecan.canard_memory_pool, sizeof(dronecan.canard_memory_pool),
|
2023-08-26 06:53:59 -03:00
|
|
|
onTransferReceived_trampoline, shouldAcceptTransfer_trampoline, this);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
if (PreferredNodeID != CANARD_BROADCAST_NODE_ID) {
|
|
|
|
canardSetLocalNodeID(&dronecan.canard, PreferredNodeID);
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 01:18:20 -04:00
|
|
|
|
2021-12-08 19:52:38 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
|
|
|
#if HAL_WITH_ESC_TELEM
|
|
|
|
/*
|
|
|
|
send ESC status packets based on AP_ESC_Telem
|
|
|
|
*/
|
|
|
|
void AP_Periph_FW::esc_telem_update()
|
|
|
|
{
|
2022-06-10 01:55:54 -03:00
|
|
|
uint32_t mask = esc_telem.get_active_esc_mask();
|
|
|
|
while (mask != 0) {
|
|
|
|
int8_t i = __builtin_ffs(mask) - 1;
|
|
|
|
mask &= ~(1U<<i);
|
2021-12-08 19:52:38 -04:00
|
|
|
const float nan = nanf("");
|
|
|
|
uavcan_equipment_esc_Status pkt {};
|
|
|
|
const auto *channel = SRV_Channels::srv_channel(i);
|
|
|
|
// try to map the ESC number to a motor number. This is needed
|
|
|
|
// for when we have multiple CAN nodes, one for each ESC
|
|
|
|
if (channel == nullptr) {
|
|
|
|
pkt.esc_index = i;
|
|
|
|
} else {
|
|
|
|
const int8_t motor_num = channel->get_motor_num();
|
|
|
|
pkt.esc_index = motor_num==-1? i:motor_num;
|
|
|
|
}
|
|
|
|
if (!esc_telem.get_voltage(i, pkt.voltage)) {
|
|
|
|
pkt.voltage = nan;
|
|
|
|
}
|
|
|
|
if (!esc_telem.get_current(i, pkt.current)) {
|
|
|
|
pkt.current = nan;
|
|
|
|
}
|
|
|
|
int16_t temperature;
|
2022-09-02 18:41:38 -03:00
|
|
|
if (esc_telem.get_motor_temperature(i, temperature)) {
|
|
|
|
pkt.temperature = C_TO_KELVIN(temperature*0.01);
|
|
|
|
} else if (esc_telem.get_temperature(i, temperature)) {
|
2022-01-12 08:03:25 -04:00
|
|
|
pkt.temperature = C_TO_KELVIN(temperature*0.01);
|
2021-12-08 19:52:38 -04:00
|
|
|
} else {
|
|
|
|
pkt.temperature = nan;
|
|
|
|
}
|
|
|
|
float rpm;
|
|
|
|
if (esc_telem.get_raw_rpm(i, rpm)) {
|
|
|
|
pkt.rpm = rpm;
|
|
|
|
}
|
|
|
|
pkt.power_rating_pct = 0;
|
|
|
|
pkt.error_count = 0;
|
|
|
|
|
|
|
|
uint8_t buffer[UAVCAN_EQUIPMENT_ESC_STATUS_MAX_SIZE] {};
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = uavcan_equipment_esc_Status_encode(&pkt, buffer, !canfdout());
|
2021-12-08 19:52:38 -04:00
|
|
|
canard_broadcast(UAVCAN_EQUIPMENT_ESC_STATUS_SIGNATURE,
|
|
|
|
UAVCAN_EQUIPMENT_ESC_STATUS_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
&buffer[0],
|
|
|
|
total_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAL_WITH_ESC_TELEM
|
|
|
|
|
2021-03-02 14:49:09 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_ESC_APD
|
|
|
|
void AP_Periph_FW::apd_esc_telem_update()
|
|
|
|
{
|
|
|
|
for(uint8_t i = 0; i < ARRAY_SIZE(apd_esc_telem); i++) {
|
|
|
|
if (apd_esc_telem[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ESC_APD_Telem &esc = *apd_esc_telem[i];
|
|
|
|
|
|
|
|
if (esc.update()) {
|
|
|
|
const ESC_APD_Telem::telem &t = esc.get_telem();
|
|
|
|
|
|
|
|
uavcan_equipment_esc_Status pkt {};
|
|
|
|
static_assert(APD_ESC_INSTANCES <= ARRAY_SIZE(g.esc_number), "There must be an ESC instance number for each APD ESC");
|
|
|
|
pkt.esc_index = g.esc_number[i];
|
|
|
|
pkt.voltage = t.voltage;
|
|
|
|
pkt.current = t.current;
|
|
|
|
pkt.temperature = t.temperature;
|
|
|
|
pkt.rpm = t.rpm;
|
|
|
|
pkt.power_rating_pct = t.power_rating_pct;
|
|
|
|
pkt.error_count = t.error_count;
|
|
|
|
|
|
|
|
uint8_t buffer[UAVCAN_EQUIPMENT_ESC_STATUS_MAX_SIZE] {};
|
2023-08-26 06:53:59 -03:00
|
|
|
uint16_t total_size = uavcan_equipment_esc_Status_encode(&pkt, buffer, !canfdout());
|
2021-03-02 14:49:09 -04:00
|
|
|
canard_broadcast(UAVCAN_EQUIPMENT_ESC_STATUS_SIGNATURE,
|
|
|
|
UAVCAN_EQUIPMENT_ESC_STATUS_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
&buffer[0],
|
|
|
|
total_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // HAL_PERIPH_ENABLE_ESC_APD
|
|
|
|
#endif // HAL_PERIPH_ENABLE_RC_OUT
|
2019-05-26 22:46:41 -03:00
|
|
|
|
|
|
|
void AP_Periph_FW::can_update()
|
|
|
|
{
|
2023-08-21 22:41:18 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
2021-07-23 05:19:47 -03:00
|
|
|
const uint32_t led_pattern = 0xAAAA;
|
|
|
|
const uint32_t led_change_period = 50;
|
|
|
|
static uint8_t led_idx = 0;
|
|
|
|
static uint32_t last_led_change;
|
|
|
|
|
|
|
|
if ((now - last_led_change > led_change_period) && no_iface_finished_dna) {
|
|
|
|
// blink LED in recognisable pattern while waiting for DNA
|
|
|
|
#ifdef HAL_GPIO_PIN_LED
|
|
|
|
palWriteLine(HAL_GPIO_PIN_LED, (led_pattern & (1U<<led_idx))?1:0);
|
|
|
|
#elif defined(HAL_GPIO_PIN_SAFE_LED)
|
|
|
|
// or use safety LED if defined
|
|
|
|
palWriteLine(HAL_GPIO_PIN_SAFE_LED, (led_pattern & (1U<<led_idx))?1:0);
|
|
|
|
#else
|
|
|
|
(void)led_pattern;
|
|
|
|
(void)led_idx;
|
|
|
|
#endif
|
|
|
|
led_idx = (led_idx+1) % 32;
|
|
|
|
last_led_change = now;
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:27:57 -03:00
|
|
|
if (AP_HAL::millis() > dronecan.send_next_node_id_allocation_request_at_ms) {
|
|
|
|
can_do_dna();
|
2021-06-19 13:19:07 -03:00
|
|
|
}
|
|
|
|
|
2019-05-26 22:46:41 -03:00
|
|
|
static uint32_t last_1Hz_ms;
|
|
|
|
if (now - last_1Hz_ms >= 1000) {
|
|
|
|
last_1Hz_ms = now;
|
2023-08-21 22:41:18 -03:00
|
|
|
process1HzTasks(AP_HAL::micros64());
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
2022-04-29 08:49:13 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
if (!hal.run_in_maintenance_mode())
|
2020-12-12 05:08:45 -04:00
|
|
|
#endif
|
2022-04-29 08:49:13 -03:00
|
|
|
{
|
2023-08-26 06:53:59 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_MAG
|
2022-04-29 08:49:13 -03:00
|
|
|
can_mag_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_GPS
|
2022-04-29 08:49:13 -03:00
|
|
|
can_gps_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
2023-06-26 06:55:54 -03:00
|
|
|
#if AP_UART_MONITOR_ENABLED
|
|
|
|
send_serial_monitor_data();
|
|
|
|
#endif
|
2023-08-26 06:53:59 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_BATTERY
|
2022-04-29 08:49:13 -03:00
|
|
|
can_battery_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_BARO
|
2022-04-29 08:49:13 -03:00
|
|
|
can_baro_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_AIRSPEED
|
2022-04-29 08:49:13 -03:00
|
|
|
can_airspeed_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_RANGEFINDER
|
2022-04-29 08:49:13 -03:00
|
|
|
can_rangefinder_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_PROXIMITY
|
2022-09-27 16:45:07 -03:00
|
|
|
can_proximity_update();
|
2023-08-26 06:53:59 -03:00
|
|
|
#endif
|
2022-04-29 08:49:13 -03:00
|
|
|
#if defined(HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY) || defined (HAL_PERIPH_ENABLE_NOTIFY)
|
|
|
|
can_buzzer_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_GPIO_PIN_SAFE_LED
|
|
|
|
can_safety_LED_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_GPIO_PIN_SAFE_BUTTON
|
|
|
|
can_safety_button_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_PWM_HARDPOINT
|
|
|
|
pwm_hardpoint_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_HWESC
|
|
|
|
hwesc_telem_update();
|
|
|
|
#endif
|
2021-03-02 14:49:09 -04:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_ESC_APD
|
|
|
|
apd_esc_telem_update();
|
|
|
|
#endif
|
2022-04-29 08:49:13 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_MSP
|
|
|
|
msp_sensor_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_RC_OUT
|
|
|
|
rcout_update();
|
|
|
|
#endif
|
|
|
|
#ifdef HAL_PERIPH_ENABLE_EFI
|
|
|
|
can_efi_update();
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-23 15:48:56 -03:00
|
|
|
const uint32_t now_us = AP_HAL::micros();
|
|
|
|
while ((AP_HAL::micros() - now_us) < 1000) {
|
2021-09-02 07:27:09 -03:00
|
|
|
hal.scheduler->delay_microseconds(HAL_PERIPH_LOOP_DELAY_US);
|
2023-04-09 18:34:59 -03:00
|
|
|
|
|
|
|
#if HAL_CANFD_SUPPORTED
|
|
|
|
// allow for user enabling/disabling CANFD at runtime
|
2023-08-26 06:53:59 -03:00
|
|
|
dronecan.canard.tao_disabled = g.can_fdmode == 1;
|
2023-04-09 18:34:59 -03:00
|
|
|
#endif
|
|
|
|
|
2021-07-23 15:48:56 -03:00
|
|
|
processTx();
|
|
|
|
processRx();
|
|
|
|
}
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// printf to CAN LogMessage for debugging
|
|
|
|
void can_printf(const char *fmt, ...)
|
|
|
|
{
|
2022-10-13 15:23:33 -03:00
|
|
|
#if HAL_PERIPH_SUPPORT_LONG_CAN_PRINTF
|
|
|
|
const uint8_t packet_count_max = 4; // how many packets we're willing to break up an over-sized string into
|
|
|
|
const uint8_t packet_data_max = 90; // max single debug string length = sizeof(uavcan_protocol_debug_LogMessage.text.data)
|
|
|
|
uint8_t buffer_data[packet_count_max*packet_data_max] {};
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
// strip off any negative return errors by treating result as 0
|
|
|
|
uint32_t char_count = MAX(vsnprintf((char*)buffer_data, sizeof(buffer_data), fmt, ap), 0);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
// send multiple uavcan_protocol_debug_LogMessage packets if the fmt string is too long.
|
|
|
|
uint16_t buffer_offset = 0;
|
|
|
|
for (uint8_t i=0; i<packet_count_max && char_count > 0; i++) {
|
|
|
|
uavcan_protocol_debug_LogMessage pkt {};
|
|
|
|
pkt.text.len = MIN(char_count, sizeof(pkt.text.data));
|
|
|
|
char_count -= pkt.text.len;
|
|
|
|
|
|
|
|
memcpy(pkt.text.data, &buffer_data[buffer_offset], pkt.text.len);
|
|
|
|
buffer_offset += pkt.text.len;
|
|
|
|
|
|
|
|
uint8_t buffer_packet[UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_MAX_SIZE] {};
|
|
|
|
const uint32_t len = uavcan_protocol_debug_LogMessage_encode(&pkt, buffer_packet, !periph.canfdout());
|
|
|
|
|
2023-07-03 21:12:19 -03:00
|
|
|
periph.canard_broadcast(UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
buffer_packet,
|
|
|
|
len);
|
2022-10-13 15:23:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2019-05-26 22:46:41 -03:00
|
|
|
uavcan_protocol_debug_LogMessage pkt {};
|
2020-11-11 02:33:12 -04:00
|
|
|
uint8_t buffer[UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_MAX_SIZE] {};
|
2019-05-26 22:46:41 -03:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2021-09-29 15:34:32 -03:00
|
|
|
uint32_t n = vsnprintf((char*)pkt.text.data, sizeof(pkt.text.data), fmt, ap);
|
2019-05-26 22:46:41 -03:00
|
|
|
va_end(ap);
|
2021-09-29 15:34:32 -03:00
|
|
|
pkt.text.len = MIN(n, sizeof(pkt.text.data));
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2021-05-17 14:26:34 -03:00
|
|
|
uint32_t len = uavcan_protocol_debug_LogMessage_encode(&pkt, buffer, !periph.canfdout());
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2023-07-03 21:12:19 -03:00
|
|
|
periph.canard_broadcast(UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_SIGNATURE,
|
|
|
|
UAVCAN_PROTOCOL_DEBUG_LOGMESSAGE_ID,
|
|
|
|
CANARD_TRANSFER_PRIORITY_LOW,
|
|
|
|
buffer,
|
|
|
|
len);
|
2019-05-26 22:46:41 -03:00
|
|
|
|
2022-10-13 15:23:33 -03:00
|
|
|
#endif
|
2019-05-26 22:46:41 -03:00
|
|
|
}
|