Skip to content

Instantly share code, notes, and snippets.

@rusty-snake
Last active July 2, 2019 09:39
Show Gist options
  • Save rusty-snake/546ce0ba04484a7236f07205dd0adb29 to your computer and use it in GitHub Desktop.
Save rusty-snake/546ce0ba04484a7236f07205dd0adb29 to your computer and use it in GitHub Desktop.
ev3dev: python3-ev3dev2: A pogramm to steer a robot with two touch-sensors.
#!/usr/bin/env python3
#
# MIT License
#
# Copyright (c) 2018 rusty-snake (https://github.com/rusty-snake) <print_hello_world+License@protonmail.com>
#
# 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.
#
print("Starting...")
print("Loading Modules...", end="")
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3
from ev3dev2.sensor.lego import TouchSensor
print(" [OK]")
print("Initializing...", end="")
ts_left = TouchSensor(INPUT_2)
ts_right = TouchSensor(INPUT_3)
ts_exit = TouchSensor(INPUT_1)
motor_left = LargeMotor(OUTPUT_A)
motor_right = LargeMotor(OUTPUT_B)
print(" [OK]")
print("Ready...")
while True:
if ts_left.is_pressed and not motor_left.is_running:
print("Starting left Motor...", end="")
motor_left.on(SpeedPercent(50))
print(" [OK]")
elif not ts_left.is_pressed and motor_left.is_running:
print("Stoping left Motor...", end="")
motor_left.stop()
print(" [OK]")
if ts_right.is_pressed and not motor_right.is_running:
print("Starting right Motor...", end="")
motor_right.on(SpeedPercent(50))
print(" [OK]")
elif not ts_right.is_pressed and motor_right.is_running:
print("Stoping right Motor...", end="")
motor_right.stop()
print(" [OK]")
if ts_exit.is_pressed:
print("Reseting Motors...", end="")
motor_left.reset()
motor_right.reset()
print(" [OK]")
print("Terminating...")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment