当前位置:网站首页>MAVROS发送自定义话题消息给PX4
MAVROS发送自定义话题消息给PX4
2022-07-01 08:12:00 【Mbot】
文章目录
前言
ROS Melodic
PX4 1.13.0
一、PX4添加自定义mavlink消息
修改下面路径下的common.xml文件
在下图位置添加
<message id="229" name="KEY_COMMAND">
<description>Keyboard char command.</description>
<field type="char" name="command"> </field>
</message>

然后在~/PX4-Autopilot目录下先执行
make clean
再执行
make px4_sitl_default
编译成功后会在下图的路径中找到
mavlink_msg_key_command.h

这时自定义mavlink消息文件就成功生成了
二、PX4添加自定义uorb消息
在下图目录添加key_command.msg文件
文件内容如下:
uint64 timestamp
char cmd

修改下图文件
在下图位置添加:
key_command.msg

三、PX4接收自定义mavlink消息
修改下图mavlink_receiver.h
下图位置添加:
#include <uORB/topics/key_command.h>

下图位置添加:
void handle_message_key_command(mavlink_message_t *msg);

下图位置添加:
orb_advert_t _key_command_pub{nullptr};

修改下图mavlink_receiver.cpp
下图位置添加:
case MAVLINK_MSG_ID_KEY_COMMAND:
handle_message_key_command(msg);
break;

下图位置添加:
void
MavlinkReceiver::handle_message_key_command(mavlink_message_t *msg)
{
mavlink_key_command_t man;
mavlink_msg_key_command_decode(msg, &man);
struct key_command_s key = {};
key.timestamp = hrt_absolute_time();
key.cmd = man.command;
if (_key_command_pub == nullptr) {
_key_command_pub = orb_advertise(ORB_ID(key_command), &key);
} else {
orb_publish(ORB_ID(key_command), _key_command_pub, &key);
}
}

四、PX4打印自定义mavlink数据
在下图目录添加key_receiver文件夹
在里面添加下图三个文件
各个文件的内容如下:
1
CMakeLists.txt
px4_add_module(
MODULE modules__key_receiver
MAIN key_receiver
COMPILE_FLAGS
#-DDEBUG_BUILD # uncomment for PX4_DEBUG output
#-O0 # uncomment when debugging
SRCS
key_receiver.cpp
DEPENDS
px4_work_queue
)
2
Kconfig
menuconfig MODULES_KEY_RECEIVER
bool "key_receiver"
default n
---help---
Enable support for key_receiver
3
key_receiver.cpp
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/tasks.h>
#include <px4_platform_common/posix.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <string.h>
#include <math.h>
#include <uORB/uORB.h>
#include <uORB/topics/key_command.h>
extern "C" __EXPORT int key_receiver_main(int argc, char **argv);
int key_receiver_main(int argc, char **argv)
{
int key_sub_fd = orb_subscribe(ORB_ID(key_command));
orb_set_interval(key_sub_fd, 200); // limit the update rate to 200ms
px4_pollfd_struct_t fds[1];
fds[0].fd = key_sub_fd, fds[0].events = POLLIN;
int error_counter = 0;
while(true)
{
int poll_ret = px4_poll(fds, 1, 1000);
if (poll_ret == 0)
{
PX4_ERR("Got no data within a second");
}
else if (poll_ret < 0)
{
if (error_counter < 10 || error_counter % 50 == 0)
{
PX4_ERR("ERROR return value from poll(): %d", poll_ret);
}
error_counter++;
}
else
{
if (fds[0].revents & POLLIN)
{
struct key_command_s input;
orb_copy(ORB_ID(key_command), key_sub_fd, &input);
PX4_INFO("Recieved Char : %c", input.cmd);
}
}
}
return 0;
}
修改下图文件
在下图位置添加:
CONFIG_MODULES_KEY_RECEIVER=y

