|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | +# Copyright (c) 2015 Denis Demidov <dennis.demidov@gmail.com> |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# This demo shows how to remote control an Explor3r robot with touch sensor |
| 26 | +# attachment. |
| 27 | +# |
| 28 | +# Red buttons control left motor, blue buttons control right motor. |
| 29 | +# Leds are used to indicate movement direction. |
| 30 | +# Whenever an obstacle is bumped, robot backs away and apologises. |
| 31 | + |
| 32 | +from time import sleep |
| 33 | +from ev3dev.auto import * |
| 34 | + |
| 35 | +# Connect two large motors on output ports B and C |
| 36 | +lmotor, rmotor = [LargeMotor(port) for port in (OUTPUT_B, OUTPUT_C)] |
| 37 | + |
| 38 | +# Check that the motors are actually connected |
| 39 | +assert lmotor.connected |
| 40 | +assert rmotor.connected |
| 41 | + |
| 42 | +# Connect touch sensor and remote control |
| 43 | +ts = TouchSensor(); assert ts.connected |
| 44 | +rc = RemoteControl(); assert rc.connected |
| 45 | + |
| 46 | +# Initialize button handler |
| 47 | +button = Button() |
| 48 | + |
| 49 | +# Turn leds off |
| 50 | +Leds.all_off() |
| 51 | + |
| 52 | +def roll(motor, led_group, direction): |
| 53 | + """ |
| 54 | + Generate remote control event handler. It rolls given motor into given |
| 55 | + direction (1 for forward, -1 for backward). When motor rolls forward, the |
| 56 | + given led group flashes green, when backward -- red. When motor stops, the |
| 57 | + leds are turned off. |
| 58 | +
|
| 59 | + The on_press function has signature required by RemoteControl class. |
| 60 | + It takes boolean state parameter; True when button is pressed, False |
| 61 | + otherwise. |
| 62 | + """ |
| 63 | + def on_press(state): |
| 64 | + if state: |
| 65 | + # Roll when button is pressed |
| 66 | + motor.run_forever(duty_cycle_sp=90*direction) |
| 67 | + Leds.set_color(led_group, direction > 0 and Leds.GREEN or Leds.RED) |
| 68 | + else: |
| 69 | + # Stop otherwise |
| 70 | + motor.stop(stop_command='brake') |
| 71 | + Leds.set(led_group, brightness_pct=0) |
| 72 | + |
| 73 | + return on_press |
| 74 | + |
| 75 | +# Assign event handler to each of the remote buttons |
| 76 | +rc.on_red_up = roll(lmotor, Leds.LEFT, 1) |
| 77 | +rc.on_red_down = roll(lmotor, Leds.LEFT, -1) |
| 78 | +rc.on_blue_up = roll(rmotor, Leds.RIGHT, 1) |
| 79 | +rc.on_blue_down = roll(rmotor, Leds.RIGHT, -1) |
| 80 | + |
| 81 | +# Enter event processing loop |
| 82 | +while not button.any(): |
| 83 | + rc.process() |
| 84 | + |
| 85 | + # Backup when bumped an obstacle |
| 86 | + if ts.value(): |
| 87 | + Sound.speak('Oops, excuse me!') |
| 88 | + |
| 89 | + for motor in (lmotor, rmotor): |
| 90 | + motor.stop(stop_command='brake') |
| 91 | + |
| 92 | + # Turn red lights on |
| 93 | + for led in (Leds.LEFT, Leds.RIGHT): |
| 94 | + Leds.set_color(led, Leds.RED) |
| 95 | + |
| 96 | + # Run both motors backwards for 0.5 seconds |
| 97 | + for motor in (lmotor, rmotor): |
| 98 | + motor.run_timed(duty_cycle_sp=-90, time_sp=500) |
| 99 | + |
| 100 | + # Wait 0.5 seconds while motors are rolling |
| 101 | + sleep(0.5) |
| 102 | + |
| 103 | + Leds.all_off() |
| 104 | + |
| 105 | + sleep(0.01) |
0 commit comments