20 #include <gmock/gmock.h> 23 #include <std_srvs/SetBool.h> 27 #include <pilz_testutils/async_test.h> 28 #include <prbt_hardware_support/FrameSpeeds.h> 29 #include <prbt_hardware_support/SetSpeedLimit.h> 36 using ::testing::AllOf;
37 using ::testing::AtLeast;
38 using ::testing::AtMost;
39 using ::testing::DoAll;
40 using ::testing::Return;
41 using ::testing::SetArgReferee;
60 using ::testing::AllOf;
61 using ::testing::AtLeast;
62 using ::testing::AtMost;
67 void SetUp()
override;
69 MOCK_METHOD1(frame_speeds_cb_mock,
void(
const FrameSpeeds::ConstPtr&
msg));
70 MOCK_METHOD2(stop_cb_mock,
bool(std_srvs::SetBool::Request& req, std_srvs::SetBool::Response& res));
71 void publishTfAtSpeed(
const double v,
const double observer_frequency =
OBSERVER_FREQUENCY);
72 void stopTfPublisher();
80 bool tf_publisher_running_{
false };
98 tf_publisher_running_ =
true;
99 while (tf_publisher_running_)
102 t = (current - start).toSec();
104 geometry_msgs::TransformStamped transform_stamped_a;
105 transform_stamped_a.header.stamp = current;
108 transform_stamped_a.transform.translation.x = 99.0;
109 transform_stamped_a.transform.translation.y = 99.0;
110 transform_stamped_a.transform.translation.z = 99.0;
111 transform_stamped_a.transform.rotation.w = 1;
113 geometry_msgs::TransformStamped transform_stamped_b;
114 transform_stamped_b.header.stamp = current;
118 transform_stamped_b.transform.translation.x = speed *
cos(t);
119 transform_stamped_b.transform.translation.y = speed *
SQRT_2_HALF * -
sin(t);
120 transform_stamped_b.transform.translation.z = speed *
SQRT_2_HALF * sin(t);
121 transform_stamped_b.transform.rotation.w = 1;
125 if (tf_publisher_running_)
132 tf_publisher_running_ =
false;
135 using ::testing::PrintToString;
136 MATCHER_P(RunPermittedState, x,
"RunPermitted state " + std::string(negation ?
"is not" :
"is") +
": " + PrintToString(x) +
".")
138 return arg.data == x;
142 "Name at index " + PrintToString(i) + std::string(negation ?
"is not" :
"is") +
": " + name +
".")
144 return static_cast<size_t>(i) < arg->name.size() && arg->name.at(static_cast<size_t>(i)).compare(name) == 0;
148 "Speed at index " + PrintToString(i) + std::string(negation ?
"is not" :
"is") +
" greater or equal to" +
149 PrintToString(x) +
".")
151 return static_cast<size_t>(i) < arg->name.size() && arg->speed.at(static_cast<size_t>(i)) >= x;
155 "Speed at index " + PrintToString(i) + std::string(negation ?
"is not" :
"is") +
" less or equal to" +
156 PrintToString(x) +
".")
158 return static_cast<size_t>(i) < arg->name.size() && arg->speed.at(static_cast<size_t>(i)) <= x;
188 SpeedAtILe(0, .1), SpeedAtILe(1, .26),
189 SpeedAtIGe(0, 0), SpeedAtIGe(1, 0))))
193 .WillRepeatedly(Return());
195 EXPECT_CALL(*
this, stop_cb_mock(_, _)).Times(0);
208 pubisher_thread_slow.join();
215 observer_thread.join();
249 SpeedAtILe(0, .1), SpeedAtILe(1, .32),
250 SpeedAtIGe(0, 0), SpeedAtIGe(1, 0))))
253 SpeedAtILe(0, .1), SpeedAtILe(1, .32),
254 SpeedAtIGe(0, 0), SpeedAtIGe(1, .28))));
256 std_srvs::SetBool::Response run_permitted_srv_resp;
257 run_permitted_srv_resp.success =
true;
259 EXPECT_CALL(*
this, stop_cb_mock(RunPermittedState(
false), _))
260 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_srv_resp), ACTION_OPEN_BARRIER(
BARRIER_FAST)));
273 pubisher_thread_fast.join();
280 observer_thread.join();
320 SpeedAtILe(0, .1), SpeedAtILe(1, .32),
321 SpeedAtIGe(0, 0), SpeedAtIGe(1, 0))))
324 SpeedAtILe(0, .1), SpeedAtILe(1, .32),
325 SpeedAtIGe(0, 0), SpeedAtIGe(1, .28))))
329 .WillRepeatedly(Return());
330 EXPECT_CALL(*
this, stop_cb_mock(_, _)).Times(0);
335 SetSpeedLimitRequest req = SetSpeedLimitRequest();
336 SetSpeedLimitResponse res = SetSpeedLimitResponse();
337 req.speed_limit = .4;
351 std_srvs::SetBool::Response run_permitted_srv_resp;
352 run_permitted_srv_resp.success =
true;
353 EXPECT_CALL(*
this, stop_cb_mock(RunPermittedState(
false), _))
354 .WillRepeatedly(DoAll(SetArgReferee<1>(run_permitted_srv_resp), ACTION_OPEN_BARRIER(
BARRIER_LIMIT_LOW)));
356 req.speed_limit = .2;
364 ROS_DEBUG_STREAM(
"Tear Down");
366 pubisher_thread_fast.join();
368 observer_thread.join();
392 std::vector<std::string> frames_to_observe{
TEST_FRAME_A };
419 EXPECT_CALL(*
this, stop_cb_mock(RunPermittedState(
false), _))
421 .WillOnce(DoAll(ACTION_OPEN_BARRIER_VOID(
BARRIER_FAST), Return(
false)));
438 pubisher_thread_fast.join();
440 observer_thread.join();
465 EXPECT_CALL(*
this, stop_cb_mock(RunPermittedState(
false), _))
467 .WillRepeatedly(DoAll(ACTION_OPEN_BARRIER_VOID(
BARRIER_FAST), Return(
true)));
484 pubisher_thread_fast.join();
486 observer_thread.join();
491 int main(
int argc,
char* argv[])
493 ros::init(argc, argv,
"unittest_speed_observer");
499 testing::InitGoogleTest(&argc, argv);
500 return RUN_ALL_TESTS();
void publishTfAtSpeed(const double v, const double observer_frequency=OBSERVER_FREQUENCY)
static const double TEST_FREQUENCY
void terminateNow()
This method will terminate the observation cycle started by startObserving().
static const std::string TEST_BASE_FRAME
std::vector< std::string > additional_frames_
static const std::string BARRIER_LIMIT
static const double SQRT_2_HALF
static const std::string BARRIER_LIMIT_LOW
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.
int main(int argc, char *argv[])
geometry_msgs::TransformStamped t
static constexpr double OBSERVER_FREQUENCY
static const std::string RUN_PERMITTED_SERVICE_NAME
static void waitForService(const std::string service_name, const double retry_timeout=DEFAULT_RETRY_TIMEOUT, const double msg_output_period=DEFAULT_MSG_OUTPUT_PERIOD)
static const std::string BARRIER_SLOW
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
bool setSpeedLimitCb(SetSpeedLimit::Request &req, SetSpeedLimit::Response &res)
Callback for service to set the currently active speed limit.
static const std::string BARRIER_FAST
static const std::string TEST_FRAME_A
#define ROS_DEBUG_STREAM(args)
static const std::string FRAME_SPEEDS_TOPIC_NAME
static constexpr unsigned int DEFAULT_ALLOWED_MISSED_CALCULATIONS
Allowed number of missed calculations. If it is exceeded a Stop1 is triggered.
ros::Subscriber speed_subscriber_
static const std::string TEST_FRAME_B
MATCHER_P(RunPermittedState, x,"RunPermitted state "+std::string(negation?"is not":"is")+": "+PrintToString(x)+".")
MATCHER_P2(NameAtI, i, name,"Name at index "+PrintToString(i)+std::string(negation?"is not":"is")+": "+name+".")
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
void startObserving(const double frequency, const unsigned int allowed_missed_calculations=DEFAULT_ALLOWED_MISSED_CALCULATIONS)
Starts the observation cycle. The function blocks until ros shuts down.
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
ros::ServiceServer stop_subscriber_