柴CK《机器人操作系统入门》笔记

一、仿真参数

启动仿真界面

roslaunch robot_sim_demo robot_spawn.launch

方向控制

rosrun robot_sim_demo robot_keyboard_teleop.py

二、roscpptopic_demo

功能简述:两个node(进程),一个发布模拟的GPS消息(格式自定义,包括坐标和工作状态),另一个接收并处理该消息(计算到原点的距离)。

步骤:
①、package

cd ~/tutorial_ws/src
catkin_create_pkg topic_demo roscpp rospy std_msgs

②、msg

cd topic_demo
mkdir msg
cd msg
vi gps.msg

gps.msg

float32 x
float32 y
string state

编译之后会生成gps文件,路径为:~/tutorial_ws/devel/include/topic_demo/gps.h
该msg使用方式如下

#include 
...
topic_demo::gps msg;
...

③、talker.cpp

#include  //#ros程序必备
#include 

int main(int argc, char** argv)
{
	ros::int(argc, argv, "talker"); //解析参数,命名当前节点node
	ros::NodeHandle nh;//创建句柄,实例化node
	topic_demo::gps msg;//创建gps消息
	msg.x = 1.0;
	msg.y = 1.0;
	msg.state = "working";
	ros::Publisher pub = nh.advertise("gps_info", 1);//创建publisher,gps_info为topic名称,1为消息队列长度
	ros::Rate loop_rate(1.0);//定义循环发布频率
	while(ros::ok())
	{
		msg.x = 1.03 * msg.x;
		msg.y = 1.03 * msg.y;
		ROS_INFO("Talker:GPS: x = %f, y = %f", msg.x, msg.y);//输出当前msg,类似printf
		pub.publish(msg);//发布消息
		loop_rate.sleep();//定义发布评论,sleep
	}
	return 0;
}

④、listener.cpp
⑤、CMakeList.txt & package.xml

你可能感兴趣的:(ROS)