-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVTOL_Test1.py
More file actions
61 lines (47 loc) · 1.88 KB
/
VTOL_Test1.py
File metadata and controls
61 lines (47 loc) · 1.88 KB
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
import rospy
from geometry_msgs.msg import PoseStamped
from mavros_msgs.msg import State
from mavros_msgs.srv import CommandBool, CommandBoolRequest, SetMode, SetModeRequest
current_state = State()
def state_cb(msg):
global current_state
current_state = msg
def change_mode(mode):
rospy.wait_for_service('/mavros/set_mode')
try:
set_mode_service = rospy.ServiceProxy('/mavros/set_mode', SetMode)
resp = set_mode_service(custom_mode=mode)
return resp.mode_sent
except rospy.ServiceException as e:
print("Service call failed: %s" % e)
def control_vtol():
rospy.init_node("vtol_control_node")
rospy.Subscriber("mavros/state", State, state_cb)
local_pos_pub = rospy.Publisher("mavros/setpoint_position/local", PoseStamped, queue_size=10)
rospy.wait_for_service("/mavros/cmd/arming")
arming_client = rospy.ServiceProxy("mavros/cmd/arming", CommandBool)
pose = PoseStamped()
pose.pose.position.x = 0
pose.pose.position.y = 0
pose.pose.position.z = 10 # Kalkış yüksekliği
arm_cmd = CommandBoolRequest()
arm_cmd.value = True
rate = rospy.Rate(20) # 20 Hz
while not rospy.is_shutdown():
if current_state.mode != "OFFBOARD":
change_mode("OFFBOARD") # Kalkış için MC modu
rospy.sleep(5)
if current_state.mode == "OFFBOARD" and current_state.armed:
change_mode("AUTO.LOITER") # Uçuş için FW modu
rospy.sleep(5)
# İniş Koşulu: Belirli bir yüksekliğe ulaşıldığında MC moduna geç
if current_state.armed and pose.pose.position.z < 1.0: # Örnek yükseklik koşulu
change_mode("OFFBOARD") # İniş için MC modu
rospy.sleep(5)
local_pos_pub.publish(pose)
rate.sleep()
if __name__ == "__main__":
try:
control_vtol()
except rospy.ROSInterruptException:
pass