美文网首页
ROS-I simple_message 源码分析:TypedM

ROS-I simple_message 源码分析:TypedM

作者: play_robot | 来源:发表于2019-03-20 17:47 被阅读0次

TypedMessage是一个辅助类,功能有两个:

  • 初始化(填充)不同类型SimpleMessage对象
  • 从已有的SimpleMessage对象初始化TypedMessage对象

这里所说的类型就是在SimpleMessage中定义的枚举消息类型,如果不记得了,我们再来回顾一下:

  enum StandardMsgType
  {
   INVALID = 0,
   PING = 1,

   JOINT_POSITION = 10,
   JOINT = 10, 
   READ_INPUT = 20,
   WRITE_OUTPUT = 21,

   JOINT_TRAJ_PT = 11,  //Joint trajectory point message (typically for streaming)
   JOINT_TRAJ = 12,   //Joint trajectory message (typically for trajectory downloading)
   STATUS = 13,                //Robot status message (for reporting the robot state)
   JOINT_TRAJ_PT_FULL = 14,  // Joint trajectory point message (all message fields)
   JOINT_FEEDBACK = 15,      // Feedback of joint pos/vel/accel


   SWRI_MSG_BEGIN     = 1000,
   UR_MSG_BEGIN       = 1100,
   ADEPT_MSG_BEGIN    = 1200,
   ABB_MSG_BEGIN      = 1300,
   FANUC_MSG_BEGIN    = 1400,
   MOTOMAN_MSG_BEGIN  = 2000
  };

TypedMessage接口定义源码

namespace industrial
{
namespace typed_message
{

class TypedMessage : public industrial::simple_serialize::SimpleSerialize
{

public:

  virtual bool init(industrial::simple_message::SimpleMessage & msg)=0;
  virtual void init()=0;
  virtual bool toRequest(industrial::simple_message::SimpleMessage & msg)
  {
      industrial::byte_array::ByteArray data;
      data.load(*this);
      return msg.init(this->getMessageType(),
              industrial::simple_message::CommTypes::SERVICE_REQUEST,
              industrial::simple_message::ReplyTypes::INVALID, data);
  }

  virtual bool toReply(industrial::simple_message::SimpleMessage & msg,
          industrial::simple_message::ReplyType reply)
  {
      industrial::byte_array::ByteArray data;
    data.load(*this);
    return msg.init(this->getMessageType(),
            industrial::simple_message::CommTypes::SERVICE_REPLY,
            reply, data);
  }

  virtual bool toTopic(industrial::simple_message::SimpleMessage & msg)
  {
    industrial::byte_array::ByteArray data;
    data.load(*this);
    return msg.init(this->getMessageType(),
            industrial::simple_message::CommTypes::TOPIC,
            industrial::simple_message::ReplyTypes::INVALID, data);
  }

  int getMessageType() const
  {
    return message_type_;
  }
  
  int getCommType() const
  {
    return comm_type_;
  }

protected:

  void setMessageType(int message_type = industrial::simple_message::StandardMsgTypes::INVALID)
  {
    this->message_type_ = message_type;
  }

  void setCommType(int comm_type = industrial::simple_message::CommTypes::INVALID)
  {
    this->comm_type_ = comm_type;
  }

private:
  int message_type_;
  int comm_type_;
};

}
}

为何需要定义TypedMessage这样一个辅助类呢?注意到我们之前说的JointData,JointTrajPt,JointTrajPtFull,JointFeedback,RobotStatus等等这些类,对应的是SimpleMessage中的data域,而为了实现ROS和机器人控制器通信,需要把这些data与消息类型和通信类型捆绑起来,那么TypedMessage就应运而生了。

具体如何使用这个TypedMessage类呢,我们后面再分析一些源码掌握它的使用方法。

相关文章

网友评论

      本文标题:ROS-I simple_message 源码分析:TypedM

      本文链接:https://www.haomeiwen.com/subject/fmttvqtx.html