20 #include <gmock/gmock.h> 23 #include <sensor_msgs/JointState.h> 24 #include <std_srvs/SetBool.h> 27 #include <pilz_testutils/async_test.h> 30 #include <prbt_hardware_support/FrameSpeeds.h> 31 #include <prbt_hardware_support/OperationModes.h> 36 using ::testing::AtLeast;
37 using ::testing::InSequence;
38 using ::testing::PrintToString;
39 using ::testing::Return;
40 using ::testing::SetArgReferee;
60 MATCHER_P(RunPermittedState, x,
"RunPermitted state " + std::string(negation ?
"is not" :
"is") +
": " + PrintToString(x) +
".")
66 "Names" + PrintToString(names) + std::string(negation ?
"are not" :
"are") +
" in message.")
68 for (
auto& name : names)
71 for (
auto& argn : arg->name)
73 if (argn.compare(name) == 0)
90 void SetUp()
override;
91 void TearDown()
override;
93 MOCK_METHOD2(run_permitted_cb,
bool(std_srvs::SetBool::Request& req, std_srvs::SetBool::Response& res));
94 MOCK_METHOD1(frame_speeds_cb,
void(
const FrameSpeeds::ConstPtr&
msg));
96 void publishTfAtSpeed(
double speed,
const std::string& frame);
97 void publishJointStatesAtSpeed(
double v);
98 void stopJointStatePublisher();
99 void stopTfPublisher();
100 void publishOperationMode(OperationModes::_value_type omv);
111 bool joint_publisher_running_{
false };
112 bool tf_publisher_running_{
false };
124 fake_controller_joint_states_pub_ =
131 ROS_DEBUG_STREAM(
"SetUp pnh:" << pnh_.getNamespace() <<
" nh:" << nh_.getNamespace());
132 for (
auto& frame : additional_frames_)
137 run_permitted_srv_ = nh_.advertiseService(
RUN_PERMITTED_SERVICE, &SpeedObserverIntegrationTest::run_permitted_cb,
this);
139 run_permitted_res.success =
true;
140 run_permitted_res.message =
"testing ...";
143 waitForNode(
"/operation_mode_setup_executor_node");
148 joint_publisher_running_ =
false;
159 tf_publisher_running_ =
true;
160 while (tf_publisher_running_)
163 t = (current - start).toSec();
164 geometry_msgs::TransformStamped tranform_stamped_b;
165 tranform_stamped_b.header.stamp = current +
ros::Duration(0.1);
167 tranform_stamped_b.child_frame_id = frame;
169 tranform_stamped_b.transform.translation.x = speed *
cos(t);
170 tranform_stamped_b.transform.translation.y = speed *
SQRT_2_HALF * -
sin(t);
171 tranform_stamped_b.transform.translation.z = speed *
SQRT_2_HALF * sin(t);
172 tranform_stamped_b.transform.rotation.w = 1;
175 if (tf_publisher_running_)
185 operation_mode_pub_.publish(om);
190 sensor_msgs::JointState js;
191 js.name = {
"testing_world-a" };
197 joint_publisher_running_ =
true;
198 bool first_round{
true};
199 while (joint_publisher_running_ & nh_.ok())
202 t = (current - start).toSec();
210 js.position = { (t - 0.5*t_zero) * speed };
212 fake_controller_joint_states_pub_.publish(js);
213 if (joint_publisher_running_ & nh_.ok())
220 joint_publisher_running_ =
false;
225 tf_publisher_running_ =
false;
260 EXPECT_CALL(*
this, run_permitted_cb(_, _)).Times(0);
262 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
266 .WillRepeatedly(Return());
269 publishOperationMode(OperationModes::T1);
278 EXPECT_CALL(*
this, run_permitted_cb(RunPermittedState(
false), _))
280 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_res), Return(
true)));
283 speed_limit_t1_ + 0.01);
285 stopJointStatePublisher();
286 pubisher_thread.join();
289 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
292 .WillRepeatedly(Return());
331 EXPECT_CALL(*
this, run_permitted_cb(_, _)).Times(0);
333 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
337 .WillRepeatedly(Return());
340 publishOperationMode(OperationModes::AUTO);
348 EXPECT_CALL(*
this, run_permitted_cb(_, _)).Times(0);
350 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
354 .WillRepeatedly(Return());
357 speed_limit_automatic_ - 0.01);
365 EXPECT_CALL(*
this, run_permitted_cb(RunPermittedState(
false), _))
367 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_res), Return(
true)));
369 publishOperationMode(OperationModes::T1);
371 stopJointStatePublisher();
372 pubisher_thread.join();
375 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
378 .WillRepeatedly(Return());
411 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
412 .WillRepeatedly(Return());
414 EXPECT_CALL(*
this, run_permitted_cb(RunPermittedState(
false), _))
416 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_res), Return(
true)));
418 publishOperationMode(OperationModes::T1);
422 speed_limit_t1_ + 0.01,
423 additional_frames_[0]);
426 pubisher_thread.join();
429 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
432 .WillRepeatedly(Return());
468 run_permitted_res.success =
false;
469 EXPECT_CALL(*
this, run_permitted_cb(RunPermittedState(
false), _))
471 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_res), Return(
true)));
473 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
477 .WillRepeatedly(Return());
480 publishOperationMode(OperationModes::T1);
483 speed_limit_t1_ + 0.01);
485 stopJointStatePublisher();
486 pubisher_thread.join();
489 EXPECT_CALL(*
this, frame_speeds_cb(ContainsAllNames(additional_frames_)))
492 .WillRepeatedly(Return());
499 int main(
int argc,
char* argv[])
501 ros::init(argc, argv,
"unittest_speed_observer");
504 testing::InitGoogleTest(&argc, argv);
505 return RUN_ALL_TESTS();
static const std::string BARRIER_NO_SVC_SUCESS
double speed_limit_automatic_
static const std::string BARRIER_STOP_HAPPENED
std::vector< std::string > additional_frames_
static const std::string RUN_PERMITTED_SERVICE
static const double TEST_FREQUENCY
ros::ServiceServer run_permitted_srv_
static const std::string BARRIER_NO_STOP_HAPPENED
void publishOperationMode(OperationModes::_value_type omv)
void waitForNode(std::string node_name, double loop_frequency=10.0)
Blocks until a node defined by node_name comes up.
static const double SQRT_2_HALF
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TEST_F(SpeedObserverIntegrationTest, testOperationModeT1)
Tests speed observer with operation mode T1.
static const std::string SPEED_LIMIT_AUTOMATIC_PARAM_NAME
geometry_msgs::TransformStamped t
int main(int argc, char *argv[])
ros::Publisher operation_mode_pub_
static const std::string ADDITIONAL_FRAMES_PARAM_NAME
ros::Subscriber subscriber_
static const std::string TEST_WORLD_FRAME
void publishJointStatesAtSpeed(double v)
static const std::string SPEED_LIMIT_T1_PARAM_NAME
void stopJointStatePublisher()
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
#define ROS_DEBUG_STREAM(args)
static const std::string FRAME_SPEEDS_TOPIC_NAME
MATCHER_P(RunPermittedState, x,"RunPermitted state "+std::string(negation?"is not":"is")+": "+PrintToString(x)+".")
static const std::string FAKE_CONTROLLER_JOINT_STATES_TOPIC_NAME
void publishTfAtSpeed(double speed, const std::string &frame)
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
ros::Publisher fake_controller_joint_states_pub_
static const std::string OPERATION_MODE_TOPIC
static const std::string BARRIER_WAIT_OUT_STOP
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
std_srvs::SetBool::Response run_permitted_res