ROS ros study notes 9- create messages and services

The two subsections section come from official documents:
1. Use rosed to edit
2. Create a ros message service

First look rosed:

  1. rosed
    rosed command is rosbash part of a file using rosed can directly edit the package without having to type the full path to the file is located, used as follows:
    $ rosed [package_name] [filename]
    

    For example, we want to edit roscpp package adds Logger.msg, enter the following command:

    $ rosed roscpp Logger.msg
    

     Opens Logger.msg enter the edit mode use vim.
    Meanwhile, rosed support the use of the tab key automatic completion function is used as follows:

    $ rosed [package_name] <tab><tab>
    

    E.g:

    $ rosed roscpp <tab><tab>
    

    Note: There are two spaces between the tab and the package name.
    It lists all files roscpp package:

    Empty.srv                   roscpp.cmake
    genmsg_cpp.py               roscppConfig.cmake
    gensrv_cpp.py               roscppConfig-version.cmake
    GetLoggers.srv              roscpp-msg-extras.cmake
    Logger.msg                  roscpp-msg-paths.cmake
    msg_gen.py                  SetLoggerLevel.srv
    package.xml                 
    

     And, rosed can also specify the editor, using the default vim editor, the editor can be specified by setting the ~ / .bashrc file, environment variables:
    for example, enter a command to set the editor nano editor.

    export EDITOR='nano -w'
    

     You may also use the following command set gedit:

    export EDITOR='gedit -w'
    
  2. msg and srv document presents
    • msg : msg file is described ros msg News Members of a text file, he can generate a message code in different languages.
    • SRV : SRV service description file is a text file type a member of ros, he included the request and response in two parts.
    msg Each row contains a file type and a variable name:
    wherein variable type may be of the following types:
    • int8, int16, int32, int64 (and uint)
    • float32, float64
    • string
    • time, duration
    • Other message types
    • An array of variable length and a predetermined length []
    There is also a special type, type header (Header), Header type comprise a timestamp and axis information, which is often used in the ROS. A typical message ros defined as follows:
     Header header
      string child_frame_id
      geometry_msgs/PoseWithCovariance pose
      geometry_msgs/TwistWithCovariance twist
    

    srv file defines msg similar, but also different rows, each row containing a variable name and a variable type, srv but comprises two portions request (request) and response (Response), which two parts with one row and three dashes - - separated. E.g:

    int64 A
    int64 B
    ---
    int64 Sum
    

    In the example above where: A and B is a request, Sum response.

  3. Use msg
    1. Creating msg
      such as using the following command to create a new msg in the example of the bag before:
      $ roscd beginner_tutorials
      $ mkdir msg
      $ echo "int64 num" > msg/Num.msg
      

      The msg only one line, of course, can create a more complex msg

      string first_name
      string last_name
      uint8 age
      uint32 score
      

       Then, to make sure that the msg files can be converted to the code in other languages, you need to check package.xml in the following two lines exist and has not been commented out :( after the discovery of view is commented out)

      <build_depend>message_generation</build_depend>
        <exec_depend>message_runtime</exec_depend>
      

      During compilation, we need to enable message_generation, during operation, we need to enable message_runtime.
      Then use your favorite editor to open CMakeLists.txt (you can use rosed)
      and then add entries message_generation in find_package in:

      ## Find catkin macros and libraries
      ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
      ## is used, also find other catkin packages
      find_package(catkin REQUIRED COMPONENTS
        roscpp
        rospy
        std_msgs
        message_generation
      )
      

       Also, make sure the runtime dependencies also added dependent on the message:

      catkin_package(
      #  INCLUDE_DIRS include
      #  LIBRARIES begginner_tutorials
      #  CATKIN_DEPENDS roscpp rospy std_msgs
      #  DEPENDS system_lib
      CATKIN_DEPENS message_runtime
      )
      

      Find the code snippet message file:

      # add_message_files(
      #   FILES
      #   Message1.msg
      #   Message2.msg
      # )
      

       Uncomment and add the message file name:

      add_message_files(
        FILES
        Num.msg
      )
      

      Ensure generate_message is called:

      generate_messages(
        DEPENDENCIES
        std_msgs
      )
      
    2. Check the information msg
      After defining information, you can use rosmsg command to view the information message:
      $ rosmsg show [message type]
      

      For example, with the information defined above, with the following command:

      $ rosmsg show beginner_tutorials/Num
      

      It can be seen as a return:

      a int64
      

       I can not write the full path, if you do not remember what the message in the following package, you can write:

      $ rosmsg show Num
      

       Returns:

      [beginner_tutorials/Num]:
      int64 num
      
  4. Use srv
    1.  Creating srv
      similar msg, we first create a folder srv:
      $ roscd beginner_tutorials
      $ mkdir srv
      

      In which you can manually srv write a file, you can copy the file over the rest of the srv, presented here in a command file of ros reproduced below, roscp, the command is used as follows:

      $ roscp [package_name] [file_to_copy_path] [copy_path]
      

      Then we will rospy_tutorials copy srv file package over:

      $ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
      

       And also enable message_generation message_runtime (step with the same msg) in the package.xml.

      Then add message_generation (the same as the msg) in the call find_package's.
      The difference is in the call add_service_files to be added to the corresponding service entry.

      add_service_files(
        FILES
        AddTwoInts.srv
      )
      
    2. View srv information
      similar to rosmsg, ros also provides rossrv to see srv service-related information:
      for example:
      $ rossrv show beginner_tutorials/AddTwoInts
      

      return:

      int64 a
      int64 b
      ---
      int64 sum
      

      You can not add paths:

      $ rossrv show AddTwoInts
      [beginner_tutorials/AddTwoInts]:
      int64 a
      int64 b
      ---
      int64 sum
      
      [rospy_tutorials/AddTwoInts]:
      int64 a
      int64 b
      ---
      int64 sum
  5. General procedure (personal feeling that the festival should be renamed the compile step)
    If the above steps finished, you can then compile these msg and srv, CMakeLists.txt uncomment the following lines:
    generate_messages(
      DEPENDENCIES
      std_msgs
    )
    

    Then run catkin_make compile:

    # In your catkin workspace
    $ roscd beginner_tutorials
    $ cd ../..
    $ catkin_make install
    $ cd -
    

     Msg and srv files will be compiled into code in other languages. For example, msg, C ++ header file included in the ~ / catkin_ws / devel / include / beginner_tutorials / in;
    Python source file will be included in ~ / catkin_ws / devel / lib / python2.7 / dist-packages / beginner_tutorials / msg in;
    ~ / catkin_ws / devel / Share /-Common Lisp / ROS / beginner_tutorials / MSG / in.
    srv file for C ++ will and msg files generated source files in a unified folder, and python and lisp of srv files generated files in a separate srv folder, the folder with the msg folder in the same directory.
    The msg look at a C ++ header files:

    // Generated by gencpp from file begginner_tutorials/Num.msg
    // DO NOT EDIT!
    
    
    #ifndef BEGGINNER_TUTORIALS_MESSAGE_NUM_H
    #define BEGGINNER_TUTORIALS_MESSAGE_NUM_H
    
    
    #include <string>
    #include <vector>
    #include <map>
    
    #include <ros/types.h>
    #include <ros/serialization.h>
    #include <ros/builtin_message_traits.h>
    #include <ros/message_operations.h>
    
    
    namespace begginner_tutorials
    {
    template <class ContainerAllocator>
    struct Num_
    {
      typedef Num_<ContainerAllocator> Type;
    
      Num_()
        : num(0)  {
        }
      Num_(const ContainerAllocator& _alloc)
        :a (0) {
      (void)_alloc;
        }
    
    
    
       typedef int64_t _num_type;
      _num_type num;
    
    
    
    
    
      typedef boost::shared_ptr< ::begginner_tutorials::Num_<ContainerAllocator> > Ptr;
      typedef boost::shared_ptr< ::begginner_tutorials::Num_<ContainerAllocator> const> ConstPtr;
    
    }; // struct Num_
    
    typedef ::begginner_tutorials::Num_<std::allocator<void> > Num;
    
    typedef boost::shared_ptr< ::begginner_tutorials::Num > NumPtr;
    typedef boost::shared_ptr< ::begginner_tutorials::Num const> NumConstPtr;
    
    // constants requiring out of line definition
    
    
    
    template<typename ContainerAllocator>
    std::ostream& operator<<(std::ostream& s, const ::begginner_tutorials::Num_<ContainerAllocator> &v) 
    {
    ros::message_operations::Printer< ::begginner_tutorials::Num_<ContainerAllocator> >::stream(s, "", v);
    return s;
    }
    
    } // namespace begginner_tutorials
    
    namespace ros
    {
    namespace message_traits
    {
    
    
    
    // BOOLTRAITS {'IsFixedSize': True, 'IsMessage': True, 'HasHeader': False}
    // {'std_msgs': ['/opt/ros/kinetic/share/std_msgs/cmake/../msg'], 'begginner_tutorials': ['/home/shao/catkin_ws/src/begginner_tutorials/msg']}
    
    // !!!!!!!!!!! ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_parsed_fields', 'constants', 'fields', 'full_name', 'has_header', 'header_present', 'names', 'package', 'parsed_fields', 'short_name', 'text', 'types']
    
    
    
    
    template <class ContainerAllocator>
    struct IsFixedSize< ::begginner_tutorials::Num_<ContainerAllocator> >
      : TrueType
      { };
    
    template <class ContainerAllocator>
    struct IsFixedSize< ::begginner_tutorials::Num_<ContainerAllocator> const>
      : TrueType
      { };
    
    template <class ContainerAllocator>
    struct IsMessage< ::begginner_tutorials::Num_<ContainerAllocator> >
      : TrueType
      { };
    
    template <class ContainerAllocator>
    struct IsMessage< ::begginner_tutorials::Num_<ContainerAllocator> const>
      : TrueType
      { };
    
    template <class ContainerAllocator>
    struct HasHeader< ::begginner_tutorials::Num_<ContainerAllocator> >
      : FalseType
      { };
    
    template <class ContainerAllocator>
    struct HasHeader< ::begginner_tutorials::Num_<ContainerAllocator> const>
      : FalseType
      { };
    
    
    template<class ContainerAllocator>
    struct MD5Sum< ::begginner_tutorials::Num_<ContainerAllocator> >
    {
      static const char* value()
      {
        return "57d3c40ec3ac3754af76a83e6e73127a";
      }
    
      static const char* value(const ::begginner_tutorials::Num_<ContainerAllocator>&) { return value(); }
      static const uint64_t static_value1 = 0x57d3c40ec3ac3754ULL;
      static const uint64_t static_value2 = 0xaf76a83e6e73127aULL;
    };
    
    template<class ContainerAllocator>
    struct DataType< ::begginner_tutorials::Num_<ContainerAllocator> >
    {
      static const char* value()
      {
        return "begginner_tutorials/Num";
      }
    
      static const char* value(const ::begginner_tutorials::Num_<ContainerAllocator>&) { return value(); }
    };
    
    template<class ContainerAllocator>
    struct Definition< ::begginner_tutorials::Num_<ContainerAllocator> >
    {
      static const char* value()
      {
        return "int64 num\n\
    ";
      }
    
      static const char* value(const ::begginner_tutorials::Num_<ContainerAllocator>&) { return value(); }
    };
    
    } // namespace message_traits
    } // namespace ros
    
    namespace ros
    {
    namespace serialization
    {
    
      template<class ContainerAllocator> struct Serializer< ::begginner_tutorials::Num_<ContainerAllocator> >
      {
        template<typename Stream, typename T> inline static void allInOne(Stream& stream, T m)
        {
          stream.next(m.num);
        }
    
        ROS_DECLARE_ALLINONE_SERIALIZER
      }; // struct Num_
    
    } // namespace serialization
    } // namespace ros
    
    namespace ros
    {
    namespace message_operations
    {
    
    template<class ContainerAllocator>
    struct Printer< ::begginner_tutorials::Num_<ContainerAllocator> >
    {
      template<typename Stream> static void stream(Stream& s, const std::string& indent, const ::begginner_tutorials::Num_<ContainerAllocator>& v)
      {
        s << indent << "num: ";
        Printer<int64_t>::stream(s, indent + "  ", v.num);
      }
    };
    
    } // namespace message_operations
    } // namespace ros
    
    #endif // BEGGINNER_TUTORIALS_MESSAGE_NUM_H
    

    It was quite complicated.

Guess you like

Origin www.cnblogs.com/spyplus/p/11544359.html