drone_patrol.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import rospy
00003 import sys
00004 from std_msgs.msg import String
00005 from std_msgs.msg import Empty
00006 from geometry_msgs.msg import Twist
00007 from ardrone_autonomy.msg import  Navdata
00008 from nav_msgs.msg import Odometry
00009 import tf
00010 from math import *
00011 
00012 x=0
00013 y=0
00014 altitude=0
00015 yaw=0
00016 roll=0
00017 pitch=0
00018 
00019 def distance(x1,y1,x2,y2):
00020     return sqrt(((x1-x2)**2)+((y1-y2)**2))
00021 
00022 def distance3d(x1,y1,z1,x2,y2,z2):
00023     return sqrt(((x1-x2)**2)+((y1-y2)**2)+((z1-z2)**2))
00024 
00025 def takeoff():    
00026     takeoff_pub.publish(Empty())
00027 
00028 
00029 def land():    
00030     land_pub.publish(Empty())
00031     
00032 def circle(): 
00033     twist = Twist()  
00034     twist.linear.x=4.0
00035     twist.angular.z=4.0 
00036     velocity_pub.publish(twist)
00037     
00038     
00039 def cube(): 
00040     twist = Twist()  
00041     twist.linear.x=4.0
00042     twist.angular.z=4.0 
00043     velocity_pub.publish(twist)
00044     
00045 def sin(): 
00046     rate = rospy.Rate(1)
00047     twist = Twist()
00048     x=0  
00049     stop()
00050     while(x<10):
00051         twist.linear.x=0.8
00052         if (x%2==0): 
00053             twist.linear.z=3.0 
00054         if (x%2==1): 
00055             twist.linear.z=-3.0
00056         velocity_pub.publish(twist)
00057         rate.sleep()
00058         x=x+1
00059         print x
00060     stop()
00061     
00062 def horse(d): 
00063     global x,y
00064     rate = rospy.Rate(1)
00065     twist = Twist()
00066     x0=x
00067     y0=y  
00068     z0=altitude
00069     distance = 0
00070     stop()
00071     while(distance<d):
00072         distance = distance3d(x0,y0,z0,x,y,altitude)
00073         print distance
00074         twist.linear.x=0.8
00075         if (x%2==0): 
00076             twist.linear.z=3.0 
00077         if (x%2==1): 
00078             twist.linear.z=-3.0
00079         velocity_pub.publish(twist)
00080         rate.sleep()
00081         x=x+1
00082         print x
00083     stop()
00084     
00085     
00086 def patrol(): 
00087     rate = rospy.Rate(1)
00088     twist = Twist()
00089     x=0  
00090     stop()
00091     while(x<10):
00092         twist.linear.x=0.8
00093         if (x%2==0): 
00094             twist.linear.z=3.0 
00095         if (x%2==1): 
00096             twist.linear.z=-3.0
00097         velocity_pub.publish(twist)
00098         rate.sleep()
00099         x=x+1
00100         print x
00101     stop()
00102     
00103 def stop(): 
00104     twist = Twist()  
00105     twist.linear.x=0.0
00106     twist.linear.y=0.0
00107     twist.angular.z=0.0 
00108     velocity_pub.publish(twist)
00109 
00110 
00111 def menu():
00112     print ("t: takeoff")
00113     print ("l: land")
00114     print ("c: circle")
00115     print ("n: sin")
00116     print ("h: horse")
00117     print ("s: stop")
00118     
00119 def odometryCallback(msg):
00120     #position 
00121     global y,y,z,yaw,roll,picth
00122     
00123     x= msg.pose.pose.position.x
00124     y= msg.pose.pose.position.y
00125     z= msg.pose.pose.position.z
00126     #orientation
00127     quaternion = (msg.pose.pose.orientation.x,msg.pose.pose.orientation.y, msg.pose.pose.orientation.z, msg.pose.pose.orientation.w)
00128     euler = tf.transformations.euler_from_quaternion(quaternion)
00129     roll = euler[0]
00130     pitch = euler[1]
00131     yaw = euler[2]
00132     
00133 def navdataCallback(msg):
00134     global altitude, state, battery
00135     battery = msg.batteryPercent
00136     state = msg.state
00137     altitude = msg.altd
00138 
00139 
00140 
00141 if __name__ == '__main__':
00142     rospy.init_node('ardrone_control_node', anonymous=True)
00143     takeoff_pub = rospy.Publisher("/ardrone/takeoff", Empty, queue_size=10 )
00144     land_pub = rospy.Publisher("ardrone/land", Empty, queue_size=10 )
00145     velocity_pub = rospy.Publisher("cmd_vel", Twist, queue_size=10 )
00146     rospy.Subscriber("/ground_truth/state", Odometry, odometryCallback)
00147     rospy.Subscriber("/ardrone/navdata", Navdata, navdataCallback)
00148     #rate = rospy.Rate(10) # 10hz
00149     try:
00150         while not rospy.is_shutdown():
00151             menu()
00152             #key= input("press a key for action")
00153             key=sys.stdin.read(1)
00154             if (key == str('t')):
00155                 takeoff()
00156             elif (key == str('l')):
00157                 land()
00158             elif (key == str('c')):
00159                 circle()
00160             elif (key == str('n')):
00161                 sin()
00162             elif (key == str('h')):
00163                 horse(3)
00164             elif (key == str('s')):
00165                 stop()
00166     except rospy.ROSInterruptException:
00167         pass


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13