Невозможно получить значение датчика положения в объекте webots :: PositionSensor - PullRequest
0 голосов
/ 20 февраля 2020

Я пытаюсь получить позицию PositionSensor с помощью Webots. Я использую робота e-puck, и мой код ниже

#include <webots/Robot.hpp>
#include <webots/Motor.hpp>
#include <iostream>

#define TIME_STEP 64
#define MAX_SPEED 6.28

// All the webots classes are defined in the "webots" namespace
using namespace webots;

int main(int argc, char **argv) 
{
  Robot *robot = new Robot();

  Motor *leftMotor = robot->getMotor("left wheel motor");
  Motor *rightMotor = robot->getMotor("right wheel motor");
  PositionSensor *leftSensor = robot->getPositionSensor("left wheel sensor");

  leftMotor->setPosition(INFINITY);
  rightMotor->setPosition(INFINITY);


  leftMotor->setVelocity(MAX_SPEED);
  rightMotor->setVelocity(MAX_SPEED);

  while (robot->step(TIME_STEP) != -1) 
  {
    std::cout << leftSensor->getValue() << std::endl; 
  };

  delete robot;
  return 0;
}

Когда я пытаюсь построить этот код сверху, я получаю следующую ошибку:

TestController.cpp: In function ‘int main(int, char**)’:
TestController.cpp:38:28: error: invalid use of incomplete type ‘class webots::PositionSensor’
     std::cout << leftSensor->getValue() << std::endl;
                            ^~
In file included from TestController.cpp:1:0:
/usr/local/webots/include/controller/cpp/webots/Robot.hpp:42:9: note: forward declaration of ‘class webots::PositionSensor’
   class PositionSensor;
         ^~~~~~~~~~~~~~

Я не могу понять, почему пытаюсь получить значение, которое должно возвращать double в операторе print. Спасибо

Заголовок для класса PositionSensor ниже

// Copyright 1996-2020 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.

#ifndef POSITION_SENSOR_HPP
#define POSITION_SENSOR_HPP

#include <webots/Device.hpp>

namespace webots {
  class Brake;
  class Motor;

  class PositionSensor : public Device {
  public:
    typedef enum { ROTATIONAL = 0, LINEAR } Type;

    explicit PositionSensor(const std::string &name) :
      Device(name),
      brake(NULL),
      motor(NULL) {}  // Use Robot::getPositionSensor() instead
    virtual ~PositionSensor() {}
    virtual void enable(int samplingPeriod);  // milliseconds
    virtual void disable();
    int getSamplingPeriod() const;
    double getValue() const;  // rad or meters
    Type getType() const;

    Brake *getBrake();
    Motor *getMotor();

    // internal functions
    int getBrakeTag() const;
    int getMotorTag() const;

    enum {  // kept for backward compatibility R2018b
      ANGULAR = 0
    };

  private:
    Brake *brake;
    Motor *motor;
  };
}  // namespace webots

#endif  // POSITION_SENSOR_HPP

1 Ответ

1 голос
/ 20 февраля 2020

В вашем коде есть несколько проблем:

  1. Вы не добавляете датчики положения: #include <webots/PositionSensor.hpp
  2. Вам необходимо включить датчик: leftSensor->enable(TIME_STEP);

Вот как должен выглядеть ваш код:

#include <webots/Robot.hpp>
#include <webots/Motor.hpp>
#include <webots/PositionSensor.hpp
#include <iostream>

#define TIME_STEP 64
#define MAX_SPEED 6.28

// All the webots classes are defined in the "webots" namespace
using namespace webots;

int main(int argc, char **argv) 
{
  Robot *robot = new Robot();

  Motor *leftMotor = robot->getMotor("left wheel motor");
  Motor *rightMotor = robot->getMotor("right wheel motor");
  PositionSensor *leftSensor = robot->getPositionSensor("left wheel sensor");

  leftSensor->enable(TIME_STEP);

  leftMotor->setPosition(INFINITY);
  rightMotor->setPosition(INFINITY);


  leftMotor->setVelocity(MAX_SPEED);
  rightMotor->setVelocity(MAX_SPEED);

  while (robot->step(TIME_STEP) != -1) 
  {
    std::cout << leftSensor->getValue() << std::endl; 
  };

  delete robot;
  return 0;
}

Я бы настоятельно рекомендовал следовать руководству по программированию роботов: в Webots: https://cyberbotics.com/doc/guide/tutorial-4-more-about-controllers?tab-language=c++

...