Skip to content

Commit 245d8ae

Browse files
authored
Added DistanceValue classes (ev3dev#550)
1 parent 95ccb36 commit 245d8ae

4 files changed

Lines changed: 233 additions & 3 deletions

File tree

‎debian/changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium
22

3+
* Added DistanceValue classes
34
* wait_until_not_moving should consider "running holding" as "not moving"
45
* brickpi(3) raise exception if LargeMotor not used for a motor
56
* MoveJoyStick should use SpeedPercent

‎ev3dev2/motor.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -----------------------------------------------------------------------------
22
# Copyright (c) 2015 Ralph Hempel <rhempel@hempeldesigngroup.com>
3-
# Copyright (c) 2015 Anton Vanhoucke <antonvh@gmail.com>
4-
# Copyright (c) 2015 Denis Demidov <dennis.demidov@gmail.com>
5-
# Copyright (c) 2015 Eric Pascual <eric@pobot.org>
63
#
74
# Permission is hereby granted, free of charge, to any person obtaining a copy
85
# of this software and associated documentation files (the "Software"), to deal

‎ev3dev2/unit.py‎

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2015 Ralph Hempel
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
# -----------------------------------------------------------------------------
22+
23+
24+
import sys
25+
26+
if sys.version_info < (3,4):
27+
raise SystemError('Must be using Python 3.4 or higher')
28+
29+
CENTIMETER_MM = 10
30+
DECIMETER_MM = 100
31+
METER_MM = 1000
32+
INCH_MM = 25.4
33+
FOOT_MM = 304.8
34+
YARD_MM = 914.4
35+
STUD_MM = 8
36+
37+
38+
class DistanceValue(object):
39+
"""
40+
A base class for other unit types. Don't use this directly; instead, see
41+
:class:`DistanceMillimeters`, :class:`DistanceCentimeters`, :class:`DistanceDecimeters`, :class:`DistanceMeters`,
42+
:class:`DistanceInches`, :class:`DistanceFeet`, :class:`DistanceYards` and :class:`DistanceStuds`.
43+
"""
44+
45+
# This allows us to sort lists of DistanceValue objects
46+
def __lt__(self, other):
47+
return self.mm < other.mm
48+
49+
def __rmul__(self, other):
50+
return self.__mul__(other)
51+
52+
53+
class DistanceMillimeters(DistanceValue):
54+
"""
55+
Distance in millimeters
56+
"""
57+
58+
def __init__(self, millimeters):
59+
self.millimeters = millimeters
60+
61+
def __str__(self):
62+
return str(self.millimeters) + "mm"
63+
64+
def __mul__(self, other):
65+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
66+
return DistanceMillimeters(self.millimeters * other)
67+
68+
@property
69+
def mm(self):
70+
return self.millimeters
71+
72+
73+
class DistanceCentimeters(DistanceValue):
74+
"""
75+
Distance in centimeters
76+
"""
77+
78+
def __init__(self, centimeters):
79+
self.centimeters = centimeters
80+
81+
def __str__(self):
82+
return str(self.centimeters) + "cm"
83+
84+
def __mul__(self, other):
85+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
86+
return DistanceCentimeters(self.centimeters * other)
87+
88+
@property
89+
def mm(self):
90+
return self.centimeters * CENTIMETER_MM
91+
92+
93+
class DistanceDecimeters(DistanceValue):
94+
"""
95+
Distance in decimeters
96+
"""
97+
98+
def __init__(self, decimeters):
99+
self.decimeters = decimeters
100+
101+
def __str__(self):
102+
return str(self.decimeters) + "dm"
103+
104+
def __mul__(self, other):
105+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
106+
return DistanceDecimeters(self.decimeters * other)
107+
108+
@property
109+
def mm(self):
110+
return self.decimeters * DECIMETER_MM
111+
112+
113+
class DistanceMeters(DistanceValue):
114+
"""
115+
Distance in meters
116+
"""
117+
118+
def __init__(self, meters):
119+
self.meters = meters
120+
121+
def __str__(self):
122+
return str(self.meters) + "m"
123+
124+
def __mul__(self, other):
125+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
126+
return DistanceMeters(self.meters * other)
127+
128+
@property
129+
def mm(self):
130+
return self.meters * METER_MM
131+
132+
133+
class DistanceInches(DistanceValue):
134+
"""
135+
Distance in inches
136+
"""
137+
138+
def __init__(self, inches):
139+
self.inches = inches
140+
141+
def __str__(self):
142+
return str(self.inches) + "in"
143+
144+
def __mul__(self, other):
145+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
146+
return DistanceInches(self.inches * other)
147+
148+
@property
149+
def mm(self):
150+
return self.inches * INCH_MM
151+
152+
153+
class DistanceFeet(DistanceValue):
154+
"""
155+
Distance in feet
156+
"""
157+
158+
def __init__(self, feet):
159+
self.feet = feet
160+
161+
def __str__(self):
162+
return str(self.feet) + "ft"
163+
164+
def __mul__(self, other):
165+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
166+
return DistanceFeet(self.feet * other)
167+
168+
@property
169+
def mm(self):
170+
return self.feet * FOOT_MM
171+
172+
173+
class DistanceYards(DistanceValue):
174+
"""
175+
Distance in yards
176+
"""
177+
178+
def __init__(self, yards):
179+
self.yards = yards
180+
181+
def __str__(self):
182+
return str(self.yards) + "yd"
183+
184+
def __mul__(self, other):
185+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
186+
return DistanceYards(self.yards * other)
187+
188+
@property
189+
def mm(self):
190+
return self.yards * YARD_MM
191+
192+
193+
class DistanceStuds(DistanceValue):
194+
"""
195+
Distance in studs
196+
"""
197+
198+
def __init__(self, studs):
199+
self.studs = studs
200+
201+
def __str__(self):
202+
return str(self.studs) + "stud"
203+
204+
def __mul__(self, other):
205+
assert isinstance(other, (float, int)), "{} can only be multiplied by an int or float".format(self)
206+
return DistanceStuds(self.studs * other)
207+
208+
@property
209+
def mm(self):
210+
return self.studs * STUD_MM

‎tests/api_tests.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
MoveTank, MoveSteering, MoveJoystick, \
1717
SpeedPercent, SpeedDPM, SpeedDPS, SpeedRPM, SpeedRPS, SpeedNativeUnits, \
1818
OUTPUT_A, OUTPUT_B
19+
from ev3dev2.unit import (
20+
DistanceMillimeters,
21+
DistanceCentimeters,
22+
DistanceDecimeters,
23+
DistanceMeters,
24+
DistanceInches,
25+
DistanceFeet,
26+
DistanceYards,
27+
DistanceStuds
28+
)
1929

2030
ev3dev2.Device.DEVICE_ROOT_PATH = os.path.join(FAKE_SYS, 'arena')
2131

@@ -336,5 +346,17 @@ def test_units(self):
336346
self.assertEqual(SpeedRPS(2).to_native_units(m), 360 * 2)
337347
self.assertEqual(SpeedRPM(100).to_native_units(m), (360 * 100 / 60))
338348

349+
self.assertEqual(DistanceMillimeters(42).mm, 42)
350+
self.assertEqual(DistanceCentimeters(42).mm, 420)
351+
self.assertEqual(DistanceDecimeters(42).mm, 4200)
352+
self.assertEqual(DistanceMeters(42).mm, 42000)
353+
354+
self.assertAlmostEqual(DistanceInches(42).mm, 1066.8)
355+
self.assertAlmostEqual(DistanceFeet(42).mm, 12801.6)
356+
self.assertAlmostEqual(DistanceYards(42).mm, 38404.8)
357+
358+
self.assertEqual(DistanceStuds(42).mm, 336)
359+
360+
339361
if __name__ == "__main__":
340362
unittest.main()

0 commit comments

Comments
 (0)