ROS/MAVROS 发送自定义消息
按照 Source Installation 中的指导,从 mavlink/mavros 安装“ROS Kinetic”版本。
**Ubuntu:**18.04
**ROS:**Melodic
PX4 Firmware: 1.9.0
However these steps are fairly general and so it should work with other distros/versions with little to no modifications. :::
MAVROS 安装
Follow Source Installation instructions from mavlink/mavros to install "ROS Kinetic".
MAVROS
首先,我们创建一个新的MAVROS 插件,在keyboard_command.cpp(workspace/src/mavros/mavros_extras/src/plugins)示例中添加以下代码:
代码功能是从ROS消息主题
/mavros/keyboard_command/keyboard_sub
中订阅了一个字符消息,并且将其作为MAVLink 消息发送出去。Edit mavros_plugins.xml (in workspace/src/mavros/mavros_extras) and add the following lines:
Edit CMakeLists.txt (in workspace/src/mavros/mavros_extras) and add the following line in
add_library
.Inside common.xml in (workspace/src/mavlink/message_definitions/v1.0), copy the following lines to add your MAVLink message:
PX4 修改
Inside common.xml (in PX4-Autopilot/mavlink/include/mavlink/v2.0/message_definitions), add your MAVLink message as following (same procedure as for MAVROS section above):
Remove common, standard directories in (PX4-Autopilot/mavlink/include/mavlink/v2.0).
git 克隆"mavlink_generator"到你想要的文件夹下并执行。
你会看到一个“MAVLink Generator”应用程序窗口:
For XML, "Browse" to /PX4-Autopilot/mavlink/include/mavlink/v2.0/message_definitions/standard.xml.
For Out, "Browse" to /PX4-Autopilot/mavlink/include/mavlink/v2.0/.
Select Language C
Select Protocol 2.0
Check Validate
Then, press Generate. You will see common, and standard directories created in /PX4-Autopilot/mavlink/include/mavlink/v2.0/.
Make your own uORB message file key_command.msg in (PX4-Autopilot/msg). 示例中的“key_command.msg”文件只包含以下代码:
Then, in CMakeLists.txt (in PX4-Autopilot/msg), include
Edit mavlink_receiver.h (in PX4-Autopilot/src/modules/mavlink)
Edit mavlink_receiver.cpp (in PX4-Autopilot/src/modules/mavlink). 这是 PX4 接收 ROS 发送过来的 MAVLink 消息的地方,并且将消息作为 uORB 主题发布。
像其他示例一样订阅你自己的uORB主题。 For this example lets create the model in (/PX4-Autopilot/src/modules/key_receiver). In this directory, create two files CMakeLists.txt, key_receiver.cpp. 两个文件如下所示。
-CMakeLists.txt
-key_receiver.cpp
For a more detailed explanation see the topic Writing your first application.
Lastly add your module in the default.px4board file correspondent to your board in PX4-Autopilot/boards/. For example for the Pixhawk 4 add the following code in PX4-Autopilot/boards/px4/fmu-v5/default.px4board: ```CONFIG_MODULES_KEY_RECEIVER=y
MODULES ...
key_receiver ... In your workspace enter: catkin build
.
Beforehand, you have to set your "px4.launch" in (/workspace/src/mavros/mavros/launch). Edit "px4.launch" as below. If you are using USB to connect your computer with Pixhawk, you have to set "fcu_url" as shown below. But, if you are using CP2102 to connect your computer with Pixhawk, you have to replace "ttyACM0" with "ttyUSB0". Modifying "gcs_url" is to connect your Pixhawk with UDP, because serial communication cannot accept MAVROS, and your nutshell connection simultaneously.
Write your IP address at "xxx.xx.xxx.xxx"
Build for PX4
Build PX4-Autopilot and upload in the normal way.
For example, to build for Pixhawk 4/FMUv5 execute the following command in the root of the PX4-Autopilot directory:
构建
测试是否从你的 ROS 话题中接收到 a
字符。
PX4 编译
In a terminal enter
In a second terminal run:
This means, publish 97 ('a' in ASCII) to ROS topic "/mavros/keyboard_command/keyboard_sub" in message type "std_msgs/Char". "-r 10" means to publish continuously in "10Hz".
Running PX4
Enter the Pixhawk nutshell through UDP. Replace xxx.xx.xxx.xxx with your IP.
After few seconds, press Enter a couple of times. You should see a prompt in the terminal as below:
Type "key_receiver", to run your subscriber module.
Check if it successfully receives a
from your ROS topic.
Last updated