c++ - 在 Webots C++ 中将速度设置为 DifferentialWheels 的问题

标签 c++ webots

这里的社区很小,但希望有人能看到。我正在尝试为电子冰球做 Webots 模拟的纯 C++ 实现。 C++ 文档非常缺乏,我似乎无法找到解决此问题的方法(C 实现非常出色,但所有函数调用都针对 C++ 进行了更改)。

本质上,我只是想启动并运行一个简单的应用程序...我想让 E-puck 向前发展。我将在下面发布我的全部代码...我所做的只是实例化一个 Robot 实体,打印出所有 IR 传感器值,并尝试将其向前推进。

问题是它不动。我认为会有一些调用将 DifferentialWheel 对象链接到 E-puck(类似于 camera = getCamera("camera") 调用)。

如果我注释掉对 setSpeed 的调用,程序将完美运行(不移动,但打印值)。如果我把它留在里面,一旦到达那个调用,模拟就会在一步后卡住。老实说,我不太确定自己做错了什么。

// webots
#include <webots/Robot.hpp>
#include <webots/Camera.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/DifferentialWheels.hpp>
#include <webots/LED.hpp>

// standard
#include <iostream>

using namespace webots;

#define TIME_STEP 16

class MyRobot : public Robot
{
  private:
    Camera *camera;
    DistanceSensor *distanceSensors[8];
    LED *leds[8];
    DifferentialWheels *diffWheels;

  public: 
    MyRobot() : Robot()
    {
      // camera
      camera             = getCamera("camera");

      // sensors
      distanceSensors[0] = getDistanceSensor("ps0");
      distanceSensors[1] = getDistanceSensor("ps1");
      distanceSensors[2] = getDistanceSensor("ps2");
      distanceSensors[3] = getDistanceSensor("ps3");
      distanceSensors[4] = getDistanceSensor("ps4");
      distanceSensors[5] = getDistanceSensor("ps5");
      distanceSensors[6] = getDistanceSensor("ps6");
      distanceSensors[7] = getDistanceSensor("ps7");
      for (unsigned int i = 0; i < 8; ++i)
        distanceSensors[i]->enable(TIME_STEP);

      // leds
      leds[0] = getLED("led0");
      leds[1] = getLED("led1");
      leds[2] = getLED("led2");
      leds[3] = getLED("led3");
      leds[4] = getLED("led4");
      leds[5] = getLED("led5");
      leds[6] = getLED("led6");
      leds[7] = getLED("led7");
    }

    virtual ~MyRobot()
    {
      // cleanup
    }

    void run()
    {
      double speed[2] = {20.0, 0.0};

      // main loop
      while (step(TIME_STEP) != -1)
      {
        // read sensor values
        for (unsigned int i = 0; i < 8; ++i)
          std::cout << " [" << distanceSensors[i]->getValue() << "]";
        std::cout << std::endl;

        // process data

        // send actuator commands
// this call kills the simulation
//        diffWheels->setSpeed(1000, 1000);

      }
    }
};

int main(int argc, char* argv[])
{

  MyRobot *robot = new MyRobot();
  robot->run();
  delete robot;
  return 0;
}

现在,如果这是 C 实现,我会调用 wb_differential_wheels_set_speed(1000, 1000); 但是,该调用在 C++ 头文件中不可用。

最佳答案

导致卡住的问题是由于使用了未初始化的变量 diffWheels。 DifferentialWheels(以及 Robot 和 Supervisor)不需要初始化。

您必须将 MyRobot 类的基类更改为 DifferentialWheels

class MyRobot : public DifferentialWheels

然后简单地调用

setSpeed(1000, 1000)

不是

diffWheels->setSpeed(1000, 1000)

关于c++ - 在 Webots C++ 中将速度设置为 DifferentialWheels 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893629/

相关文章:

c++ - Webots ROS默认 Controller "RosInertialUnit.cpp"中的四元数计算

c++ - 如何拦截 I/O 端口访问以实现硬件虚拟化

c++ - 链表 const 迭代器

javascript - 向现有 QT 应用程序添加 javascript 支持

c++ - WEBOTS - 将 Controller 数据写入外部 CSV 文件

webots - 在 webots 中为机器人 Controller 添加延迟

c++ - 外部符号上的静态链接 OpenCV CUDA 应用程序错误

c++ - 树结构的泛型遍历

c++ - Webots Visual Studio 交叉编译

matlab - 连接 webbot 和 MATLAB