ardrone_pilot.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 
00009 
00010 import threading
00011 import tf
00012 import time
00013 from std_msgs.msg import Empty
00014 from nav_msgs.msg import Odometry
00015 from sensor_msgs.msg import Image, CameraInfo, CompressedImage
00016 from cv_bridge import CvBridge, CvBridgeError
00017 import cv2
00018 from geometry_msgs.msg import Twist
00019 from geometry_msgs.msg import PoseStamped
00020 
00021 import numpy as np
00022 
00023 from states_variables.ARdroneStateVariables import ARdroneStateVariables
00024 
00025 
00026 def takeoff():    
00027     takeoff_pub.publish(Empty())
00028 
00029 
00030 def land():    
00031     land_pub.publish(Empty())
00032     
00033 def circle(): 
00034     twist = Twist()  
00035     twist.linear.x=4.0
00036     twist.angular.z=4.0 
00037     velocity_pub.publish(twist)
00038     
00039 def stop(): 
00040     twist = Twist()  
00041     twist.linear.x=0.0
00042     twist.linear.y=0.0
00043     twist.angular.z=0.0 
00044     velocity_pub.publish(twist)
00045 
00046 
00047 def menu():
00048     print ("t: takeoff")
00049     print ("l: land")
00050     print ("c: circle")
00051     print ("s: stop")
00052     print ("q: quit")
00053     
00054     
00055 
00056 def frontCompressedImageCallback(data):
00057     try:
00058       #cv_image = ROSLinkBridgeARDrone.bridge.imgmsg_to_cv2(data, "bgr8")
00059       np_arr = np.fromstring(data.data, np.uint8)
00060       cv_image = cv2.imdecode(np_arr, cv2.CV_16U)
00061     except CvBridgeError as e:
00062       print(e)
00063     cv2.imshow("ARDrone Front Compressed Image Viewer", cv_image)
00064     cv2.waitKey(3)
00065 
00066  
00067 def odometryCallback(msg):
00068     #position 
00069     ARdroneStateVariables.time_boot_ms=time.time()
00070     ARdroneStateVariables.x= msg.pose.pose.position.x
00071     ARdroneStateVariables.y= msg.pose.pose.position.y
00072     ARdroneStateVariables.z= msg.pose.pose.position.z
00073     #orientation
00074     quaternion = (msg.pose.pose.orientation.x,msg.pose.pose.orientation.y, msg.pose.pose.orientation.z, msg.pose.pose.orientation.w)
00075     euler = tf.transformations.euler_from_quaternion(quaternion)
00076     ARdroneStateVariables.roll = euler[0]
00077     ARdroneStateVariables.pitch = euler[1]
00078     ARdroneStateVariables.yaw = euler[2]
00079     #twist: linear
00080     ARdroneStateVariables.vx_truth = msg.twist.twist.linear.x
00081     ARdroneStateVariables.vy_truth = msg.twist.twist.linear.y
00082     ARdroneStateVariables.vz_truth = msg.twist.twist.linear.z
00083     #twist: angular
00084     ARdroneStateVariables.wx_truth = msg.twist.twist.angular.x
00085     ARdroneStateVariables.wy_truth = msg.twist.twist.angular.y
00086     ARdroneStateVariables.wz_truth = msg.twist.twist.angular.z
00087     
00088     #print self.x
00089     #print self.y
00090  
00091 
00092 def navdataCallback(msg):
00093     ARdroneStateVariables.vx = msg.vx
00094     ARdroneStateVariables.vy = msg.vy
00095     ARdroneStateVariables.vz = msg.vz
00096     ARdroneStateVariables.wx = msg.ax
00097     ARdroneStateVariables.wy = msg.ay
00098     ARdroneStateVariables.wz = msg.az
00099     ARdroneStateVariables.battery = msg.batteryPercent
00100     ARdroneStateVariables.state = msg.state
00101     ARdroneStateVariables.magX = msg.magX
00102     ARdroneStateVariables.magY = msg.magY
00103     ARdroneStateVariables.magZ = msg.magZ
00104     ARdroneStateVariables.pressure = msg.pressure
00105     ARdroneStateVariables.temp = msg.temp
00106     ARdroneStateVariables.wind_speed = msg.wind_speed
00107     ARdroneStateVariables.wind_angle = msg.wind_angle
00108     ARdroneStateVariables.rotX = msg.rotX
00109     ARdroneStateVariables.rotY = msg.rotY
00110     ARdroneStateVariables.rotZ = msg.rotZ
00111     ARdroneStateVariables.altitude = msg.altd
00112     ARdroneStateVariables.motor1 = msg.motor1
00113     ARdroneStateVariables.motor2 = msg.motor2
00114     ARdroneStateVariables.motor3 = msg.motor3
00115     ARdroneStateVariables.motor4 = msg.motor4
00116     ARdroneStateVariables.tags_count = msg.tags_count
00117     ARdroneStateVariables.tags_type = msg.tags_type
00118     
00119 
00120 class AltitudeControlThread ():
00121     
00122     kpx=0.0000425
00123     kdx=0.000000000
00124     kix=0.00000000
00125     
00126     kpy=0.0000425
00127     kdy=0.0000000000
00128     kiy=0.00000000
00129     
00130     kpz=0.000888
00131     kdz=0.0000
00132     kiz=0.0000
00133     
00134     x_ref = 0.0 #desired relative pose in x-axis
00135     y_ref = 0.0 #desired relative pose in y-axis
00136     z_ref = 2000 #desired altitude
00137     
00138     relative_pose_x=0.0
00139     relative_pose_y=0.0
00140     
00141     
00142     def __init__(self, thread_name='altitude_control_thread', data_rate=10.0):
00143         self.data_rate = data_rate # define the data_rate
00144         self.dt = 1.0/data_rate #this is delta(t) that represents one unit of time
00145         self.name = thread_name #give a name to the thread
00146         self.twist = Twist() #declare a Twist message
00147         self.log_file = open("log.txt",'w') #open a log file
00148         self.t = threading.Thread(target=self.run) #create the thread and target the method run
00149         self.t.setName(thread_name) #set the name to the thread
00150         self.t.start() #start the thread
00151         self.old_error_x=0.0
00152         self.error_x_sum=0.0
00153         self.old_error_y=0.0
00154         self.error_y_sum=0.0
00155         self.old_error_z=0.0
00156         self.error_z_sum=0.0
00157     
00158     def run ( self ):
00159         while True:
00160             time.sleep(1.0/self.data_rate)
00161             #print 'thread %s %d\n'%(self.name, self.count)
00162             
00163             print "\n\n"
00164             print"-------------------------------------------------------"
00165             print "state: ", ARdroneStateVariables.state
00166             print "x: ", ARdroneStateVariables.x ,"y: ", ARdroneStateVariables.y
00167             
00168             #controlling the altitude
00169            
00170             error_z = self.z_ref-ARdroneStateVariables.altitude
00171             error_z_dot=(error_z-self.old_error_z)
00172             self.error_z_sum=self.error_z_sum+error_z
00173             self.old_error_z= error_z;
00174             
00175             print ""
00176             print "altitude: ", ARdroneStateVariables.altitude
00177             print "error_z: ",  error_z
00178             print "error_z_dot: ",  error_z_dot
00179             print "error_z_sum: ",  self.error_z_sum
00180             
00181             #controlling the x pose: the idea is to estimate the new relative pose in x by integrating with respect to the linear velocity in x direction
00182            
00183             self.relative_pose_x =  self.relative_pose_x + (ARdroneStateVariables.vx*self.dt) #new pose = old pose + speed * time
00184             error_x= self.x_ref-self.relative_pose_x
00185             error_x_dot=(error_x-self.old_error_x)
00186             self.error_x_sum=self.error_x_sum+error_x
00187             self.old_error_x= error_x
00188             
00189             print ""
00190             #print "self.dt  ",  self.dt 
00191             print "relative_pose_x ",  self.relative_pose_x
00192             print "vx: ", ARdroneStateVariables.vx
00193             print "error_x: ",  error_x
00194             
00195             
00196             #controlling the y pose: the idea is to estimate the new relative pose in y by integrating with respect to the linear velocity in y direction
00197            
00198             self.relative_pose_y =  self.relative_pose_y + (ARdroneStateVariables.vy*self.dt) #new pose = old pose + speed * time
00199             error_y= self.y_ref-self.relative_pose_y
00200             error_y_dot=(error_y-self.old_error_y)
00201             self.error_y_sum=self.error_y_sum+error_y
00202             self.old_error_y= error_y
00203             
00204             print ""
00205             print "relative_pose_y: ",  self.relative_pose_y
00206             print "vy: ", ARdroneStateVariables.vy
00207             print "error_y: ",  error_y
00208            
00209             #logging results for analysis
00210             pose_coordinate = (self.relative_pose_x, self.relative_pose_y, ARdroneStateVariables.altitude, ARdroneStateVariables.state)
00211             self.log_file.write(str(pose_coordinate)+"\n")
00212             self.log_file.flush()
00213 
00214             # 0: Unknown, 1: Init, 2: Landed, 3: Flying, 4: Hovering, 5: Test
00215             # 6: Taking off, 7: Goto Fix Point, 8: Landing, 9: Looping
00216             # Note: 3,7 seems to discriminate type of flying (isFly = 3 | 7)
00217             
00218             self.twist.linear.x=AltitudeControlThread.kpx*error_x #+ (AltitudeControlThread.kix*self.error_x_sum) + (AltitudeControlThread.kdx*error_x_dot)
00219             self.twist.linear.y=AltitudeControlThread.kpy*error_y #+ (AltitudeControlThread.kiy*self.error_y_sum) + (AltitudeControlThread.kdy*error_y_dot)
00220             self.twist.linear.z=(AltitudeControlThread.kpz*error_z) + (AltitudeControlThread.kiz*self.error_z_sum) + (AltitudeControlThread.kdz*error_z_dot)
00221             
00222             print ""
00223             print "self.twist.linear.x: ",  self.twist.linear.x, "self.twist.linear.y: ",  self.twist.linear.y, "self.twist.linear.z: ",  self.twist.linear.z, 
00224 
00225             
00226             if (ARdroneStateVariables.state == 3):
00227                 velocity_pub.publish(self.twist)
00228 
00229 
00230 
00231 if __name__ == '__main__':
00232     rospy.init_node('ardrone_control_node', anonymous=True)
00233     takeoff_pub = rospy.Publisher("/ardrone/takeoff", Empty, queue_size=10 )
00234     land_pub = rospy.Publisher("ardrone/land", Empty, queue_size=10 )
00235     velocity_pub = rospy.Publisher("cmd_vel", Twist, queue_size=10 )
00236     
00237     rospy.Subscriber("/ground_truth/state", Odometry, odometryCallback)
00238     rospy.Subscriber("/ardrone/navdata", Navdata, navdataCallback)
00239     #rospy.Subscriber("/ardrone/front/image_raw/compressed", CompressedImage, frontCompressedImageCallback)
00240     
00241     #start the altitudcmd.linear.ze control thread
00242     altitude_control_thread = AltitudeControlThread()
00243     
00244     rate = rospy.Rate(1) # 10hz
00245     
00246     for x in range(1, 5):
00247         rate.sleep() #sleep 1 second
00248     print 'takeoff'
00249     takeoff()
00250     for x in range(1, 10):
00251         rate.sleep()
00252     land()
00253     
00254     altitude_control_thread = None
00255     
00256     #try:
00257     #    while not rospy.is_shutdown():
00258             #menu()
00259             #key= input("press a key for action")
00260             #key=sys.stdin.read(1)
00261             #if (key == str('t')):
00262             #    takeoff()
00263             #elif (key == str('l')):
00264             #    land()
00265             #elif (key == str('c')):
00266             #    circle()
00267             #elif (key == str('s')):
00268             #    stop()
00269             #elif (key == str('q')):
00270             #    break
00271             
00272             
00273     #except rospy.ROSInterruptException:
00274     #    pass
00275     
00276 


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