Webots中使用大疆“御”2专业版-DJI-Mavic 2 Pro进行无人机仿真实践

本文主要是介绍Webots中使用大疆“御”2专业版-DJI-Mavic 2 Pro进行无人机仿真实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

兼顾体积和性能的超强无人机大疆-“御”-DJI-Mavic现在有了Webots仿真版,可以零成本愉快玩耍了。

Webots-Mavic

控制器支持C++、Python,并全面支持ROS。

DJI-Tello:https://blog.csdn.net/ZhangRelay/article/details/99291395

Webots-ROS:https://blog.csdn.net/ZhangRelay/article/details/85247284


将demo下载到对应文件下:

mavic仿真包文件树

第一次使用时,控制器需要编译,否则会出现无控制器报错~

使用Mavic:

打开Webots,单击File->Open World->mavic_2_pro.wbt。

打开环境文件

这时候会加载如下环境:

环境初始化,无人机起飞

如果报错~请在右侧程序编辑部分,编译代码,生成控制器即可。

使用键盘就可以控制Mavic 2 pro了。

飞行定高在12m
  // Constants, empirically found.const double k_vertical_thrust = 68.5;  // with this thrust, the drone lifts.const double k_vertical_offset = 0.6;   // Vertical offset where the robot actually targets to stabilize itself.const double k_vertical_p = 3.0;        // P constant of the vertical PID.const double k_roll_p = 50.0;           // P constant of the roll PID.const double k_pitch_p = 30.0;          // P constant of the pitch PID.// Variables.double target_altitude = 1.0;  // The target altitude. Can be changed by the user.

初始参数和配置,可以修改。如果使用键盘控制而非手柄,键盘控制的指令需要调整,修改如下代码:

      switch (key) {case WB_KEYBOARD_UP:pitch_disturbance = 2.0;break;case WB_KEYBOARD_DOWN:pitch_disturbance = -2.0;break;case WB_KEYBOARD_RIGHT:yaw_disturbance = 1.3;break;case WB_KEYBOARD_LEFT:yaw_disturbance = -1.3;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_RIGHT):roll_disturbance = -1.0;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_LEFT):roll_disturbance = 1.0;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_UP):target_altitude += 0.05;printf("target altitude: %f [m]\n", target_altitude);break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_DOWN):target_altitude -= 0.05;printf("target altitude: %f [m]\n", target_altitude);break;}

 默认,每次点击按键给出的控制量如上所示,需要修改可以适当调整数值。


当然啦,可以使用OpenCV和OpenAI等进行编程,实现自主飞行。

参考文献,使用ROS加入更多复杂功能,比如SLAM等。留作思考题吧。


 https://github.com/omichel/webots/tree/revision/projects/robots/dji/mavic

