-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
171 lines (124 loc) · 4.32 KB
/
helper.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
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, ColorSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
from pybricks.iodevices import Ev3devSensor
def CorrectSpeed(x):
return (x/100) * 1400
class Claw:
def __init__(self, port: Port):
self.motor = Motor(port)
self.motor.control.limits(1500)
def run_angle(self, speed, angle, wait = True):
self.motor.reset_angle(0)
self.motor.run_angle(CorrectSpeed(speed), angle, wait = wait)
def run_time(self, speed, time, wait = True):
self.motor.run_time(CorrectSpeed(speed), time, wait = wait)
def dc(self, dir = 1, speed = 50):
self.motor.dc(speed * dir)
def hold(self):
self.motor.hold()
def reset(self):
self.motor.reset_angle(0)
def measureAngleRange(self, moveTime):
results = []
print("Full results:", end=' ')
while len(results) < 10:
self.motor.dc(-40)
wait(moveTime)
self.motor.hold()
wait(1000)
self.motor.reset_angle(0)
self.motor.dc(40)
wait(moveTime)
self.motor.hold()
wait(1000)
angle = self.motor.angle()
if angle != 0: # Skips erroneous results (see fixme above).
results.append(angle)
print(results[-1], end=' ')
print("\nAverage:", sum(results) / len(results))
class FrontClaw(Claw):
def __init__(self, port: Port):
super().__init__(port)
self.closeDist = -460
def run_target(self, speed, angle, wait = True):
self.motor.run_target(CorrectSpeed(speed), angle, wait = wait)
def goUp(self, speed = 50, wait = True, load = False, full = False):
if load:
self.run_target(speed, 0, wait = wait)
elif full:
self.run_target(speed, 160, wait = wait)
else:
self.run_target(speed, 200, wait = wait)
def goDown(self, speed = 30, wait = True):
self.run_target(speed, 405, wait = wait)
def openUp(self, wait = True):
self.run_target(100, 860, wait = wait)
def openSmall(self, wait = True):
self.run_target(80, 550, wait = wait)
def solar(self, wait = True):
self.run_target(80, 475, wait = wait)
def defaultPos(self):
self.dc()
wait(1500)
self.run_target(-50, self.closeDist, wait=False)
# self.run_target(100 * dir, deg)
# self.hold()
# self.dc(dir = dir)
class BackClaw(Claw):
def __init__(self, port: Port):
super().__init__(port)
def run_target(self, speed, angle, wait = True, reset = True):
self.motor.reset_angle(0)
self.motor.run_target(CorrectSpeed(speed), angle, wait = wait)
def mid(self):
self.run_target(-50, -180)
def defaultPos(self):
self.run_time(100, 1000)
self.run_target(-50, -185)
wait(100)
self.run_target(50, 40)
class Base:
def __init__(self,
leftMotor: motor,
rightMotor: motor,
colLeft: ColorSensor,
colRight: ColorSensor,
frontClaw: motor,
backClaw: motor):
self.leftMotor = leftMotor
self.rightMotor = rightMotor
self.colLeft = colLeft
self.colRight = colRight
self.clock = StopWatch()
self.frontClaw = frontClaw
self.backClaw = backClaw
def stop(self):
self.leftMotor.brake()
self.rightMotor.brake()
def hold(self):
self.leftMotor.hold()
self.rightMotor.hold()
wait(10)
def move(self, speed, condition):
while condition():
self.run(speed, speed)
def reset(self):
self.leftMotor.reset_angle(0)
self.rightMotor.reset_angle(0)
def run(self, leftSpeed: float, rightSpeed: float):
self.leftMotor.run(CorrectSpeed(leftSpeed))
self.rightMotor.run(CorrectSpeed(rightSpeed))
def run_time(self, speed: float, time: int):
# time in seconds
start = self.clock.time()
while self.clock.time() - start < time:
self.run(speed, speed)
self.stop()
def run_target(self, speed, angle, stop = Stop.HOLD):
self.reset()
self.leftMotor.run_target(CorrectSpeed(speed), angle, wait=False, then = stop)
self.rightMotor.run_target(CorrectSpeed(speed), angle, wait=True, then = stop)