ardrone_control.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (c) 2012, Falkor Systems, Inc.  All rights reserved.
00003 
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions are
00006 # met:
00007 
00008 # Redistributions of source code must retain the above copyright notice,
00009 # this list of conditions and the following disclaimer.  Redistributions
00010 # in binary form must reproduce the above copyright notice, this list of
00011 # conditions and the following disclaimer in the documentation and/or
00012 # other materials provided with the distribution.  THIS SOFTWARE IS
00013 # PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00014 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00015 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00017 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00018 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00019 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00020 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00021 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00022 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024 
00025 import roslib
00026 roslib.load_manifest('falkor_ardrone')
00027 
00028 import rospy
00029 from geometry_msgs.msg import Twist
00030 from std_msgs.msg import Empty
00031 from ardrone_autonomy.msg import Navdata
00032 import pid
00033 import time
00034 
00035 class ArdroneControl:
00036     def __init__( self ):
00037         self.nav_sub = rospy.Subscriber( "ardrone/navdata", Navdata, self.callback_navdata )
00038         self.cmd_vel_pub = rospy.Publisher( "cmd_vel", Twist )
00039         self.goal_vel_sub = rospy.Subscriber( "goal_vel", Twist, self.callback_goal_vel )
00040 
00041         # gain_p, gain_i, gain_d
00042         self.linearxpid = pid.Pid2( 0.5, 0.0, 0.5 )
00043         self.linearypid = pid.Pid2( 0.5, 0.0, 0.5 )
00044 
00045         self.vx = self.vy = self.vz = self.ax = self.ay = self.az = 0.0
00046         self.last_time = None
00047         self.goal_vel = Twist()
00048 
00049     def callback_goal_vel( self, data ):
00050         self.goal_vel = data
00051 
00052     def callback_navdata( self, data ):
00053         self.vx = data.vx/1e3
00054         self.vy = data.vy/1e3
00055         self.vz = data.vz/1e3
00056 
00057         # these accelerations must take into account orientation, so we get acceleration relative
00058         # to base_stabilized. This does not.
00059 #        self.ax = (data.ax*9.82)
00060 #        self.ay = (data.ay*9.82)
00061 #        self.az = (data.az - 1.0)*9.82
00062 
00063     def update( self ):
00064         if self.last_time == None:
00065             self.last_time = rospy.Time.now()
00066             dt = 0.0
00067         else:
00068             time = rospy.Time.now()
00069             dt = ( time - self.last_time ).to_sec()
00070             self.last_time = time
00071             
00072         cmd = Twist()
00073         cmd.angular.y = 0
00074         cmd.angular.x = 0
00075         cmd.angular.z = self.goal_vel.angular.z
00076         cmd.linear.z = self.goal_vel.linear.z
00077         cmd.linear.x = self.linearxpid.update( self.goal_vel.linear.x, self.vx, 0.0, dt )
00078         cmd.linear.y = self.linearypid.update( self.goal_vel.linear.y, self.vy, 0.0, dt )
00079 
00080         self.cmd_vel_pub.publish( cmd )
00081 
00082 def main():
00083   rospy.init_node( 'ardrone_control' )
00084 
00085   control = ArdroneControl()
00086   r = rospy.Rate(100)
00087 
00088   try:
00089       while not rospy.is_shutdown():
00090           control.update()
00091           r.sleep()
00092   except KeyboardInterrupt:
00093     print "Shutting down"
00094 
00095 if __name__ == '__main__':
00096     main()


falkor_ardrone
Author(s): Sameer Parekh
autogenerated on Tue Jan 7 2014 11:12:13