Skip to content

Commit 426d1a9

Browse files
authored
Merge pull request #228 from miguelprada/velocity_iface_tests
Add tests for velocity_controllers::JointTrajectoryController
2 parents 8a5a690 + 16361b9 commit 426d1a9

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

joint_trajectory_controller/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ if(CATKIN_ENABLE_TESTING)
8888
test/joint_trajectory_controller.test
8989
test/joint_trajectory_controller_test.cpp)
9090
target_link_libraries(joint_trajectory_controller_test ${catkin_LIBRARIES})
91+
92+
add_rostest_gtest(joint_trajectory_controller_vel_test
93+
test/joint_trajectory_controller_vel.test
94+
test/joint_trajectory_controller_test.cpp)
95+
target_link_libraries(joint_trajectory_controller_vel_test ${catkin_LIBRARIES})
9196
endif()
9297

9398
# Install
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<launch>
2+
<!-- Load RRbot model -->
3+
<param name="robot_description"
4+
command="$(find xacro)/xacro.py '$(find joint_trajectory_controller)/test/rrbot.xacro'" />
5+
6+
<!-- Start RRbot -->
7+
<node name="rrbot"
8+
pkg="joint_trajectory_controller"
9+
type="rrbot"/>
10+
11+
<!-- Load controller config -->
12+
<rosparam command="load" file="$(find joint_trajectory_controller)/test/rrbot_vel_controllers.yaml" />
13+
14+
<!-- Spawn controller -->
15+
<node name="controller_spawner"
16+
pkg="controller_manager" type="spawner" output="screen"
17+
args="rrbot_controller" />
18+
19+
<!-- Rxplot monitoring -->
20+
<node name="rrbot_pos_monitor"
21+
pkg="rqt_plot"
22+
type="rqt_plot"
23+
args="/rrbot_controller/state/desired/positions[0]:positions[1],/rrbot_controller/state/actual/positions[0]:positions[1]" />
24+
25+
<node name="rrbot_vel_monitor"
26+
pkg="rqt_plot"
27+
type="rqt_plot"
28+
args="/rrbot_controller/state/desired/velocities[0]:velocities[1],/rrbot_controller/state/actual/velocities[0]:velocities[1]" />
29+
30+
<!-- Controller test -->
31+
<test test-name="joint_trajectory_controller_vel_test"
32+
pkg="joint_trajectory_controller"
33+
type="joint_trajectory_controller_test"
34+
time-limit="80.0"/>
35+
</launch>

joint_trajectory_controller/test/rrbot.cpp

+63-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class RRbot : public hardware_interface::RobotHW
4747
pos_[0] = 0.0; pos_[1] = 0.0;
4848
vel_[0] = 0.0; vel_[1] = 0.0;
4949
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;
5152

5253
// Connect and register the joint state interface
5354
hardware_interface::JointStateHandle state_handle_1("joint1", &pos_[0], &vel_[0], &eff_[0]);
@@ -59,14 +60,23 @@ class RRbot : public hardware_interface::RobotHW
5960
registerInterface(&jnt_state_interface_);
6061

6162
// 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]);
6364
jnt_pos_interface_.registerHandle(pos_handle_1);
6465

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]);
6667
jnt_pos_interface_.registerHandle(pos_handle_2);
6768

6869
registerInterface(&jnt_pos_interface_);
6970

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+
7080
// Smoothing subscriber
7181
smoothing_sub_ = ros::NodeHandle().subscribe("smoothing", 1, &RRbot::smoothingCB, this);
7282
smoothing_.initRT(0.0);
@@ -82,21 +92,67 @@ class RRbot : public hardware_interface::RobotHW
8292
const double smoothing = *(smoothing_.readFromRT());
8393
for (unsigned int i = 0; i < 2; ++i)
8494
{
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;
86111

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+
}
89131
}
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];
90141
}
91142

92143
private:
93144
hardware_interface::JointStateInterface jnt_state_interface_;
94145
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];
96149
double pos_[2];
97150
double vel_[2];
98151
double eff_[2];
99152

153+
std::string active_interface_[2];
154+
std::string next_active_interface_[2];
155+
100156
realtime_tools::RealtimeBuffer<double> smoothing_;
101157
void smoothingCB(const std_msgs::Float64& smoothing) {smoothing_.writeFromNonRT(smoothing.data);}
102158

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
rrbot_controller:
2+
type: "velocity_controllers/JointTrajectoryController"
3+
joints:
4+
- joint1
5+
- joint2
6+
7+
constraints:
8+
goal_time: 0.5
9+
joint1:
10+
goal: 0.01
11+
trajectory: 0.05
12+
joint2:
13+
goal: 0.01
14+
trajectory: 0.05
15+
16+
gains:
17+
joint1: {p: 60.0, i: 0.0, d: 0.0, i_clamp: 1}
18+
joint2: {p: 60.0, i: 0.0, d: 0.0, i_clamp: 1}

0 commit comments

Comments
 (0)