forked from aero-iitg/leap_motion_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeoff.py
83 lines (64 loc) · 2.33 KB
/
takeoff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
from geometry_msgs.msg import PoseStamped,TwistStamped,PositionTarget
from mavros_msgs.srv import CommandBool, SetMode
from mavros_msgs.msg import State
from std_msgs.msg import Int32MultiArray,MultiArrayDimension
import rospy
pi=3.14
def state_cb(state):
global current_state
current_state = state
print("state")
arming_client(True)
set_mode_client(base_mode=0, custom_mode="OFFBOARD")
def move_cb(msg):
pos = TwistStamped() #MESSEGE FOR CURRENT SETPOINT
pos.twist.linear.x =msg.data[0]
pos.twist.linear.y =msg.data[1]
pos.twist.linear.z =msg.data[2]
publis.publish(pos)
def setOffboardMode(self):
rospy.wait_for_service('mavros/set_mode')
try:
flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
flightModeService(custom_mode='OFFBOARD')
print("ENTERING OFFBOARD MODE")
except (rospy.ServiceException, e):
print ("service set_mode call failed: %s. Offboard Mode could not be set."%e)
def setArm(self):
rospy.wait_for_service('mavros/cmd/arming')
try:
armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
armService(True)
except (rospy.ServiceException, e):
print ("Service arming call failed: %s"%e)
rospy.init_node("scanner")
#rospy.Rate(20)
rospy.Subscriber('/mavros/state', State, state_cb)
arming_client = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool)
set_mode_client = rospy.ServiceProxy('/mavros/set_mode', SetMode)
rospy.Subscriber('/mavros/state', State, move_cb)
publis = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=10)
current_state = State() #DECLARE VARIABLE FOR CURRENT STATE
rospy.wait_for_message('/mavros/state', State)
if not current_state.armed:
print("CURRENTLY NOT ARMED")
arming_client(True)
print("NOW ARMED")
if current_state.mode != "OFFBOARD":
print("MODE IS NOT OFFBOARD")
set_mode_client(base_mode=0, custom_mode="OFFBOARD")
print("MODE CHANGED TO OFFBOARD")
while not rospy.is_shutdown():
#print("Pub")
#publis.publish(pos)
rospy.wait_for_message('/mavros/state', State)
if not current_state.armed:
#print("CURRENTLY NOT ARMED")
arming_client(True)
#print("NOW ARMED")
if current_state.mode != "OFFBOARD":
#print("MODE IS NOT OFFBOARD")
set_mode_client(base_mode=0, custom_mode="OFFBOARD")
#print("MODE CHANGED TO OFFBOARD")
rospy.spin()