跑ros教程(私人)

   printenv | grep ROS
    source /opt/ros/kinetic/setup.bash
    mkdir -p ~/catkin_ws/src
    cd ~/catkin_ws/
    catkin_make
    source devel/setup.bash
    echo $ROS_PACKAGE_PATH  #/home/wsy/catkin_ws/src:/opt/ros/kinetic/share
    sudo apt-get install ros-kinetic-ros-tutorials
    rospack find roscpp   #/opt/ros/kinetic/share/roscpp
    roscd roscpp
    pwd    #/opt/ros/melodic/share/roscpp
    echo $ROS_PACKAGE_PATH  #/home/wsy/catkin_ws/src:/opt/ros/melodic/share
    roscd roscpp/cmake
    pwd  #/opt/ros/melodic/share/roscpp/cmake
    rosls roscpp_tutorials #cmake launch package.xml  srv
    roscd roscpp_tut<<< now push the TAB key >>> #roscd roscpp_tutorials/
    roscd tur<<< now push the TAB key >>> #roscd turtle
    <<< now push the TAB key again >>> #turtle_actionlib/  turtlesim/         turtle_tf/
    roscd turtles<<< now push the TAB key >>> #roscd turtlesim/
    rosls <<< now push the TAB key twice >>>
    cd ~/catkin_ws/src
    catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
    cd ~/catkin_ws
    catkin_make
    . ~/catkin_ws/devel/setup.bash
    rospack depends1 beginner_tutorials #rospack depends1 beginner_tutorials 
    roscd beginner_tutorials
    cat package.xml
    rospack depends1 rospy
    rospack depends beginner_tutorials
    source /opt/ros/kinetic/setup.bash    
    #In a catkin workspace
    catkin_make
    catkin_make install  # (optionally)
    cd ~/catkin_ws/
    ls src  #beginner_tutorials/  CMakeLists.txt@  
    catkin_make
    ls #build devel src
    sudo apt-get install ros-kinetic-ros-tutorials
    roscore   #roscore是使用ROS时应该运行的第一件事。

跑ros教程(私人)_第1张图片

    source ./devel/setup.bash
    rosnode list#/rosout
    rosnode info /rosout
    rosrun turtlesim turtlesim_node

跑ros教程(私人)_第2张图片
In a new terminal:

rosnode list#/rosout /turtlesim
rosrun turtlesim turtlesim_node __name:=my_turtle
rosnode list#/my_turtle /rosout
rosnode ping my_turtle

原来的终端关了,打开新的终端

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key      #Use arrow keys to move the turtle.

跑ros教程(私人)_第3张图片

sudo apt-get install ros-kinetic-rqt
sudo apt-get install ros-kinetic-rqt-common-plugins
rosrun rqt_graph rqt_graph

跑ros教程(私人)_第4张图片

rostopic -h

跑ros教程(私人)_第5张图片

rostopic<<< now push the TAB key >>>#bw    echo  find  hz    info  list  pub   type  
rostopic echo /turtle1/cmd_vel    #调到turtle_teleop_key终端,使小乌龟移动,能看到新的终端上出现

跑ros教程(私人)_第6张图片

rostopic list -h

跑ros教程(私人)_第7张图片

rostopic list -v

跑ros教程(私人)_第8张图片

rostopic type /turtle1/cmd_vel #geometry_msgs/Twist
rosmsg show geometry_msgs/Twist #详细
rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'  #publishing and latching message for 3.0 seconds

跑ros教程(私人)_第9张图片

rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

跑ros教程(私人)_第10张图片
打开刚才的rqt_graph,按左上角的刷新。可以看到如下图所示
跑ros教程(私人)_第11张图片

rostopic echo /turtle1/pose  #查看发布的数据
rostopic hz /turtle1/pose

跑ros教程(私人)_第12张图片

rostopic type /turtle1/cmd_vel | rosmsg show  #深入信息
rosrun rqt_plot rqt_plot

在左上角添加/turtle1/pose/,就能看到曲线
跑ros教程(私人)_第13张图片 rosservice list跑ros教程(私人)_第14张图片

rosservice type /clear #std_srvs/Empty`
rosservice call /clear

跑ros教程(私人)_第15张图片

rosservice type /spawn | rossrv show

跑ros教程(私人)_第16张图片

rosservice call /spawn 2 2 0.2 "" #name: "turtle2"

跑ros教程(私人)_第17张图片rosparam list
跑ros教程(私人)_第18张图片

rosparam set /background_r 150#改变背景参数
rosservice call /clear#使参数生效

跑ros教程(私人)_第19张图片

rosparam get /background_g #86获取绿色背景的参数
rosparam dump params.yaml
rosparam load params.yaml copy
rosparam get /copy/background_b#255
sudo apt-get install ros-kinetic-rqt ros-kinetic-rqt-common-plugins ros-kinetic-turtlesim

在两个新终端中启动

