Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

webots epuck line follower code

"""line_following_behavior controller."""


from controller import Robot, DistanceSensor, Motor
import numpy as np

#-------------------------------------------------------
# Initialize variables

TIME_STEP = 64
MAX_SPEED = 6.28

speed = 1 * MAX_SPEED

# create the Robot instance.
robot = Robot()

# get the time step of the current world.
timestep = int(robot.getBasicTimeStep())   # [ms]

# states
states = ['forward', 'turn_right', 'turn_left']
current_state = states[0]

# counter: used to maintain an active state for a number of cycles
counter = 0
counter_max = 5

#-------------------------------------------------------
# Initialize devices

# distance sensors
ps = []
psNames = ['ps0', 'ps1', 'ps2', 'ps3', 'ps4', 'ps5', 'ps6', 'ps7']
for i in range(8):
    ps.append(robot.getDistanceSensor(psNames[i]))
    ps[i].enable(timestep)

# ground sensors
gs = []
gsNames = ['gs0', 'gs1', 'gs2']
for i in range(3):
    gs.append(robot.getDistanceSensor(gsNames[i]))
    gs[i].enable(timestep)


# motors    
leftMotor = robot.getMotor('left wheel motor')
rightMotor = robot.getMotor('right wheel motor')
leftMotor.setPosition(float('inf'))
rightMotor.setPosition(float('inf'))
leftMotor.setVelocity(0.0)
rightMotor.setVelocity(0.0)


#-------------------------------------------------------
# Main loop:
# - perform simulation steps until Webots is stopping the controller
while robot.step(timestep) != -1:
    # Update sensor readings
    psValues = []
    for i in range(8):
        psValues.append(ps[i].getValue())

    gsValues = []
    for i in range(3):
        gsValues.append(gs[i].getValue())

    # detect obstacles
    right_obstacle = psValues[0] > 80.0 or psValues[1] > 80.0 or psValues[2] > 80.0
    left_obstacle = psValues[5] > 80.0 or psValues[6] > 80.0 or psValues[7] > 80.0

    # initialize motor speeds at 50% of MAX_SPEED.
    leftSpeed  = speed
    rightSpeed = speed
    # modify speeds according to obstacles
    if left_obstacle:
        # turn right
        leftSpeed  = speed
        rightSpeed = -speed
    elif right_obstacle:
        # turn left
        leftSpeed  = -speed
        rightSpeed = speed
    
    

    # Process sensor data
    line_right = gsValues[0] > 600
    line_left = gsValues[2] > 600

    # Implement the line-following state machine
    if current_state == 'forward':
        # Action for the current state
        leftSpeed = speed
        rightSpeed = speed
        # update current state if necessary
        if line_right and not line_left:
            current_state = 'turn_right'
            counter = 0
        elif line_left and not line_right:
            current_state = 'turn_left'
            counter = 0
            
    if current_state == 'turn_right':
        # Action for the current state
        leftSpeed = 0.8 * speed
        rightSpeed = 0.4 * speed
        # update current state if necessary
        if counter == counter_max:
            current_state = 'forward'

    if current_state == 'turn_left':
        # Action for the current state
        leftSpeed = 0.4 * speed
        rightSpeed = 0.8 * speed
        # update current state if necessary
        if counter == counter_max:
            current_state = 'forward'        

    # increment counter
    counter += 1
    
    #print('Counter: '+ str(counter), gsValues[0], gsValues[1], gsValues[2])
    print('Counter: '+ str(counter) + '. Current state: ' + current_state)

    # Update reference velocities for the motors
    leftMotor.setVelocity(leftSpeed)
    rightMotor.setVelocity(rightSpeed)
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript inline switch 
Typescript :: how to get ppt screen shots from a video using python :: keyframes 
Typescript :: Modify the program so it also prints the number of A, T, C, and G characters in the sequence in python 
Typescript :: a file consists of mcq 
Typescript :: top 100 employers in the united states 
Typescript :: Adding API request 
Typescript :: powershell copy contents of keyvault to another keyvault 
Typescript :: angular8 PrimeNg tabview 
Typescript :: how to find matching brackets in eclipse 
Typescript :: gravitate a particle to another 
Typescript :: .htaccess Preventing requests with invalid characters 
Typescript :: Job for pm2-rfb.service failed because the service did not take the steps required by its unit configuration. 
Typescript :: accessing python dictionary values with dot 
Typescript :: call appply bind 
Typescript :: whats app link target blank 
Typescript :: whcih commands lets you an ip adress log 
Typescript :: git remove two commits but not the code 
Typescript :: kingthings tryperwriter fonts premier 
Typescript :: can check constraints reference other tables 
Typescript :: Highcharts error #17: www.highcharts.com/errors/17/?missingModuleFor=candlestick - missingModuleFor: candlestick 
Typescript :: find most similar words from a list python 
Typescript :: js Validating nested promises 
Typescript :: declare function iwth interface typescript 
Typescript :: typescript dictionary usestate 
Typescript :: gets syntax 
Typescript :: Environ 2.020.000 résultats (0,60 secondes) << Add Grepper Answer (a) Résultats de recherche Résultats Web 
Typescript :: typescript dynamic type 
Typescript :: Alert cannot operate on nodes the current scene inherits from 
Cpp :: find largest number in vector c++ 
Cpp :: a c++ program to set a countdown timer 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =