1
$\begingroup$

In my Webots simulation, I am trying to use a supervisor in order to get parameters from other nodes to then have a motor respond by increasing or decreasing its torque, to which I have written the code below:

#include <webots/Supervisor.hpp>
#include <webots/Field.hpp>
#include <webots/Motor.hpp>
#include <webots/Robot.hpp>

#define TIME_STEP 32

using namespace webots;

int main() {
  Supervisor *supervisor = new Supervisor();
  Motor *wheel[1];
  Robot *robot = new Robot();

    // do this once only
  Node *mass = supervisor->getFromDef("Mass");
  Node *motor = supervisor->getFromDef("Motor1");
  
  if (mass == NULL) {
    std::cerr << "No DEF MY_ROBOT node found in the current world file" << std::endl;
    exit(1);
  }
  Field *trans_field = mass->getField("translation");
  Field *mass_field = mass->getField("mass");
  wheel[1] = robot->getMotor("Motor1");
  wheel[1]->setVelocity(1);
  
  while (supervisor->step(TIME_STEP) != -1) {
    // this is done repeatedly
    const double *values = trans_field->getSFVec3f();
    double mass = mass_field->getSFFloat();

    double radius = values[1];
    double newtons = mass*9.8;
    double torque = newtons *radius;
    wheel[1]->setTorque(torque);
    wheel[1]->setPosition(1.57); 
  }

  delete supervisor;
  return 0;
}

However, when I run it I end up getting an error like this:

Only one instance of the Robot class should be created
WARNING: 'Supervisor' controller exited with status: -1.

Can someone please explain what is going on?

$\endgroup$

1 Answer 1

0
$\begingroup$

As it is explained by the error message you can only have one instance of the Robot class for each controller program.

In your code you are instantiating two instances of Robot class because the Supervisor class inherits from Robot.

In your case, if you need the Supervisor functionalities you should create directly an instance of the Supervisor class and remove the Robot instance.

The resulting code will simply be like the following and you can call both Robot and Supervisor API function from the Supervisor instance:

...
int main() {
  Supervisor *robot = new Supervisor();
  Motor *wheel[1];
  // Robot *robot = new Robot(); // this can be removed
  ..
  // do this once only
  Node *mass = robot->getFromDef("Mass");
  Node *motor = robot->getFromDef("Motor1");
  ...
  wheel[1] = robot->getMotor("Motor1");
  ..
}

Disclaimer: I am a Webots developer working at Cyberbotics.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.