Webots实现大疆Mavic2pro无人机定点飞行

这篇具有很好参考价值的文章主要介绍了Webots实现大疆Mavic2pro无人机定点飞行。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

由于项目要求,现在需要做一个能够实现无人机根据事先给定的点位实现定点飞行,这里由于webots的跨平台性,考虑使用webots进行仿真

一、将无人机当成一个对象

1.1定义无人机相关属性

由于无人机有pitch、yaw、roll三个属性,分别对应前后运动、左右偏航和左右横滚、这里定义相关的所有属性用于控制。
同时定义相应的用于控制运动的函数

1.2定义用于控制无人机运动的代码

import math
import time
from controller import Robot, Camera, Compass, GPS, Gyro, InertialUnit, Keyboard, LED, Motor

# 自定义无人机类,继承机器人父类
class UAV(Robot):

    timestep = 0
    # Constants, empirically found.
    k_vertical_thrust = 68.5  # with this thrust, the drone lifts.
    k_vertical_offset = 0.6   # Vertical offset where the robot actually targets to stabilize itself.
    k_vertical_p = 3.0        # P constant of the vertical PID.
    k_roll_p = 50.0           # P constant of the roll PID.
    k_pitch_p = 30.0          # P constant of the pitch PID.
    # 初始化变量
    def __init__(self):
        # Get and enable devices.
        self.camera = Camera("camera")
        self.camera.enable(timestep)
        self.front_left_led = LED("front left led")
        self.front_right_led = LED("front right led")
        self.imu = InertialUnit("inertial unit")
        self.imu.enable(timestep)
        self.gps = GPS("gps")
        self.gps.enable(timestep)
        self.compass = Compass("compass")
        self.compass.enable(timestep)
        # 检测角速度
        self.gyro = Gyro("gyro")
        self.gyro.enable(timestep)
        # keyboard = Keyboard()
        # keyboard.enable(timestep)
        # 横滚检测器
        self.camera_roll_motor = Motor("camera roll")
        # 前后俯仰检测器
        self.camera_pitch_motor = Motor("camera pitch")
        # 用于控制无人机平稳飞行的变量
        self.roll_disturbance = 0.0
        self.pitch_disturbance = 0.0
        self.yaw_disturbance = 0.0
        # 设置初始目标噶度
        self.target_altitude = 10.0
        # Get propeller motors and set them to velocity mode.
        self.front_left_motor = Motor("front left propeller")
        self.front_right_motor = Motor("front right propeller")
        self.rear_left_motor = Motor("rear left propeller")
        self.rear_right_motor = Motor("rear right propeller")
        # 将所有的驱动器保存到一个数组中
        self.motors = [self.front_left_motor, self.front_right_motor, self.rear_left_motor, self.rear_right_motor]
    
    # 前进
    def forward():
        self.pitch_disturbance = 2.0
    # 后退
    def backward():
        self.pitch_disturbance = -2.0
    # 向右运动
    def right():
        self.yaw_disturbance = 1.3
    # 向左运动
    def left():
        self.yaw_disturbance = -1.3
    # 向右横滚
    def roll_right():
        self.roll_disturbance = -1.0
    # 向左横滚
    def roll_left():
        self.roll_disturbance = 1.0
    # 上升
    def up():
        self.target_altitude += 0.05
        print("target altitude:", target_altitude, "[m]")
    # 下降
    def down():
        self.target_altitude -= 0.05
        print("target altitude:", target_altitude, "[m]")
    # 获取无人机当前位置
    def getPosition():
        self.roll = self.imu.getRollPitchYaw()[0] + math.pi / 2.0
        self.pitch = self.imu.getRollPitchYaw()[1]
        self.altitude = self.gps.getValues()[1]
        # 获取角速度
        self.roll_acceleration = self.gyro.getValues()[0]
        self.pitch_acceleration = self.gyro.getValues()[1]

        # Blink the front LEDs alternatively with a 1 second rate.
        self.led_state = int(time) % 2
        self.front_left_led.set(led_state)
        self.front_right_led.set(1 - led_state)

    # 根据相关参数进行运动控制
    def Move():
        # Stabilize the Camera by actuating the camera motors according to the gyro feedback.
        self.camera_roll_motor.setPosition(-0.115 * self.roll_acceleration)
        self.camera_pitch_motor.setPosition(-0.1 * self.pitch_acceleration)

        # Compute the roll, pitch, and yaw errors.
        roll_input = self.k_roll_p * CLAMP(self.roll, -1.0, 1.0) + self.roll_acceleration + self.roll_disturbance
        pitch_input = self.k_pitch_p * CLAMP(self.pitch, -1.0, 1.0) - self.pitch_acceleration + self.pitch_disturbance
        yaw_input = self.yaw_disturbance
        clamped_difference_altitude = CLAMP(self.target_altitude - self.altitude + self.k_vertical_offset, -1.0, 1.0)
        vertical_input = self.k_vertical_p * pow(clamped_difference_altitude, 3.0)


        # Accute the motor taking into consideration all the computed inputs.
        front_left_motor_input = self.k_vertical_thrust + vertical_input - roll_input - pitch_input + yaw_input
        front_right_motor_input = self.k_vertical_thrust + vertical_input + roll_input - pitch_input - yaw_input
        rear_left_motor_input = self.k_vertical_thrust + vertical_input - roll_input + pitch_input - yaw_input
        rear_right_motor_input = self.k_vertical_thrust + vertical_input + roll_input + pitch_input + yaw_input
        self.front_left_motor.setVelocity(front_left_motor_input)
        self.front_right_motor.setVelocity(-front_right_motor_input)
        self.rear_left_motor.setVelocity(-rear_left_motor_input)
        self.rear_right_motor.setVelocity(rear_right_motor_input)