/** Copyright 1996-2019 Cyberbotics Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*//** Description:  Simplistic drone control:* - Stabilize the robot using the embedded sensors.* - Use PID technique to stabilize the drone roll/pitch/yaw.* - Use a cubic function applied on the vertical difference to stabilize the robot vertically.* - Stabilize the camera.* - Control the robot using the computer keyboard.*/#include <math.h>
#include <stdio.h>
#include <stdlib.h>#include <webots/robot.h>#include <webots/camera.h>
#include <webots/compass.h>
#include <webots/gps.h>
#include <webots/gyro.h>
#include <webots/inertial_unit.h>
#include <webots/keyboard.h>
#include <webots/led.h>
#include <webots/motor.h>#define SIGN(x) ((x) > 0) - ((x) < 0)
#define CLAMP(value, low, high) ((value) < (low) ? (low) : ((value) > (high) ? (high) : (value)))int main(int argc, char **argv) {wb_robot_init();int timestep = (int)wb_robot_get_basic_time_step();// Get and enable devices.WbDeviceTag camera = wb_robot_get_device("camera");wb_camera_enable(camera, timestep);WbDeviceTag front_left_led = wb_robot_get_device("front left led");WbDeviceTag front_right_led = wb_robot_get_device("front right led");WbDeviceTag imu = wb_robot_get_device("inertial unit");wb_inertial_unit_enable(imu, timestep);WbDeviceTag gps = wb_robot_get_device("gps");wb_gps_enable(gps, timestep);WbDeviceTag compass = wb_robot_get_device("compass");wb_compass_enable(compass, timestep);WbDeviceTag gyro = wb_robot_get_device("gyro");wb_gyro_enable(gyro, timestep);wb_keyboard_enable(timestep);WbDeviceTag camera_roll_motor = wb_robot_get_device("camera roll");WbDeviceTag camera_pitch_motor = wb_robot_get_device("camera pitch");// WbDeviceTag camera_yaw_motor = wb_robot_get_device("camera yaw");  // Not used in this example.// Get propeller motors and set them to velocity mode.WbDeviceTag front_left_motor = wb_robot_get_device("front left propeller");WbDeviceTag front_right_motor = wb_robot_get_device("front right propeller");WbDeviceTag rear_left_motor = wb_robot_get_device("rear left propeller");WbDeviceTag rear_right_motor = wb_robot_get_device("rear right propeller");WbDeviceTag motors[4] = {front_left_motor, front_right_motor, rear_left_motor, rear_right_motor};int m;for (m = 0; m < 4; ++m) {wb_motor_set_position(motors[m], INFINITY);wb_motor_set_velocity(motors[m], 1.0);}// Display the welcome message.printf("Start the drone...\n");// Wait one second.while (wb_robot_step(timestep) != -1) {if (wb_robot_get_time() > 1.0)break;}// Display manual control message.printf("You can control the drone with your computer keyboard:\n");printf("- 'up': move forward.\n");printf("- 'down': move backward.\n");printf("- 'right': turn right.\n");printf("- 'left': turn left.\n");printf("- 'shift + up': increase the target altitude.\n");printf("- 'shift + down': decrease the target altitude.\n");printf("- 'shift + right': strafe right.\n");printf("- 'shift + left': strafe left.\n");// Constants, empirically found.const double k_vertical_thrust = 68.5;  // with this thrust, the drone lifts.const double k_vertical_offset = 0.6;   // Vertical offset where the robot actually targets to stabilize itself.const double k_vertical_p = 3.0;        // P constant of the vertical PID.const double k_roll_p = 50.0;           // P constant of the roll PID.const double k_pitch_p = 30.0;          // P constant of the pitch PID.// Variables.double target_altitude = 1.0;  // The target altitude. Can be changed by the user.// Main loopwhile (wb_robot_step(timestep) != -1) {const double time = wb_robot_get_time();  // in seconds.// Retrieve robot position using the sensors.const double roll = wb_inertial_unit_get_roll_pitch_yaw(imu)[0] + M_PI / 2.0;const double pitch = wb_inertial_unit_get_roll_pitch_yaw(imu)[1];const double altitude = wb_gps_get_values(gps)[1];const double roll_acceleration = wb_gyro_get_values(gyro)[0];const double pitch_acceleration = wb_gyro_get_values(gyro)[1];// Blink the front LEDs alternatively with a 1 second rate.const bool led_state = ((int)time) % 2;wb_led_set(front_left_led, led_state);wb_led_set(front_right_led, !led_state);// Stabilize the Camera by actuating the camera motors according to the gyro feedback.wb_motor_set_position(camera_roll_motor, -0.115 * roll_acceleration);wb_motor_set_position(camera_pitch_motor, -0.1 * pitch_acceleration);// Transform the keyboard input to disturbances on the stabilization algorithm.double roll_disturbance = 0.0;double pitch_disturbance = 0.0;double yaw_disturbance = 0.0;int key = wb_keyboard_get_key();while (key > 0) {switch (key) {case WB_KEYBOARD_UP:pitch_disturbance = 2.0;break;case WB_KEYBOARD_DOWN:pitch_disturbance = -2.0;break;case WB_KEYBOARD_RIGHT:yaw_disturbance = 1.3;break;case WB_KEYBOARD_LEFT:yaw_disturbance = -1.3;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_RIGHT):roll_disturbance = -1.0;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_LEFT):roll_disturbance = 1.0;break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_UP):target_altitude += 0.05;printf("target altitude: %f [m]\n", target_altitude);break;case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_DOWN):target_altitude -= 0.05;printf("target altitude: %f [m]\n", target_altitude);break;}key = wb_keyboard_get_key();}// Compute the roll, pitch, yaw and vertical inputs.const double roll_input = k_roll_p * CLAMP(roll, -1.0, 1.0) + roll_acceleration + roll_disturbance;const double pitch_input = k_pitch_p * CLAMP(pitch, -1.0, 1.0) - pitch_acceleration + pitch_disturbance;const double yaw_input = yaw_disturbance;const double clamped_difference_altitude = CLAMP(target_altitude - altitude + k_vertical_offset, -1.0, 1.0);const double vertical_input = k_vertical_p * pow(clamped_difference_altitude, 3.0);// Actuate the motors taking into consideration all the computed inputs.const double front_left_motor_input = k_vertical_thrust + vertical_input - roll_input - pitch_input + yaw_input;const double front_right_motor_input = k_vertical_thrust + vertical_input + roll_input - pitch_input - yaw_input;const double rear_left_motor_input = k_vertical_thrust + vertical_input - roll_input + pitch_input - yaw_input;const double rear_right_motor_input = k_vertical_thrust + vertical_input + roll_input + pitch_input + yaw_input;wb_motor_set_velocity(front_left_motor, front_left_motor_input);wb_motor_set_velocity(front_right_motor, -front_right_motor_input);wb_motor_set_velocity(rear_left_motor, -rear_left_motor_input);wb_motor_set_velocity(rear_right_motor, rear_right_motor_input);};wb_robot_cleanup();return EXIT_SUCCESS;
}

 

这篇关于Webots中使用大疆“御”2专业版-DJI-Mavic 2 Pro进行无人机仿真实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/391665

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

mac安装nvm(node.js)多版本管理实践步骤

《mac安装nvm(node.js)多版本管理实践步骤》:本文主要介绍mac安装nvm(node.js)多版本管理的相关资料,NVM是一个用于管理多个Node.js版本的命令行工具,它允许开发者在... 目录NVM功能简介MAC安装实践一、下载nvm二、安装nvm三、安装node.js总结NVM功能简介N

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

Java使用Tesseract-OCR实战教程

《Java使用Tesseract-OCR实战教程》本文介绍了如何在Java中使用Tesseract-OCR进行文本提取,包括Tesseract-OCR的安装、中文训练库的配置、依赖库的引入以及具体的代... 目录Java使用Tesseract-OCRTesseract-OCR安装配置中文训练库引入依赖代码实