Skip to content
Merged

Misc #395

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ev3dev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_current_platform():
"""
Look in /sys/class/board-info/ to determine the platform type.

This can return 'ev3', 'evb', 'pistorms', 'brickpi' or 'brickpi3'.
This can return 'ev3', 'evb', 'pistorms', 'brickpi', 'brickpi3' or 'fake'.
"""
board_info_dir = '/sys/class/board-info/'

Expand Down Expand Up @@ -74,6 +74,10 @@ def get_current_platform():

elif value == 'Dexter Industries BrickPi3':
return 'brickpi3'

elif value == 'FAKE-SYS':
return 'fake'

return None


Expand Down
17 changes: 17 additions & 0 deletions ev3dev/_platform/fake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

OUTPUT_A = 'outA'
OUTPUT_B = 'outB'
OUTPUT_C = 'outC'
OUTPUT_D = 'outD'

INPUT_1 = 'in1'
INPUT_2 = 'in2'
INPUT_3 = 'in3'
INPUT_4 = 'in4'

BUTTONS_FILENAME = None
EVDEV_DEVICE_NAME = None

LEDS = {}
LED_GROUPS = {}
LED_COLORS = {}
3 changes: 3 additions & 0 deletions ev3dev/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
elif platform == 'brickpi3':
from ev3dev._platform.brickpi3 import BUTTONS_FILENAME, EVDEV_DEVICE_NAME

elif platform == 'fake':
from ev3dev._platform.fake import BUTTONS_FILENAME, EVDEV_DEVICE_NAME

else:
raise Exception("Unsupported platform '%s'" % platform)

Expand Down
3 changes: 3 additions & 0 deletions ev3dev/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
elif platform == 'brickpi3':
from ev3dev._platform.brickpi3 import LEDS, LED_GROUPS, LED_COLORS

elif platform == 'fake':
from ev3dev._platform.fake import LEDS, LED_GROUPS, LED_COLORS

else:
raise Exception("Unsupported platform '%s'" % platform)

Expand Down
225 changes: 118 additions & 107 deletions ev3dev/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import select
import time
from os.path import abspath
from ev3dev import get_current_platform, Device
from ev3dev import get_current_platform, Device, list_device_names

# The number of milliseconds we wait for the state of a motor to
# update to 'running' in the "on_for_XYZ" methods of the Motor class
Expand All @@ -56,6 +56,9 @@
elif platform == 'brickpi3':
from ev3dev._platform.brickpi3 import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D

elif platform == 'fake':
from ev3dev._platform.fake import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D

else:
raise Exception("Unsupported platform '%s'" % platform)

Expand All @@ -77,6 +80,110 @@ class Motor(Device):
SYSTEM_CLASS_NAME = 'tacho-motor'
SYSTEM_DEVICE_NAME_CONVENTION = '*'

__slots__ = [
'_address',
'_command',
'_commands',
'_count_per_rot',
'_count_per_m',
'_driver_name',
'_duty_cycle',
'_duty_cycle_sp',
'_full_travel_count',
'_polarity',
'_position',
'_position_p',
'_position_i',
'_position_d',
'_position_sp',
'_max_speed',
'_speed',
'_speed_sp',
'_ramp_up_sp',
'_ramp_down_sp',
'_speed_p',
'_speed_i',
'_speed_d',
'_state',
'_stop_action',
'_stop_actions',
'_time_sp',
'_poll',
]

#: 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 action specified in `stop_action`.
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 action specified by `stop_action`.
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 action specified by `stop_action`.
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
#: action specified by `stop_action`.
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'

#: Power is being sent to the motor.
STATE_RUNNING = 'running'

#: The motor is ramping up or down and has not yet reached a constant output level.
STATE_RAMPING = 'ramping'

#: The motor is not turning, but rather attempting to hold a fixed position.
STATE_HOLDING = 'holding'

#: The motor is turning, but cannot reach its `speed_sp`.
STATE_OVERLOADED = 'overloaded'

#: The motor is not turning when it should be.
STATE_STALLED = 'stalled'

#: Power will be removed from the motor and it will freely coast to a stop.
STOP_ACTION_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_ACTION_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_ACTION_HOLD = 'hold'

def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs):

