-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev3.py
201 lines (147 loc) · 5.3 KB
/
ev3.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env pybricks-micropython
"""
This script controls a robot using the Pybricks Micropython framework on an EV3 brick.
It utilizes motors for movement, a gyro sensor for orientation, and communicates sensor data over a network socket.
__
<(o )___
( ._> / @ikonodim
`---'
"""
import time
import _thread
import socket
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, GyroSensor
from pybricks.parameters import Port
# Initialize hardware components
EV3 = EV3Brick()
GYRO = GyroSensor(Port.S4)
MOTORS = [Motor(Port.A), Motor(Port.D)]
# Constants
PAUSE_BEFORE_TURN = 500 / 1000 # Pause before turning in seconds
SPEED = 150 # Global speed
# Global variables
class _GLOBALS:
sensors_host = ('192.168.2.152', 2000) # IP address and port for sensor data communication
turning = False # Flag indicating if the robot is currently turning
sensor_data = (0, 0, 0) # Tuple to hold sensor data (front, left, right)
stabilization_angle = 0 # Gyro stabilization angle
GLOBALS = _GLOBALS
def start_both_motors(speed: int) -> None:
"""
Starts both motors at the given speed.
:param speed: Speed to run the motors at.
"""
for motor in MOTORS:
motor.run(speed)
def stop_both_motors(hold: bool = False) -> None:
"""
Stops both motors.
:param hold: If True, motors will hold their position; otherwise, they will stop completely.
"""
if hold:
for motor in MOTORS:
motor.hold()
else:
for motor in MOTORS:
motor.stop()
def set_speed_both_motors(speed: int):
"""
Sets the speed of both motors.
:param speed: Speed to set for both motors.
"""
_ = [m.run(speed) for m in MOTORS]
def turn(speed: int, angle: int) -> None:
"""
Turns the robot to a specific angle using both motors.
:param speed: Speed of the motors during turning.
:param angle: Angle (in degrees) to turn. Positive for right, negative for left.
"""
global GLOBALS, GYRO, MOTORS
GLOBALS.turning = True
if angle < 0: # Left turn
MOTORS[0].run(speed * (-1))
MOTORS[1].run(speed)
GLOBALS.stabilization_angle -= angle
if angle > 0: # Right turn
MOTORS[1].run(speed * (-1))
MOTORS[0].run(speed)
GLOBALS.stabilization_angle += angle
while not GYRO.angle() in range(angle - 1, angle + 1):
pass
time.sleep(1)
set_speed_both_motors(speed)
GLOBALS.turning = False
def stabilize_motors() -> None:
"""
Continuously adjusts the robot's orientation using the gyro sensor to maintain stability.
"""
global GLOBALS, GYRO, MOTORS, SPEED
while True:
if not GLOBALS.turning:
if GYRO.angle() > GLOBALS.stabilization_angle + 4:
turn(SPEED, GLOBALS.stabilization_angle)
elif GYRO.angle() < GLOBALS.stabilization_angle - 4:
turn(SPEED, GLOBALS.stabilization_angle)
def stream_sensor_data() -> None:
"""
Establishes a socket connection to stream sensor data and continuously updates `GLOBALS.sensor_data`.
"""
global GLOBALS
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(GLOBALS.sensors_host)
init = sock.recv(256).decode("utf-8")
if init.startswith("READY_STREAM||"):
buffer_size = int(init.split("||")[1])
sock.send(b"START_BROADCAST")
else:
exit(1)
while True:
try:
operation, data = sock.recv(buffer_size).decode("utf-8").split("||")
except ValueError:
continue
if operation == "HB":
sock.send(str("HB||" + str(int(data) * 2 // 3)).encode("utf-8"))
if operation == "SD":
GLOBALS.sensor_data = [int(sv) for sv in data.split(";")]
def algorithm():
"""
Main algorithm that decides robot actions based on sensor data.
"""
global GLOBALS, GYRO, SPEED
while True:
if GLOBALS.sensor_data[0] == 1: # FRONT BLOCKED
if GLOBALS.sensor_data[1] == 0: # LEFT FREE
turn(SPEED, -90)
elif GLOBALS.sensor_data[2] == 0: # RIGHT FREE
turn(SPEED, 90)
else: # ALL BLOCKED
set_speed_both_motors(SPEED * (-1))
while GLOBALS.sensor_data[1] == 1 or GLOBALS.sensor_data[2] == 1:
pass
turn(SPEED, -90 if GLOBALS.sensor_data[1] == 0 else 90 if GLOBALS.sensor_data[2] == 0 else 0)
elif GLOBALS.sensor_data[1] == 0: # LEFT FREE
if GLOBALS.sensor_data[0] == 1 and GLOBALS.sensor_data[2] == 1:
turn(SPEED, -90)
elif GLOBALS.stabilization_angle > 0:
turn(SPEED, GLOBALS.stabilization_angle * (-1))
elif GLOBALS.sensor_data[2] == 0: # RIGHT FREE
if GLOBALS.sensor_data[0] == 1 and GLOBALS.sensor_data[1] == 1:
turn(SPEED, 90)
elif GLOBALS.stabilization_angle < 0:
turn(SPEED, GLOBALS.stabilization_angle * (-1))
def main():
"""
Main function to start the robot control script.
"""
global GLOBALS
start_both_motors(SPEED)
_thread.start_new_thread(stabilize_motors, ())
_thread.start_new_thread(stream_sensor_data, ())
try:
algorithm()
finally:
stop_both_motors()
if __name__ == '__main__':
main()