14 from sensor_msgs.msg
import LaserScan
15 from geometry_msgs.msg
import Twist, Point
16 from nav_msgs.msg
import Odometry
17 from tf
import transformations
30 desired_position_ = Point()
31 desired_position_.x = rospy.get_param(
'des_pos_x')
32 desired_position_.y = rospy.get_param(
'des_pos_y')
33 desired_position_.z = 0
35 yaw_precision_ = math.pi / 9
36 yaw_precision_2_ = math.pi / 90
54 res = SetBoolResponse()
67 position_ = msg.pose.pose.position
71 msg.pose.pose.orientation.x,
72 msg.pose.pose.orientation.y,
73 msg.pose.pose.orientation.z,
74 msg.pose.pose.orientation.w)
75 euler = transformations.euler_from_quaternion(quaternion)
82 print (
'State changed to [%s]' % state_)
86 if(math.fabs(angle) > math.pi):
87 angle = angle - (2 * math.pi * angle) / (math.fabs(angle))
92 global yaw_, pub, yaw_precision_2_, state_
93 desired_yaw = math.atan2(des_pos.y - position_.y, des_pos.x - position_.x)
96 rospy.loginfo(err_yaw)
99 if math.fabs(err_yaw) > yaw_precision_2_:
100 twist_msg.angular.z = kp_a*err_yaw
101 if twist_msg.angular.z > ub_a:
102 twist_msg.angular.z = ub_a
103 elif twist_msg.angular.z < lb_a:
104 twist_msg.angular.z = lb_a
106 pub.publish(twist_msg)
109 if math.fabs(err_yaw) <= yaw_precision_2_:
110 print (
'Yaw error: [%s]' % err_yaw)
115 global yaw_, pub, yaw_precision_, state_
116 desired_yaw = math.atan2(des_pos.y - position_.y, des_pos.x - position_.x)
117 err_yaw = desired_yaw - yaw_
118 err_pos = math.sqrt(pow(des_pos.y - position_.y, 2) +
119 pow(des_pos.x - position_.x, 2))
121 if err_pos > dist_precision_:
123 twist_msg.linear.x = kp_d*(err_pos)
124 if twist_msg.linear.x > ub_d:
125 twist_msg.linear.x = ub_d
127 twist_msg.angular.z = kp_a*err_yaw
128 pub.publish(twist_msg)
130 print (
'Position error: [%s]' % err_pos)
134 if math.fabs(err_yaw) > yaw_precision_:
135 print (
'Yaw error: [%s]' % err_yaw)
141 twist_msg.linear.x = 0
142 twist_msg.angular.z = 0
143 pub.publish(twist_msg)
144 err_pos = math.sqrt(pow(des_pos.y - position_.y, 2) +
145 pow(des_pos.x - position_.x, 2))
151 global pub, active_, desired_position_
153 rospy.init_node(
'go_to_point')
155 pub = rospy.Publisher(
'/cmd_vel', Twist, queue_size=1)
157 sub_odom = rospy.Subscriber(
'/odom', Odometry, clbk_odom)
159 srv = rospy.Service(
'go_to_point_switch', SetBool, go_to_point_switch)
161 rate = rospy.Rate(20)
162 while not rospy.is_shutdown():
163 desired_position_.x = rospy.get_param(
'des_pos_x')
164 desired_position_.y = rospy.get_param(
'des_pos_y')
173 done(desired_position_)
175 rospy.logerr(
'Unknown state!')
180 if __name__ ==
'__main__':
def go_to_point_switch(req)
def normalize_angle(angle)
def go_straight_ahead(des_pos)