-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrover.py
62 lines (47 loc) · 1.46 KB
/
rover.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
""" rover.py
The Rover class can be used for a simple rover build with an LEGO EV3-brick
with two motors. This class is used in the sample program linetracker.py,
part of the tutorial how to use Pixy2 on Pybricks.
Author : Kees Smit
Date : June 16 2020
Version : 1.00
License :
"""
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port
SPEED_FAST = 0 # 400
SPEED_SLOW = 150
class Rover:
def __init__(self):
""" Initialization of Rover."""
# Constants
self._GAIN = 10
self._speed = SPEED_FAST
# Initialize the EV3 brick
self.ev3 = EV3Brick()
# Initialize the motors
self.left_motor = Motor(Port.B)
self.right_motor = Motor(Port.C)
def move(self, speed):
speed *= self._GAIN
speed_left = limit_speed(self._speed - speed)
speed_right = limit_speed(self._speed + speed)
self.left_motor.run(speed_left)
self.right_motor.run(speed_right)
def move_slow(self):
""" Set initial speed to SPEED_SLOW."""
self._speed = SPEED_SLOW
def move_fast(self):
""" Set initial speed to SPEED_FAST."""
self._speed = SPEED_FAST
def stop(self):
self.left_motor.stop()
self.right_motor.stop()
def limit_speed(speed):
"""Limit speed in range [-900,900]."""
if speed > 900:
speed = 900
elif speed < -900:
speed = -900
return speed