@@ -47,7 +47,8 @@ class RRbot : public hardware_interface::RobotHW
47
47
pos_[0 ] = 0.0 ; pos_[1 ] = 0.0 ;
48
48
vel_[0 ] = 0.0 ; vel_[1 ] = 0.0 ;
49
49
eff_[0 ] = 0.0 ; eff_[1 ] = 0.0 ;
50
- cmd_[0 ] = 0.0 ; cmd_[1 ] = 0.0 ;
50
+ pos_cmd_[0 ] = 0.0 ; pos_cmd_[1 ] = 0.0 ;
51
+ vel_cmd_[0 ] = 0.0 ; vel_cmd_[1 ] = 0.0 ;
51
52
52
53
// Connect and register the joint state interface
53
54
hardware_interface::JointStateHandle state_handle_1 (" joint1" , &pos_[0 ], &vel_[0 ], &eff_[0 ]);
@@ -59,14 +60,23 @@ class RRbot : public hardware_interface::RobotHW
59
60
registerInterface (&jnt_state_interface_);
60
61
61
62
// Connect and register the joint position interface
62
- hardware_interface::JointHandle pos_handle_1 (jnt_state_interface_.getHandle (" joint1" ), &cmd_ [0 ]);
63
+ hardware_interface::JointHandle pos_handle_1 (jnt_state_interface_.getHandle (" joint1" ), &pos_cmd_ [0 ]);
63
64
jnt_pos_interface_.registerHandle (pos_handle_1);
64
65
65
- hardware_interface::JointHandle pos_handle_2 (jnt_state_interface_.getHandle (" joint2" ), &cmd_ [1 ]);
66
+ hardware_interface::JointHandle pos_handle_2 (jnt_state_interface_.getHandle (" joint2" ), &pos_cmd_ [1 ]);
66
67
jnt_pos_interface_.registerHandle (pos_handle_2);
67
68
68
69
registerInterface (&jnt_pos_interface_);
69
70
71
+ // Connect and register the joint velocity interface
72
+ hardware_interface::JointHandle vel_handle_1 (jnt_state_interface_.getHandle (" joint1" ), &vel_cmd_[0 ]);
73
+ jnt_vel_interface_.registerHandle (vel_handle_1);
74
+
75
+ hardware_interface::JointHandle vel_handle_2 (jnt_state_interface_.getHandle (" joint2" ), &vel_cmd_[1 ]);
76
+ jnt_vel_interface_.registerHandle (vel_handle_2);
77
+
78
+ registerInterface (&jnt_vel_interface_);
79
+
70
80
// Smoothing subscriber
71
81
smoothing_sub_ = ros::NodeHandle ().subscribe (" smoothing" , 1 , &RRbot::smoothingCB, this );
72
82
smoothing_.initRT (0.0 );
@@ -82,21 +92,67 @@ class RRbot : public hardware_interface::RobotHW
82
92
const double smoothing = *(smoothing_.readFromRT ());
83
93
for (unsigned int i = 0 ; i < 2 ; ++i)
84
94
{
85
- vel_[i] = (cmd_[i] - pos_[i]) / getPeriod ().toSec ();
95
+ if (active_interface_[i] == " hardware_interface::PositionJointInterface" )
96
+ {
97
+ vel_[i] = (pos_cmd_[i] - pos_[i]) / getPeriod ().toSec ();
98
+
99
+ const double next_pos = smoothing * pos_[i] + (1.0 - smoothing) * pos_cmd_[i];
100
+ pos_[i] = next_pos;
101
+ }
102
+ else if (active_interface_[i] == " hardware_interface::VelocityJointInterface" )
103
+ {
104
+ vel_[i] = (1.0 - smoothing) * vel_cmd_[i];
105
+ pos_[i] = pos_[i] + vel_[i] * getPeriod ().toSec ();
106
+ }
107
+ }
108
+ }
109
+
110
+ typedef std::list<hardware_interface::ControllerInfo> ControllerList;
86
111
87
- const double next_pos = smoothing * pos_[i] + (1.0 - smoothing) * cmd_[i];
88
- pos_[i] = next_pos;
112
+ bool prepareSwitch (const ControllerList& start_list,
113
+ const ControllerList& stop_list)
114
+ {
115
+ for (ControllerList::const_iterator it = start_list.begin (); it != start_list.end (); ++it)
116
+ {
117
+ for (std::vector<hardware_interface::InterfaceResources>::const_iterator iface_it = it->claimed_resources .begin (); iface_it != it->claimed_resources .end (); ++iface_it)
118
+ {
119
+ for (std::set<std::string>::const_iterator res_it = iface_it->resources .begin (); res_it != iface_it->resources .end (); ++res_it)
120
+ {
121
+ if (*res_it == " joint1" )
122
+ {
123
+ next_active_interface_[0 ] = iface_it->hardware_interface ;
124
+ }
125
+ else if (*res_it == " joint2" )
126
+ {
127
+ next_active_interface_[1 ] = iface_it->hardware_interface ;
128
+ }
129
+ }
130
+ }
89
131
}
132
+
133
+ return true ;
134
+ }
135
+
136
+ void doSwitch (const ControllerList& start_list,
137
+ const ControllerList& stop_list)
138
+ {
139
+ active_interface_[0 ] = next_active_interface_[0 ];
140
+ active_interface_[1 ] = next_active_interface_[1 ];
90
141
}
91
142
92
143
private:
93
144
hardware_interface::JointStateInterface jnt_state_interface_;
94
145
hardware_interface::PositionJointInterface jnt_pos_interface_;
95
- double cmd_[2 ];
146
+ hardware_interface::VelocityJointInterface jnt_vel_interface_;
147
+ double pos_cmd_[2 ];
148
+ double vel_cmd_[2 ];
96
149
double pos_[2 ];
97
150
double vel_[2 ];
98
151
double eff_[2 ];
99
152
153
+ std::string active_interface_[2 ];
154
+ std::string next_active_interface_[2 ];
155
+
100
156
realtime_tools::RealtimeBuffer<double > smoothing_;
101
157
void smoothingCB (const std_msgs::Float64 & smoothing) {smoothing_.writeFromNonRT (smoothing.data );}
102
158
0 commit comments