test_joint_pos_controller.cpp
Go to the documentation of this file.
1 
28 
30 
31 #include <gtest/gtest.h>
32 
34 
36  public TestControllers
37 {
38 public:
40 
43  {
44  this->pid = pid;
46 
47  init();
48  }
49 
51  {
52  }
53 
55  {
56  controller::SrController *control_tmp = controller.get();
58  dynamic_cast< controller::SrhJointPositionController * >(control_tmp);
59  sr_control_tmp->init(robot_state.get(), "FFJ3", pid);
60  }
61 
62  double compute_output(double input, double current_position)
63  {
64  hw->current_time_ = ros::Time::now();
65  joint_state->position_ = current_position;
66  joint_state->commanded_position_ = input;
67 
68  controller->update();
69 
70  return joint_state->commanded_effort_;
71  }
72 };
73 
74 
76 {
77 // TESTING A PURE P CONTROLLER
79  pid = boost::shared_ptr<control_toolbox::Pid>(new control_toolbox::Pid(1.0, 0.0, 0.0, 0.0, 0.0));
80 
83 
84  const unsigned int nb_values = 7;
85 
86  bool with_friction_compensation = false;
87  if (ros::param::has("with_friction_compensation"))
88  {
89  int wfc;
90  ros::param::get("with_friction_compensation", wfc);
91  if (wfc == 1)
92  {
93  with_friction_compensation = true;
94  }
95  else
96  {
97  with_friction_compensation = false;
98  }
99  }
100 
101  const double values[nb_values] = {-123.123, -1.0, -0.5, 0.0, 0.5, 1.0, 456.456};
102  const double expected_values_no_fc[nb_values] = {-123.123, -1.0, -0.5, 0.0, 0.5, 1.0, 456.456};
103  const double expected_values_with_fc[nb_values] = {-323.123, -1.0, -0.5, 0.0, 0.5, 1.0, 656.456};
104 
105  ros::Duration pause(0.01);
106  double ctrl_output = 0.0;
107  for (unsigned int i = 0; i < nb_values; ++i)
108  {
109  ROS_INFO_STREAM("Sending demand: " << values[i]);
110  pause.sleep();
111 
112  ctrl_output = test_jpc->compute_output(values[i], 0.0);
113  if (with_friction_compensation)
114  {
115  ROS_INFO_STREAM("Expected value: " << expected_values_with_fc[i] << " Computed value: " << ctrl_output);
116  EXPECT_EQ(ctrl_output, expected_values_with_fc[i]);
117  }
118  else
119  {
120  ROS_INFO_STREAM("Expected value: " << expected_values_no_fc[i] << " Computed value: " << ctrl_output);
121  EXPECT_EQ(ctrl_output, expected_values_no_fc[i]);
122  }
123  }
124 
125  // Test the position deadband as well:
126  double target = 0.1;
127  const double pos[5] = {0.0, 0.11, 0.099, 0.116, 0.115};
128  const double expected_values[5] = {0.1, 0.0, 0.0, -0.016, 0.0};
129  for (unsigned int i = 0; i < 5; ++i)
130  {
131  pause.sleep();
132 
133  ctrl_output = test_jpc->compute_output(target, pos[i]);
134  ROS_INFO_STREAM("Expected value: " << expected_values[i] << " Computed value: " << ctrl_output);
135  EXPECT_EQ(ctrl_output, expected_values[i]);
136  }
137 
138  // TESTING A PURE I CONTROLLER
139  pid->reset();
140 
141  pid->setGains(0.0, 1.0, 0.0, 2.0, -2.0);
142 
143  ros::Duration one_sec_pause(1.0);
144  ros::Duration half_sec_pause(0.5);
145  // ros::Duration pause(0.01);
146 
147  const double expected_values_one_sec[nb_values] = {-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0};
148  const double expected_values_half_sec[nb_values] = {-2.0, -0.5, -0.25, 0.0, 0.25, 0.5, 2.0};
149 
150  for (unsigned int i = 0; i < nb_values; ++i)
151  {
152  ROS_INFO_STREAM("Sending demand: " << values[i]);
153  pid->reset();
154 
155  pause.sleep();
156 
157  ctrl_output = test_jpc->compute_output(0.0, 0.0);
158  one_sec_pause.sleep();
159 
160  ctrl_output = test_jpc->compute_output(values[i], 0.0);
161 
162  ROS_INFO_STREAM("Expected value: " << expected_values_one_sec[i] << " Computed value: " << ctrl_output);
163  EXPECT_TRUE(fabs(ctrl_output - expected_values_one_sec[i])
164  < 0.001);
165  }
166  for (unsigned int i = 0; i < nb_values; ++i)
167  {
168  ROS_INFO_STREAM("Sending demand: " << values[i]);
169  pid->reset();
170 
171  pause.sleep();
172 
173  ctrl_output = test_jpc->compute_output(0.0, 0.0);
174  half_sec_pause.sleep();
175 
176  ctrl_output = test_jpc->compute_output(values[i], 0.0);
177 
178  ROS_INFO_STREAM("Expected value: " << expected_values_half_sec[i] << " Computed value: " << ctrl_output);
179  EXPECT_TRUE(fabs(ctrl_output - expected_values_half_sec[i]) < 0.001);
180  }
181 };
182 
183 
184 
186 // MAIN //
188 
189 int main(int argc, char **argv)
190 {
191  ros::init(argc, argv, "test_joint_pos_controller");
192 
193  testing::InitGoogleTest(&argc, argv);
194  return RUN_ALL_TESTS();
195 }
196 
197 /* For the emacs weenies in the crowd.
198  Local Variables:
199  c-basic-offset: 2
200  End:
201 */
pr2_mechanism_model::JointState * joint_state
std::vector< double > values
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TestJointPositionController(boost::shared_ptr< control_toolbox::Pid > pid)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
int main(int argc, char **argv)
ROSCPP_DECL bool has(const std::string &key)
boost::shared_ptr< pr2_mechanism_model::RobotState > robot_state
boost::shared_ptr< control_toolbox::Pid > pid
#define ROS_INFO_STREAM(args)
static Time now()
TEST(SrhJointPositionController, TestPID)
bool init(ros_ethercat_model::RobotStateInterface *robot, ros::NodeHandle &n)
boost::shared_ptr< pr2_hardware_interface::HardwareInterface > hw
double compute_output(double input, double current_position)


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:58