rosrun rqt_console rqt_console
rosrun rqt_logger_level rqt_logger_level

看到如下两个界面
跑ros教程(私人)_第20张图片打开一个新终端

rosrun turtlesim turtlesim_node

跑ros教程(私人)_第21张图片现在让我们通过刷新rqt_logger_level窗口中的节点并选择Warn来将记录器级别更改为Warn,如下所示:
跑ros教程(私人)_第22张图片rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}'
跑ros教程(私人)_第23张图片

cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials
mkdir launch
cd launch

新建turtlemimic.launch文件https://blog.csdn.net/weixin_43981221/article/details/89245754



  
    
  

  
    
  

  
    
    
  



roslaunch beginner_tutorials turtlemimic.launch#出现两个乌龟
rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

跑ros教程(私人)_第24张图片

rqt_graph

跑ros教程(私人)_第25张图片Ctrl+C关闭所有的终端

sudo apt install vim       
rosed roscpp Logger.msg#按esc,:q!退出
rosed roscpp<按两下tab>

设置默认编辑器,在主目录下,ctrl+h,找到隐藏文件.bashrc,打开,在末尾加入export EDITOR='emacs -nw'
打开新的终端

 echo $EDITOR #emacs -nw

使用消息

cd catkin_ws/
source ./devel/setup.bash
roscd beginner_tutorials
mkdir msg
echo "int64 num" > msg/Num.msg

打开package.xml,在其中加入

 message_generation
  message_runtime

打开CMakeList.txt,在其中加入message_generation,使它看起来如图片所示
跑ros教程(私人)_第26张图片
更改文件如图所示跑ros教程(私人)_第27张图片
取消注释
跑ros教程(私人)_第28张图片

rosmsg show beginner_tutorials/Num #int64 num
rosmsg show Num #[beginner_tutorials/Num]:#int64 num
mkdir srv
#roscp [package_name] [file_to_copy_path] [copy_path]
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

打开CMakeList.txt
跑ros教程(私人)_第29张图片

  rossrv show beginner_tutorials/AddTwoInts
  #int64 a
  #int64 b
  #---
  #int64 sum
# In your catkin workspace
$ roscd beginner_tutorials
$ cd ../..
$ catkin_make install
$ cd -

rosmsg -h

跑ros教程(私人)_第30张图片rosmsg show -h
跑ros教程(私人)_第31张图片roscd beginner_tutorials
mkdir -p src
创建文件src/talker.cpp ,复制如下代码

#include "ros/ros.h"
#include "std_msgs/String.h"

#include 

/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "talker");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;

  /**
   * The advertise() function is how you tell ROS that you want to
   * publish on a given topic name. This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing. After this advertise() call is made, the master
   * node will notify anyone who is trying to subscribe to this topic name,
   * and they will in turn negotiate a peer-to-peer connection with this
   * node.  advertise() returns a Publisher object which allows you to
   * publish messages on that topic through a call to publish().  Once
   * all copies of the returned Publisher object are destroyed, the topic
   * will be automatically unadvertised.
   *
   * The second parameter to advertise() is the size of the message queue
   * used for publishing messages.  If messages are published more quickly
   * than we can send them, the number here specifies how many messages to
   * buffer up before throwing some away.
   */
  ros::Publisher chatter_pub = n.advertise("chatter", 1000);

  ros::Rate loop_rate(10);

  /**
   * A count of how many messages we have sent. This is used to create
   * a unique string for each message.
   */
  int count = 0;
  while (ros::ok())
  {
    /**
     * This is a message object. You stuff it with data, and then publish it.
     */
    std_msgs::String msg;

    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();

    ROS_INFO("%s", msg.data.c_str());

    /**
     * The publish() function is how you send messages. The parameter
     * is the message object. The type of this object must agree with the type
     * given as a template parameter to the advertise<>() call, as was done
     * in the constructor above.
     */
    chatter_pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();
    ++count;
  }


  return 0;
}

创建src/listener.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

/**
 * This tutorial demonstrates simple receipt of messages over the ROS system.
 */
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "listener");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;

  /**
   * The subscribe() call is how you tell ROS that you want to receive messages
   * on a given topic.  This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing.  Messages are passed to a callback function, here
   * called chatterCallback.  subscribe() returns a Subscriber object that you
   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
   * object go out of scope, this callback will automatically be unsubscribed from
   * this topic.
   *
   * The second parameter to the subscribe() function is the size of the message
   * queue.  If messages are arriving faster than they are being processed, this
   * is the number of messages that will be buffered up before beginning to throw
   * away the oldest ones.
   */
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  /**
   * ros::spin() will enter a loop, pumping callbacks.  With this version, all
   * callbacks will be called from within this thread (the main one).  ros::spin()
   * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
   */
  ros::spin();

  return 0;
}


在CMakeList.txt中添加

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)

打开终端

cd catkin_ws
catkin_make

你可能感兴趣的:(ROS)