参考自“机器人操作系统浅析”
ROS程序-helloworld
ROS程序-velocity-publish
软件包agitr——helloworld
src/agitr/hello.cpp
//This is first ros package of standard "hello world" program
#include <ros/ros.h>
int main(int argc ,char** argv)
{
ros::init(argc,argv,"hello_ros");//node name :
ros::NodeHandle nh;
ROS_INFO_STREAM("hello, ROS!");
}
src/agitr/package.xml
<?xml version="1.0"?>
<package>
<name>agitr</name>
<version>0.0.0</version>
<description>LEEMAN_CAFFEE</description>
<maintainer email="mli@leemanchina.com">HOPPS</maintainer>
<license>BSD</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>turtlesim</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>geometry_msgs</run_depend>
<run_depend>turtlesim</run_depend>
src/agitr/CMakeList.txt
cmake_minimum_required(VERSION 2.8.3)
project(agitr)
find_package(catkin REQUIRED COMPONENTS roscpp geometry_msgs)
##下面()中#不能去除
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES agitr
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
add_executable(hello hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})
source devel/setup.bash
rosrun agitr hello
pubvel 随机速度发布
src/pubvel/pubvel.cpp
//This is a first Velocity topic publish program
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
int main(int argc,char** argv)
{
ros::init(argc,argv,"velocitypublish");
ros::NodeHandle nh;
ros::Publisher pub=nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",1000);
srand(time(0));
ros::Rate rate(2);
while(ros::ok())
{
geometry_msgs::Twist msg;
msg.linear.x=double(rand())/double(RAND_MAX);
msg.angular.z=2*double(rand())/double(RAND_MAX)-1;
pub.publish(msg);
ROS_INFO_STREAM("Sending random velocity command:"<<"linear="<<msg.linear.x<<"angular=msg.angular.z"<<msg.angular.z);
rate.sleep();
}
}
src/pubvel/CMakeList.txt
<?xml version="1.0"?>
<package>
<name>pubvel</name>
<version>0.0.0</version>
<description>LEEMAN_CAFFEE</description>
<license>BSD</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>turtlesim</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>geometry_msgs</run_depend>
<run_depend>turtlesim</run_depend>
</package>
src/pubvel/CMakeList.txt
cmake_minimum_required(VERSION 2.8.3)
project(pubvel)
find_package(catkin REQUIRED roscpp geometry_msgs)
catkin_package(
#INCLUDE_DIRS include
#LIBRARIES agitr_pubVel
#CATKIN_DEPENDS other_catkin_pkg
#DEPENDS system_lib
)
add_executable(pubvel pubvel.cpp)
target_link_libraries(pubvel ${catkin_LIBRARIES})
catkin_make
source devel/setup.bash
rosrun turtlesim turtlesim_node
rosrun pubvel pubvel
rostopic echo /turtle1/pose
rostopic echo /turtle1/cmd_vel
回掉函数与话题订阅
src/subturtlesimpose/subturtlesimpose.cpp
//This is a first Velocity topic publish program
#include <ros/ros.h>
#include <turtlesim/Pose.h>
#include <iomanip>
//callback funciton
void poseMessageReceived(const turtlesim::Pose& msg)
{
ROS_INFO_STREAM(std::setprecision(2)<<std::fixed<<"positon=("<<msg.x<<","<<msg.y<<")"<<"*direcion="<<msg.theta);
}
int main(int argc,char** argv)
{
ros::init(argc,argv,"subscribe_to_turtlesim_pose");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("turtle1/pose",1000,&poseMessageReceived);
ros::spin();
}
src/subturtlesimpose/package.xml
<?xml version="1.0"?>
<package>
<name>subturtlesimpose</name>
<version>0.0.0</version>
<description>LEEMAN_CAFFEE</description>
<maintainer email="mli@leemanchina.com">HOPPS</maintainer>
<license>BSD</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>turtlesim</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>geometry_msgs</run_depend>
<run_depend>turtlesim</run_depend>
</package>
src/subturtlesimpose/CMakeList.txt
cmake_minimum_required(VERSION 2.8.3)
project(subturtlesimpose)
find_package(catkin REQUIRED roscpp geometry_msgs)
catkin_package(
#INCLUDE_DIRS include
#LIBRARIES agitr_pubVel
#CATKIN_DEPENDS other_catkin_pkg
#DEPENDS system_lib
)
add_executable(subturtlesimpose subturtlesimpose.cpp)
target_link_libraries(subturtlesimpose ${catkin_LIBRARIES})
rosrun turtlesim turtlesim_node
rosrun pubvel pubvel
rosrun subturtlesimpose subturtlesimpose