found the error "only one instance of the Robot class should be created"

74 Views Asked by At

Intoduction I am working on a Webots project in which four E-puck robots are used that push box to target locations with the Python class for each robot"Class Environment, class robot followed by main control loop"

Code Snippet:

    _robot_instance = None
    _supervisor_instance = None
    def __init__(self, use_supervisor=False):
        self.use_supervisor = use_supervisor
        self.TIME_STEP = 32
        self.receiver = None
        self.webots_supervisor = None
        if use_supervisor:
            if Environment._supervisor_instance is None:
                Environment._supervisor_instance = Supervisor()
            self.webots_supervisor = Environment._supervisor_instance
            self.robot = self.webots_supervisor.getFromDef("agent1")
        else:
            if Environment._robot_instance is None:
                Environment._robot_instance = Robot()
            self.robot = Environment._robot_instance
   def reset(self):
        print(f"Supervisor: {self.webots_supervisor}")
        # Release existing Robot/Supervisor instance if it exists
        if self.use_supervisor:
            if Environment._supervisor_instance is not None:
                self.webots_supervisor = None
                self.robot = None
                del Environment._supervisor_instance
        else:
            if Environment._robot_instance is not None:
                self.robot = None
        # Create a new Robot/Supervisor instance
        if self.use_supervisor:
            Environment._supervisor_instance = Supervisor()
            self.webots_supervisor = Environment._supervisor_instance
            self.robot = self.webots_supervisor.getFromDef("agent1")
        else:
            if Environment._robot_instance is None:
                Environment._robot_instance = Robot()
            self.robot = Environment._robot_instance
        try:
            # Ensure that Supervisor instance is used when appropriate
            root = (
                self.webots_supervisor.getRoot()
                if self.use_supervisor
                else self. robot.getRoot()
            )
            self.print_node_names(root)
        except AttributeError as e:
            print(f"Error getting nodes: {e}")```
**Issue:**
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.
0

There are 0 best solutions below