Hover.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import tf
5 from crazyflie_driver.msg import Hover
6 from std_msgs.msg import Empty
7 from crazyflie_driver.srv import UpdateParams
8 from threading import Thread
9 
10 class Crazyflie:
11  def __init__(self, prefix):
12  self.prefix = prefix
13 
14  worldFrame = rospy.get_param("~worldFrame", "/world")
15  self.rate = rospy.Rate(10)
16 
17  rospy.wait_for_service(prefix + '/update_params')
18  rospy.loginfo("found update_params service")
19  self.update_params = rospy.ServiceProxy(prefix + '/update_params', UpdateParams)
20 
21  self.setParam("kalman/resetEstimation", 1)
22 
23  self.pub = rospy.Publisher(prefix + "/cmd_hover", Hover, queue_size=1)
24  self.msg = Hover()
25  self.msg.header.seq = 0
26  self.msg.header.stamp = rospy.Time.now()
27  self.msg.header.frame_id = worldFrame
28  self.msg.yawrate = 0
29 
30  self.stop_pub = rospy.Publisher(prefix + "/cmd_stop", Empty, queue_size=1)
31  self.stop_msg = Empty()
32 
33 
34  # determine direction of speed based on distance
35  def getSpeed(self, distance):
36  if distance > 0:
37  return 0.1
38  elif distance < 0:
39  return -0.1
40  else:
41  return 0
42 
43  def setParam(self, name, value):
44  rospy.set_param(self.prefix + "/" + name, value)
45  self.update_params([name])
46 
47  # x, y is the x, y distance relative to itself
48  # z is absolute z distance
49  # TODO: solve 0
50  def goTo (self, x, y, zDistance, yaw):
51  duration = 0
52  duration_x = 0
53  duration_y = 0
54  duration_z = 0
55  vx = 0
56  vy = 0
57  z = self.msg.zDistance # the zDistance we have before
58  z_scale = self.getSpeed(z) # the z distance each time z has to increment, will be changed
59 
60  # for x, in secs
61  if x != 0:
62  duration_x = abs(x/0.1)
63  vx = self.getSpeed(x)
64 
65  # for y, in secs
66  if y != 0:
67  duration_y = abs(y/0.1)
68  vy = self.getSpeed(y)
69 
70  duration_z = abs(z-zDistance)/0.1
71  durations = [duration_x, duration_y, duration_z]
72  duration = max(durations)
73 
74  if duration == 0:
75  return
76  elif duration == duration_x:
77  # x is the longest path
78  vy *= abs(y/x)
79  z_scale *= abs((z-zDistance)/x)
80  elif duration == duration_y:
81  # y is the longest path
82  vx *= abs(x/y)
83  z_scale *= abs((z-zDistance)/y)
84  elif duration == duration_z:
85  # z is the longest path
86  vx *= abs(x/(z-zDistance))
87  vy *= abs(y/(z-zDistance))
88 
89  print(vx)
90  print(vy)
91  print(z_scale)
92  print(duration)
93 
94  start = rospy.get_time()
95  while not rospy.is_shutdown():
96  self.msg.vx = vx
97  self.msg.vy = vy
98  self.msg.yawrate = 0.0
99  self.msg.zDistance = z
100  if z < zDistance:
101  print(zDistance)
102  print(z)
103  z += z_scale
104  else:
105  z = zDistance
106  now = rospy.get_time()
107  if (now - start > duration):
108  break
109  self.msg.header.seq += 1
110  self.msg.header.stamp = rospy.Time.now()
111  rospy.loginfo("sending...")
112  rospy.loginfo(self.msg.vx)
113  rospy.loginfo(self.msg.vy)
114  rospy.loginfo(self.msg.yawrate)
115  rospy.loginfo(self.msg.zDistance)
116  self.pub.publish(self.msg)
117  self.rate.sleep()
118 
119  # take off to z distance
120  def takeOff(self, zDistance):
121  time_range = 1 + int(10*zDistance/0.4)
122  while not rospy.is_shutdown():
123  for y in range(time_range):
124  self.msg.vx = 0.0
125  self.msg.vy = 0.0
126  self.msg.yawrate = 0.0
127  self.msg.zDistance = y / 25.0
128  self.msg.header.seq += 1
129  self.msg.header.stamp = rospy.Time.now()
130  self.pub.publish(self.msg)
131  self.rate.sleep()
132  for y in range(20):
133  self.msg.vx = 0.0
134  self.msg.vy = 0.0
135  self.msg.yawrate = 0.0
136  self.msg.zDistance = zDistance
137  self.msg.header.seq += 1
138  self.msg.header.stamp = rospy.Time.now()
139  self.pub.publish(self.msg)
140  self.rate.sleep()
141  break
142 
143  # land from last zDistance
144  def land (self):
145  # get last height
146  zDistance = self.msg.zDistance
147 
148  while not rospy.is_shutdown():
149  while zDistance > 0:
150  self.msg.vx = 0.0
151  self.msg.vy = 0.0
152  self.msg.yawrate = 0.0
153  self.msg.zDistance = zDistance
154  self.msg.header.seq += 1
155  self.msg.header.stamp = rospy.Time.now()
156  self.pub.publish(self.msg)
157  self.rate.sleep()
158  zDistance -= 0.2
159  self.stop_pub.publish(self.stop_msg)
160 
161 def handler(cf):
162  cf.takeOff(0.4)
163  cf.goTo(0.4, 0.1, 0.2, 0)
164  cf.land()
165 
166 if __name__ == '__main__':
167  rospy.init_node('hover', anonymous=True)
168 
169  cf1 = Crazyflie("cf1")
170  cf2 = Crazyflie("cf2")
171 
172  t1 = Thread(target=handler, args=(cf1,))
173  t2 = Thread(target=handler, args=(cf2,))
174  t1.start()
175  t2.start()
176 
177 
178 
Definition: Hover.py:1
def land(self)
Definition: Hover.py:144
def __init__(self, prefix)
Definition: Hover.py:11
def goTo(self, x, y, zDistance, yaw)
Definition: Hover.py:50
def getSpeed(self, distance)
Definition: Hover.py:35
def setParam(self, name, value)
Definition: Hover.py:43
def handler(cf)
Definition: Hover.py:161
def takeOff(self, zDistance)
Definition: Hover.py:120


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:12