5 from crazyflie_driver.msg
import Hover
6 from std_msgs.msg
import Empty
8 from threading
import Thread
14 worldFrame = rospy.get_param(
"~worldFrame",
"/world")
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)
21 self.
setParam(
"kalman/resetEstimation", 1)
23 self.
pub = rospy.Publisher(prefix +
"/cmd_hover", Hover, queue_size=1)
25 self.msg.header.seq = 0
26 self.msg.header.stamp = rospy.Time.now()
27 self.msg.header.frame_id = worldFrame
30 self.
stop_pub = rospy.Publisher(prefix +
"/cmd_stop", Empty, queue_size=1)
44 rospy.set_param(self.
prefix +
"/" + name, value)
50 def goTo (self, x, y, zDistance, yaw):
57 z = self.msg.zDistance
62 duration_x = abs(x/0.1)
67 duration_y = abs(y/0.1)
70 duration_z = abs(z-zDistance)/0.1
71 durations = [duration_x, duration_y, duration_z]
72 duration = max(durations)
76 elif duration == duration_x:
79 z_scale *= abs((z-zDistance)/x)
80 elif duration == duration_y:
83 z_scale *= abs((z-zDistance)/y)
84 elif duration == duration_z:
86 vx *= abs(x/(z-zDistance))
87 vy *= abs(y/(z-zDistance))
94 start = rospy.get_time()
95 while not rospy.is_shutdown():
98 self.msg.yawrate = 0.0
99 self.msg.zDistance = z
106 now = rospy.get_time()
107 if (now - start > duration):
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)
121 time_range = 1 + int(10*zDistance/0.4)
122 while not rospy.is_shutdown():
123 for y
in range(time_range):
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)
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)
146 zDistance = self.msg.zDistance
148 while not rospy.is_shutdown():
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)
159 self.stop_pub.publish(self.
stop_msg)
163 cf.goTo(0.4, 0.1, 0.2, 0)
166 if __name__ ==
'__main__':
167 rospy.init_node(
'hover', anonymous=
True)
172 t1 = Thread(target=handler, args=(cf1,))
173 t2 = Thread(target=handler, args=(cf2,))
def __init__(self, prefix)
def goTo(self, x, y, zDistance, yaw)
def getSpeed(self, distance)
def setParam(self, name, value)
def takeOff(self, zDistance)