#Author: Daniel Zou #Date: 09-05-15 #Description: PiUI-based remote for MotorHAT-driven motors #imports import functools import os import random import time from piui import PiUi from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor import atexit #setup current_dir = os.path.dirname(os.path.abspath(__file__)) mh = Adafruit_MotorHAT(addr=0x61) turnsRightMotor = mh.getMotor(1) turnsLeftMotor = mh.getMotor(2) upDownMotor = mh.getMotor(3) direction = Adafruit_MotorHAT.FORWARD def turnOffMotors(): mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(2).run(Adafruit_MotorHAT.ELEASE) mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE) def forward(): turnsLeftMotor.run(direction) turnsRightMotor.run(direction) turnsLeftMotor.setSpeed(255) turnsRightMotor.setSpeed(255) time.sleep(.5) def left(): turnsLeftMotor.run(direction) for s in reversed(range(255)): turnsLeftMotor.setSpeed(s) time.sleep(0.01) time.sleep(.5) def right(): turnsRightMotor.run(direction) for s in reversed(range(255)): turnsRightMotor.setSpeed(s) time.sleep(0.01) time.sleep(.5) def up(): upDownMotor.run(direction) upDownMotor.setSpeed(255) time.sleep(.5) def changeGearsTo(gear): if gear == "drive": direction = Adafruit_MotorHAT.FORWARD elif gear == "reverse": direction = Adafruit_MotorHAT.BACKWARD else: direction = Adafruit_MotorHAT.FORWARD class PiUI(object): def __init__(self): self.ui = PiUi(img_dir=os.path.join(current_dir, 'imgs')) def page_remote(self): self.page = self.ui.new_ui_page(title="ROV Remote") Go = self.page.add_button("Go", forward()) self.page.add_element("br") Right = self.page.add_button("Right", right()) self.page.add_element("br") Left = self.page.add_button("Left", left()) self.page.add_element("br") Up = self.page.add_button("Lift", up()) self.page.add_element("br") Drive = self.page.add_button("Drive", changeGearsTo("drive")) self.page.add_element("br") Reverse = self.page.add_button("Reverse", changeGearsTo("reverse")) def main(self): self.page_remote() self.ui.done() def main(): piui = PiUI() piui.main() atexit.register(piui.turnOffMotors) while True: turnOffMotors() if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question