-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathinitialpose_uwb_average.py
executable file
·157 lines (122 loc) · 4.75 KB
/
initialpose_uwb_average.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
'''
__author__ = "Bekir Bostanci"
__license__ = "BSD"
__version__ = "0.0.1"
__maintainer__ = "Bekir Bostanci"
__email__ = "[email protected]"
'''
import rospy
from geometry_msgs.msg import PoseWithCovarianceStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import PointStamped
from geometry_msgs.msg import Twist
import sys
import math
from threading import Timer
import time
import numpy as np
global first_pose
global second_pose
first_pose = None
second_pose = None
global control_finish_move
control_finish_move = False
poses = []
poses_initial= []
poses_final= []
counter = 0
def setInitialPosition(pose_ini_x,pose_ini_y,theta):
#publish initialpose node
initial_pose_publisher = rospy.Publisher("initialpose", PoseWithCovarianceStamped, queue_size=1)
initial_pose = PoseWithCovarianceStamped()
initial_pose.header.seq = 1
initial_pose.header.stamp = rospy.Time.now()
initial_pose.header.frame_id = "map"
initial_pose.pose.pose.position.x = pose_ini_x
initial_pose.pose.pose.position.y = pose_ini_y
initial_pose.pose.pose.position.z = 0.0
initial_pose.pose.pose.orientation.x = 0.0
initial_pose.pose.pose.orientation.y = 0.0
initial_pose.pose.pose.orientation.z = math.sin(theta/2)
initial_pose.pose.pose.orientation.w = math.cos(theta/2)
#standart covariance
initial_pose.pose.covariance = [0.24440499777841992, 0.0025508121327049984, 0.0, 0.0, 0.0, 0.0, 0.0025508121327049976, 0.24027264216307176, 0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.07042916431048865]
#before the one message publishing we should sleep 1 second
rospy.sleep(1)
initial_pose_publisher.publish(initial_pose)
def subscribe_data_pose(PointStamped):
global control_finish_move
#second uwb measurement set every update
if poses_final == [] and len(poses_initial)<30:
poses_initial.append([PointStamped.point.x,PointStamped.point.y])
elif control_finish_move == True and len(poses_final)<30 :
poses_final.append([PointStamped.point.x,PointStamped.point.y])
if len(poses_final) == 30:
finish_move()
def move():
global control_finish_move
# Starts a new node
velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
vel_msg = Twist()
#Receiveing the user's input
speed =0.05
distance =0.2
vel_msg.linear.x = abs(speed)
#Since we are moving just in x-axis
vel_msg.linear.y = 0
vel_msg.linear.z = 0
vel_msg.angular.x = 0
vel_msg.angular.y = 0
vel_msg.angular.z = 0
current_distance = 0
while (current_distance < distance):
rospy.sleep(0.01)
#Setting the current time for distance calculus
t0 = rospy.Time.now().to_sec()
current_distance = 0
#Loop to move the turtle in an specified distance
while(current_distance < distance):
#Publish the velocity
velocity_publisher.publish(vel_msg)
#Takes actual time to velocity calculus
t1=rospy.Time.now().to_sec()
#Calculates distancePoseStamped
current_distance= speed*(t1-t0)
#After the loop, stops the robot
vel_msg.linear.x = 0
#Force the robot to stop
velocity_publisher.publish(vel_msg)
#After the loop, stops the robot
vel_msg.linear.x = 0
#Force the robot to stop
velocity_publisher.publish(vel_msg)
time.sleep(1)
control_finish_move = True
def finish_move():
global first_pose
global second_pose
robot_radian=0
a = np.array(poses_initial)
b = np.array(poses_final)
first_pose = [a[:,0].mean() ,a[:,1].mean()]
second_pose = [b[:,0].mean() ,b[:,1].mean()]
#distance of first_pose and second_pose components'
y_dif = b[:,1].mean() - a[:,1].mean()
x_dif = b[:,0].mean() - a[:,0].mean()
robot_radian = math.atan2(y_dif,x_dif)
result_log = []
result_log.append("First Pose : "+ str(poses_initial))
result_log.append("Second Pose : "+ str(poses_final))
result_log.append("Degree Robot: "+ str(math.degrees(robot_radian)))
rospy.loginfo(result_log)
#publish initial pose with this information
setInitialPosition(second_pose[0],second_pose[1],robot_radian)
#finish nodes
rospy.signal_shutdown('initialpose_uwb_average')
if __name__ == "__main__":
rospy.init_node('initialpose_uwb_average', anonymous=True)
subscriber =rospy.Subscriber('localization_data_topic', PointStamped, subscribe_data_pose)
t = Timer(3, move)
t.start()
rospy.spin()