Hover.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import tf
00005 from crazyflie_driver.msg import Hover
00006 from std_msgs.msg import Empty
00007 from crazyflie_driver.srv import UpdateParams
00008 from threading import Thread
00009 
00010 class Crazyflie:
00011     def __init__(self, prefix):
00012         self.prefix = prefix
00013 
00014         worldFrame = rospy.get_param("~worldFrame", "/world")
00015         self.rate = rospy.Rate(10)
00016 
00017         rospy.wait_for_service(prefix + '/update_params')
00018         rospy.loginfo("found update_params service")
00019         self.update_params = rospy.ServiceProxy(prefix + '/update_params', UpdateParams)
00020 
00021         self.setParam("kalman/resetEstimation", 1)
00022 
00023         self.pub = rospy.Publisher(prefix + "/cmd_hover", Hover, queue_size=1)
00024         self.msg = Hover()
00025         self.msg.header.seq = 0
00026         self.msg.header.stamp = rospy.Time.now()
00027         self.msg.header.frame_id = worldFrame
00028         self.msg.yawrate = 0
00029 
00030         self.stop_pub = rospy.Publisher(prefix + "/cmd_stop", Empty, queue_size=1)
00031         self.stop_msg = Empty()
00032 
00033 
00034     # determine direction of speed based on distance
00035     def getSpeed(self, distance):
00036         if distance > 0:
00037             return 0.1
00038         elif distance < 0:
00039             return -0.1
00040         else:
00041             return 0
00042 
00043     def setParam(self, name, value):
00044         rospy.set_param(self.prefix + "/" + name, value)
00045         self.update_params([name])
00046 
00047     # x, y is the x, y distance relative to itself
00048     # z is absolute z distance
00049     # TODO: solve 0
00050     def goTo (self, x, y, zDistance, yaw):
00051         duration = 0
00052         duration_x = 0
00053         duration_y = 0
00054         duration_z = 0
00055         vx = 0
00056         vy = 0
00057         z = self.msg.zDistance # the zDistance we have before
00058         z_scale = self.getSpeed(z) # the z distance each time z has to increment, will be changed
00059 
00060         # for x, in secs
00061         if x != 0:
00062             duration_x = abs(x/0.1)
00063             vx = self.getSpeed(x)
00064 
00065         # for y, in secs
00066         if y != 0:
00067             duration_y = abs(y/0.1)
00068             vy = self.getSpeed(y)
00069 
00070         duration_z = abs(z-zDistance)/0.1
00071         durations = [duration_x, duration_y, duration_z]
00072         duration = max(durations)
00073 
00074         if duration == 0:
00075             return
00076         elif duration == duration_x:
00077             # x is the longest path
00078             vy *= abs(y/x)
00079             z_scale *= abs((z-zDistance)/x)
00080         elif duration == duration_y:
00081             # y is the longest path
00082             vx *= abs(x/y)
00083             z_scale *= abs((z-zDistance)/y)
00084         elif duration == duration_z:
00085             # z is the longest path
00086             vx *= abs(x/(z-zDistance))
00087             vy *= abs(y/(z-zDistance))
00088 
00089         print(vx)
00090         print(vy)
00091         print(z_scale)
00092         print(duration)
00093 
00094         start = rospy.get_time()
00095         while not rospy.is_shutdown():
00096             self.msg.vx = vx
00097             self.msg.vy = vy
00098             self.msg.yawrate = 0.0
00099             self.msg.zDistance = z
00100             if z < zDistance:
00101                 print(zDistance)
00102                 print(z)
00103                 z += z_scale
00104             else:
00105                 z = zDistance
00106             now = rospy.get_time()
00107             if (now - start > duration):
00108                 break
00109             self.msg.header.seq += 1
00110             self.msg.header.stamp = rospy.Time.now()
00111             rospy.loginfo("sending...")
00112             rospy.loginfo(self.msg.vx)
00113             rospy.loginfo(self.msg.vy)
00114             rospy.loginfo(self.msg.yawrate)
00115             rospy.loginfo(self.msg.zDistance)
00116             self.pub.publish(self.msg)
00117             self.rate.sleep()
00118 
00119     # take off to z distance
00120     def takeOff(self, zDistance):
00121         time_range = 1 + int(10*zDistance/0.4)
00122         while not rospy.is_shutdown():
00123             for y in range(time_range):
00124                 self.msg.vx = 0.0
00125                 self.msg.vy = 0.0
00126                 self.msg.yawrate = 0.0
00127                 self.msg.zDistance = y / 25.0
00128                 self.msg.header.seq += 1
00129                 self.msg.header.stamp = rospy.Time.now()
00130                 self.pub.publish(self.msg)
00131                 self.rate.sleep()
00132             for y in range(20):
00133                 self.msg.vx = 0.0
00134                 self.msg.vy = 0.0
00135                 self.msg.yawrate = 0.0
00136                 self.msg.zDistance = zDistance
00137                 self.msg.header.seq += 1
00138                 self.msg.header.stamp = rospy.Time.now()
00139                 self.pub.publish(self.msg)
00140                 self.rate.sleep()
00141             break
00142 
00143     # land from last zDistance
00144     def land (self):
00145         # get last height
00146         zDistance = self.msg.zDistance
00147 
00148         while not rospy.is_shutdown():
00149             while zDistance > 0:
00150                 self.msg.vx = 0.0
00151                 self.msg.vy = 0.0
00152                 self.msg.yawrate = 0.0
00153                 self.msg.zDistance = zDistance
00154                 self.msg.header.seq += 1
00155                 self.msg.header.stamp = rospy.Time.now()
00156                 self.pub.publish(self.msg)
00157                 self.rate.sleep()
00158                 zDistance -= 0.2
00159         self.stop_pub.publish(self.stop_msg)
00160 
00161 def handler(cf):
00162     cf.takeOff(0.4)
00163     cf.goTo(0.4, 0.1, 0.2, 0)
00164     cf.land()
00165 
00166 if __name__ == '__main__':
00167     rospy.init_node('hover', anonymous=True)
00168 
00169     cf1 = Crazyflie("cf1")
00170     cf2 = Crazyflie("cf2")
00171 
00172     t1 = Thread(target=handler, args=(cf1,))
00173     t2 = Thread(target=handler, args=(cf2,))
00174     t1.start()
00175     t2.start()
00176 
00177 
00178 


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Wed Jun 12 2019 19:20:46