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

举报
zhangrelay 发表于 2021/07/15 02:47:53 2021/07/15
【摘要】 兼顾体积和性能的超强无人机大疆-“御”-DJI-Mavic现在有了Webots仿真版,可以零成本愉快玩耍了。 Webots-Mavic 控制器支持C++、Python,并全面支持ROS。 DJI-Tello:https://blog.csdn.net/ZhangRelay/article/details/99291395 Webots-ROS...

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

控制器支持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:

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

这时候会加载如下环境:

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

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


  
  1. // Constants, empirically found.
  2. const double k_vertical_thrust = 68.5; // with this thrust, the drone lifts.
  3. const double k_vertical_offset = 0.6; // Vertical offset where the robot actually targets to stabilize itself.
  4. const double k_vertical_p = 3.0; // P constant of the vertical PID.
  5. const double k_roll_p = 50.0; // P constant of the roll PID.
  6. const double k_pitch_p = 30.0; // P constant of the pitch PID.
  7. // Variables.
  8. double target_altitude = 1.0; // The target altitude. Can be changed by the user.

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


  
  1. switch (key) {
  2. case WB_KEYBOARD_UP:
  3. pitch_disturbance = 2.0;
  4. break;
  5. case WB_KEYBOARD_DOWN:
  6. pitch_disturbance = -2.0;
  7. break;
  8. case WB_KEYBOARD_RIGHT:
  9. yaw_disturbance = 1.3;
  10. break;
  11. case WB_KEYBOARD_LEFT:
  12. yaw_disturbance = -1.3;
  13. break;
  14. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_RIGHT):
  15. roll_disturbance = -1.0;
  16. break;
  17. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_LEFT):
  18. roll_disturbance = 1.0;
  19. break;
  20. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_UP):
  21. target_altitude += 0.05;
  22. printf("target altitude: %f [m]\n", target_altitude);
  23. break;
  24. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_DOWN):
  25. target_altitude -= 0.05;
  26. printf("target altitude: %f [m]\n", target_altitude);
  27. break;
  28. }

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


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

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


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


  
  1. /*
  2. * Copyright 1996-2019 Cyberbotics Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Description: Simplistic drone control:
  18. * - Stabilize the robot using the embedded sensors.
  19. * - Use PID technique to stabilize the drone roll/pitch/yaw.
  20. * - Use a cubic function applied on the vertical difference to stabilize the robot vertically.
  21. * - Stabilize the camera.
  22. * - Control the robot using the computer keyboard.
  23. */
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <webots/robot.h>
  28. #include <webots/camera.h>
  29. #include <webots/compass.h>
  30. #include <webots/gps.h>
  31. #include <webots/gyro.h>
  32. #include <webots/inertial_unit.h>
  33. #include <webots/keyboard.h>
  34. #include <webots/led.h>
  35. #include <webots/motor.h>
  36. #define SIGN(x) ((x) > 0) - ((x) < 0)
  37. #define CLAMP(value, low, high) ((value) < (low) ? (low) : ((value) > (high) ? (high) : (value)))
  38. int main(int argc, char **argv) {
  39. wb_robot_init();
  40. int timestep = (int)wb_robot_get_basic_time_step();
  41. // Get and enable devices.
  42. WbDeviceTag camera = wb_robot_get_device("camera");
  43. wb_camera_enable(camera, timestep);
  44. WbDeviceTag front_left_led = wb_robot_get_device("front left led");
  45. WbDeviceTag front_right_led = wb_robot_get_device("front right led");
  46. WbDeviceTag imu = wb_robot_get_device("inertial unit");
  47. wb_inertial_unit_enable(imu, timestep);
  48. WbDeviceTag gps = wb_robot_get_device("gps");
  49. wb_gps_enable(gps, timestep);
  50. WbDeviceTag compass = wb_robot_get_device("compass");
  51. wb_compass_enable(compass, timestep);
  52. WbDeviceTag gyro = wb_robot_get_device("gyro");
  53. wb_gyro_enable(gyro, timestep);
  54. wb_keyboard_enable(timestep);
  55. WbDeviceTag camera_roll_motor = wb_robot_get_device("camera roll");
  56. WbDeviceTag camera_pitch_motor = wb_robot_get_device("camera pitch");
  57. // WbDeviceTag camera_yaw_motor = wb_robot_get_device("camera yaw"); // Not used in this example.
  58. // Get propeller motors and set them to velocity mode.
  59. WbDeviceTag front_left_motor = wb_robot_get_device("front left propeller");
  60. WbDeviceTag front_right_motor = wb_robot_get_device("front right propeller");
  61. WbDeviceTag rear_left_motor = wb_robot_get_device("rear left propeller");
  62. WbDeviceTag rear_right_motor = wb_robot_get_device("rear right propeller");
  63. WbDeviceTag motors[4] = {front_left_motor, front_right_motor, rear_left_motor, rear_right_motor};
  64. int m;
  65. for (m = 0; m < 4; ++m) {
  66. wb_motor_set_position(motors[m], INFINITY);
  67. wb_motor_set_velocity(motors[m], 1.0);
  68. }
  69. // Display the welcome message.
  70. printf("Start the drone...\n");
  71. // Wait one second.
  72. while (wb_robot_step(timestep) != -1) {
  73. if (wb_robot_get_time() > 1.0)
  74. break;
  75. }
  76. // Display manual control message.
  77. printf("You can control the drone with your computer keyboard:\n");
  78. printf("- 'up': move forward.\n");
  79. printf("- 'down': move backward.\n");
  80. printf("- 'right': turn right.\n");
  81. printf("- 'left': turn left.\n");
  82. printf("- 'shift + up': increase the target altitude.\n");
  83. printf("- 'shift + down': decrease the target altitude.\n");
  84. printf("- 'shift + right': strafe right.\n");
  85. printf("- 'shift + left': strafe left.\n");
  86. // Constants, empirically found.
  87. const double k_vertical_thrust = 68.5; // with this thrust, the drone lifts.
  88. const double k_vertical_offset = 0.6; // Vertical offset where the robot actually targets to stabilize itself.
  89. const double k_vertical_p = 3.0; // P constant of the vertical PID.
  90. const double k_roll_p = 50.0; // P constant of the roll PID.
  91. const double k_pitch_p = 30.0; // P constant of the pitch PID.
  92. // Variables.
  93. double target_altitude = 1.0; // The target altitude. Can be changed by the user.
  94. // Main loop
  95. while (wb_robot_step(timestep) != -1) {
  96. const double time = wb_robot_get_time(); // in seconds.
  97. // Retrieve robot position using the sensors.
  98. const double roll = wb_inertial_unit_get_roll_pitch_yaw(imu)[0] + M_PI / 2.0;
  99. const double pitch = wb_inertial_unit_get_roll_pitch_yaw(imu)[1];
  100. const double altitude = wb_gps_get_values(gps)[1];
  101. const double roll_acceleration = wb_gyro_get_values(gyro)[0];
  102. const double pitch_acceleration = wb_gyro_get_values(gyro)[1];
  103. // Blink the front LEDs alternatively with a 1 second rate.
  104. const bool led_state = ((int)time) % 2;
  105. wb_led_set(front_left_led, led_state);
  106. wb_led_set(front_right_led, !led_state);
  107. // Stabilize the Camera by actuating the camera motors according to the gyro feedback.
  108. wb_motor_set_position(camera_roll_motor, -0.115 * roll_acceleration);
  109. wb_motor_set_position(camera_pitch_motor, -0.1 * pitch_acceleration);
  110. // Transform the keyboard input to disturbances on the stabilization algorithm.
  111. double roll_disturbance = 0.0;
  112. double pitch_disturbance = 0.0;
  113. double yaw_disturbance = 0.0;
  114. int key = wb_keyboard_get_key();
  115. while (key > 0) {
  116. switch (key) {
  117. case WB_KEYBOARD_UP:
  118. pitch_disturbance = 2.0;
  119. break;
  120. case WB_KEYBOARD_DOWN:
  121. pitch_disturbance = -2.0;
  122. break;
  123. case WB_KEYBOARD_RIGHT:
  124. yaw_disturbance = 1.3;
  125. break;
  126. case WB_KEYBOARD_LEFT:
  127. yaw_disturbance = -1.3;
  128. break;
  129. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_RIGHT):
  130. roll_disturbance = -1.0;
  131. break;
  132. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_LEFT):
  133. roll_disturbance = 1.0;
  134. break;
  135. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_UP):
  136. target_altitude += 0.05;
  137. printf("target altitude: %f [m]\n", target_altitude);
  138. break;
  139. case (WB_KEYBOARD_SHIFT + WB_KEYBOARD_DOWN):
  140. target_altitude -= 0.05;
  141. printf("target altitude: %f [m]\n", target_altitude);
  142. break;
  143. }
  144. key = wb_keyboard_get_key();
  145. }
  146. // Compute the roll, pitch, yaw and vertical inputs.
  147. const double roll_input = k_roll_p * CLAMP(roll, -1.0, 1.0) + roll_acceleration + roll_disturbance;
  148. const double pitch_input = k_pitch_p * CLAMP(pitch, -1.0, 1.0) - pitch_acceleration + pitch_disturbance;
  149. const double yaw_input = yaw_disturbance;
  150. const double clamped_difference_altitude = CLAMP(target_altitude - altitude + k_vertical_offset, -1.0, 1.0);
  151. const double vertical_input = k_vertical_p * pow(clamped_difference_altitude, 3.0);
  152. // Actuate the motors taking into consideration all the computed inputs.
  153. const double front_left_motor_input = k_vertical_thrust + vertical_input - roll_input - pitch_input + yaw_input;
  154. const double front_right_motor_input = k_vertical_thrust + vertical_input + roll_input - pitch_input - yaw_input;
  155. const double rear_left_motor_input = k_vertical_thrust + vertical_input - roll_input + pitch_input - yaw_input;
  156. const double rear_right_motor_input = k_vertical_thrust + vertical_input + roll_input + pitch_input + yaw_input;
  157. wb_motor_set_velocity(front_left_motor, front_left_motor_input);
  158. wb_motor_set_velocity(front_right_motor, -front_right_motor_input);
  159. wb_motor_set_velocity(rear_left_motor, -rear_left_motor_input);
  160. wb_motor_set_velocity(rear_right_motor, rear_right_motor_input);
  161. };
  162. wb_robot_cleanup();
  163. return EXIT_SUCCESS;
  164. }

 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/99670331

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。