move_shapes.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 moveSquare():
00076     square_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
00077     square = TwistStamped()
00078 
00079     user_input = raw_input("Enter the square side: "); # take from user
00080     side_length = float(user_input)
00081     flag_x = 1
00082     flag_y = 1
00083     for x in range(2,6):
00084         print 'x=', (x)
00085         print '4%x=', (4%x)
00086         if 4%x == 0:
00087                 square.twist.linear.x = side_length
00088                 flag_x= -1
00089         else:
00090                 square.twist.linear.y = side_length
00091                 flag_y= -1
00092 
00093         square_pub.publish(square)
00094         rospy.sleep(5)
00095 
00096         square.twist.linear.x=0;
00097         square.twist.linear.y=0;
00098         square_pub.publish(square);
00099         rospy.sleep(2);
00100         if flag_x == -1 and flag_y == -1:
00101                 side_length *= -1
00102                 flag_x = 1
00103                 flag_y = 1
00104 
00105 
00106 def moveCircle():
00107 
00108     circle_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
00109     circle = TwistStamped()
00110 
00111 
00112     circle.twist.linear.x=0.5
00113     circle.twist.linear.z=0.5
00114     circle_pub.publish(circle)
00115 
00116 def moveSpiral():
00117     constant_speed=4
00118     rk =1.0
00119     rk_step = 0.1
00120     duration = 10.0 #10 seconds
00121     rate = 20.0
00122     number_of_iteration = duration*rate
00123     loop = rospy.Rate(rate)
00124 
00125     i=0
00126 
00127     while(i<number_of_iteration):
00128 
00129         #print t1-t0
00130         vel_msg = TwistStamped()
00131         i=i+1
00132         rk=rk+rk_step
00133         vel_msg.twist.linear.x =rk
00134         vel_msg.twist.linear.y =0
00135         vel_msg.twist.linear.z =0
00136         vel_msg.twist.angular.x = 0
00137         vel_msg.twist.angular.y = 0
00138         vel_msg.twist.angular.z =constant_speed
00139         
00140 
00141         #publish the velocity
00142         velocity_pub.publish(vel_msg)
00143         loop.sleep()
00144 
00145 def stop(): 
00146 
00147     stop_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
00148     stop = TwistStamped()
00149  
00150     stop.linear.x=0.0
00151     stop.linear.y=0.0
00152     stop.angular.z=0.0 
00153     stop_pub.publish(twist)
00154 
00155 
00156 
00157 def menu():
00158     print "Press"
00159     print "1: to move on square"
00160     print "2: to move on circle"
00161     print "3: to move on spiral"
00162     print "4: to stop"
00163     
00164 def myLoop():
00165     x='1'
00166     while ((not rospy.is_shutdown())and (x in ['1','2','3','4'])):
00167         menu()
00168         x = raw_input("Enter your input: ");
00169         if (x=='1'):
00170             moveSquare()
00171         elif(x=='2'):
00172             moveCircle()
00173         elif(x=='3'):
00174             moveSpiral()
00175         elif(x=='4'):
00176             stop()
00177         else: 
00178             print "Exit"
00179         
00180         
00181     
00182 
00183 if __name__ == '__main__':
00184     rospy.init_node('gapter_pilot_node', anonymous=True)
00185     rospy.Subscriber("/mavros/global_position/raw/fix", NavSatFix, globalPositionCallback)
00186     velocity_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
00187     # spin() simply keeps python from exiting until this node is stopped
00188     
00189     #listener()
00190     myLoop()
00191     #rospy.spin()


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