if address is not None:
Expand Down Expand Up @@ -112,37 +219,6 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
self._time_sp = None
self._poll = None

__slots__ = [
'_address',
'_command',
'_commands',
'_count_per_rot',
'_count_per_m',
'_driver_name',
'_duty_cycle',
'_duty_cycle_sp',
'_full_travel_count',
'_polarity',
'_position',
'_position_p',
'_position_i',
'_position_d',
'_position_sp',
'_max_speed',
'_speed',
'_speed_sp',
'_ramp_up_sp',
'_ramp_down_sp',
'_speed_p',
'_speed_i',
'_speed_d',
'_state',
'_stop_action',
'_stop_actions',
'_time_sp',
'_poll',
]

@property
def address(self):
"""
Expand Down Expand Up @@ -491,79 +567,6 @@ def time_sp(self):
def time_sp(self, value):
self._time_sp = self.set_attr_int(self._time_sp, 'time_sp', value)

#: 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 action specified in `stop_action`.
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 action specified by `stop_action`.
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 action specified by `stop_action`.
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
#: action specified by `stop_action`.
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'

#: Power is being sent to the motor.
STATE_RUNNING = 'running'

#: The motor is ramping up or down and has not yet reached a constant output level.
STATE_RAMPING = 'ramping'

#: The motor is not turning, but rather attempting to hold a fixed position.
STATE_HOLDING = 'holding'

#: The motor is turning, but cannot reach its `speed_sp`.
STATE_OVERLOADED = 'overloaded'

#: The motor is not turning when it should be.
STATE_STALLED = 'stalled'

#: Power will be removed from the motor and it will freely coast to a stop.
STOP_ACTION_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_ACTION_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_ACTION_HOLD = 'hold'

def run_forever(self, **kwargs):
"""Run the motor until another command is sent.
"""
Expand Down Expand Up @@ -726,12 +729,20 @@ def wait_while(self, s, timeout=None):
return self.wait(lambda state: s not in state, timeout)

def _set_position_rotations(self, speed_pct, rotations):

# +/- speed is used to control direction, rotations must be positive
assert rotations >= 0, "rotations is %s, must be >= 0" % rotations

if speed_pct > 0:
self.position_sp = self.position + int(rotations * self.count_per_rot)
else:
self.position_sp = self.position - int(rotations * self.count_per_rot)

def _set_position_degrees(self, speed_pct, degrees):

# +/- speed is used to control direction, degrees must be positive
assert degrees >= 0, "degrees is %s, must be >= 0" % degrees

if speed_pct > 0:
self.position_sp = self.position + int((degrees * self.count_per_rot)/360)
else:
Expand Down Expand Up @@ -1538,9 +1549,9 @@ def on_for_rotations(self, left_speed_pct, right_speed_pct, rotations, brake=Tru

if left_speed > right_speed:
left_rotations = rotations
right_rotations = float(right_speed / left_speed) * rotations
right_rotations = abs(float(right_speed / left_speed)) * rotations
else:
left_rotations = float(left_speed / right_speed) * rotations
left_rotations = abs(float(left_speed / right_speed)) * rotations
right_rotations = rotations

# Set all parameters
Expand Down
5 changes: 4 additions & 1 deletion ev3dev/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import numbers
from os.path import abspath
from struct import unpack
from ev3dev import get_current_platform, Device
from ev3dev import get_current_platform, Device, list_device_names


# INPUT ports have platform specific values that we must import
Expand All @@ -52,6 +52,9 @@
elif platform == 'brickpi3':
from ev3dev._platform.brickpi3 import INPUT_1, INPUT_2, INPUT_3, INPUT_4

elif platform == 'fake':
from ev3dev._platform.fake import INPUT_1, INPUT_2, INPUT_3, INPUT_4

else:
raise Exception("Unsupported platform '%s'" % platform)

Expand Down
2 changes: 1 addition & 1 deletion utils/move_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
where you can"t move the motor by hand.
"""

from ev3dev.auto import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, Motor
from ev3dev.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D, Motor
import argparse
import logging
import sys
Expand Down
2 changes: 1 addition & 1 deletion utils/stop_all_motors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Stop all motors
"""
from ev3dev.auto import list_motors
from ev3dev.motor import list_motors

for motor in list_motors():
motor.stop(stop_action='brake')