def execute(): print('Startup') initio.init(Motors=True) cmd_forwards = 'f' cmd_backwards = 'b' cmd_left = 'l' cmd_right = 'r' cmd_stop = 's' cmd_query = '?' r = redis.Redis(host='192.168.0.1', port=6379, db=0, decode_responses=True) p = r.pubsub(ignore_subscribe_messages=True) p.subscribe('fernando') r.publish('services', 'fernando.on') systemd.daemon.notify('READY=1') print('Startup complete') try: initio.stop() r.publish('fernando.status', cmd_stop) last_cmd = cmd_stop for message in p.listen(): cmd = message['data'] if cmd == cmd_forwards: initio.forward(100) elif cmd == cmd_backwards: initio.reverse(100) elif cmd == cmd_left: initio.spinLeft(100) elif cmd == cmd_right: initio.spinRight(100) elif cmd == cmd_stop: initio.stop() elif cmd == cmd_query: # Don't need to do anything in this case pass if cmd != cmd_query: last_cmd = cmd r.publish('fernando.status', last_cmd) except: p.close() initio.stop() initio.cleanup() r.publish('services', 'fernando.off') print('Goodbye')
def init(): global running, xboxController initio.init() xboxController = XboxController.XboxController( controllerCallBack = controllerCallBack, joystickNo = 0, deadzone = 0.5, scale = 1, invertYAxis = False) print "Set RTHUMBX callback" xboxController.setupControlCallback(xboxController.XboxControls.RTHUMBX, rightThumbXCallBack) print "Set RTHUMBY callback" xboxController.setupControlCallback(xboxController.XboxControls.RTHUMBY, rightThumbYCallBack) print "Set START callback" xboxController.setupControlCallback(xboxController.XboxControls.START, startButtonCallBack) print "Start xboxController" xboxController.start() running = True
####### # # app.py ####### from flask import Flask, render_template, request, redirect, url_for, make_response import initio import RPi.GPIO as GPIO #GPIO.setmode(GPIO.BOARD) #set up GPIO initio.init() speed = 60 app = Flask(__name__) #set up flask server #when the root IP is selected, return index.html page @app.route('/') def index(): return render_template('index.html') #recieve which pin to change from the button press on index.html #each button returns a number that triggers a command in this function # #Uses methods from initio.py to send commands to the GPIO to operate the initio @app.route('/<changepin>', methods=['POST']) def reroute(changepin): changePin = int(changepin) #cast changepin to an int
# Creates thread to detect and avoid obstacles class obstacleThread(threading.Thread) : def _init_(self) : threading.Thread._init_(self) def run(self) : checkObstacle() # Creates thread to send ultrasonic sensor distance every 2 seconds class ultraThread(threading.Thread) : def _init_(self) : threading.Thread._init_(self) def run(self) : sendUltra() # Defines some variables and sets up GPIO pins initio.init() shouldCheck = False shouldUltra = False speed = 60 commands = [] values = [] # Creates socket which waits for a connection createSocket() # Once connection received creates and starts all threads to listen for, # store and execute commands lock = threading.Lock() thread1 = inputThread() thread2 = actionThread()
def execute(): print('Startup') if len(sys.argv) > 1 and sys.argv[1] == 'line': print('Line mode') initio.init(Line=True) message_prefix = 'line' left_sensor = initio.lineLeft right_sensor = initio.lineRight else: print('Obstacle mode') initio.init(IR=True) message_prefix = 'obstacle' left_sensor = initio.irFL right_sensor = initio.irFR redis_queue = 'redeye.' + message_prefix redis_queue_left = redis_queue + '.left' redis_queue_right = redis_queue + '.right' message_left_off = message_prefix + '.left.off' message_left_on = message_prefix + '.left.on' message_right_off = message_prefix + '.right.off' message_right_on = message_prefix + '.right.on' r = redis.Redis(host='192.168.0.1', port=6379, db=0, decode_responses=True) p = r.pubsub(ignore_subscribe_messages=True) p.subscribe(redis_queue) r.publish('services', redis_queue + '.on') systemd.daemon.notify('READY=1') print('Startup complete') try: def left_callback(c): if GPIO.input(left_sensor): r.publish(redis_queue_left, message_left_off) else: r.publish(redis_queue_left, message_left_on) def right_callback(c): if GPIO.input(right_sensor): r.publish(redis_queue_right, message_right_off) else: r.publish(redis_queue_right, message_right_on) GPIO.add_event_detect(left_sensor, GPIO.BOTH, callback=left_callback, bouncetime=100) GPIO.add_event_detect(right_sensor, GPIO.BOTH, callback=right_callback, bouncetime=100) left_callback(left_sensor) right_callback(right_sensor) for message in p.listen(): # If message is received, send current status left_callback(left_sensor) right_callback(right_sensor) except: p.close() initio.cleanup() r.publish('services', redis_queue + '.off') print('Goodbye')
import initio initio.init(Motors=True) initio.stop() initio.cleanup()
def execute(): print('Startup') x = {'init': 0, 'max': 80, 'min': -80} y = {'init': 10, 'max': 80, 'min': -25} initio.init(Servos=True) initio.setServo(0, x['init']) initio.setServo(1, y['init']) r = redis.Redis(host='192.168.0.1', port=6379, db=0, decode_responses=True) p = r.pubsub(ignore_subscribe_messages=True) p.subscribe('jaxx') r.publish('services', 'jaxx.on') systemd.daemon.notify('READY=1') print('Startup complete') try: r.publish('jaxx.head', str(x['init']) + "," + str(y['init'])) x_angle = x['init'] y_angle = y['init'] for message in p.listen(): cmd = message['data'] if cmd == '0': x_angle = x['init'] y_angle = y['init'] elif cmd == '?': # Query mode - don't need to update anything - just pass through pass else: angles = cmd.split(',') x_angle = int(angles[0]) y_angle = int(angles[1]) # Make sure it's valid if x_angle > x['max']: x_angle = x['max'] if x_angle < x['min']: x_angle = x['min'] if y_angle > y['max']: y_angle = y['max'] if y_angle < y['min']: y_angle = y['min'] initio.setServo(0, x_angle) initio.setServo(1, y_angle) r.publish('jaxx.head', str(x_angle) + "," + str(y_angle)) except: p.close() initio.setServo(0, x['init']) initio.setServo(1, y['init']) initio.cleanup() r.publish('services', 'jaxx.off') print('Goodbye')