Skip to content

Commit 06c1058

Browse files
committed
Make JointStateInterface class extendable
1 parent a708bcf commit 06c1058

File tree

1 file changed

+84
-20
lines changed

1 file changed

+84
-20
lines changed

hardware_interface/include/hardware_interface/joint_state_interface.h

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,88 @@
3737
namespace hardware_interface
3838
{
3939

40+
class PositionJointStateHandle
41+
{
42+
public:
43+
PositionJointStateHandle() = default;
44+
45+
PositionJointStateHandle(const double* pos) : pos_(pos)
46+
{
47+
if (!pos)
48+
{
49+
throw HardwareInterfaceException("Position data pointer is null.");
50+
}
51+
}
52+
53+
virtual double getPosition() const
54+
{
55+
assert(pos_);
56+
return *pos_;
57+
}
58+
59+
virtual std::string getName() const = 0;
60+
61+
protected:
62+
const double* pos_ = nullptr;
63+
};
64+
65+
class VelocityJointStateHandle
66+
{
67+
public:
68+
VelocityJointStateHandle() = default;
69+
70+
VelocityJointStateHandle(const double* vel) : vel_(vel)
71+
{
72+
if (!vel)
73+
{
74+
throw HardwareInterfaceException("Velocity data pointer is null.");
75+
}
76+
}
77+
78+
virtual double getVelocity() const
79+
{
80+
assert(vel_);
81+
return *vel_;
82+
}
83+
84+
virtual std::string getName() const = 0;
85+
86+
protected:
87+
const double* vel_ = nullptr;
88+
};
89+
90+
class EffortJointStateHandle
91+
{
92+
public:
93+
EffortJointStateHandle() = default;
94+
95+
EffortJointStateHandle(const double* eff) : eff_(eff)
96+
{
97+
if (!eff)
98+
{
99+
throw HardwareInterfaceException("Effort data pointer is null.");
100+
}
101+
}
102+
103+
virtual double getEffort() const
104+
{
105+
assert(eff_);
106+
return *eff_;
107+
}
108+
109+
virtual std::string getName() const = 0;
110+
111+
protected:
112+
const double* eff_ = nullptr;
113+
};
114+
40115
/** A handle used to read the state of a single joint. */
41-
class JointStateHandle
116+
class JointStateHandle : public PositionJointStateHandle,
117+
public VelocityJointStateHandle,
118+
public EffortJointStateHandle
42119
{
43120
public:
44-
JointStateHandle() : name_(), pos_(0), vel_(0), eff_(0) {}
121+
JointStateHandle() = default;
45122

46123
/**
47124
* \param name The name of the joint
@@ -50,32 +127,19 @@ class JointStateHandle
50127
* \param eff A pointer to the storage for this joint's effort (force or torque)
51128
*/
52129
JointStateHandle(const std::string& name, const double* pos, const double* vel, const double* eff)
53-
: name_(name), pos_(pos), vel_(vel), eff_(eff)
54-
{
55-
if (!pos)
56-
{
57-
throw HardwareInterfaceException("Cannot create handle '" + name + "'. Position data pointer is null.");
58-
}
59-
if (!vel)
130+
try : PositionJointStateHandle(pos), VelocityJointStateHandle(vel), EffortJointStateHandle(eff), name_(name)
60131
{
61-
throw HardwareInterfaceException("Cannot create handle '" + name + "'. Velocity data pointer is null.");
62132
}
63-
if (!eff)
133+
catch(const HardwareInterfaceException& ex)
64134
{
65-
throw HardwareInterfaceException("Cannot create handle '" + name + "'. Effort data pointer is null.");
135+
throw HardwareInterfaceException("Cannot create handle '" + name + "'. " + ex.what());
66136
}
67-
}
137+
68138

69139
std::string getName() const {return name_;}
70-
double getPosition() const {assert(pos_); return *pos_;}
71-
double getVelocity() const {assert(vel_); return *vel_;}
72-
double getEffort() const {assert(eff_); return *eff_;}
73140

74141
private:
75-
std::string name_;
76-
const double* pos_;
77-
const double* vel_;
78-
const double* eff_;
142+
std::string name_ = "";
79143
};
80144

81145
/** \brief Hardware interface to support reading the state of an array of joints

0 commit comments

Comments
 (0)