# 辅助函数
def CLAMP(value, low, high):
    return max(low, min(value, high))

1.3主函数实现无人机的点位固定和飞行检测

将主函数声明成控制器就可以了

from Uav import Uav
def main():
    uav = Uav()
    timestep = int(uav.getBasicTimeStep())
    uav.timestep = timestep
    keyboard = Keyboard()
    keyboard.enable(timestep)
    while uav.step(timestep) != -1:
        key = keyboard.getKey()
        uav.roll_disturbance = 0.0
        uav.pitch_disturbance = 0.0
        uav.yaw_disturbance = 0.0
        while key > 0:
            # 上升函数
            if key == Keyboard.UP:
                uav.forward()
            elif key == Keyboard.DOWN:
                uav.backward()
            elif key == Keyboard.RIGHT:
                uav.right()
            elif key == Keyboard.LEFT:
                uav.left()
            elif key == (Keyboard.SHIFT + Keyboard.RIGHT):
                uav.roll_right()
            elif key == (Keyboard.SHIFT + Keyboard.LEFT):
                uav.roll_left()
            elif key == (Keyboard.SHIFT + Keyboard.UP):
                uav.up()
            elif key == (Keyboard.SHIFT + Keyboard.DOWN):
                uav.down()
            key = keyboard.getKey()
        uav.getPosition()
        uav.Move()
    wb_robot_cleanup();

if __name__ == "__main__" :
    main()

二、用键盘控制测试代码

由于webots默认给的是通过C++代码实现键盘对无人机进行控制,然而开发使用的多是python,这里给出根据原本C++代码改写的python控制代码,直接新建成一个控制器然后在webots中选择这个.py文件作为控制器就可以了,记得放到controler文件夹中。

import math
import time
from controller import Robot, Camera, Compass, GPS, Gyro, InertialUnit, Keyboard, LED, Motor

def CLAMP(value, low, high):
    return max(low, min(value, high))

