机器人工程专业实践镜像2021版-功能扩展-coppeliasim+webots

举报
zhangrelay 发表于 2021/10/17 22:36:00 2021/10/17
【摘要】 镜像虽然提供了大部分课程所需功能,但同样支持扩展。这里以两款仿真软件为例 coppeliasimwebots 其实就是在官网下载,解压到硬盘就可以使用的。 分别解压就行。 启动V-Rep(新版为coppeliasim) : ./vrep.sh 启动webots: ./webots  等待启动完成,即...

镜像虽然提供了大部分课程所需功能,但同样支持扩展。这里以两款仿真软件为例

  • coppeliasim
  • webots

其实就是在官网下载,解压到硬盘就可以使用的。

分别解压就行。

启动V-Rep(新版为coppeliasim) :

  • ./vrep.sh

启动webots:

  • ./webots

 等待启动完成,即可愉快玩耍。忽略更新。

 

缺少的功能包依据上学期课程讲解,或者依据提示补充安装即可。

现在打开一个cpp案例:

尝试一下编译:

完全可以正常使用。

 


  
  1. void Driver::displayHelp() {
  2. string s("Commands:\n"
  3. " 这只是一个测试^_^\n"
  4. " I for displaying the commands\n"
  5. " A for avoid obstacles\n"
  6. " F for move forward\n"
  7. " S for stop\n"
  8. " T for turn\n"
  9. " R for positioning ROBOT1 at (0.1,0.3)\n"
  10. " G for knowing the (x,z) position of ROBOT1");
  11. cout << s << endl;
  12. }

 

 

webots中C++的案例都可以直接编译后使用,非常方便。

