Ich habe mir ein Fahrzeug gebaut das ich gern über ein Smartphone steuern wollte. Im Fahrzeug ist ein Raspberry verbaut das ein WLAN-AP aufbaut damit ich mich mit dem Smartphone verbinden kann und hier eine HTML-Seite zur Steuerung aufrufen kann. Zum einen wollte ich über Schaltflächen Steuerungsbefehle manuell weitergeben, das funktioniert auch alles problemlos. Als nächstes wollte ich eine automatische Steuerung realisieren bei der das Fehrzeug über zwei Ultraschallsensoren Hinternisse erkennt und automatisch ausweicht. Die automatische Steuerung funktioniert auch soweit gut. Aber ich wollte gern über die HTML-Seite über das Smartphone die automatische Steuerung ein und ausschalten, das funktioniert leider nicht. Wahrscheinlich liegt es daran dass in main_automatik eine Schleife intergriert ist. Gibt es eine Möglichkeit diese Schleife irgendwie über die HTML-Seite zu unterbrechen? Oder hat jemand vielleicht eine andere Idee? Ich hoffe ich konnte mein Problem einigermaßen verständlich erklären. Für jede Hilfe bin ich sehr dankbar.
Hier mein Python-Skript:
Code: Alles auswählen
# Import Module
import time
import RPi.GPIO as GPIO
from flask import Flask, request, redirect, render_template
from multiprocessing import Process
# Variablen definieren
app = Flask(__name__)
global varhit
# Pins definieren
GPIOgruen = 18 # LED gruen
GPIOgelb = 23 # LED gelb
GPIOrot = 24 # LED rot
GPIOecho_re = 25 # Echo Pin am Ultraschallsensor rechts
GPIOtrig_re = 12 # Trigger Pin am Ultraschallsensor rechts
GPIOecho_li = 20 # Echo Pin am Ultraschallsensor links
GPIOtrig_li = 16 # Trigger Pin am Ultraschallsensor links
GPIOmotor1en1 = 4 # Motor 1 enable
GPIOmotor1in1 = 17 # Motor 1 input1
GPIOmotor1in2 = 27 # Motor 1 input2
GPIOmotor2en2 = 5 # Motor 2 enable
GPIOmotor2in3 = 6 # Motor 2 input3
GPIOmotor2in4 = 13 # Motor 2 input4
# GPIO Modus festlegen
GPIO.setmode(GPIO.BCM) # Modus BCM festgelegt
GPIO.setwarnings(False) # GPIO Warnungen deaktiviert
# Pins IN und OUT festlegen
GPIO.setup(GPIOgruen, GPIO.OUT)
GPIO.setup(GPIOgelb, GPIO.OUT)
GPIO.setup(GPIOrot, GPIO.OUT)
GPIO.setup(GPIOecho_re, GPIO.IN)
GPIO.setup(GPIOtrig_re, GPIO.OUT)
GPIO.setup(GPIOecho_li, GPIO.IN)
GPIO.setup(GPIOtrig_li, GPIO.OUT)
GPIO.setup(GPIOmotor1en1, GPIO.OUT)
GPIO.setup(GPIOmotor1in1, GPIO.OUT)
GPIO.setup(GPIOmotor1in2, GPIO.OUT)
GPIO.setup(GPIOmotor2en2, GPIO.OUT)
GPIO.setup(GPIOmotor2in3, GPIO.OUT)
GPIO.setup(GPIOmotor2in4, GPIO.OUT)
# Pins zuruecksetzen
GPIO.output(GPIOgruen, False)
GPIO.output(GPIOgelb, False)
GPIO.output(GPIOrot, False)
GPIO.output(GPIOtrig_re, False)
GPIO.output(GPIOtrig_li, False)
GPIO.output(GPIOmotor1en1, False)
GPIO.output(GPIOmotor1in1, False)
GPIO.output(GPIOmotor1in2, False)
GPIO.output(GPIOmotor2en2, False)
GPIO.output(GPIOmotor2in3, False)
GPIO.output(GPIOmotor2in4, False)
def led_start():
GPIO.output(GPIOrot, True)
time.sleep (1.0)
GPIO.output(GPIOgelb, True)
time.sleep (1.0)
GPIO.output(GPIOgruen, True)
time.sleep (1.0)
GPIO.output(GPIOgelb, False)
GPIO.output(GPIOrot, False)
def led_gruen():
GPIO.output(GPIOrot, False)
GPIO.output(GPIOgelb, False)
GPIO.output(GPIOgruen, True)
def led_gelb():
GPIO.output(GPIOrot, False)
GPIO.output(GPIOgelb, True)
GPIO.output(GPIOgruen, False)
def led_rot():
GPIO.output(GPIOrot, True)
GPIO.output(GPIOgelb, False)
GPIO.output(GPIOgruen, False)
def measure_distance_re():
GPIO.output(GPIOtrig_re, True) # High Signal auf Trigger geben
time.sleep(0.00001)
GPIO.output(GPIOtrig_re, False) # Low Signal auf Trigger geben
start_time = time.time() # Initialisiere Zeit
while GPIO.input(GPIOecho_re) == 0: # Solange Echo low ist
start_time = time.time() # Zeit wird in Variable start_time geschrieben
while GPIO.input(GPIOecho_re) == 1: # Solange Echo high ist
stop_time = time.time() # Zeit wird in Variable stop_time geschrieben
# Entfernung berechnen
time_elapsed = stop_time - start_time # start_time wird von stop_time abgezogen
distance_re = (time_elapsed * 34300) / 2 # Berechnung der Distanz in cm
return distance_re # Gibt das Ergebnis zurueck
def measure_distance_li():
GPIO.output(GPIOtrig_li, True) # High Signal auf Trigger geben
time.sleep(0.00001)
GPIO.output(GPIOtrig_li, False) # Low Signal auf Trigger geben
start_time = time.time() # Initialisiere Zeit
while GPIO.input(GPIOecho_li) == 0: # Solange Echo low ist
start_time = time.time() # Zeit wird in Variable start_time geschrieben
while GPIO.input(GPIOecho_li) == 1: # Solange Echo high ist
stop_time = time.time() # Zeit wird in Variable stop_time geschrieben
# Entfernung berechnen
time_elapsed = stop_time - start_time # start_time wird von stop_time abgezogen
distance_li = (time_elapsed * 34300) / 2 # Berechnung der Distanz in cm
return distance_li # Gibt das Ergebnis zurueck
def move_stop():
# Motor1
GPIO.output(GPIOmotor1en1, False)
GPIO.output(GPIOmotor1in1, False)
GPIO.output(GPIOmotor1in2, False)
# Motor2
GPIO.output(GPIOmotor2en2, False)
GPIO.output(GPIOmotor2in3, False)
GPIO.output(GPIOmotor2in4, False)
def move_forward():
# Motor1
GPIO.output(GPIOmotor1en1, True)
GPIO.output(GPIOmotor1in1, True)
GPIO.output(GPIOmotor1in2, False)
# Motor2
GPIO.output(GPIOmotor2en2, True)
GPIO.output(GPIOmotor2in3, True)
GPIO.output(GPIOmotor2in4, False)
def move_backward():
# Motor1
GPIO.output(GPIOmotor1en1, True)
GPIO.output(GPIOmotor1in1, False)
GPIO.output(GPIOmotor1in2, True)
# Motor2
GPIO.output(GPIOmotor2en2, True)
GPIO.output(GPIOmotor2in3, False)
GPIO.output(GPIOmotor2in4, True)
def move_left():
# Motor1
GPIO.output(GPIOmotor1en1, True)
GPIO.output(GPIOmotor1in1, False)
GPIO.output(GPIOmotor1in2, True)
# Motor2
GPIO.output(GPIOmotor2en2, True)
GPIO.output(GPIOmotor2in3, True)
GPIO.output(GPIOmotor2in4, False)
def move_right():
# Motor1
GPIO.output(GPIOmotor1en1, True)
GPIO.output(GPIOmotor1in1, True)
GPIO.output(GPIOmotor1in2, False)
# Motor2
GPIO.output(GPIOmotor2en2, True)
GPIO.output(GPIOmotor2in3, False)
GPIO.output(GPIOmotor2in4, True)
def turn_left():
led_rot()
move_stop()
time.sleep(0.5)
move_backward()
time.sleep(2.0)
move_stop()
time.sleep(0.5)
move_left()
time.sleep(1.2)
move_stop()
time.sleep(0.5)
def turn_right():
led_rot()
move_stop()
time.sleep(0.5)
move_backward()
time.sleep(2.0)
move_stop()
time.sleep(0.5)
move_right()
time.sleep(1.2)
move_stop()
time.sleep(0.5)
def main_automatik(): # Funktion automatische Steuerung
try:
while True:
distance_re = measure_distance_re() # Fuehrt die Funktion measure_distance_re aus und uebernimmt Ergebnis
print "Entfernung rechts = %.2f cm" % distance_re
distance_li = measure_distance_li() # Fuehrt die Funktion measure_distance_li aus und uebernimmt Ergebnis
print "Entfernung links = %.2f cm" % distance_li
time.sleep(0.2)
if distance_re < 30.00 or distance_li < 30.00:
if distance_re < 30.00:
turn_left()
if distance_li < 30.00:
turn_right()
elif distance_re < 40.00 or distance_li < 40.00:
led_gelb()
else:
led_gruen()
move_forward()
except KeyboardInterrupt: # Unterbricht bei Eingabe von Strg + c
print "Messung gestoppt"
GPIO.cleanup()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/controlit', methods = ['POST'])
def main_manuell(): # Funktion manuelle Steuerung
button_hit = request.form['buttonPress']
global varhit
varhit = button_hit
if button_hit == 'Vorwaerts':
print "Vorward"
move_stop()
time.sleep(0.5)
move_forward()
if button_hit == 'Zurueck':
print "Back"
move_stop()
time.sleep(0.5)
move_backward()
if button_hit == 'Links':
print "Left"
move_stop()
move_left()
time.sleep(0.5)
move_stop()
if button_hit == 'Rechts':
print "Right"
move_stop()
move_right()
time.sleep(0.5)
move_stop()
if button_hit == 'Stop':
print "Stop"
move_stop()
#if button_hit == 'Automatik':
#main_automatik
return redirect('/')
if __name__ == '__main__':
led_start()
app.run(host='0.0.0.0', port=5000, debug = True)
main_manuell()