def main():
    # 创建一个机器人对象
    robot = Robot()
    # 每个物理动作的持续时间
    timestep = int(robot.getBasicTimeStep())

    # Get and enable devices.
    camera = Camera("camera")
    camera.enable(timestep)
    front_left_led = LED("front left led")
    front_right_led = LED("front right led")
    imu = InertialUnit("inertial unit")
    imu.enable(timestep)
    gps = GPS("gps")
    gps.enable(timestep)
    compass = Compass("compass")
    compass.enable(timestep)
    # 检测角速度
    gyro = Gyro("gyro")
    gyro.enable(timestep)
    keyboard = Keyboard()
    keyboard.enable(timestep)
    # 横滚检测器
    camera_roll_motor = Motor("camera roll")
    # 前后俯仰检测器
    camera_pitch_motor = Motor("camera pitch")

    # Get propeller motors and set them to velocity mode.
    front_left_motor = Motor("front left propeller")
    front_right_motor = Motor("front right propeller")
    rear_left_motor = Motor("rear left propeller")
    rear_right_motor = Motor("rear right propeller")
    motors = [front_left_motor, front_right_motor, rear_left_motor, rear_right_motor]
    for motor in motors:
        # 初始化无限旋转的运动
        motor.setPosition(float('inf'))
        # 启动!
        motor.setVelocity(1.0)

    # Display the welcome message.
    print("Start the drone...")

    # Wait one second.
    while robot.step(timestep) != -1:
        if robot.getTime() > 1.0:
            break

    # Display manual control message.
    print("You can control the drone with your computer keyboard:")
    print("- 'up': move forward.")
    print("- 'down': move backward.")
    print("- 'right': turn right.")
    print("- 'left': turn left.")
    print("- 'shift + up': increase the target altitude.")
    print("- 'shift + down': decrease the target altitude.")
    print("- 'shift + right': strafe right.")
    print("- 'shift + left': strafe left.")

    # Constants, empirically found.
    k_vertical_thrust = 68.5  # with this thrust, the drone lifts.
    k_vertical_offset = 0.6   # Vertical offset where the robot actually targets to stabilize itself.
    k_vertical_p = 3.0        # P constant of the vertical PID.
    k_roll_p = 50.0           # P constant of the roll PID.
    k_pitch_p = 30.0          # P constant of the pitch PID.

    # Variables.
    # 设置初始高度
    target_altitude = 1.0  # The target altitude. Can be changed by the user.

    # Main loop
    # - perform simulation steps until Webots is stopping the controller
    while robot.step(timestep) != -1:
        time = robot.getTime()

        # Retrieve robot position using the sensors.
        roll = imu.getRollPitchYaw()[0] + math.pi / 2.0
        pitch = imu.getRollPitchYaw()[1]
        altitude = gps.getValues()[1]
        # 获取角速度
        roll_acceleration = gyro.getValues()[0]
        pitch_acceleration = gyro.getValues()[1]

        # Blink the front LEDs alternatively with a 1 second rate.
        led_state = int(time) % 2
        front_left_led.set(led_state)
        front_right_led.set(1 - led_state)

        # Stabilize the Camera by actuating the camera motors according to the gyro feedback.
        camera_roll_motor.setPosition(-0.115 * roll_acceleration)
        camera_pitch_motor.setPosition(-0.1 * pitch_acceleration)

        # Transform the keyboard input to disturbances on the stabilization algorithm.
        roll_disturbance = 0.0
        pitch_disturbance = 0.0
        yaw_disturbance = 0.0
        key = keyboard.getKey()
        while key > 0:
            # 上升函数
            if key == Keyboard.UP:
                pitch_disturbance = 2.0
            elif key == Keyboard.DOWN:
                pitch_disturbance = -2.0
            elif key == Keyboard.RIGHT:
                yaw_disturbance = 1.3
            elif key == Keyboard.LEFT:
                yaw_disturbance = -1.3
            elif key == (Keyboard.SHIFT + Keyboard.RIGHT):
                roll_disturbance = -1.0
            elif key == (Keyboard.SHIFT + Keyboard.LEFT):
                roll_disturbance = 1.0
            elif key == (Keyboard.SHIFT + Keyboard.UP):
                target_altitude += 0.05
                print("target altitude:", target_altitude, "[m]")
            elif key == (Keyboard.SHIFT + Keyboard.DOWN):
                target_altitude -= 0.05
                print("target altitude:", target_altitude, "[m]")
            key = keyboard.getKey()

        # Compute the roll, pitch, and yaw errors.
        roll_input = k_roll_p * CLAMP(roll, -1.0, 1.0) + roll_acceleration + roll_disturbance
        pitch_input = k_pitch_p * CLAMP(pitch, -1.0, 1.0) - pitch_acceleration + pitch_disturbance
        yaw_input = yaw_disturbance
        clamped_difference_altitude = CLAMP(target_altitude - altitude + k_vertical_offset, -1.0, 1.0)
        vertical_input = k_vertical_p * pow(clamped_difference_altitude, 3.0)


        # Accute the motor taking into consideration all the computed inputs.
        front_left_motor_input = k_vertical_thrust + vertical_input - roll_input - pitch_input + yaw_input
        front_right_motor_input = k_vertical_thrust + vertical_input + roll_input - pitch_input - yaw_input
        rear_left_motor_input = k_vertical_thrust + vertical_input - roll_input + pitch_input - yaw_input
        rear_right_motor_input = k_vertical_thrust + vertical_input + roll_input + pitch_input + yaw_input
        front_left_motor.setVelocity(front_left_motor_input)
        front_right_motor.setVelocity(-front_right_motor_input)
        rear_left_motor.setVelocity(-rear_left_motor_input)
        rear_right_motor.setVelocity(rear_right_motor_input)
        
    
    wb_robot_cleanup()

