简介
UAVCAN是一种轻量级协议,旨在通过CAN总线在航空航天和机器人应用中实现可靠通信。要实现通信,最基本需要data_type_ id, signature、数据结构、设备程序初始化。
添加设备数据结构文件(.uavcan格式)
1.在以下路径添加设备数据结构文件,根据设备类型选择对应的文件夹。这里使用actuator示例。
/ardupilot/modules/DroneCAN/DSDL/uavcan/equipment/actuator
2.新添加2018.HimarkServos.uavcan(发布功能)和2019.HimarkStatus.uavcan(订阅功能)
3.在2018.HimarkServos.uavcan和2019.HimarkStatus.uavcan中添加数据类型:(参考其他uavcan文件格式)
使用libuavcan_dsdlc生成.hpp头文件
1.要生成.hpp文件,需要使用libuavcan_dsdlc编译生成,基于python,可以在wscript文件配置自动生成。(生成.hpp文件后可以注释这段代码)
参考路径:Ardupilot/ArduCopter/wscript
在文件内添加以下代码:
bld(
# build libcanard headers
source=bld.path.ant_glob("modules/DroneCAN/DSDL/**/*.uavcan"),
rule="python3 ${SRCROOT}/modules/uavcan/libuavcan/dsdl_compiler/libuavcan_dsdlc --outdir ${BUILDROOT}/modules/DroneCAN/libcanard/dsdlc_generated ${SRCROOT}/modules/DroneCAN/DSDL/uavcan ${SRCROOT}/modules/DroneCAN/DSDL/ardupilot ${SRCROOT}/modules/DroneCAN/DSDL/com ${SRCROOT}/modules/DroneCAN/DSDL/dronecan",
group='dynamic_sources',
)
2.配置board类型 (以雷迅CUAVv5为例)
./waf configure --board CUAVv5
3.根据机架类型编译工程
./waf copter
4.在以下路径可以找到生成.hpp文件
5.复制对应的HimarServos.hpp与HimarkStatus.hpp文件到以下路径
/Ardupilot/libraries/AP_UAVCAN/dsdl/
6.如果需要自定义签名,则需要在HimarkServos.hpp\HimarkStatus.hpp文件中修改
UAVCAN新设备调用
1.包含新设备头文件
#include <AP_UAVCAN/dsdl/HimarkServos.hpp>
#include <AP_UAVCAN/dsdl/HimarkStatus.hpp>
2.新添加发布信息接口
// himark servo
static uavcan::Publisher<uavcan::equipment::actuator::HimarkServos>* srv_out_array[HAL_MAX_CAN_PROTOCOL_DRIVERS];
3.新添加订阅服务接口
// handler Himark servo status
UC_REGISTRY_BINDER(HimarkStatusCb, uavcan::equipment::actuator::HimarkStatus);
static uavcan::Subscriber<uavcan::equipment::actuator::HimarkStatus, HimarkStatusCb> *himark_srv_status_listener[HAL_MAX_CAN_PROTOCOL_DRIVERS];
4.在void AP_UAVCAN::init()函数中添加设备初始化
//servo
srv_out_array[driver_index] = new uavcan::Publisher<uavcan::equipment::actuator::HimarkServos>(*_node);
srv_out_array[driver_index]->setTxTimeout(uavcan::MonotonicDuration::fromMSec(2));
srv_out_array[driver_index]->setPriority(uavcan::TransferPriority::OneLowerThanHighest);
himark_srv_status_listener[driver_index] = new uavcan::Subscriber<uavcan::equipment::actuator::HimarkStatus, HimarkStatusCb>(*_node);
if (himark_srv_status_listener[driver_index]) {
himark_srv_status_listener[driver_index]->start(HimarkStatusCb(this, &handle_himark_srv_status));
}
5.发布服务实现
6.订阅服务实现
7.在AP_UAVCAN.H添加HimarkStatusCb类
8.在uavcan类里添加函数接口
public:
/ himark srv output /
void himark_srv_send_actuator(void);
static void handle_himark_srv_status(AP_UAVCAN* ap_uavcan, uint8_t node_id, const HimarkStatusCb &cb);
结束
UAVCAN新设备已经添加完成,可通信测试。