2012-08-09 84 views
1

这里的小社区,但希望有人看到这一点。我正在尝试为E-puck做一个纯粹的C++实现。 C++文档非常缺乏,而且我似乎无法找到解决此问题的办法(C实现非常好,但所有函数调用都针对C++进行了更改)。在Webots C++中将速度设置为DifferentialWheels的问题

基本上,我只是试图让一个简单的应用程序运行起来......我想让电子冰球前进。我会在下面发布我的代码的全部内容......我所做的只是实例化一个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++头文件可用。

回答

1

它似乎并不像你初始化diffWheels,所以我想你会得到一个段错误来解引用一个垃圾指针。试穿

diffWheels = new DifferentialWheels; 

在构造函数MyRobot

+0

那......会做。谢谢! – espais 2012-08-10 00:53:37

2

导致冻结的问题是由于使用了未初始化的变量diffWheels。差速轮(以及机器人和主管)不需要初始化。

你有基类的MyRobot类改为DifferentialWheels

class MyRobot : public DifferentialWheels 

,然后只需拨打

setSpeed(1000, 1000) 

,而不是

diffWheels->setSpeed(1000, 1000)