目录
joint_data
在ROS-Industrial 之 simple_message 实现分析二中,已经分析理解了对字节数据的操纵、对协议头部Header部分的相关消息定义以及如何对消息的头信息进行编码与解码,而在这篇文章中,则是在上一篇的基础上,对消息Header的后继部分,即data进行分析与理解,明确其在simple_message协议中是如何实现的。首先看一下关节数据部分的代码:
#ifndef JOINT_DATA_H
#define JOINT_DATA_H
#ifndef FLATHEADERS
#include "simple_message/simple_message.h"
#include "simple_message/simple_serialize.h"
#include "simple_message/shared_types.h"
#else
#include "simple_message.h"
#include "simple_serialize.h"
#include "shared_types.h"
#endif
namespace industrial
{
namespace joint_data
{
/**
* 本类使用固定长度的数组对关节数据进行封装。类成员如下:
* member: type size
* joints (industrial::shared_types::shared_real) 4 * MAX_NUM_JOINTS
*/
class JointData : public industrial::simple_serialize::SimpleSerialize
{
public:
JointData(void);
~JointData(void);
//初始化
void init();
//设置关节数据
bool setJoint(industrial::shared_types::shared_int index, industrial::shared_types::shared_real value);
//获取关节数据
bool getJoint(industrial::shared_types::shared_int index, industrial::shared_types::shared_real & value) const;
industrial::shared_types::shared_real getJoint(industrial::shared_types::shared_int index) const;
//拷贝关节数据
void copyFrom(JointData &src);
//返回最大关节数目
int getMaxNumJoints() const
{
return MAX_NUM_JOINTS;
}
//重载等于逻辑关系运算符
bool operator==(JointData &rhs);
// 实现SimpleSerialize类中的虚函数
bool load(industrial::byte_array::ByteArray *buffer);
bool unload(industrial::byte_array::ByteArray *buffer);
//返回数据长度
unsigned int byteLength()
{
return MAX_NUM_JOINTS * sizeof(industrial::shared_types::shared_real);
}
private:
//定义最大关节数目
static const industrial::shared_types::shared_int MAX_NUM_JOINTS = 10;
//定义关节数组变量
industrial::shared_types::shared_real joints_[MAX_NUM_JOINTS];
};
}
}
#endif /* JOINT_DATA_H */
先来看一下这个类的结构,然后再来讨论其实现问题与主要功能。
从上面的代码与类结构图中,可以分析出,JointData 类继承自industrial::simple_serialize::SimpleSerialize类,而industrial::simple_serialize::SimpleSerialize类中只有三个虚函数,要在子类中重写实现代码,而JointData 类中,给出的私有变量有两个,一个是常量,即最大的关节数量;另一个则是对应于关节数量的由关节角度值组成的数组,所以这个类最主要的实现就是围绕着关节值进行的,关节数据的设置主要是从JointData 变量进行,或者单独的由关节的索引对每个关节进行设置。而重写的load函数,则是将关节值从其数组中加载到buffer中,unload则是由buffer解码到当前类的关节数组中。这个类很简洁,只有关节值需要关心,下面再来看另一个类。
joint_traj_pt
#ifndef JOINT_TRAJ_PT_H
#define JOINT_TRAJ_PT_H
#ifndef FLATHEADERS
#include "simple_message/joint_data.h"
#include "simple_message/simple_message.h"
#include "simple_message/simple_serialize.h"
#include "simple_message/shared_types.h"
#else
#include "joint_data.h"
#include "simple_message.h"
#include "simple_serialize.h"
#include "shared_types.h"
#endif
namespace industrial
{
namespace joint_traj_pt
{
namespace SpecialSeqValues
{
enum SpecialSeqValue
{
START_TRAJECTORY_DOWNLOAD = -1, ///下载完整轨迹时的开始信号
START_TRAJECTORY_STREAMING = -2, ///流方式下载轨迹点的开始信号
END_TRAJECTORY = -3, ///下载完整轨迹的完结信号
STOP_TRAJECTORY = -4 ///流方式运行的轨迹停止信号
};
}
typedef SpecialSeqValues::SpecialSeqValue SpecialSeqValue;
/**
* 封装关节轨迹点的类
* 信息格式如下:
* 成员: 类型 大小
* sequence (industrial::shared_types::shared_int) 4 bytes
* joints (industrial::joint_data) 40 bytes
* velocity (industrial::shared_types::shared_real) 4 bytes
* duration (industrial::shared_types::shared_real) 4 bytes
*/
class JointTrajPt : public industrial::simple_serialize::SimpleSerialize
{
public:
JointTrajPt(void);
~JointTrajPt(void);
//初始化
void init();
void init(industrial::shared_types::shared_int sequence, industrial::joint_data::JointData & position,industrial::shared_types::shared_real velocity, industrial::shared_types::shared_real duration);
//设置关节成员变量
void setJointPosition(industrial::joint_data::JointData &position)
{
this->joint_position_.copyFrom(position);
}
//提取关节成员变量
void getJointPosition(industrial::joint_data::JointData &dest)
{
dest.copyFrom(this->joint_position_);
}
//设置序列号
void