test_planner_2dof_serial_joints.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
31 #include <trajectory_msgs/JointTrajectory.h>
32 #include <sensor_msgs/JointState.h>
33 
34 #include <gtest/gtest.h>
35 
36 TEST(Planner2DOFSerialJoints, Plan)
37 {
38  ros::NodeHandle nh;
39  ros::Publisher pub_state = nh.advertise<sensor_msgs::JointState>("joint_states", 1, true);
40  ros::Publisher pub_cmd = nh.advertise<trajectory_msgs::JointTrajectory>("trajectory_in", 1, true);
41 
42  trajectory_msgs::JointTrajectory::ConstPtr planned;
43  const auto cb_plan = [&planned](const trajectory_msgs::JointTrajectory::ConstPtr& msg)
44  {
45  planned = msg;
46  };
47  ros::Subscriber sub_plan = nh.subscribe<trajectory_msgs::JointTrajectory>("joint_trajectory", 1, cb_plan);
48 
49  sensor_msgs::JointState s;
50  s.name.push_back("front");
51  s.position.push_back(-1.57);
52  s.name.push_back("rear");
53  s.position.push_back(0.0);
54 
55  trajectory_msgs::JointTrajectory cmd;
56  cmd.joint_names.push_back("front");
57  cmd.joint_names.push_back("rear");
58  trajectory_msgs::JointTrajectoryPoint p;
59  p.positions.push_back(-4.71);
60  p.positions.push_back(0.0);
61  cmd.points.push_back(p);
62 
63  pub_state.publish(s);
64  ros::Duration(1.0).sleep();
65 
66  ros::Rate rate(1);
67  while (ros::ok())
68  {
69  pub_state.publish(s);
70  pub_cmd.publish(cmd);
71 
72  ros::spinOnce();
73  rate.sleep();
74  if (planned)
75  break;
76  }
77  ASSERT_TRUE(ros::ok());
78 
79  ASSERT_EQ(2u, planned->joint_names.size());
80  ASSERT_EQ("front", planned->joint_names[0]);
81  ASSERT_EQ("rear", planned->joint_names[1]);
82 
83  // collision at front=-3.14, rear=0.0 must be avoided.
84  const float fc = -3.14;
85  const float rc = 0.0;
86  for (int i = 1; i < static_cast<int>(planned->points.size()); ++i)
87  {
88  const trajectory_msgs::JointTrajectoryPoint& p0 = planned->points[i - 1];
89  const trajectory_msgs::JointTrajectoryPoint& p1 = planned->points[i];
90  ASSERT_EQ(2u, p0.positions.size());
91  ASSERT_EQ(2u, p1.positions.size());
92 
93  const float f0 = p0.positions[0];
94  const float r0 = p0.positions[1];
95  const float f1 = p1.positions[0];
96  const float r1 = p1.positions[1];
97 
98  ASSERT_LT(-6.28, f0);
99  ASSERT_GT(0.0, f0);
100  ASSERT_LT(-3.14, r0);
101  ASSERT_GT(3.14, r0);
102  ASSERT_LT(-6.28, f1);
103  ASSERT_GT(0.0, f1);
104  ASSERT_LT(-3.14, r1);
105  ASSERT_GT(3.14, r1);
106 
107  const float front_diff = f1 - f0;
108  const float rear_diff = r1 - r0;
109 
110  const float d =
111  std::abs(
112  rear_diff * fc -
113  front_diff * rc +
114  f1 * r0 -
115  r1 * f0) /
116  std::hypot(front_diff, rear_diff);
117  // std::cerr << d << std::endl;
118  ASSERT_GT(d, 0.15);
119  }
120 }
121 
122 int main(int argc, char** argv)
123 {
124  testing::InitGoogleTest(&argc, argv);
125  ros::init(argc, argv, "test_planner_2dof_serial_joints");
126 
127  return RUN_ALL_TESTS();
128 }
d
msg
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
XmlRpcServer s
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
int main(int argc, char **argv)
ROSCPP_DECL void spinOnce()
TEST(Planner2DOFSerialJoints, Plan)


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:42