然后编译
make px4_sitl_default gazebo
在当前终端输入
help
能找到key_receiver说明编译正常
五、MAVROS发送自定义mavlink数据
在下图位置添加keyboard_command.cpp文件夹
文件内容如下:
#include <mavros/mavros_plugin.h>
#include <pluginlib/class_list_macros.h>
#include <iostream>
#include <std_msgs/Char.h>
namespace mavros {
namespace extra_plugins{
class KeyboardCommandPlugin : public plugin::PluginBase {
public:
KeyboardCommandPlugin() : PluginBase(),
nh("~keyboard_command")
{ };
void initialize(UAS &uas_)
{
PluginBase::initialize(uas_);
keyboard_sub = nh.subscribe("keyboard_sub", 10, &KeyboardCommandPlugin::keyboard_cb, this);
};
Subscriptions get_subscriptions()
{
return {/* RX disabled */ };
}
private:
ros::NodeHandle nh;
ros::Subscriber keyboard_sub;
void keyboard_cb(const std_msgs::Char::ConstPtr &req)
{
std::cout << "Got Char : " << req->data << std::endl;
mavlink::common::msg::KEY_COMMAND key_command{};
key_command.command=req->data;
UAS_FCU(m_uas)->send_message_ignore_drop(key_command);
}
};
} // namespace extra_plugins
} // namespace mavros
PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::KeyboardCommandPlugin, mavros::plugin::PluginBase)
修改下图文件
在下图位置添加:
<class name="keyboard_command" type="mavros::extra_plugins::KeyboardCommandPlugin" base_class_type="mavros::plugin::PluginBase">
<description>Accepts keyboard command.</description>
</class>

修改下图文件
在下图位置添加:
src/plugins/keyboard_command.cpp

修改下图common.xml文件
在下图位置添加:
<message id="229" name="KEY_COMMAND">
<description>Keyboard char command.</description>
<field type="char" name="command"> </field>
</message>

然后在mavros工作空间下编译mavros
catkin build
六、测试
首先运行:
roslaunch px4 mavros_posix_sitl.launch
然后在启动launch文件的终端下执行
key_receiver start
如下图:
然后新开一个终端,执行:
rostopic pub -r 10 /mavros/keyboard_command/keyboard_sub std_msgs/Char 97
启动launch文件的终端的打印信息会从原来的Got no data within a second变成
Got Char : a
INFO [key_receiver] Recieved Char : a
如下图,如果发布的数据改成98,则会打印 Recieved Char : b。
边栏推荐
- Book of quantitative trading - reading notes of the man who conquers the market
- Anddroid text to speech TTS implementation
- empirical study and case study
- Contenttype comparison of all types
- AArdio - 【问题】bass库回调时内存增长的问题
- [dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
- P4 installation bmv2 detailed tutorial
- golang中的正则表达式使用注意事项与技巧
- [staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
- The Windows C disk is full
猜你喜欢

Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
![Thread safety analysis of [concurrent programming JUC] variables](/img/f9/a3604bec6f7e5317dd2c578da73018.jpg)
Thread safety analysis of [concurrent programming JUC] variables

Adding color blocks to Seaborn clustermap matrix

Suivi des cibles de manoeuvre - - mise en oeuvre du modèle statistique actuel (modèle CS) filtre Kalman étendu / filtre Kalman sans trace par MATLAB

Using settoolkit to forge sites to steal user information

谈谈数字化转型的几个关键问题

【入门】提取不重复的整数
![[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order](/img/87/07783593dbabcf29700fa207ecda08.png)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
![[question brushing] character statistics [0]](/img/cc/f5aaecd920c502180303d92447e54f.png)
[question brushing] character statistics [0]

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
随机推荐
谈谈数字化转型的几个关键问题
shardingSphere
P4 installation bmv2 detailed tutorial
OJ input and output exercise
Aardio - Shadow Gradient Text
软键盘高度报错
Serial port oscilloscope software ns-scope
getInputStream() has already been called for this request
機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
【力扣10天SQL入门】Day10 控制流
机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现
Anddroid text to speech TTS implementation
EDA开源仿真工具verilator入门6:调试实例
Aardio - 阴影渐变文字
[question brushing] character statistics [0]
php laravel微信支付
How can beginners correctly understand Google's official suggested architectural principles (questions?)
Insufficient executors to build thread pool
Find the nearest n-th power of 2
使用 setoolkit 伪造站点窃取用户信息