forked from felipenmartins/Robotics-Simulation-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_following_behavior.py
118 lines (91 loc) · 3.32 KB
/
line_following_behavior.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""line_following_behavior controller."""
# This program implements a state-machine based line-following behavior
# for the e-puck robot.
# Author: Felipe N. Martins
# Date: 7th of April, 2020
# Update: 17 September 2021 - add comments and adjust variable names
from controller import Robot, DistanceSensor, Motor
import numpy as np
#-------------------------------------------------------
# Initialize variables
TIME_STEP = 64
MAX_SPEED = 6.28
# 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.getDevice(psNames[i]))
ps[i].enable(timestep)
# ground sensors
gs = []
gsNames = ['gs0', 'gs1', 'gs2']
for i in range(3):
gs.append(robot.getDevice(gsNames[i]))
gs[i].enable(timestep)
# motors
leftMotor = robot.getDevice('left wheel motor')
rightMotor = robot.getDevice('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())
# 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: update speed variables
leftSpeed = MAX_SPEED
rightSpeed = MAX_SPEED
# check if it is necessary to update current_state
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: update speed variables
leftSpeed = 0.8 * MAX_SPEED
rightSpeed = 0.4 * MAX_SPEED
# check if it is necessary to update current_state
if counter == COUNTER_MAX:
current_state = 'forward'
if current_state == 'turn_left':
# Action for the current state: update speed variables
leftSpeed = 0.4 * MAX_SPEED
rightSpeed = 0.8 * MAX_SPEED
# check if it is necessary to update current_state
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)
# Set motor speeds with the values defined by the state-machine
leftMotor.setVelocity(leftSpeed)
rightMotor.setVelocity(rightSpeed)
# Repeat all steps while the simulation is running.