Created
August 17, 2014 12:38
-
-
Save idreamsi/69c718c2fa921ce1e641 to your computer and use it in GitHub Desktop.
PiZumo v1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from flask import Flask, render_template, request, jsonify | |
# Initialize the Flask application | |
app = Flask(__name__) | |
import pigpio | |
pi = pigpio.pi() # Connect to local Pi. | |
# Motor PINs | |
MOTOR1A = 17 #left fwd | |
MOTOR1B = 18 #left rev | |
MOTOR2A = 23 #right fwd | |
MOTOR2B = 22 #right rev | |
# speed = pwm duty cycle, 0 = off, 255 = max | |
speed = 255 | |
current_direction = (0, 0) | |
# setup gpios | |
pi.set_mode(MOTOR1A, pigpio.OUTPUT) | |
pi.set_mode(MOTOR1B, pigpio.OUTPUT) | |
pi.set_mode(MOTOR2A, pigpio.OUTPUT) | |
pi.set_mode(MOTOR2B, pigpio.OUTPUT) | |
# Route '/' and '/index' to `index` | |
@app.route('/') | |
@app.route('/index') | |
def index(): | |
# Render template | |
return render_template('index.html') | |
@app.route('/_run') | |
def run(): | |
global current_direction | |
global speed | |
id1 = request.args.get('a', 0, type=int) | |
id2 = request.args.get('b', 0, type=int) | |
# (1,1) = fwd, (2,2) = rev, (1,2) = left, (0,0) = right, (1,0) = fwd left, (0,1) = fwd right, (2,0) = rev left, (0,2) = rev right | |
if (id1 >= 0 and id1 <= 2 and id2 >= 0 and id2 <= 2) : | |
print 'Motor1 is = >>> ' + str (id1) + ' - Motor2 is = >>> ' + str (id2) | |
current_direction = (id1,id2) | |
elif (id1 == 3 and id2 == 10): ## low speed | |
speed -= 10 | |
if speed < 50 : | |
speed = 50 | |
print "Speed : "+str(speed)+"\n" | |
################################ | |
elif (id1 == 3 and id2 == 20): | |
print 'id is >>> ' + str (id2) | |
################################ | |
elif (id1 == 3 and id2 == 15): ## high speed | |
speed += 10 | |
if speed > 255 : | |
speed = 255 | |
print "Speed : "+str(speed)+"\n" | |
################################ | |
else: | |
current_direction = (0,0) | |
#time.sleep(.2) | |
motor_change() | |
return jsonify(result=id1,result2=id2,spdresult=speed) | |
@app.route ('/stop') | |
def stopgpio(): | |
pi.stop() | |
def motor_change(): | |
# motor 1 | |
if (current_direction[0] == 1) : | |
pi.set_PWM_dutycycle(MOTOR1A, speed) | |
pi.set_PWM_dutycycle(MOTOR1B, 0) | |
elif (current_direction[0] == 2) : | |
pi.set_PWM_dutycycle(MOTOR1A, 0) | |
pi.set_PWM_dutycycle(MOTOR1B, speed) | |
# if 0 (stop) or invalid stop anyway | |
else : | |
pi.set_PWM_dutycycle(MOTOR1A, 0) | |
pi.set_PWM_dutycycle(MOTOR1B, 0) | |
# motor 2 | |
if (current_direction[1] == 1) : | |
pi.set_PWM_dutycycle(MOTOR2A, speed) | |
pi.set_PWM_dutycycle(MOTOR2B, 0) | |
elif (current_direction[1] == 2) : | |
pi.set_PWM_dutycycle(MOTOR2A, 0) | |
pi.set_PWM_dutycycle(MOTOR2B, speed) | |
# if 0 (stop) or invalid stop anyway | |
else : | |
pi.set_PWM_dutycycle(MOTOR2A, 0) | |
pi.set_PWM_dutycycle(MOTOR2B, 0) | |
# Run | |
if __name__ == '__main__': | |
app.run(host = "0.0.0.0",port = 80,debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment