ich habe sehr wenig erfahrung mit Python, aber geringe Erfahrung mit Programmieren allgemein.
ich arbeite im Moment an einen Script, welches mit Servo-Motor steuern lässt. Script läuft auf den Raspberry Pi.
Über eine html Seite kann ich diese mit Steuerelementen steuern. (Links, Rechts, ... )
Ich drücke auf "Links" (Motor schwenkt nach links, und fährt wieder zurück)
Ich drücke auf "Rechts" (Motor schwenkt nach rechts, und fährt wieder zurück)
Das Problem:
Ich möchte die Schwenkung nach Links abbrechen, sobald ich auf Rechts derückt habe.
Wenn ich jetzt auf Links drücke und gleich auch auf Rechts, fährt der Motor gemütlich nach links und danach nach rechts.
Ich würde es gerne schaffen dass in solch einer Situation die Links-Bewegung abgebrochen wird und der Motor sofort nach rechts schwenkt. (also "Links.py" sofort beenden und "Rechts.py" sofort starten)
Realisiert hab ich das folgendermaßen:
Code: Alles auswählen
import time
import atexit
from flask import Flask, render_template, request
app = Flask(__name__)
from Adafruit_PWM_Servo_Driver import PWM
import time
# Load the main form template on webrequest for the root page
@app.route("/")
def main():
# Create a template data dictionary to send any data to the template
templateData = {
'title' : 'PiCam'
}
# Pass the template data into the template picam.html and return it to the user
return render_template('picam.html', **templateData)
# The function below is executed when someone requests a URL with a move direction
@app.route("/<direction>")
def move(direction):
# Choose the direction of the request
if direction == 'left':
execfile("links.py")
elif direction == 'right':
execfile("rechts.py")
elif direction == 'up':
execfile("rauf.py")
elif direction == 'down':
execfile("runter.py")
# Function to manually set a motor to a specific pluse width
@app.route("/<motor>/<pulsewidth>")
def manual(motor,pulsewidth):
# if motor == "pan":
# elif motor == "tilt":
#servoTilt.set_servo(22, int(pulsewidth))
return "Moved"
# Clean everything up when the app exits
#atexit.register(cleanup)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
Code: Alles auswählen
#!/usr/bin/python
from Adafruit_PWM_Servo_Driver import PWM
import time
# ===========================================================================
# LINKS
# ===========================================================================
# Initialise the PWM device using the default address
pwm = PWM(0x40)
# Note if you'd like more debug output you can instead run:
#pwm = PWM(0x40, debug=True)
def setServoPulse(channel, pulse):
pulseLength = 1000000 # 1,000,000 us per second
pulseLength /= 60 # 60 Hz
print "%d us per period" % pulseLength
pulseLength /= 4096 # 12 bits of resolution
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.setPWM(channel, 0, pulse)
pwm.setPWMFreq(60) # Set frequency to 60 Hz
n = 150
i = 300
while n <= i:
i = i - 1
pwm.setPWM(0, 0, i)
time.sleep(0.005)