ROS程序发布随机速到turltlesim

48 篇文章 1 订阅

参考自“机器人操作系统浅析”
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值