文章目录
hello world
from controller import Robot
robot = Robot()
while robot.step(32) != -1:
print("Hello World!")
在这个“Hello World!”中 例如,while循环的退出条件是函数的返回值wb_robot_step。-1当 Webots 终止控制器时,这个函数确实会返回(参见控制器终止)。
读数传感器
示例会不断更新并打印DistanceSensor返回的值:
from controller import Robot, DistanceSensor
TIME_STEP = 32
robot = Robot()
sensor = robot.getDevice("my_distance_sensor")
sensor.enable(TIME_STEP)
while robot.step(TIME_STEP) != -1:
value = sensor.getValue()
print("Sensor value is: ", value)
GPS.getValues()
Accelerometer.getValues()
Gyro.getValues()
values = gps.getValues()
# OK, to read the values they should never be explicitly deleted by the controller code
print("MY_ROBOT is at position: %g %g %g" % (values[0], values[1], values[2]