zad3.py
1 |
#!/usr/bin/env python
|
---|---|
2 |
import rospy |
3 |
from geometry_msgs.msg import Twist, PoseStamped |
4 |
from turtlesim.msg import Pose |
5 |
from nav_msgs.msg import Path |
6 |
from nav_msgs.srv import GetPlan |
7 |
import tf2_ros |
8 |
import tf |
9 |
|
10 |
global new_path
|
11 |
global tfBuffer
|
12 |
global listener
|
13 |
global robot_pose
|
14 |
global pose
|
15 |
def get_pose(): |
16 |
global robot_pose
|
17 |
global pose
|
18 |
got_transform = False
|
19 |
# pozycja robota -- transformacja z ukladu mapy do ukladu robota
|
20 |
while not got_transform: |
21 |
try:
|
22 |
#rospy.sleep(4)
|
23 |
trans = tfBuffer.lookup_transform('map', 'base_link', rospy.Time(), rospy.Duration(4.0)) |
24 |
got_transform = True
|
25 |
except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
|
26 |
rate.sleep() |
27 |
|
28 |
euler = tf.transformations.euler_from_quaternion([trans.transform.rotation.x,trans.transform.rotation.y,trans.transform.rotation.z,trans.transform.rotation.w]) |
29 |
theta = euler[2]
|
30 |
robot_pose = [trans.transform.translation.x, trans.transform.translation.y, theta] |
31 |
pose = Pose() |
32 |
pose.x = trans.transform.translation.x |
33 |
pose.y = trans.transform.translation.y |
34 |
pose.theta = theta |
35 |
print trans.transform
|
36 |
return trans.transform
|
37 |
|
38 |
def get_path(x,y): |
39 |
global new_path
|
40 |
start =PoseStamped() |
41 |
goal =PoseStamped() |
42 |
start.header.stamp = rospy.Time.now() |
43 |
start.header.frame_id="map"
|
44 |
# pozycja robota -- transformacja z ukladu mapy do ukladu robota
|
45 |
trans = get_pose() |
46 |
# pozycja robota jako poczatek sciezki
|
47 |
start.pose.position.x = trans.translation.x |
48 |
start.pose.position.y = trans.translation.y |
49 |
start.pose.orientation = trans.rotation |
50 |
# cel ruchu robota pobrany z wiadomosci
|
51 |
goal.header.frame_id="map"
|
52 |
goal.header.stamp = rospy.Time.now() |
53 |
goal.pose.position.x=x |
54 |
goal.pose.position.y = y |
55 |
# czekamy az usluga planowania bedzie dostepna
|
56 |
rospy.wait_for_service('/move_base/GlobalPlanner/make_plan' )
|
57 |
try :
|
58 |
# Tworzymy obiekt klienta uslugi
|
59 |
get_plan = rospy.ServiceProxy('/move_base/GlobalPlanner/make_plan',
|
60 |
GetPlan) |
61 |
# wolamy usluge okreslajac zadanie
|
62 |
resp=get_plan(start, goal, 1)
|
63 |
print resp
|
64 |
# zapisujemy sciezke do zmiennej globalnej
|
65 |
new_path = resp.plan |
66 |
#print new_path
|
67 |
except rospy.ServiceException , e:
|
68 |
print"Service call failed: %s "%e |
69 |
|
70 |
|
71 |
if __name__== "__main__": |
72 |
global new_path
|
73 |
global tfBuffer
|
74 |
global listener
|
75 |
global pose
|
76 |
new_path = Path() |
77 |
new_vel = Twist() |
78 |
rospy.init_node('zad3', anonymous=True) |
79 |
tfBuffer = tf2_ros.Buffer() |
80 |
listener = tf2_ros.TransformListener(tfBuffer) |
81 |
print("ready")
|
82 |
rate=rospy.Rate(10) # 10Hz |
83 |
pub = rospy.Publisher('/mobile_base_controller/cmd_vel', Twist, queue_size=10) |
84 |
get_path(2,2) |
85 |
while not rospy.is_shutdown(): |
86 |
# Wstawic kod odpowiedzialny za aktualizację zmiennej przechowujacej pozycje
|
87 |
# ...
|
88 |
if not len(new_path.poses) == 0: |
89 |
# dostep do pozycji robota jest poprzez zmienna globalna 'pose'
|
90 |
#
|
91 |
#
|
92 |
#
|
93 |
# petla wyznaczajaca kolejne predkosci zadane dla robota
|
94 |
# na podstawie sciezki, ktorej elementy odczytuje sie
|
95 |
# poprzez:
|
96 |
# new_path.poses[i].pose.position.x = wspolrzedna 'x' punktu
|
97 |
# new_path.poses[i].pose.position.y = wspolrzedna 'y' punktu
|
98 |
# gdzie 'i' jest identyfikatorem punktu sciezki.
|
99 |
# Dlugosc sciezki odczytuje sie poprzez: len(new_path.poses)
|
100 |
# ...
|
101 |
# ...
|
102 |
# ...
|
103 |
# wyslanie nowej predkosci zadanej
|
104 |
pub.publish(new_vel)#wyslaniepredkoscizadanej
|
105 |
|
106 |
rate.sleep() |
107 |
|
108 |
print("END")
|