- ROS-I simple_message 源码分析:JointM
- ROS-I simple_message 源码分析:Messag
- ROS-I simple_message 源码分析:SmplMs
- ROS-I simple_message 源码分析:Messag
- ROS-I simple_message 源码分析:JointT
- ROS-I simple_message 源码分析:JointT
- ROS-I simple_message 源码分析:JointT
- ROS-I simple_message 源码分析:TypedM
- ROS-I simple_message 源码分析:RobotS
- ROS-I simple_message 源码分析:JointT
分析完RobotStatusMessage类,我们再看一下JointMessage类。JointMessage封装的是 JointData数据类型,用于从关节位置生成SimpleMessage消息,或者从SimpleMessage消息重建关节位置。
namespace SpecialSeqValues
{
enum SpecialSeqValue
{
END_TRAJECTORY = -1, STOP_TRAJECTORY = -2
};
}
typedef SpecialSeqValues::SpecialSeqValue SpecialSeqValue;
class JointMessage : public industrial::typed_message::TypedMessage
{
public:
JointMessage(void);
~JointMessage(void);
bool init(industrial::simple_message::SimpleMessage & msg);
void init(industrial::shared_types::shared_int seq, industrial::joint_data::JointData & joints);
void init();
void setSequence(industrial::shared_types::shared_int sequence);
industrial::shared_types::shared_int getSequence()
{
return sequence_;
}
industrial::joint_data::JointData& getJoints()
{
return this->joints_;
}
// Overrides - SimpleSerialize
bool load(industrial::byte_array::ByteArray *buffer);
bool unload(industrial::byte_array::ByteArray *buffer);
unsigned int byteLength()
{
return sizeof(industrial::shared_types::shared_int) + this->joints_.byteLength();
}
private:
//序号
industrial::shared_types::shared_int sequence_;
//每个关节的位置数据
industrial::joint_data::JointData joints_;
};
}
}
- 从SimpleMessage创建JointMessage
bool JointMessage::init(industrial::simple_message::SimpleMessage & msg)
{
bool rtn = false;
ByteArray data = msg.getData();
this->setMessageType(StandardMsgTypes::JOINT_POSITION);
if (data.unload(this->joints_))
{
if (data.unload(this->sequence_))
{
rtn = true;
}
else
{
rtn = false;
LOG_ERROR("Failed to unload sequence data");
}
}
else
{
LOG_ERROR("Failed to unload joint data");
}
return rtn;
}
在上面的init
中,程序首先从SimpleMessage中获取原始字节数据,再依次卸载序号、关节位置数据,从而完成由SimpleMessage创建JointMessage的操作。
- 从JointMessage创建SimpleMessage
这个方向的创建与RobotStatusMessage是一样的,我们再温习一下:
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);
}
同样,JointMessage允许创建所有的SimpleMessage通信类型:SERVICE_REQUEST,SERVICE_REPLY,TOPIC,如果希望用户不可创建某种通信类型的SimpleMessage,只需要将相应的方法重写返回false即可。
网友评论