-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathev3dev.py
More file actions
2531 lines (1999 loc) · 79.7 KB
/
ev3dev.py
File metadata and controls
2531 lines (1999 loc) · 79.7 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------
# Copyright (c) 2015 Ralph Hempel
# Copyright (c) 2015 Anton Vanhoucke
# Copyright (c) 2015 Denis Demidov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# -----------------------------------------------------------------------------
#~autogen autogen-header
# Sections of the following code were auto-generated based on spec v0.9.3-pre, rev 2
#~autogen
import os
import fnmatch
import numbers
import platform
import fcntl
import array
import mmap
import ctypes
import re
from os.path import abspath
from PIL import Image, ImageDraw
from struct import pack, unpack
from subprocess import Popen
#------------------------------------------------------------------------------
# Guess platform we are running on
def current_platform():
machine = platform.machine()
if machine == 'armv5tejl':
return 'ev3'
elif machine == 'armv6l':
return 'brickpi'
else:
return 'unsupported'
#------------------------------------------------------------------------------
# Attribute reader/writer with cached file access
class FileCache(object):
def __init__(self):
self._cache = {}
def __del__(self):
for f in self._cache.values():
f.close()
def file_handle( self, path, mode, reopen=False ):
"""Manages the file handle cache and opening the files in the correct mode"""
if path not in self._cache:
f = open( path, mode )
self._cache[path] = f
elif reopen == True:
self._cache[path].close()
f = open( path, mode )
self._cache[path] = f
else:
f = self._cache[path]
return f
def read(self, path):
f = self.file_handle(path, 'r')
try:
f.seek(0)
value = f.readline()
except IOError:
f = self.file_handle( path, 'w+', reopen=True )
value = f.readline()
return value.strip()
def write(self, path, value):
f = self.file_handle( path, 'w' )
try:
f.seek(0)
f.write( value )
except IOError:
f = self.file_handle( path, 'w+', reopen=True )
f.write( value )
f.flush()
#------------------------------------------------------------------------------
# Define the base class from which all other ev3dev classes are defined.
class Device(object):
"""The ev3dev device base class"""
DEVICE_ROOT_PATH = '/sys/class'
_DEVICE_INDEX = re.compile(r'^.*(?P<idx>\d+)$')
def __init__(self, class_name, name='*', **kwargs ):
"""Spin through the Linux sysfs class for the device type and find
a device that matches the provided name and attributes (if any).
Parameters:
class_name: class name of the device, a subdirectory of /sys/class.
For example, 'tacho-motor'.
name: pattern that device name should match.
For example, 'sensor*' or 'motor*'. Default value: '*'.
keyword arguments: used for matching the corresponding device
attributes. For example, port_name='outA', or
driver_name=['lego-ev3-us', 'lego-nxt-us']. When argument value
is a list, then a match against any entry of the list is
enough.
Example::
d = ev3dev.Device('tacho-motor', port_name='outA')
s = ev3dev.Device('lego-sensor', driver_name=['lego-ev3-us', 'lego-nxt-us'])
When connected succesfully, the `connected` attribute is set to True.
"""
classpath = abspath( Device.DEVICE_ROOT_PATH + '/' + class_name )
self._attribute_cache = FileCache()
for file in os.listdir( classpath ):
if fnmatch.fnmatch(file, name):
self._path = abspath( classpath + '/' + file )
# See if requested attributes match:
if all([self._matches(k, kwargs[k]) for k in kwargs]):
self.connected = True
match = Device._DEVICE_INDEX.match(file)
if match:
self._device_index = int(match.group('idx'))
else:
self._device_index = None
return
self._path = ''
self.connected = False
def _matches(self, attribute, pattern):
"""Test if attribute value matches pattern (that is, if pattern is a
substring of attribute value). If pattern is a list, then a match with
any one entry is enough.
"""
value = self._get_attribute(attribute)
if isinstance(pattern, list):
return any([value.find(pat) >= 0 for pat in pattern])
else:
return value.find(pattern) >= 0
def _get_attribute( self, attribute ):
"""Device attribute getter"""
return self._attribute_cache.read(abspath(self._path + '/' + attribute))
def _set_attribute( self, attribute, value ):
"""Device attribute setter"""
self._attribute_cache.write( abspath(self._path + '/' + attribute), value )
def get_attr_int( self, attribute ):
return int( self._get_attribute( attribute ) )
def set_attr_int( self, attribute, value ):
self._set_attribute( attribute, '{0:d}'.format( int(value) ) )
def get_attr_string( self, attribute ):
return self._get_attribute( attribute )
def set_attr_string( self, attribute, value ):
self._set_attribute( attribute, "{0}".format(value) )
def get_attr_line( self, attribute ):
return self._get_attribute( attribute )
def get_attr_set( self, attribute ):
return [v.strip('[]') for v in self.get_attr_line( attribute ).split()]
def get_attr_from_set( self, attribute ):
for a in self.get_attr_line( attribute ).split():
v = a.strip( '[]' )
if v != a:
return v
return ""
@property
def device_index(self):
return self._device_index
#~autogen generic-class classes.motor>currentClass
class Motor(Device):
"""
The motor class provides a uniform interface for using motors with
positional and directional feedback such as the EV3 and NXT motors.
This feedback allows for precise control of the motors. This is the
most common type of motor, so we just call it `motor`.
"""
SYSTEM_CLASS_NAME = 'tacho-motor'
SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
if port is not None:
kwargs['port_name'] = port
Device.__init__(self, self.SYSTEM_CLASS_NAME, name, **kwargs)
#~autogen
#~autogen generic-get-set classes.motor>currentClass
@property
def command(self):
"""
Sends a command to the motor controller. See `commands` for a list of
possible values.
"""
raise Exception( "command is a write-only property!" )
@command.setter
def command(self, value):
self.set_attr_string( 'command', value )
@property
def commands(self):
"""
Returns a list of commands that are supported by the motor
controller. Possible values are `run-forever`, `run-to-abs-pos`, `run-to-rel-pos`,
`run-timed`, `run-direct`, `stop` and `reset`. Not all commands may be supported.
- `run-forever` will cause the motor to run until another command is sent.
- `run-to-abs-pos` will run to an absolute position specified by `position_sp`
and then stop using the command specified in `stop_command`.
- `run-to-rel-pos` will run to a position relative to the current `position` value.
The new position will be current `position` + `position_sp`. When the new
position is reached, the motor will stop using the command specified by `stop_command`.
- `run-timed` will run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
- `run-direct` will run the motor at the duty cycle specified by `duty_cycle_sp`.
Unlike other run commands, changing `duty_cycle_sp` while running *will*
take effect immediately.
- `stop` will stop any of the run commands before they are complete using the
command specified by `stop_command`.
- `reset` will reset all of the motor parameter attributes to their default value.
This will also have the effect of stopping the motor.
"""
return self.get_attr_set( 'commands' )
@property
def count_per_rot(self):
"""
Returns the number of tacho counts in one rotation of the motor. Tacho counts
are used by the position and speed attributes, so you can use this value
to convert rotations or degrees to tacho counts. In the case of linear
actuators, the units here will be counts per centimeter.
"""
return self.get_attr_int( 'count_per_rot' )
@property
def driver_name(self):
"""
Returns the name of the driver that provides this tacho motor device.
"""
return self.get_attr_string( 'driver_name' )
@property
def duty_cycle(self):
"""
Returns the current duty cycle of the motor. Units are percent. Values
are -100 to 100.
"""
return self.get_attr_int( 'duty_cycle' )
@property
def duty_cycle_sp(self):
"""
Writing sets the duty cycle setpoint. Reading returns the current value.
Units are in percent. Valid values are -100 to 100. A negative value causes
the motor to rotate in reverse. This value is only used when `speed_regulation`
is off.
"""
return self.get_attr_int( 'duty_cycle_sp' )
@duty_cycle_sp.setter
def duty_cycle_sp(self, value):
self.set_attr_int( 'duty_cycle_sp', value )
@property
def encoder_polarity(self):
"""
Sets the polarity of the rotary encoder. This is an advanced feature to all
use of motors that send inversed encoder signals to the EV3. This should
be set correctly by the driver of a device. It You only need to change this
value if you are using a unsupported device. Valid values are `normal` and
`inversed`.
"""
return self.get_attr_string( 'encoder_polarity' )
@encoder_polarity.setter
def encoder_polarity(self, value):
self.set_attr_string( 'encoder_polarity', value )
@property
def polarity(self):
"""
Sets the polarity of the motor. With `normal` polarity, a positive duty
cycle will cause the motor to rotate clockwise. With `inversed` polarity,
a positive duty cycle will cause the motor to rotate counter-clockwise.
Valid values are `normal` and `inversed`.
"""
return self.get_attr_string( 'polarity' )
@polarity.setter
def polarity(self, value):
self.set_attr_string( 'polarity', value )
@property
def port_name(self):
"""
Returns the name of the port that the motor is connected to.
"""
return self.get_attr_string( 'port_name' )
@property
def position(self):
"""
Returns the current position of the motor in pulses of the rotary
encoder. When the motor rotates clockwise, the position will increase.
Likewise, rotating counter-clockwise causes the position to decrease.
Writing will set the position to that value.
"""
return self.get_attr_int( 'position' )
@position.setter
def position(self, value):
self.set_attr_int( 'position', value )
@property
def position_p(self):
"""
The proportional constant for the position PID.
"""
return self.get_attr_int( 'hold_pid/Kp' )
@position_p.setter
def position_p(self, value):
self.set_attr_int( 'hold_pid/Kp', value )
@property
def position_i(self):
"""
The integral constant for the position PID.
"""
return self.get_attr_int( 'hold_pid/Ki' )
@position_i.setter
def position_i(self, value):
self.set_attr_int( 'hold_pid/Ki', value )
@property
def position_d(self):
"""
The derivative constant for the position PID.
"""
return self.get_attr_int( 'hold_pid/Kd' )
@position_d.setter
def position_d(self, value):
self.set_attr_int( 'hold_pid/Kd', value )
@property
def position_sp(self):
"""
Writing specifies the target position for the `run-to-abs-pos` and `run-to-rel-pos`
commands. Reading returns the current value. Units are in tacho counts. You
can use the value returned by `counts_per_rot` to convert tacho counts to/from
rotations or degrees.
"""
return self.get_attr_int( 'position_sp' )
@position_sp.setter
def position_sp(self, value):
self.set_attr_int( 'position_sp', value )
@property
def speed(self):
"""
Returns the current motor speed in tacho counts per second. Not, this is
not necessarily degrees (although it is for LEGO motors). Use the `count_per_rot`
attribute to convert this value to RPM or deg/sec.
"""
return self.get_attr_int( 'speed' )
@property
def speed_sp(self):
"""
Writing sets the target speed in tacho counts per second used when `speed_regulation`
is on. Reading returns the current value. Use the `count_per_rot` attribute
to convert RPM or deg/sec to tacho counts per second.
"""
return self.get_attr_int( 'speed_sp' )
@speed_sp.setter
def speed_sp(self, value):
self.set_attr_int( 'speed_sp', value )
@property
def ramp_up_sp(self):
"""
Writing sets the ramp up setpoint. Reading returns the current value. Units
are in milliseconds. When set to a value > 0, the motor will ramp the power
sent to the motor from 0 to 100% duty cycle over the span of this setpoint
when starting the motor. If the maximum duty cycle is limited by `duty_cycle_sp`
or speed regulation, the actual ramp time duration will be less than the setpoint.
"""
return self.get_attr_int( 'ramp_up_sp' )
@ramp_up_sp.setter
def ramp_up_sp(self, value):
self.set_attr_int( 'ramp_up_sp', value )
@property
def ramp_down_sp(self):
"""
Writing sets the ramp down setpoint. Reading returns the current value. Units
are in milliseconds. When set to a value > 0, the motor will ramp the power
sent to the motor from 100% duty cycle down to 0 over the span of this setpoint
when stopping the motor. If the starting duty cycle is less than 100%, the
ramp time duration will be less than the full span of the setpoint.
"""
return self.get_attr_int( 'ramp_down_sp' )
@ramp_down_sp.setter
def ramp_down_sp(self, value):
self.set_attr_int( 'ramp_down_sp', value )
@property
def speed_regulation_enabled(self):
"""
Turns speed regulation on or off. If speed regulation is on, the motor
controller will vary the power supplied to the motor to try to maintain the
speed specified in `speed_sp`. If speed regulation is off, the controller
will use the power specified in `duty_cycle_sp`. Valid values are `on` and
`off`.
"""
return self.get_attr_string( 'speed_regulation' )
@speed_regulation_enabled.setter
def speed_regulation_enabled(self, value):
self.set_attr_string( 'speed_regulation', value )
@property
def speed_regulation_p(self):
"""
The proportional constant for the speed regulation PID.
"""
return self.get_attr_int( 'speed_pid/Kp' )
@speed_regulation_p.setter
def speed_regulation_p(self, value):
self.set_attr_int( 'speed_pid/Kp', value )
@property
def speed_regulation_i(self):
"""
The integral constant for the speed regulation PID.
"""
return self.get_attr_int( 'speed_pid/Ki' )
@speed_regulation_i.setter
def speed_regulation_i(self, value):
self.set_attr_int( 'speed_pid/Ki', value )
@property
def speed_regulation_d(self):
"""
The derivative constant for the speed regulation PID.
"""
return self.get_attr_int( 'speed_pid/Kd' )
@speed_regulation_d.setter
def speed_regulation_d(self, value):
self.set_attr_int( 'speed_pid/Kd', value )
@property
def state(self):
"""
Reading returns a list of state flags. Possible flags are
`running`, `ramping` `holding` and `stalled`.
"""
return self.get_attr_set( 'state' )
@property
def stop_command(self):
"""
Reading returns the current stop command. Writing sets the stop command.
The value determines the motors behavior when `command` is set to `stop`.
Also, it determines the motors behavior when a run command completes. See
`stop_commands` for a list of possible values.
"""
return self.get_attr_string( 'stop_command' )
@stop_command.setter
def stop_command(self, value):
self.set_attr_string( 'stop_command', value )
@property
def stop_commands(self):
"""
Returns a list of stop modes supported by the motor controller.
Possible values are `coast`, `brake` and `hold`. `coast` means that power will
be removed from the motor and it will freely coast to a stop. `brake` means
that power will be removed from the motor and a passive electrical load will
be placed on the motor. This is usually done by shorting the motor terminals
together. This load will absorb the energy from the rotation of the motors and
cause the motor to stop more quickly than coasting. `hold` does not remove
power from the motor. Instead it actively try to hold the motor at the current
position. If an external force tries to turn the motor, the motor will 'push
back' to maintain its position.
"""
return self.get_attr_set( 'stop_commands' )
@property
def time_sp(self):
"""
Writing specifies the amount of time the motor will run when using the
`run-timed` command. Reading returns the current value. Units are in
milliseconds.
"""
return self.get_attr_int( 'time_sp' )
@time_sp.setter
def time_sp(self, value):
self.set_attr_int( 'time_sp', value )
#~autogen
#~autogen generic-property-value classes.motor>currentClass
# Run the motor until another command is sent.
COMMAND_RUN_FOREVER = 'run-forever'
# Run to an absolute position specified by `position_sp` and then
# stop using the command specified in `stop_command`.
COMMAND_RUN_TO_ABS_POS = 'run-to-abs-pos'
# Run to a position relative to the current `position` value.
# The new position will be current `position` + `position_sp`.
# When the new position is reached, the motor will stop using
# the command specified by `stop_command`.
COMMAND_RUN_TO_REL_POS = 'run-to-rel-pos'
# Run the motor for the amount of time specified in `time_sp`
# and then stop the motor using the command specified by `stop_command`.
COMMAND_RUN_TIMED = 'run-timed'
# Run the motor at the duty cycle specified by `duty_cycle_sp`.
# Unlike other run commands, changing `duty_cycle_sp` while running *will*
# take effect immediately.
COMMAND_RUN_DIRECT = 'run-direct'
# Stop any of the run commands before they are complete using the
# command specified by `stop_command`.
COMMAND_STOP = 'stop'
# Reset all of the motor parameter attributes to their default value.
# This will also have the effect of stopping the motor.
COMMAND_RESET = 'reset'
# Sets the normal polarity of the rotary encoder.
ENCODER_POLARITY_NORMAL = 'normal'
# Sets the inversed polarity of the rotary encoder.
ENCODER_POLARITY_INVERSED = 'inversed'
# With `normal` polarity, a positive duty cycle will
# cause the motor to rotate clockwise.
POLARITY_NORMAL = 'normal'
# With `inversed` polarity, a positive duty cycle will
# cause the motor to rotate counter-clockwise.
POLARITY_INVERSED = 'inversed'
# The motor controller will vary the power supplied to the motor
# to try to maintain the speed specified in `speed_sp`.
SPEED_REGULATION_ON = 'on'
# The motor controller will use the power specified in `duty_cycle_sp`.
SPEED_REGULATION_OFF = 'off'
# Power will be removed from the motor and it will freely coast to a stop.
STOP_COMMAND_COAST = 'coast'
# Power will be removed from the motor and a passive electrical load will
# be placed on the motor. This is usually done by shorting the motor terminals
# together. This load will absorb the energy from the rotation of the motors and
# cause the motor to stop more quickly than coasting.
STOP_COMMAND_BRAKE = 'brake'
# Does not remove power from the motor. Instead it actively try to hold the motor
# at the current position. If an external force tries to turn the motor, the motor
# will ``push back`` to maintain its position.
STOP_COMMAND_HOLD = 'hold'
#~autogen
#~autogen motor_commands classes.motor>currentClass
def run_forever( self, **kwargs ):
"""Run the motor until another command is sent.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-forever'
def run_to_abs_pos( self, **kwargs ):
"""Run to an absolute position specified by `position_sp` and then
stop using the command specified in `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-to-abs-pos'
def run_to_rel_pos( self, **kwargs ):
"""Run to a position relative to the current `position` value.
The new position will be current `position` + `position_sp`.
When the new position is reached, the motor will stop using
the command specified by `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-to-rel-pos'
def run_timed( self, **kwargs ):
"""Run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-timed'
def run_direct( self, **kwargs ):
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
Unlike other run commands, changing `duty_cycle_sp` while running *will*
take effect immediately.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-direct'
def stop( self, **kwargs ):
"""Stop any of the run commands before they are complete using the
command specified by `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'stop'
def reset( self, **kwargs ):
"""Reset all of the motor parameter attributes to their default value.
This will also have the effect of stopping the motor.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'reset'
#~autogen
#~autogen generic-class classes.largeMotor>currentClass
class LargeMotor(Motor):
"""
EV3 large servo motor
"""
SYSTEM_CLASS_NAME = Motor.SYSTEM_CLASS_NAME
SYSTEM_DEVICE_NAME_CONVENTION = Motor.SYSTEM_DEVICE_NAME_CONVENTION
def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
if port is not None:
kwargs['port_name'] = port
Device.__init__(self, self.SYSTEM_CLASS_NAME, name, driver_name=['lego-ev3-l-motor'], **kwargs)
#~autogen
#~autogen generic-class classes.mediumMotor>currentClass
class MediumMotor(Motor):
"""
EV3 medium servo motor
"""
SYSTEM_CLASS_NAME = Motor.SYSTEM_CLASS_NAME
SYSTEM_DEVICE_NAME_CONVENTION = Motor.SYSTEM_DEVICE_NAME_CONVENTION
def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
if port is not None:
kwargs['port_name'] = port
Device.__init__(self, self.SYSTEM_CLASS_NAME, name, driver_name=['lego-ev3-m-motor'], **kwargs)
#~autogen
#~autogen generic-class classes.dcMotor>currentClass
class DcMotor(Device):
"""
The DC motor class provides a uniform interface for using regular DC motors
with no fancy controls or feedback. This includes LEGO MINDSTORMS RCX motors
and LEGO Power Functions motors.
"""
SYSTEM_CLASS_NAME = 'dc-motor'
SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
if port is not None:
kwargs['port_name'] = port
Device.__init__(self, self.SYSTEM_CLASS_NAME, name, **kwargs)
#~autogen
#~autogen generic-get-set classes.dcMotor>currentClass
@property
def command(self):
"""
Sets the command for the motor. Possible values are `run-forever`, `run-timed` and
`stop`. Not all commands may be supported, so be sure to check the contents
of the `commands` attribute.
"""
raise Exception( "command is a write-only property!" )
@command.setter
def command(self, value):
self.set_attr_string( 'command', value )
@property
def commands(self):
"""
Returns a list of commands supported by the motor
controller.
"""
return self.get_attr_set( 'commands' )
@property
def driver_name(self):
"""
Returns the name of the motor driver that loaded this device. See the list
of [supported devices] for a list of drivers.
"""
return self.get_attr_string( 'driver_name' )
@property
def duty_cycle(self):
"""
Shows the current duty cycle of the PWM signal sent to the motor. Values
are -100 to 100 (-100% to 100%).
"""
return self.get_attr_int( 'duty_cycle' )
@property
def duty_cycle_sp(self):
"""
Writing sets the duty cycle setpoint of the PWM signal sent to the motor.
Valid values are -100 to 100 (-100% to 100%). Reading returns the current
setpoint.
"""
return self.get_attr_int( 'duty_cycle_sp' )
@duty_cycle_sp.setter
def duty_cycle_sp(self, value):
self.set_attr_int( 'duty_cycle_sp', value )
@property
def polarity(self):
"""
Sets the polarity of the motor. Valid values are `normal` and `inversed`.
"""
return self.get_attr_string( 'polarity' )
@polarity.setter
def polarity(self, value):
self.set_attr_string( 'polarity', value )
@property
def port_name(self):
"""
Returns the name of the port that the motor is connected to.
"""
return self.get_attr_string( 'port_name' )
@property
def ramp_down_sp(self):
"""
Sets the time in milliseconds that it take the motor to ramp down from 100%
to 0%. Valid values are 0 to 10000 (10 seconds). Default is 0.
"""
return self.get_attr_int( 'ramp_down_sp' )
@ramp_down_sp.setter
def ramp_down_sp(self, value):
self.set_attr_int( 'ramp_down_sp', value )
@property
def ramp_up_sp(self):
"""
Sets the time in milliseconds that it take the motor to up ramp from 0% to
100%. Valid values are 0 to 10000 (10 seconds). Default is 0.
"""
return self.get_attr_int( 'ramp_up_sp' )
@ramp_up_sp.setter
def ramp_up_sp(self, value):
self.set_attr_int( 'ramp_up_sp', value )
@property
def state(self):
"""
Gets a list of flags indicating the motor status. Possible
flags are `running` and `ramping`. `running` indicates that the motor is
powered. `ramping` indicates that the motor has not yet reached the
`duty_cycle_sp`.
"""
return self.get_attr_set( 'state' )
@property
def stop_command(self):
"""
Sets the stop command that will be used when the motor stops. Read
`stop_commands` to get the list of valid values.
"""
raise Exception( "stop_command is a write-only property!" )
@stop_command.setter
def stop_command(self, value):
self.set_attr_string( 'stop_command', value )
@property
def stop_commands(self):
"""
Gets a list of stop commands. Valid values are `coast`
and `brake`.
"""
return self.get_attr_set( 'stop_commands' )
@property
def time_sp(self):
"""
Writing specifies the amount of time the motor will run when using the
`run-timed` command. Reading returns the current value. Units are in
milliseconds.
"""
return self.get_attr_int( 'time_sp' )
@time_sp.setter
def time_sp(self, value):
self.set_attr_int( 'time_sp', value )
#~autogen
#~autogen generic-property-value classes.dcMotor>currentClass
# Run the motor until another command is sent.
COMMAND_RUN_FOREVER = 'run-forever'
# Run the motor for the amount of time specified in `time_sp`
# and then stop the motor using the command specified by `stop_command`.
COMMAND_RUN_TIMED = 'run-timed'
# Run the motor at the duty cycle specified by `duty_cycle_sp`.
# Unlike other run commands, changing `duty_cycle_sp` while running *will*
# take effect immediately.
COMMAND_RUN_DIRECT = 'run-direct'
# Stop any of the run commands before they are complete using the
# command specified by `stop_command`.
COMMAND_STOP = 'stop'
# With `normal` polarity, a positive duty cycle will
# cause the motor to rotate clockwise.
POLARITY_NORMAL = 'normal'
# With `inversed` polarity, a positive duty cycle will
# cause the motor to rotate counter-clockwise.
POLARITY_INVERSED = 'inversed'
# Power will be removed from the motor and it will freely coast to a stop.
STOP_COMMAND_COAST = 'coast'
# Power will be removed from the motor and a passive electrical load will
# be placed on the motor. This is usually done by shorting the motor terminals
# together. This load will absorb the energy from the rotation of the motors and
# cause the motor to stop more quickly than coasting.
STOP_COMMAND_BRAKE = 'brake'
#~autogen
#~autogen motor_commands classes.dcMotor>currentClass
def run_forever( self, **kwargs ):
"""Run the motor until another command is sent.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-forever'
def run_timed( self, **kwargs ):
"""Run the motor for the amount of time specified in `time_sp`
and then stop the motor using the command specified by `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-timed'
def run_direct( self, **kwargs ):
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
Unlike other run commands, changing `duty_cycle_sp` while running *will*
take effect immediately.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'run-direct'
def stop( self, **kwargs ):
"""Stop any of the run commands before they are complete using the
command specified by `stop_command`.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
self.command = 'stop'
#~autogen
#~autogen generic-class classes.servoMotor>currentClass
class ServoMotor(Device):
"""
The servo motor class provides a uniform interface for using hobby type
servo motors.
"""
SYSTEM_CLASS_NAME = 'servo-motor'
SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
if port is not None:
kwargs['port_name'] = port
Device.__init__(self, self.SYSTEM_CLASS_NAME, name, **kwargs)
#~autogen
#~autogen generic-get-set classes.servoMotor>currentClass
@property
def command(self):
"""
Sets the command for the servo. Valid values are `run` and `float`. Setting
to `run` will cause the servo to be driven to the position_sp set in the
`position_sp` attribute. Setting to `float` will remove power from the motor.
"""
raise Exception( "command is a write-only property!" )
@command.setter
def command(self, value):
self.set_attr_string( 'command', value )
@property
def driver_name(self):
"""
Returns the name of the motor driver that loaded this device. See the list
of [supported devices] for a list of drivers.
"""
return self.get_attr_string( 'driver_name' )
@property
def max_pulse_sp(self):
"""
Used to set the pulse size in milliseconds for the signal that tells the