cyberbotics / webots

Webots Robot Simulator
https://cyberbotics.com
Apache License 2.0
3.23k stars 1.7k forks source link

Supervisor return None object on 2023 version #6174

Closed kadirou31 closed 1 year ago

kadirou31 commented 1 year ago

Hi, i'm using Webots 2023a under Ubuntu 22 LST, I'm trying to use Supervisor based on the tutorial code, I'm getting this output:

traceback (most recent call last):
  File "C:\Users\Admin\Documents\my_project\controllers\my_controller_sup\my_controller_sup.py", line 10, in <module>
    trans_field = robot_node.getField("translation")
AttributeError: 'NoneType' object has no attribute 'getField'
WARNING: 'my_controller_sup' controller exited with status: 1.

i noticed that robot_node is None !! the used code is:

from math import sqrt
from controller import Supervisor

TIME_STEP = 32

supervisor = Supervisor()

# get handle to robot's translation field
robot_node = supervisor.getFromDef("e-puck")
trans_field = robot_node.getField("translation")

for a in range(0, 25):
    for b in range(0, 33):
        # evaluate robot during 60 seconds (simulation time)
        t = supervisor.getTime()
        while supervisor.getTime() - t < 60:

            # perform robot control according to a, b
            # (and possibly t) parameters.

            # controller termination
            if supervisor.step(TIME_STEP) == -1:
                quit()

        # compute travelled distance
        values = trans_field.getSFVec3f()
        dist = sqrt(values[0] * values[0] + values[2] * values[2])
        print("a=%d, b=%d -> dist=%g" % (a, b, dist))

        # reset robot position and physics
        INITIAL = [0, 0.5, 0]
        trans_field.setSFVec3f(INITIAL)
        robot_node.resetPhysics()
BenjaminDeleze commented 1 year ago

Hello, It works well on my side.

Please check:

kadirou31 commented 1 year ago

Hi, I tried it on Windows and Linux, supervisor is enabled on the robot. Could you please help to resolve this issue.

BenjaminDeleze commented 1 year ago

Can you post your world file (.wbt)?

kadirou31 commented 1 year ago

my_supervisor.zip

kadirou31 commented 1 year ago

the code from tutorial :

from controller import Supervisor

TIME_STEP = 32

robot = Supervisor()  # create Supervisor instance

# [CODE PLACEHOLDER 1]
bb8_node = robot.getFromDef('BB-8')
translation_field = bb8_node.getField('translation')

root_node = robot.getRoot()
children_field = root_node.getField('children')

children_field.importMFNodeFromString(-1, 'DEF BALL Ball { translation 0 1 1 }')
ball_node = robot.getFromDef('BALL')
color_field = ball_node.getField('color')

i = 0
while robot.step(TIME_STEP) != -1:
  # [CODE PLACEHOLDER 2]
  if (i == 0):
    new_value = [2.5, 0, 0]
    translation_field.setSFVec3f(new_value)

  if i == 10:
      bb8_node.remove()

  if i == 20:
    children_field.importMFNodeFromString(-1, 'Nao { }')

  position = ball_node.getPosition()
  print('Ball position: %f %f %f\n' %(position[0], position[1], position[2]))

  if position[2] < 0.2:
    red_color = [1, 0, 0]
    color_field.setSFColor(red_color)

  i += 1
BenjaminDeleze commented 1 year ago

There is no DEF in your world so it is normal that the getFromDef function returns null

kadirou31 commented 1 year ago

how to add DEF to the world

kadirou31 commented 1 year ago

thank you very much, the problem is solved, it was my fault I foroet to add a DEF, again thanks for your quick answers.