-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRobot.py
106 lines (78 loc) · 2.63 KB
/
Robot.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
from controller import Robot, DistanceSensor, GPS, Camera, Motor
# import cv2 as cv
import numpy as np
from math import pi
robot = Robot() # Robot object
timestep = int(robot.getBasicTimeStep())
MAX_VELOCITY = 6.28
# defining Distnace sensors
FDS = robot.getDevice("FrontDistance") # Front distance sensor
RDS = robot.getDevice("RightDistance") # Right distance sensor
LDS = robot.getDevice("LeftDistance") # Left distnace sensor
# defining Location sensor
location = robot.getDevice("GpsSensor") # Location for GPS sensor'
# defining Color sensor
colorSensor = robot.getDevice("Color") # Color Sensor
# defining Camera sensors
FCamera = robot.getDevice("FrontCamera")
RCamera = robot.getDevice("RightCamera")
LCamera = robot.getDevice("LeftCamera")
# defining Motor wheels
leftWheel = robot.getDevice("LeftWheel motor")
rightWheel = robot.getDevice("RightWheel motor")
# defining robot emitter
emitter = robot.getDevice("emitter")
# enabling Distance sensors
FDS.enable(timestep)
RDS.enable(timestep)
LDS.enable(timestep)
# enabling GPS sensor for location
location.enable(timestep)
# enabling Color Sensor
colorSensor.enable(timestep)
# enabling Camera sensors
FCamera.enable(timestep)
RCamera.enable(timestep)
LCamera.enable(timestep)
# Setting the position and velocity for the wheels
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
leftWheel.setVelocity(0.0)
rightWheel.setVelocity(0.0)
# defining FUNCTIONS
def delay(ms):
initTime = robot.getTime()
while robot.step(timestep) != -1:
if(robot.getTime() - initTime) * 1000.0 > ms:
break # break when time finished
def get_color():
image = colorSensor.getImage() # Retrieve the image frame
return colorSensor.imageGetGray(image, colorSensor.getWidth(), 0, 0) # return gray converted image
def turn(angle, colorCoeff):
leftWheel.setVelocity(-0.6 * MAX_VELOCITY)
rightWheel.setVelocity(0.6 * MAX_VELOCITY)
delay(colorCoeff * 595)
leftWheel.setVelocity(0)
rightWheel.setVelocity(0)
delay(int(600/colorCoeff))
colorCoeff = {
"yellow" : 2,
"black" : 0,
"others" : 1
}
# Main while loop for the robot
while robot.step(timestep) != -1:
# Distnace sensors Value
fd = FDS.getValue()
rd = RDS.getValue()
ld = LDS.getValue()
# Location Values
x = location.getValues()[0]
y = location.getValues()[1]
z = location.getValues()[2]
# Color sensor values
image = colorSensor.getImage() # Retrieve the image frame
r = colorSensor.imageGetRed(image, 1, 0, 0)
g = colorSensor.imageGetGreen(image, 1, 0, 0)
b = colorSensor.imageGetBlue(image, 1, 0, 0)
turn(90, colorCoeff["others"])