|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import logging |
| 4 | +import sys |
| 5 | +import time |
| 6 | +import ev3dev.auto |
| 7 | +from ev3dev.auto import (RemoteControl, list_motors, |
| 8 | + INPUT_1, INPUT_2, INPUT_3,INPUT_4, |
| 9 | + OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D) |
| 10 | +from math import pi |
| 11 | +from time import sleep |
| 12 | + |
| 13 | +log = logging.getLogger(__name__) |
| 14 | + |
| 15 | +INPUTS = (INPUT_1, INPUT_2, INPUT_3, INPUT_4) |
| 16 | +OUTPUTS = (OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D) |
| 17 | + |
| 18 | +def wait_for(condition, timeout=1e5, interval=0.01): |
| 19 | + tic = time.time() + timeout |
| 20 | + done = condition() |
| 21 | + |
| 22 | + while time.time() < tic and not done: |
| 23 | + time.sleep(interval) |
| 24 | + done = condition() |
| 25 | + |
| 26 | + return done |
| 27 | + |
| 28 | + |
| 29 | +# ============= |
| 30 | +# Motor classes |
| 31 | +# ============= |
| 32 | +class MotorMixin(object): |
| 33 | + |
| 34 | + def wait_for_running(self): |
| 35 | + return wait_for(lambda: 'running' in self.state, 1) |
| 36 | + |
| 37 | + def wait_for_stop(self): |
| 38 | + return wait_for(lambda: 'running' not in self.state, 1) |
| 39 | + |
| 40 | + |
| 41 | +class LargeMotor(ev3dev.auto.LargeMotor, MotorMixin): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class MediumMotor(ev3dev.auto.MediumMotor, MotorMixin): |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +# ============ |
| 50 | +# Tank classes |
| 51 | +# ============ |
| 52 | +class Tank(object): |
| 53 | + |
| 54 | + def __init__(self, left_motor, right_motor, polarity='normal'): |
| 55 | + |
| 56 | + for motor in (left_motor, right_motor): |
| 57 | + if motor not in OUTPUTS: |
| 58 | + log.error("%s in an invalid motor, choices are %s" % (motor, ', '.join(OUTPUTS))) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + self.left_motor = LargeMotor(left_motor) |
| 62 | + self.right_motor = LargeMotor(right_motor) |
| 63 | + |
| 64 | + for x in (self.left_motor, self.right_motor): |
| 65 | + if not x.connected: |
| 66 | + log.error("%s is not connected" % x) |
| 67 | + sys.exit(1) |
| 68 | + |
| 69 | + self.left_motor.reset() |
| 70 | + self.right_motor.reset() |
| 71 | + self.duty_cycle_sp = 90 |
| 72 | + self.left_motor.duty_cycle_sp = self.duty_cycle_sp |
| 73 | + self.right_motor.duty_cycle_sp = self.duty_cycle_sp |
| 74 | + self.set_polarity(polarity) |
| 75 | + |
| 76 | + def set_polarity(self, polarity): |
| 77 | + valid_choices = ('normal', 'inversed') |
| 78 | + assert polarity in valid_choices,\ |
| 79 | + "%s is an invalid polarity choice, must be %s" % (polarity, ', '.join(valid_choices)) |
| 80 | + |
| 81 | + self.left_motor.polarity = polarity |
| 82 | + self.right_motor.polarity = polarity |
| 83 | + |
| 84 | + |
| 85 | +class RemoteControlledTank(Tank): |
| 86 | + |
| 87 | + def __init__(self, left_motor, right_motor, polarity='inversed'): |
| 88 | + Tank.__init__(self, left_motor, right_motor, polarity) |
| 89 | + self.remote = RemoteControl(channel=1) |
| 90 | + |
| 91 | + if not self.remote.connected: |
| 92 | + log.error("%s is not connected" % self.remote) |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + self.remote.on_red_up = self.make_move(self.left_motor, self.duty_cycle_sp) |
| 96 | + self.remote.on_red_down = self.make_move(self.left_motor, self.duty_cycle_sp * -1) |
| 97 | + self.remote.on_blue_up = self.make_move(self.right_motor, self.duty_cycle_sp) |
| 98 | + self.remote.on_blue_down = self.make_move(self.right_motor, self.duty_cycle_sp * -1) |
| 99 | + |
| 100 | + def make_move(self, motor, dc_sp): |
| 101 | + def move(state): |
| 102 | + if state: |
| 103 | + motor.run_forever(duty_cycle_sp=dc_sp) |
| 104 | + else: |
| 105 | + motor.stop() |
| 106 | + return move |
| 107 | + |
| 108 | + def main(self): |
| 109 | + |
| 110 | + try: |
| 111 | + while True: |
| 112 | + self.remote.process() |
| 113 | + time.sleep(0.01) |
| 114 | + |
| 115 | + # Exit cleanly so that all motors are stopped |
| 116 | + except (KeyboardInterrupt, Exception) as e: |
| 117 | + log.exception(e) |
| 118 | + |
| 119 | + for motor in list_motors(): |
| 120 | + motor.stop() |
| 121 | + |
| 122 | + |
| 123 | +# ===================== |
| 124 | +# Wheel and Rim classes |
| 125 | +# ===================== |
| 126 | +class Wheel(object): |
| 127 | + """ |
| 128 | + A base class for various types of wheels, tires, etc |
| 129 | + All units are in mm |
| 130 | + """ |
| 131 | + |
| 132 | + def __init__(self, diameter, width): |
| 133 | + self.diameter = float(diameter) |
| 134 | + self.width = float(width) |
| 135 | + self.radius = float(diameter/2) |
| 136 | + self.circumference = diameter * pi |
| 137 | + |
| 138 | + |
| 139 | +# A great reference when adding new wheels is http://wheels.sariel.pl/ |
| 140 | +class EV3RubberWheel(Wheel): |
| 141 | + |
| 142 | + def __init__(self): |
| 143 | + Wheel.__init__(self, 43.2, 21) |
0 commit comments