5 from geometry_msgs.msg
import PoseStamped
6 from mavros_msgs.msg
import State
7 from mavros_msgs.srv
import CommandBool, CommandBoolRequest
8 from mavros_msgs.srv
import SetMode, SetModeRequest
10 current_state = State()
17 rospy.init_node(
"offb_node")
20 rospy.Subscriber(
"mavros/state", State, state_cb)
21 local_pos_pub = rospy.Publisher(
"mavros/setpoint_position/local",
24 arming_client = rospy.ServiceProxy(
"mavros/cmd/arming", CommandBool)
25 set_mode_client = rospy.ServiceProxy(
"mavros/set_mode", SetMode)
27 while not rospy.is_shutdown()
and not current_state.connected:
31 pose.pose.position.x = 0
32 pose.pose.position.y = 0
33 pose.pose.position.z = 2
36 local_pos_pub.publish(pose)
39 if rospy.is_shutdown():
42 offb_set_mode = SetModeRequest()
43 offb_set_mode.custom_mode =
"OFFBOARD" 45 arm_cmd = CommandBoolRequest()
48 last_request = rospy.Time.now()
50 while not rospy.is_shutdown():
51 if current_state.mode !=
"OFFBOARD" \
52 and (rospy.Time.now() - last_request > rospy.Duration(5)):
55 offb_set_mode_resp = set_mode_client(offb_set_mode)
56 if offb_set_mode_resp.mode_sent:
57 rospy.loginfo(
"Offboard enabled")
58 except rospy.ServiceException
as e:
61 last_request = rospy.Time.now()
64 if not current_state.armed \
65 and (rospy.Time.now() - last_request > rospy.Duration(5)):
68 arm_cmd_resp = arming_client(arm_cmd)
69 if arm_cmd_resp.success:
70 rospy.loginfo(
"Vehicle armed")
71 except rospy.ServiceException
as e:
74 last_request = rospy.Time.now()
76 local_pos_pub.publish(pose)
79 if __name__ ==
"__main__":
82 except rospy.ROSInterruptException: