Untitled
unknown
python
a year ago
5.4 kB
5
Indexable
from flask import Flask, render_template, request
import RPi.GPIO as GPIO
import time
import threading
app = Flask(__name__, static_folder="static", template_folder="templates")
# Setup GPIO pins
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
# Motor Pins Setup
GPIO.setup(12, GPIO.OUT) # Left Motor Forward
GPIO.setup(32, GPIO.OUT) # Left Motor Backward
GPIO.setup(33, GPIO.OUT) # Right Motor Forward
GPIO.setup(35, GPIO.OUT) # Right Motor Backward
# Limit switch pins
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Left limit switch
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Right limit switch
# Set up PWM for motor speed control
left_pwm_fwd = GPIO.PWM(12, 500)
left_pwm_bwd = GPIO.PWM(32, 500)
right_pwm_fwd = GPIO.PWM(33, 500)
right_pwm_bwd = GPIO.PWM(35, 500)
left_pwm_fwd.start(0)
left_pwm_bwd.start(0)
right_pwm_fwd.start(0)
right_pwm_bwd.start(0)
# Function to scale the speed from 1-1000 to 0-100 (PWM duty cycle)
def scale_speed(speed):
if speed < 0:
return 0
elif speed > 100:
return 100
return speed
# Function to control left motor
def control_left_motor(speed, direction):
scaled_speed = scale_speed(speed)
if direction == 'forward':
left_pwm_bwd.ChangeDutyCycle(0)
left_pwm_fwd.ChangeDutyCycle(scaled_speed)
elif direction == 'backward':
left_pwm_fwd.ChangeDutyCycle(0)
left_pwm_bwd.ChangeDutyCycle(scaled_speed)
# Function to control right motor
def control_right_motor(speed, direction):
scaled_speed = scale_speed(speed)
if direction == 'forward':
right_pwm_bwd.ChangeDutyCycle(0)
right_pwm_fwd.ChangeDutyCycle(scaled_speed)
elif direction == 'backward':
right_pwm_fwd.ChangeDutyCycle(0)
right_pwm_bwd.ChangeDutyCycle(scaled_speed)
# Function to read the limit switches
def read_limit_switches():
left_pressed = GPIO.input(11) == GPIO.LOW # Left limit switch pressed
right_pressed = GPIO.input(13) == GPIO.LOW # Right limit switch pressed
return left_pressed, right_pressed
# Update motor speeds based on limit switches
def update_motor_speeds():
global starting_speed
left_pressed, right_pressed = read_limit_switches()
# Print button states for debugging
print(f"Left Limit Switch Pressed: {left_pressed}, Right Limit Switch Pressed: {right_pressed}")
if left_pressed and right_pressed:
# If both limit switches are pressed, move both motors at the normal speed
control_left_motor(starting_speed, 'forward')
control_right_motor(starting_speed, 'forward')
elif left_pressed or right_pressed:
# If one limit switch is pressed, speed up both motors by 50 (but don't exceed 100)
control_left_motor(min(starting_speed - 50, 100), 'forward')
control_right_motor(min(starting_speed - 50, 100), 'forward')
else:
# Neither limit switch is pressed, move both motors at the normal speed
control_left_motor(starting_speed, 'forward')
control_right_motor(starting_speed, 'forward')
# Variables for starting speed and delay
starting_speed = 100 # Default speed in mm/s
delay = 5 # Default delay in seconds
@app.route('/')
def index():
# Render the page with the current values of speed and delay
return render_template('index.html', speed=starting_speed, delay=delay)
@app.route('/set-speed', methods=['POST'])
def set_speed():
global starting_speed
# Get the new speed value from the form and update it
new_speed = request.form.get('speed', type=int)
if 1 <= new_speed <= 1000:
starting_speed = new_speed
return render_template('index.html', speed=starting_speed, delay=delay)
@app.route('/set-delay', methods=['POST'])
def set_delay():
global delay
# Get the new delay value from the form and update it
new_delay = request.form.get('delay', type=int)
if 1 <= new_delay <= 10:
delay = new_delay
return render_template('index.html', speed=starting_speed, delay=delay)
@app.route('/start')
def start_robot():
# Simulate a delay before starting the robot
time.sleep(delay)
# Start the robot with the set starting speed
left_speed = starting_speed
right_speed = starting_speed
control_left_motor(left_speed, 'forward')
control_right_motor(right_speed, 'forward')
# Start a background thread to check limit switches and adjust speeds
threading.Thread(target=monitor_limit_switches).start()
return f"Robot started with speed {starting_speed} mm/s and delay {delay} seconds!"
@app.route('/stop')
def stop_robot():
control_left_motor(0, 'forward')
control_right_motor(0, 'forward')
return "Robot stopped!"
@app.route('/shutdown')
def shutdown():
left_pwm_fwd.stop()
left_pwm_bwd.stop()
right_pwm_fwd.stop()
right_pwm_bwd.stop()
GPIO.cleanup()
return "System shutdown!"
# Function to continuously monitor limit switches in the background
def monitor_limit_switches():
while True:
update_motor_speeds() # Update motor speeds based on limit switches
time.sleep(0.2) # Delay for 0.2 seconds
if __name__ == '__main__':
try:
app.run(host='0.0.0.0', port=5001)
except KeyboardInterrupt:
GPIO.cleanup()
Editor is loading...
Leave a Comment