通过mavros实现无人机航点飞行(mission模式)示例代码

通过mavros实现无人机航点飞行(mission模式)示例代码

通过 mavros/mission/push 这个服务进行航点上传 

Mavros-AUTO.MISSION-mode: This script will activate AUTO.MISSION mode along with pushing waypoints to be followed by the drone while PX4 gazebo simulation wrapped with mavros running for VTOL quad-plane. - Gitee.com

wayPointMission.py

#!/usr/bin/env python
# ROS python API
import rospy

# 3D point & Stamped Pose msgs
from geometry_msgs.msg import Point, PoseStamped
# import all mavros messages and services
from mavros_msgs.msg import *
from mavros_msgs.srv import *


class Modes:
    def __init__(self):
        pass

    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

    def auto_set_mode(self):
        rospy.wait_for_service('mavros/set_mode')
        try:
            # setModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.set_mode.request.custom_mode)
            setModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
            setModeService(custom_mode="AUTO.MISSION")
        except rospy.ServiceException, e:
            print "Service takeoff call failed: %s"%e

    def wpPush(self,index,wps):
        rospy.wait_for_service('mavros/mission/push')
        try:
            wpPushService = rospy.ServiceProxy('mavros/mission/push', WaypointPush,persistent=True)
            wpPushService(start_index=0,waypoints=wps)#start_index = the index at which we want the mission to start
            print "Waypoint Pushed"
        except rospy.ServiceException, e:
            print "Service takeoff call failed: %s"%e
    def wpPull(self,wps):
        rospy.wait_for_service('mavros/mission/pull')
        try:
            wpPullService = rospy.ServiceProxy('mavros/mission/pull', WaypointPull,persistent=True)
            print wpPullService().wp_received

            print "Waypoint Pulled"
        except rospy.ServiceException, e:
            print "Service Puling call failed: %s"%e

class stateMoniter:
    def __init__(self):
        self.state = State()
        # Instantiate a setpoints message
        self.sp = PositionTarget()

        # set the flag to use position setpoints and yaw angle
        self.sp.type_mask = int('010111111000', 2)
        
    def stateCb(self, msg):
        self.state = msg

class wpMissionCnt:

    def __init__(self):
        self.wp =Waypoint()
        
    def setWaypoints(self,frame,command,is_current,autocontinue,param1,param2,param3,param4,x_lat,y_long,z_alt):
        self.wp.frame =frame #  FRAME_GLOBAL_REL_ALT = 3 for more visit http://docs.ros.org/api/mavros_msgs/html/msg/Waypoint.html
        self.wp.command = command '''VTOL TAKEOFF = 84,NAV_WAYPOINT = 16, TAKE_OFF=22 for checking out other parameters go to https://github.com/mavlink/mavros/blob/master/mavros_msgs/msg/CommandCode.msg'''
        self.wp.is_current= is_current
        self.wp.autocontinue = autocontinue # enable taking and following upcoming waypoints automatically 
        self.wp.param1=param1 # no idea what these are for but the script will work so go ahead
        self.wp.param2=param2
        self.wp.param3=param3
        self.wp.param4=param4
        self.wp.x_lat= x_lat 
        self.wp.y_long=y_long
        self.wp.z_alt= z_alt #relative altitude.

        return self.wp


def main():
    rospy.init_node('waypointMission', anonymous=True)
    rate = rospy.Rate(20.0)

    stateMt = stateMoniter()
    md = Modes()
    
    wayp0 = wpMissionCnt()
    wayp1 = wpMissionCnt()
    wayp2 = wpMissionCnt()
    wayp3 = wpMissionCnt()
    
    wps = [] #List to story waypoints
    
    w = wayp0.setWaypoints(3,84,True,True,0.0,0.0,0.0,float('nan'),47.397713,8.547605,50)
    wps.append(w)

    w = wayp1.setWaypoints(3,16,False,True,0.0,0.0,0.0,float('nan'),47.398621,8.547745,50)
    wps.append(w)

    w = wayp2.setWaypoints(3,16,False,True,0.0,0.0,0.0,float('nan'),47.399151,8.545320,50)
    wps.append(w)

    print wps
    md.wpPush(0,wps)

    md.wpPull(0)
    rospy.Subscriber("/mavros/state",State, stateMt.stateCb)

    # Arming the drone
    while not stateMt.state.armed:
        md.setArm()
        rate.sleep()
    # Switching the state to auto mode
    while not stateMt.state.mode=="AUTO.MISSION":
        md.auto_set_mode()
        rate.sleep()
        print "AUTO.MISSION"

    # rospy.spin()


if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptException:
        pass

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个很好的问题。首先,你需要设计一个合适的数据结构来存储航线信息。一个简单的方案是使用一个列表,每个元素表示一个航点,包含时间、航点间距、距离平台距离、航点名称、飞行模式和平台信息等字段。 接下来,你需要编写录入航线信息的代码。这可以通过一个函数实现,接受用户输入并将信息添加到航线列表中。 然后,你可以编写自动计算航线信息的代码。这也可以通过一个函数实现,接受航线列表作为输入,并计算出时间差的秒数、平均速度、上一航点距离和隐患问题距平台距离。在计算平均速度时,需要注意时间差可能为零的情况。在计算上一航点距离和隐患问题距平台距离时,需要通过航线列表中前一个航点的信息来计算。 最后,你可以编写一个主函数,调用录入航线信息和自动计算航线信息的函数,并将结果输出到屏幕上或保存到文件中。 下面是一个示例代码,可以供你参考: ```python class Waypoint: def __init__(self, time, distance, platform_distance, name, mode, platform): self.time = time self.distance = distance self.platform_distance = platform_distance self.name = name self.mode = mode self.platform = platform def input_waypoints(): waypoints = [] while True: time = input("请输入时间(格式:HH:mm:ss):") if time == "": break distance = float(input("请输入航点间距(单位:公里):")) platform_distance = float(input("请输入距平台距离(单位:公里):")) name = input("请输入航点名称:") mode = input("请输入飞行模式:") platform = input("请输入平台信息:") waypoint = Waypoint(time, distance, platform_distance, name, mode, platform) waypoints.append(waypoint) return waypoints def calculate_waypoints(waypoints): for i in range(1, len(waypoints)): time_diff = (datetime.datetime.strptime(waypoints[i].time, "%H:%M:%S") - datetime.datetime.strptime(waypoints[i-1].time, "%H:%M:%S")).total_seconds() if time_diff == 0: avg_speed = 0 else: avg_speed = waypoints[i].distance / time_diff prev_distance = time_diff * avg_speed hazard_distance = waypoints[i].platform_distance + prev_distance print("航点名称:{}\n时间差的秒数:{}\n平均速度:{}\n上一航点距离:{}\n隐患问题距平台距离:{}".format( waypoints[i].name, time_diff, avg_speed, prev_distance, hazard_distance)) def main(): waypoints = input_waypoints() calculate_waypoints(waypoints) if __name__ == "__main__": main() ``` 当你运行这段代码时,程序会提示你逐个输入航点信息,并计算出每个航点的时间差的秒数、平均速度、上一航点距离和隐患问题距平台距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值