无法在 webots::PositionSensor object 中获取位置传感器的值
Cannot get the value of the position sensor in webots::PositionSensor object
我正在尝试使用 Webot 获取 PositionSensor 的位置。我正在使用 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;
^~~~~~~~~~~~~~
我不明白为什么要在 print 语句中获取应该 return 双精度的值。谢谢
PositionSensor class 的 header 低于
// 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
您的代码中存在几个问题:
- 您没有添加位置传感器包括:
#include <webots/PositionSensor.hpp
- 您需要启用传感器:
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++
我正在尝试使用 Webot 获取 PositionSensor 的位置。我正在使用 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;
^~~~~~~~~~~~~~
我不明白为什么要在 print 语句中获取应该 return 双精度的值。谢谢
PositionSensor class 的 header 低于
// 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
您的代码中存在几个问题:
- 您没有添加位置传感器包括:
#include <webots/PositionSensor.hpp
- 您需要启用传感器:
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++