更多案例自主学习即可。


  
  1. // Copyright 1996-2020 Cyberbotics Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. * Description: This controller gives to its node the following behavior:
  16. * Listen the keyboard. According to the pressed key, send a
  17. * message through an emitter or handle the position of Robot1
  18. */
  19. #include <webots/Emitter.hpp>
  20. #include <webots/Field.hpp>
  21. #include <webots/Keyboard.hpp>
  22. #include <webots/Node.hpp>
  23. #include <webots/Supervisor.hpp>
  24. #include <stdlib.h>
  25. #include <cstring>
  26. #include <iostream>
  27. #include <string>
  28. using namespace std;
  29. using namespace webots;
  30. class Driver : public Supervisor {
  31. public:
  32. Driver();
  33. void run();
  34. private:
  35. static void displayHelp();
  36. int timeStep;
  37. Emitter *emitter;
  38. Field *translationField;
  39. Keyboard *keyboard;
  40. double x;
  41. double z;
  42. double translation[3];
  43. };
  44. Driver::Driver() {
  45. timeStep = 128;
  46. x = 0.1f;
  47. z = 0.3f;
  48. translation[0] = x;
  49. translation[1] = 0;
  50. translation[2] = z;
  51. emitter = getEmitter("emitter");
  52. Node *robot = getFromDef("ROBOT1");
  53. if (!robot)
  54. // robot might be NULL if the controller is about to quit
  55. exit(1);
  56. translationField = robot->getField("translation");
  57. keyboard = getKeyboard();
  58. keyboard->enable(timeStep);
  59. }
  60. void Driver::run() {
  61. string previous_message("");
  62. string message("");
  63. displayHelp();
  64. // main loop
  65. while (step(timeStep) != -1) {
  66. // Read sensors; update message according to the pressed keyboard key
  67. int k = keyboard->getKey();
  68. switch (k) {
  69. case 'A':
  70. message.assign("avoid obstacles");
  71. break;
  72. case 'F':
  73. message.assign("move forward");
  74. break;
  75. case 'S':
  76. message.assign("stop");
  77. break;
  78. case 'T':
  79. message.assign("turn");
  80. break;
  81. case 'I':
  82. displayHelp();
  83. break;
  84. case 'G': {
  85. const double *translationValues = translationField->getSFVec3f();
  86. cout << "ROBOT1 is located at (" << translationValues[0] << "," << translationValues[2] << ")" << endl;
  87. break;
  88. }
  89. case 'R':
  90. cout << "Teleport ROBOT1 at (" << x << "," << z << ")" << endl;
  91. translationField->setSFVec3f(translation);
  92. break;
  93. default:
  94. message.clear();
  95. }
  96. // send actuators commands; send a new message through the emitter device
  97. if (!message.empty() && message.compare(previous_message)) {
  98. previous_message.assign(message);
  99. cout << "Please, " << message.c_str() << endl;
  100. emitter->send(message.c_str(), (int)strlen(message.c_str()) + 1);
  101. }
  102. }
  103. }
  104. void Driver::displayHelp() {
  105. string s("Commands:\n"
  106. " 这只是一个测试^_^\n"
  107. " I for displaying the commands\n"
  108. " A for avoid obstacles\n"
  109. " F for move forward\n"
  110. " S for stop\n"
  111. " T for turn\n"
  112. " R for positioning ROBOT1 at (0.1,0.3)\n"
  113. " G for knowing the (x,z) position of ROBOT1");
  114. cout << s << endl;
  115. }
  116. int main() {
  117. Driver *controller = new Driver();
  118. controller->run();
  119. delete controller;
  120. return 0;
  121. }

  
  1. // Copyright 1996-2020 Cyberbotics Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. * Description: This controller gives to its robot the following behavior:
  16. * According to the messages it receives, the robot change its
  17. * behavior.
  18. */
  19. #include <webots/Camera.hpp>
  20. #include <webots/DistanceSensor.hpp>
  21. #include <webots/Motor.hpp>
  22. #include <webots/Receiver.hpp>
  23. #include <webots/Robot.hpp>
  24. #include <webots/utils/AnsiCodes.hpp>
  25. #include <algorithm>
  26. #include <iostream>
  27. #include <limits>
  28. #include <string>
  29. using namespace std;
  30. using namespace webots;
  31. static const double maxSpeed = 10.0;
  32. class Slave : public Robot {
  33. public:
  34. Slave();
  35. void run();
  36. private:
  37. enum Mode { STOP, MOVE_FORWARD, AVOID_OBSTACLES, TURN };
  38. static double boundSpeed(double speed);
  39. int timeStep;
  40. Mode mode;
  41. Receiver *receiver;
  42. Camera *camera;
  43. DistanceSensor *distanceSensors[2];
  44. Motor *motors[2];
  45. };
  46. Slave::Slave() {
  47. timeStep = 32;
  48. mode = AVOID_OBSTACLES;
  49. camera = getCamera("camera");
  50. camera->enable(4 * timeStep);
  51. receiver = getReceiver("receiver");
  52. receiver->enable(timeStep);
  53. motors[0] = getMotor("left wheel motor");
  54. motors[1] = getMotor("right wheel motor");
  55. motors[0]->setPosition(std::numeric_limits<double>::infinity());
  56. motors[1]->setPosition(std::numeric_limits<double>::infinity());
  57. motors[0]->setVelocity(0.0);
  58. motors[1]->setVelocity(0.0);
  59. string distanceSensorNames("ds0");
  60. for (int i = 0; i < 2; i++) {
  61. distanceSensors[i] = getDistanceSensor(distanceSensorNames);
  62. distanceSensors[i]->enable(timeStep);
  63. distanceSensorNames[2]++; // for getting "ds1","ds2",...
  64. }
  65. }
  66. double Slave::boundSpeed(double speed) {
  67. return std::min(maxSpeed, std::max(-maxSpeed, speed));
  68. }
  69. void Slave::run() {
  70. // main loop
  71. while (step(timeStep) != -1) {
  72. // Read sensors, particularly the order of the supervisor
  73. if (receiver->getQueueLength() > 0) {
  74. string message((const char *)receiver->getData());
  75. receiver->nextPacket();
  76. cout << "I should " << AnsiCodes::RED_FOREGROUND << message << AnsiCodes::RESET << "!" << endl;
  77. if (message.compare("avoid obstacles") == 0)
  78. mode = AVOID_OBSTACLES;
  79. else if (message.compare("move forward") == 0)
  80. mode = MOVE_FORWARD;
  81. else if (message.compare("stop") == 0)
  82. mode = STOP;
  83. else if (message.compare("turn") == 0)
  84. mode = TURN;
  85. }
  86. double delta = distanceSensors[0]->getValue() - distanceSensors[1]->getValue();
  87. double speeds[2] = {0.0, 0.0};
  88. // send actuators commands according to the mode
  89. switch (mode) {
  90. case AVOID_OBSTACLES:
  91. speeds[0] = boundSpeed(maxSpeed / 2.0 + 0.1 * delta);
  92. speeds[1] = boundSpeed(maxSpeed / 2.0 - 0.1 * delta);
  93. break;
  94. case MOVE_FORWARD:
  95. speeds[0] = maxSpeed;
  96. speeds[1] = maxSpeed;
  97. break;
  98. case TURN:
  99. speeds[0] = maxSpeed / 2.0;
  100. speeds[1] = -maxSpeed / 2.0;
  101. break;
  102. default:
  103. break;
  104. }
  105. motors[0]->setVelocity(speeds[0]);
  106. motors[1]->setVelocity(speeds[1]);
  107. }
  108. }
  109. int main() {
  110. Slave *controller = new Slave();
  111. controller->run();
  112. delete controller;
  113. return 0;
  114. }

-End-


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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