Repository navigation
Expand file tree
/
Copy pathhelper.py
More file actions
143 lines (104 loc) · 3.94 KB
/
Copy pathhelper.py
File metadata and controls
143 lines (104 loc) · 3.94 KB
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
#!/usr/bin/env python
import logging
import sys
import time
import ev3dev.auto
from ev3dev.auto import (RemoteControl, list_motors,
INPUT_1, INPUT_2, INPUT_3,INPUT_4,
OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D)
from math import pi
from time import sleep
log = logging.getLogger(__name__)
INPUTS = (INPUT_1, INPUT_2, INPUT_3, INPUT_4)
OUTPUTS = (OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D)
def wait_for(condition, timeout=1e5, interval=0.01):
tic = time.time() + timeout
done = condition()
while time.time() < tic and not done:
time.sleep(interval)
done = condition()
return done
# =============
# Motor classes
# =============
class MotorMixin(object):
def wait_for_running(self):
return wait_for(lambda: 'running' in self.state, 1)
def wait_for_stop(self):
return wait_for(lambda: 'running' not in self.state, 1)
class LargeMotor(ev3dev.auto.LargeMotor, MotorMixin):
pass
class MediumMotor(ev3dev.auto.MediumMotor, MotorMixin):
pass
# ============
# Tank classes
# ============
class Tank(object):
def __init__(self, left_motor, right_motor, polarity='normal'):
for motor in (left_motor, right_motor):
if motor not in OUTPUTS:
log.error("%s in an invalid motor, choices are %s" % (motor, ', '.join(OUTPUTS)))
sys.exit(1)
self.left_motor = LargeMotor(left_motor)
self.right_motor = LargeMotor(right_motor)
for x in (self.left_motor, self.right_motor):
if not x.connected:
log.error("%s is not connected" % x)
sys.exit(1)
self.left_motor.reset()
self.right_motor.reset()
self.duty_cycle_sp = 90
self.left_motor.duty_cycle_sp = self.duty_cycle_sp
self.right_motor.duty_cycle_sp = self.duty_cycle_sp
self.set_polarity(polarity)
def set_polarity(self, polarity):
valid_choices = ('normal', 'inversed')
assert polarity in valid_choices,\
"%s is an invalid polarity choice, must be %s" % (polarity, ', '.join(valid_choices))
self.left_motor.polarity = polarity
self.right_motor.polarity = polarity
class RemoteControlledTank(Tank):
def __init__(self, left_motor, right_motor, polarity='inversed'):
Tank.__init__(self, left_motor, right_motor, polarity)
self.remote = RemoteControl(channel=1)
if not self.remote.connected:
log.error("%s is not connected" % self.remote)
sys.exit(1)
self.remote.on_red_up = self.make_move(self.left_motor, self.duty_cycle_sp)
self.remote.on_red_down = self.make_move(self.left_motor, self.duty_cycle_sp * -1)
self.remote.on_blue_up = self.make_move(self.right_motor, self.duty_cycle_sp)
self.remote.on_blue_down = self.make_move(self.right_motor, self.duty_cycle_sp * -1)
def make_move(self, motor, dc_sp):
def move(state):
if state:
motor.run_forever(duty_cycle_sp=dc_sp)
else:
motor.stop()
return move
def main(self):
try:
while True:
self.remote.process()
time.sleep(0.01)
# Exit cleanly so that all motors are stopped
except (KeyboardInterrupt, Exception) as e:
log.exception(e)
for motor in list_motors():
motor.stop()
# =====================
# Wheel and Rim classes
# =====================
class Wheel(object):
"""
A base class for various types of wheels, tires, etc
All units are in mm
"""
def __init__(self, diameter, width):
self.diameter = float(diameter)
self.width = float(width)
self.radius = float(diameter/2)
self.circumference = diameter * pi
# A great reference when adding new wheels is http://wheels.sariel.pl/
class EV3RubberWheel(Wheel):
def __init__(self):
Wheel.__init__(self, 43.2, 21)