利用ros_lib库中自带的hello world 例子
1. 源程序如下:
/*
* rosserial Publisher Example
* Prints "hello world!"
*/
#include <ros.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
char hello[13] = "hello world!";
void setup()
{
nh.initNode();
nh.advertise(chatter);
}
void loop()
{
str_msg.data = hello;
chatter.publish( &str_msg );
nh.spinOnce();
delay(1000);
}
2. 程序解析
2.1 arduino的setup函数
- 需要初始化ROS节点,并 声明 所有的发布或订阅
2.2 arduino的setup函数
- 需要定义发布的内容,并调用nh.spinOnce(),这样所有的ROS回调函数就会被处理
2.3 头文件必须包含
#include <ros.h> 注意:ros.h前不可加ros/
#include <std_msgs/String.h>
3. 程序运行
3.1 roscore
3.2 其中端口号视具体而定
rosrun rosserial_python serial_node.py /dev/ttyUSB0
或者
rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0
3.3 显示主题chatter,获取Arduino板反馈的信息
rostopic echo chatter