Repository navigation
Added class MotorSet #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added class MotorSet #336
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ca1471
Added class MotorSet
dwalton76 f928075
Merge branch 'develop' of https://github.com/dwalton76/ev3dev-lang-py…
dwalton76 99ec00b
Added MotorPair set_speed_ratio() and set_speed_percentage()
dwalton76 9fc40dc
Changed set_speed_ratio to set_speed_steering
dwalton76 c87e182
Change set_speed_steering power variable to speed
dwalton76 ee8d22e
Updated doc string for MotorPair set_ functions
dwalton76 e92130d
Merge branch 'develop' of https://github.com/dwalton76/ev3dev-lang-py…
dwalton76 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import sys | ||
| import time | ||
| import ev3dev.auto | ||
| from collections import OrderedDict | ||
| from ev3dev.auto import (RemoteControl, list_motors, | ||
| INPUT_1, INPUT_2, INPUT_3, INPUT_4, | ||
| OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D) | ||
|
|
@@ -146,6 +147,253 @@ class MediumMotor(ev3dev.auto.MediumMotor, MotorMixin): | |
| pass | ||
|
|
||
|
|
||
| class MotorSet(object): | ||
| """ | ||
| motor_specs is a dictionary such as | ||
| { | ||
| OUTPUT_A : LargeMotor, | ||
| OUTPUT_B : MediumMotor, | ||
| } | ||
| """ | ||
|
|
||
| def __init__(self, motor_specs, desc=None): | ||
|
|
||
| for motor_port in motor_specs.keys(): | ||
| if motor_port not in OUTPUTS: | ||
| log.error("%s in an invalid motor, choices are %s" % (motor_port, ', '.join(OUTPUTS))) | ||
| sys.exit(1) | ||
|
|
||
| self.motors = OrderedDict() | ||
| for motor_port in sorted(motor_specs.keys()): | ||
| motor_class = motor_specs[motor_port] | ||
| self.motors[motor_port] = motor_class(motor_port) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The motor objects are created here |
||
|
|
||
| self.desc = desc | ||
| self.verify_connected() | ||
|
|
||
| def __str__(self): | ||
|
|
||
| if self.desc: | ||
| return self.desc | ||
| else: | ||
| return self.__class__.__name__ | ||
|
|
||
| def verify_connected(self): | ||
| for motor in self.motors.values(): | ||
| if not motor.connected: | ||
| log.error("%s: %s is not connected" % (self, motor)) | ||
| sys.exit(1) | ||
|
|
||
| def set_args(self, **kwargs): | ||
| motors = kwargs.get('motors', self.motors.values()) | ||
|
|
||
| for motor in motors: | ||
| for key in kwargs: | ||
| if key != 'motors': | ||
| try: | ||
| setattr(motor, key, kwargs[key]) | ||
| except AttributeError as e: | ||
| log.error("%s %s cannot set %s to %s" % (self, motor, key, kwargs[key])) | ||
| raise e | ||
|
|
||
| def set_polarity(self, polarity, motors=None): | ||
| valid_choices = ('normal', 'inversed') | ||
| assert polarity in valid_choices,\ | ||
| "%s is an invalid polarity choice, must be %s" % (polarity, ', '.join(valid_choices)) | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.polarity = polarity | ||
|
|
||
| def _run_command(self, **kwargs): | ||
| motors = kwargs.get('motors', self.motors.values()) | ||
|
|
||
| for motor in motors: | ||
| for key in kwargs: | ||
| if key not in ('motors', 'commands'): | ||
| log.debug("%s: %s set %s to %s" % (self, motor, key, kwargs[key])) | ||
| setattr(motor, key, kwargs[key]) | ||
|
|
||
| for motor in motors: | ||
| motor.command = kwargs['command'] | ||
| log.debug("%s: %s command %s" % (self, motor, kwargs['command'])) | ||
|
|
||
| def run_forever(self, **kwargs): | ||
| kwargs['command'] = ev3dev.auto.LargeMotor.COMMAND_RUN_FOREVER | ||
| self._run_command(**kwargs) | ||
|
|
||
| def run_to_abs_pos(self, **kwargs): | ||
| kwargs['command'] = ev3dev.auto.LargeMotor.COMMAND_RUN_TO_ABS_POS | ||
| self._run_command(**kwargs) | ||
|
|
||
| def run_to_rel_pos(self, **kwargs): | ||
| kwargs['command'] = ev3dev.auto.LargeMotor.COMMAND_RUN_TO_REL_POS | ||
| self._run_command(**kwargs) | ||
|
|
||
| def run_timed(self, **kwargs): | ||
| kwargs['command'] = ev3dev.auto.LargeMotor.COMMAND_RUN_TIMED | ||
| self._run_command(**kwargs) | ||
|
|
||
| def run_direct(self, **kwargs): | ||
| kwargs['command'] = ev3dev.auto.LargeMotor.COMMAND_RUN_DIRECT | ||
| self._run_command(**kwargs) | ||
|
|
||
| def reset(self, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.reset() | ||
|
|
||
| def stop(self, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.stop() | ||
|
|
||
| def _is_state(self, motors, state): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| if state not in motor.state: | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| @property | ||
| def is_running(self, motors=None): | ||
| return self._is_state(motors, ev3dev.auto.LargeMotor.STATE_RUNNING) | ||
|
|
||
| @property | ||
| def is_ramping(self, motors=None): | ||
| return self._is_state(motors, ev3dev.auto.LargeMotor.STATE_RAMPING) | ||
|
|
||
| @property | ||
| def is_holding(self, motors=None): | ||
| return self._is_state(motors, ev3dev.auto.LargeMotor.STATE_HOLDING) | ||
|
|
||
| @property | ||
| def is_overloaded(self, motors=None): | ||
| return self._is_state(motors, ev3dev.auto.LargeMotor.STATE_OVERLOADED) | ||
|
|
||
| @property | ||
| def is_stalled(self): | ||
| return self._is_state(motors, ev3dev.auto.LargeMotor.STATE_STALLED) | ||
|
|
||
| def wait(self, cond, timeout=None, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.wait(cond, timeout) | ||
|
|
||
| def wait_until_not_moving(self, timeout=None, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.wait_until_not_moving(timeout) | ||
|
|
||
| def wait_until(self, s, timeout=None, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.wait_until(s, timeout) | ||
|
|
||
| def wait_while(self, s, timeout=None, motors=None): | ||
| motors = motors if motors is not None else self.motors.values() | ||
|
|
||
| for motor in motors: | ||
| motor.wait_while(s, timeout) | ||
|
|
||
|
|
||
| class MotorPair(MotorSet): | ||
|
|
||
| def __init__(self, motor_specs, desc=None): | ||
| MotorSet.__init__(self, motor_specs, desc) | ||
| (self.left_motor, self.right_motor) = self.motors.values() | ||
| self.max_speed = self.left_motor.max_speed | ||
|
|
||
| def set_speed_steering(self, direction, speed_outer_motor=100): | ||
| """ | ||
| Set the speed_sp for each motor in a pair to achieve the specified | ||
| steering. Note that calling this function alone will not make the | ||
| motors move, it only sets the speed. A run_* function must be called | ||
| afterwards to make the motors move. | ||
|
|
||
| direction [-100, 100]: | ||
| * -100 means turn left as fast as possible, | ||
| * 0 means drive in a straight line, and | ||
| * 100 means turn right as fast as possible. | ||
|
|
||
| speed_outer_motor: | ||
| The speed that should be applied to the outmost motor (the one | ||
| rotating faster). The speed of the other motor will be computed | ||
| automatically. | ||
| """ | ||
|
|
||
| assert direction >= -100 and direction <= 100,\ | ||
| "%s is an invalid direction, must be between -100 and 100 (inclusive)" % direction | ||
|
|
||
| left_speed = speed_outer_motor | ||
| right_speed = speed_outer_motor | ||
| speed = (50 - abs(float(direction))) / 50 | ||
|
|
||
| if direction >= 0: | ||
| right_speed *= speed | ||
| else: | ||
| left_speed *= speed | ||
|
|
||
| left_speed = int(left_speed) | ||
| right_speed = int(right_speed) | ||
| self.left_motor.speed_sp = left_speed | ||
| self.right_motor.speed_sp = right_speed | ||
|
|
||
| log.debug("%s: direction %d, %s speed %d, %s speed %d" % | ||
| (self, direction, self.left_motor, left_speed, self.right_motor, right_speed)) | ||
|
|
||
| def set_speed_percentage(self, left_motor_percentage, right_motor_percentage): | ||
| """ | ||
| Set the speeds of the left_motor vs right_motor by percentage of | ||
| their maximum speed. The minimum valid percentage is -100, the | ||
| maximum is 100. | ||
|
|
||
| Note that calling this function alone will not make the motors move, it | ||
| only sets the speed. A run_* function must be called afterwards to make | ||
| the motors move. | ||
| """ | ||
|
|
||
| assert left_motor_percentage >= -100 and left_motor_percentage <= 100,\ | ||
| "%s is an invalid percentage, must be between -100 and 100 (inclusive)" % left_motor_percentage | ||
|
|
||
| assert right_motor_percentage >= -100 and right_motor_percentage <= 100,\ | ||
| "%s is an invalid percentage, must be between -100 and 100 (inclusive)" % right_motor_percentage | ||
|
|
||
| # Convert left_motor_percentage and right_motor_percentage to fractions | ||
| left_motor_percentage = left_motor_percentage / 100.0 | ||
| right_motor_percentage = right_motor_percentage / 100.0 | ||
|
|
||
| self.left_motor.speed_sp = int(self.max_speed * left_motor_percentage) | ||
| self.right_motor.speed_sp = int(self.max_speed * right_motor_percentage) | ||
|
|
||
|
|
||
| class LargeMotorPair(MotorPair): | ||
|
|
||
| def __init__(self, left_motor, right_motor, desc=None): | ||
| motor_specs = { | ||
| left_motor : LargeMotor, | ||
| right_motor : LargeMotor, | ||
| } | ||
| MotorPair.__init__(self, motor_specs, desc) | ||
|
|
||
|
|
||
| class MediumMotorPair(MotorPair): | ||
|
|
||
| def __init__(self, left_motor, right_motor, desc=None): | ||
| motor_specs = { | ||
| left_motor : MediumMotor, | ||
| right_motor : MediumMotor, | ||
| } | ||
| MotorPair.__init__(self, motor_specs, desc) | ||
|
|
||
|
|
||
| class ColorSensorMixin(object): | ||
|
|
||
| def rgb(self): | ||
|
|
@@ -167,57 +415,34 @@ class ColorSensor(ev3dev.auto.ColorSensor, ColorSensorMixin): | |
| # ============ | ||
| # Tank classes | ||
| # ============ | ||
| class Tank(object): | ||
|
|
||
| def __init__(self, left_motor, right_motor, polarity='normal', name='Tank'): | ||
|
|
||
| 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) | ||
| class Tank(LargeMotorPair): | ||
| """ | ||
| This class is here for backwards compatibility for anyone who was using | ||
| this library before the days of LargeMotorPair. We wrote the Tank class | ||
| first, then LargeMotorPair. All future work will be in the MotorSet, | ||
| MotorPair, etc classes | ||
| """ | ||
|
|
||
| self.left_motor.reset() | ||
| self.right_motor.reset() | ||
| self.speed_sp = 400 | ||
| self.left_motor.speed_sp = self.speed_sp | ||
| self.right_motor.speed_sp = self.speed_sp | ||
| def __init__(self, left_motor_port, right_motor_port, polarity='normal', name='Tank'): | ||
| LargeMotorPair.__init__(self, left_motor_port, right_motor_port, name) | ||
| self.set_polarity(polarity) | ||
| self.name = name | ||
|
|
||
| def __str__(self): | ||
| return self.name | ||
|
|
||
| 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.speed_sp = 400 | ||
|
|
||
| self.left_motor.polarity = polarity | ||
| self.right_motor.polarity = polarity | ||
|
|
||
| class RemoteControlledTank(LargeMotorPair): | ||
|
|
||
| class RemoteControlledTank(Tank): | ||
| def __init__(self, left_motor_port, right_motor_port, polarity='inversed', speed=400): | ||
| LargeMotorPair.__init__(self, left_motor_port, right_motor_port) | ||
| self.set_polarity(polarity) | ||
|
|
||
| def __init__(self, left_motor, right_motor, polarity='inversed'): | ||
| Tank.__init__(self, left_motor, right_motor, polarity) | ||
| left_motor = self.motors[left_motor_port] | ||
| right_motor = self.motors[right_motor_port] | ||
| self.speed_sp = speed | ||
| 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.speed_sp) | ||
| self.remote.on_red_down = self.make_move(self.left_motor, self.speed_sp * -1) | ||
| self.remote.on_blue_up = self.make_move(self.right_motor, self.speed_sp) | ||
| self.remote.on_blue_down = self.make_move(self.right_motor, self.speed_sp * -1) | ||
| self.remote.on_red_up = self.make_move(left_motor, self.speed_sp) | ||
| self.remote.on_red_down = self.make_move(left_motor, self.speed_sp* -1) | ||
| self.remote.on_blue_up = self.make_move(right_motor, self.speed_sp) | ||
| self.remote.on_blue_down = self.make_move(right_motor, self.speed_sp * -1) | ||
|
|
||
| def make_move(self, motor, dc_sp): | ||
| def move(state): | ||
|
|
@@ -237,9 +462,7 @@ def main(self): | |
| # Exit cleanly so that all motors are stopped | ||
| except (KeyboardInterrupt, Exception) as e: | ||
| log.exception(e) | ||
|
|
||
| for motor in list_motors(): | ||
| motor.stop() | ||
| self.stop() | ||
|
|
||
|
|
||
| # ===================== | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each motor object can tell you what port it's on... why does this need to take a dictionary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The motor objects haven't been created yet though, we need to know what type of objects to make and which ports each of those objects is associated with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I read through the rest of the file then forgot to come back and delete this. I've got it now.