if __name__ == "__main__":
    main()

三、效果展示

用python控制器实现键盘控制无人机运动文章来源地址https://www.toymoban.com/news/detail-768419.html

四、注意点

  1. Webots中不支持到其他库,所以理论上应该都写在一个文件夹中,如果想要写在不用的文件夹中,需要
  2. 改变控制器以后记得重新保存一份世界文件。

到了这里,关于Webots实现大疆Mavic2pro无人机定点飞行的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • PPK大疆无人机应用教程

    PPK大疆无人机应用教程

    新建工程,设置项目名称,保存位置,控制等级,坐标系统(坐标系统选择高斯克吕格,中央子午线根据实际数据所在位置进行选择) 选择大疆数据,找到对应的文件夹 数据有:图片,EVENT.bin,PPKAW.bin,Rinex.ads和Time数据,以及静态数据 导入结果:

    2024年02月16日
    浏览(11)
  • 大疆飞卡30运载无人机技术分享

    大疆飞卡30运载无人机技术分享

    大疆飞卡30是大疆公司面向运输领域推出的一款专业运载无人机。它采用了优秀的设计,装备了多种先进传感器,以解决运输中的难题。以下我们来了解一下其主要特点: 【应用领域】 飞卡30适用于山地救灾、农业化肥施用、工程材料运送等交通不便的山区应用,也适用于海岛联通

    2024年02月12日
    浏览(13)
  • 大疆无人机基于RTMP服务推流直播

    大疆无人机基于RTMP服务推流直播

    流程:配置nginx服务器---打开服务器----配置无人机rtmp地址,将无人机画面推流到服务器上----运行vlc从服务器上拉取视频流播放。 学习视频链接(可借鉴):https://www.youtube.com/watch?v=QNEjTGQL7wc 一、在linux服务器中(ubuntu18.04)下载安装docker容器,docker分为docker engine 和 docker desktop 我

    2024年02月05日
    浏览(16)
  • 大疆精灵4无人机航测数据内业处理流程(Photoscan)

    大疆精灵4无人机航测数据内业处理流程(Photoscan)

    使用Photoscan进行空三处理。 1.打开Agisoft Metashape Professional (64 bit),也称作Photoscan。点击工具栏的“工作流程”,导入航测的照片数据,如果文件较多,也可以直接导入存放照片的文件夹。 2.打开相机校准,配置参数,一般用软件会自动识别,用默认的参数就行。然后点击ok。

    2024年02月07日
    浏览(7)
  • 获取大疆无人机的飞控记录数据并绘制曲线

    获取大疆无人机的飞控记录数据并绘制曲线

    机型M350RTK,其飞行记录文件为加密的,我的完善代码如下 git@github.com:huashu996/DJFlightRecordParsing2TXT.git 飞行记录文件在打开【我的电脑】,进入遥控器内存, 文件路径:此电脑 pm430 内部共享存储空间 DJI com.dji.industry.pilot FlightRecord  网址如下DJI Developer 注册完之后新建APP获得密

    2024年02月16日
    浏览(12)
  • 大疆无人机视频删了怎么恢复?尝试这些恢复技巧

    大疆无人机视频删了怎么恢复?尝试这些恢复技巧

    无人机拍摄的视频已经成为许多飞行爱好者和专业人士珍贵的记忆与资料。然而,误删视频是许多人都可能遇到的问题。当您不慎删除了大疆无人机中的视频时,不必过于焦虑。本文将为您详细介绍如何恢复这些误删的视频,帮助您找回宝贵的回忆。 图片来源于网络,如有侵

    2024年04月15日
    浏览(11)
  • 心得:大疆无人机RTMP推流直播(Windows版本已成功)

    心得:大疆无人机RTMP推流直播(Windows版本已成功)

    1、nginx的Gryphon版本,它内部已经集成了rtmp的推流编译(nginx-Gryphon) 2、服务器状态检查程序stat.xsl(nginx-rtmp-module) 3、ffmpeg(ffmpeg) 4、VLC(VLC) 1、将下载好的nginx 1.7.11.3 Gryphon解压修改文件名为nginx-1.7.11.3-Gryphon,绝对路径中不能有中文,必须全为英文! 2、在根目录中的con

    2024年02月03日
    浏览(12)
  • ROS环境下大疆tello无人机源码安装&驱动代码解读

    ROS环境下大疆tello无人机源码安装&驱动代码解读

            大疆tello无人机是一款微小型无人机,可以支持多种开发模式。这里用的是ROS1的kinetic版本进行开发。参考文档来自http://wiki.ros.org/tello_driver         打开终端,键入以下命令进行二进制文件安装:         然后进入到ros工作空间,下载tello驱动源码         返回

    2024年02月13日
    浏览(13)
  • 大疆无人机 MobileSDK(遥控器/手机端)开发 v4版<1>

    大疆无人机 MobileSDK(遥控器/手机端)开发 v4版<1>

    刚刚结束了项目交付,趁热打铁分享一下这次遇到的新东西。首先了解一下大疆的无人机,它大致可以分为三级。 入门级 :适合新手,没事干在野外飞一飞拍拍风景啥的。操作也简单,基本上看飞行教程都能懂,也不需要太高的专业性,飞机也相对较小安全系数相对较高。

    2024年02月06日
    浏览(8)
  • 新款解读:业内最小大疆御3无人机机场/机巢/机库功能技术解析

    新款解读:业内最小大疆御3无人机机场/机巢/机库功能技术解析

    复亚智能推出全新的S20小型无人机自动机场,具备一体化设计、快速部署、无人值守、快速起飞、高效作业能力,适配DJI Mavic 3行业版无人机,专为中低频巡检巡逻和应急场景量身打造。 S20具备卓越的可靠性和灵活的业务适应性,配置全新的软件系统、丰富的挂载、全流程安

    2024年02月06日
    浏览(9)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包