Gazebo Python Takeoff
Drone Takeoff in Gazebo using python offboard node.
mkdir pysim
cd pysim
mkdir src
catkin_make
cd sim_ws/src
catkin_create_pkg takeoff rospy mavros geometry_msgs
echo "source ~/pysim/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
cd pysim/src/takeoff/src
gedit offboard.py
paste the block in offboard.py
#!/usr/bin/env python
import rospy
import mavros
from geometry_msgs.msg import PoseStamped
from mavros_msgs.msg import *
from mavros_msgs.srv import *
# callback method for state sub
mavros.set_namespace()
current_state = State()
offb_set_mode = SetMode
def state_cb(state):
global current_state
current_state = state
local_pos_pub = rospy.Publisher(mavros.get_topic('setpoint_position', 'local'), PoseStamped, queue_size=10)
state_sub = rospy.Subscriber(mavros.get_topic('state'), State, state_cb)
arming_client = rospy.ServiceProxy(mavros.get_topic('cmd', 'arming'), CommandBool)
set_mode_client = rospy.ServiceProxy(mavros.get_topic('set_mode'), SetMode)
pose = PoseStamped()
pose.pose.position.x = 0
pose.pose.position.y = 0
pose.pose.position.z = 2
def position_control():
rospy.init_node('offb_node', anonymous=True)
prev_state = current_state
rate = rospy.Rate(20.0) # MUST be more then 2Hz
# send a few setpoints before starting
for i in range(100):
local_pos_pub.publish(pose)
rate.sleep()
# wait for FCU connection
while not current_state.connected:
rate.sleep()
last_request = rospy.get_rostime()
while not rospy.is_shutdown():
now = rospy.get_rostime()
if current_state.mode != "OFFBOARD" and (now - last_request > rospy.Duration(5.)):
set_mode_client(base_mode=0, custom_mode="OFFBOARD")
last_request = now
else:
if not current_state.armed and (now - last_request > rospy.Duration(5.)):
arming_client(True)
last_request = now
# older versions of PX4 always return success==True, so better to check Status instead
if prev_state.armed != current_state.armed:
rospy.loginfo("Vehicle armed: %r" % current_state.armed)
if prev_state.mode != current_state.mode:
rospy.loginfo("Current mode: %s" % current_state.mode)
prev_state = current_state
# Update timestamp and publish pose
pose.header.stamp = rospy.Time.now()
local_pos_pub.publish(pose)
rate.sleep()
if __name__ == '__main__':
try:
position_control()
except rospy.ROSInterruptException:
pass
cd pysim
catkin_make
source devel/setup.bash
In a New Terminal
#launch simulation environment
roslaunch px4 mavros_posix_sitl.launch
rosrun takeoff offboard.py
above is python code for takeoff similar to c++ code in simulation example.
Last updated
Was this helpful?