Make the robot rotate a given angle in Webots

109 Views Asked by At

I am working on an implementation 'make the robot turn left for 45 degree' in webots with Python. My main thought is that let the robot keep spinning until the current rotation is 45 degree. This is my code relative the 'rotation' implementation:

def turn_left(angle):
    l_speed = 1.0
    r_speed = -1.0
    leftMotor.setVelocity(l_speed)
    rightMotor.setVelocity(r_speed)
    
    while robot.step(timestep) != -1:
        if robot.getFromDef('agent').getField('rotation').getSFRotation[3] >= 3.14159 / 4:
            leftMotor.setVelocity(0.0)
            rightMotor.setVelocity(0.0)
        break

def actions(n):
    l_speed = 0.0
    r_speed = 0.0
    action = n
    print("action: ", action)
    if action == 0:
        move_forward(0.1)
    elif action == 1:
        turn_left(math.pi/4)
 

    return l_speed, r_speed

l, r = actions(1)

Just as all of the newers using Webots for the first time, my attempt was feedbacked with an error notice:

AttributeError: 'Robot' object has no attribute 'getFromDef'

This notice means that I used the error class to call getFromDef function. But because this is my first time to use Webots, I really do not know more details about the relationship between the funcions except for several after I read the API document. For my final purpose of "turn left for 45 degree", I got two questions: Firstly, if my thought is not correct, are there any better solutions? Secondly, if my thought is suitable, how can I extract the current rotation information of my robot agent not by clicking the menu in the left of my screen but by the code? Thank you for any help or advice.

0

There are 0 best solutions below