gapter_pilot.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import rospy
00003 from std_msgs.msg import String
00004 from sensor_msgs.msg import NavSatFix
00005 from mavros_msgs.srv import *
00006 from geometry_msgs.msg import TwistStamped
00007 
00008 #global variable
00009 latitude =0.0
00010 longitude=0.0
00011 
00012 
00013 def setGuidedMode():
00014     rospy.wait_for_service('/mavros/set_mode')
00015     try:
00016         flightModeService = rospy.ServiceProxy('/mavros/set_mode', mavros_msgs.srv.SetMode)
00017         #http://wiki.ros.org/mavros/CustomModes for custom modes
00018         isModeChanged = flightModeService(custom_mode='GUIDED') #return true or false
00019     except rospy.ServiceException, e:
00020         print "service set_mode call failed: %s. GUIDED Mode could not be set. Check that GPS is enabled"%e
00021         
00022 def setStabilizeMode():
00023     rospy.wait_for_service('/mavros/set_mode')
00024     try:
00025         flightModeService = rospy.ServiceProxy('/mavros/set_mode', mavros_msgs.srv.SetMode)
00026         #http://wiki.ros.org/mavros/CustomModes for custom modes
00027         isModeChanged = flightModeService(custom_mode='STABILIZE') #return true or false
00028     except rospy.ServiceException, e:
00029         print "service set_mode call failed: %s. GUIDED Mode could not be set. Check that GPS is enabled"%e
00030 
00031 def setLandMode():
00032     rospy.wait_for_service('/mavros/cmd/land')
00033     try:
00034         landService = rospy.ServiceProxy('/mavros/cmd/land', mavros_msgs.srv.CommandTOL)
00035         #http://wiki.ros.org/mavros/CustomModes for custom modes
00036         isLanding = landService(altitude = 0, latitude = 0, longitude = 0, min_pitch = 0, yaw = 0)
00037     except rospy.ServiceException, e:
00038         print "service land call failed: %s. The vehicle cannot land "%e
00039           
00040 def setArm():
00041     rospy.wait_for_service('/mavros/cmd/arming')
00042     try:
00043         armService = rospy.ServiceProxy('/mavros/cmd/arming', mavros_msgs.srv.CommandBool)
00044         armService(True)
00045     except rospy.ServiceException, e:
00046         print "Service arm call failed: %s"%e
00047         
00048 def setDisarm():
00049     rospy.wait_for_service('/mavros/cmd/arming')
00050     try:
00051         armService = rospy.ServiceProxy('/mavros/cmd/arming', mavros_msgs.srv.CommandBool)
00052         armService(False)
00053     except rospy.ServiceException, e:
00054         print "Service arm call failed: %s"%e
00055 
00056 
00057 def setTakeoffMode():
00058     rospy.wait_for_service('/mavros/cmd/takeoff')
00059     try:
00060         takeoffService = rospy.ServiceProxy('/mavros/cmd/takeoff', mavros_msgs.srv.CommandTOL) 
00061         takeoffService(altitude = 2, latitude = 0, longitude = 0, min_pitch = 0, yaw = 0)
00062     except rospy.ServiceException, e:
00063         print "Service takeoff call failed: %s"%e
00064     
00065     
00066 
00067 def globalPositionCallback(globalPositionCallback):
00068     global latitude
00069     global longitude
00070     latitude = globalPositionCallback.latitude
00071     longitude = globalPositionCallback.longitude
00072     #print ("longitude: %.7f" %longitude)
00073     #print ("latitude: %.7f" %latitude)
00074 
00075 def menu():
00076     print "Press"
00077     print "1: to set mode to GUIDED"
00078     print "2: to set mode to STABILIZE"
00079     print "3: to set mode to ARM the drone"
00080     print "4: to set mode to DISARM the drone"
00081     print "5: to set mode to TAKEOFF"
00082     print "6: to set mode to LAND"
00083     print "7: print GPS coordinates"
00084     
00085 def myLoop():
00086     x='1'
00087     while ((not rospy.is_shutdown())and (x in ['1','2','3','4','5','6','7'])):
00088         menu()
00089         x = raw_input("Enter your input: ");
00090         if (x=='1'):
00091             setGuidedMode()
00092         elif(x=='2'):
00093             setStabilizeMode()
00094         elif(x=='3'):
00095             setArm()
00096         elif(x=='4'):
00097             setDisarm()
00098         elif(x=='5'):
00099             setTakeoffMode()
00100         elif(x=='6'):
00101             setLandMode()
00102         elif(x=='7'):
00103             global latitude
00104             global longitude
00105             print ("longitude: %.7f" %longitude)
00106             print ("latitude: %.7f" %latitude)
00107         else: 
00108             print "Exit"
00109         
00110         
00111     
00112 
00113 if __name__ == '__main__':
00114     rospy.init_node('gapter_pilot_node', anonymous=True)
00115     rospy.Subscriber("/mavros/global_position/raw/fix", NavSatFix, globalPositionCallback)
00116     velocity_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
00117     # spin() simply keeps python from exiting until this node is stopped
00118     
00119     #listener()
00120     myLoop()
00121     #rospy.spin()


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