Stap 13: De Python/kolf code
Dit bot gebruikt Python en het kolf web framework. U kunt meer informatie over kolf hier als u geïnteresseerd bent.
Het grote verschil van een kolf app en normale Python script is klasse/methode gebruikt om te doen de afhandeling van URI. Buiten dat is het vrijwel normale Python voor het grootste deel.
#!/usr/bin/env python # # Wifi/Web driven Rover # # Written by Scott Beasley - 2015 # # Uses RPIO, pyserial and Flask # import time import serial from RPIO import PWM from flask import Flask, render_template, request app = Flask (__name__, static_url_path = '') # Connect to the comm port to talk to the Roboclaw motor controller try: # Change the baud rate here if different than 19200 roboclaw = serial.Serial ('/dev/ttyAMA0', 19200) except IOError: print ("Comm port not found") sys.exit (0) # Speed and drive control variables last_direction = -1 speed_offset = 84 turn_tm_offset = 0.166 run_time = 0.750 # Servo neutral position (home) servo_pos = 1250 servo = PWM.Servo ( ) servo.set_servo (18, servo_pos) # A little dwell for settling down time time.sleep (3) # # URI handlers - all the bot page actions are done here # # Send out the bots control page (home page) ("/") def index ( ): return render_template ('index.html', name = None) ("/forward") def forward ( ): global last_direction, run_time print "Forward" go_forward ( ) last_direction = 0 # sleep 100ms + run_time time.sleep (0.100 + run_time) # If not continuous, then halt after delay if run_time > 0: last_direction = -1 halt ( ) return "ok" ("/backward") def backward ( ): global last_direction, run_time print "Backward" go_backward ( ) last_direction = 1 # sleep 100ms + run_time time.sleep (0.100 + run_time) # If not continuous, then halt after delay if run_time > 0: last_direction = -1 halt ( ) return "ok" ("/left") def left ( ): global last_direction, turn_tm_offset print "Left" go_left ( ) last_direction = -1 # sleep second time.sleep (0.500 - turn_tm_offset) # stop halt ( ) time.sleep (0.100) return "ok" ("/right") def right ( ): global last_direction, turn_tm_offset print "Right" go_right ( ) # sleep second time.sleep (0.500 - turn_tm_offset) last_direction = -1 # stop halt ( ) time.sleep (0.100) return "ok" ("/ltforward") def ltforward ( ): global last_direction, turn_tm_offset print "Left forward turn" go_left ( ) # sleep second time.sleep (0.250 - (turn_tm_offset / 2)) last_direction = -1 # stop halt ( ) time.sleep (0.100) return "ok" ("/rtforward") def rtforward ( ): global last_direction, turn_tm_offset print "Right forward turn" go_right ( ) # sleep second time.sleep (0.250 - (turn_tm_offset / 2)) last_direction = -1 # stop halt ( ) time.sleep (0.100) return "ok" ("/stop") def stop ( ): global last_direction print "Stop" halt ( ) last_direction = -1 # sleep 100ms time.sleep (0.100) return "ok" ("/panlt") def panlf ( ): global servo_pos print "Panlt" servo_pos -= 100 if servo_pos < 500: servo_pos = 500 servo.set_servo (18, servo_pos) # sleep 150ms time.sleep (0.150) return "ok" ("/panrt") def panrt ( ): global servo_pos print "Panrt" servo_pos += 100 if servo_pos > 2500: servo_pos = 2500 servo.set_servo (18, servo_pos) # sleep 150ms time.sleep (0.150) return "ok" ("/home") def home ( ): global servo_pos print "Home" servo_pos = 1250 servo.set_servo (18, servo_pos) # sleep 150ms time.sleep (0.150) return "ok" ("/panfull_lt") def panfull_lt ( ): global servo_pos print "Pan full left" servo_pos = 500 servo.set_servo (18, servo_pos) # sleep 150ms time.sleep (0.150) return "ok" ("/panfull_rt") def panfull_rt ( ): global servo_pos print "Pan full right" servo_pos = 2500 servo.set_servo (18, servo_pos) # sleep 150ms time.sleep (0.150) return "ok" ("/speed_low") def speed_low ( ): global speed_offset, last_direction, turn_tm_offset speed_offset = 42 turn_tm_offset = 0.001 # Update current direction to get new speed if last_direction == 0: go_forward ( ) if last_direction == 1: go_backward ( ) # sleep 150ms time.sleep (0.150) return "ok" ("/speed_mid") def speed_mid ( ): global speed_offset, last_direction, turn_tm_offset speed_offset = 84 turn_tm_offset = 0.166 # Update current direction to get new speed if last_direction == 0: go_forward ( ) if last_direction == 1: go_backward ( ) # sleep 150ms time.sleep (0.150) return "ok" ("/speed_hi") def speed_hi ( ): global speed_offset, last_direction, turn_tm_offset speed_offset = 126 turn_tm_offset = 0.332 # Update current direction to get new speed if last_direction == 0: go_forward ( ) if last_direction == 1: go_backward ( ) # sleep 150ms time.sleep (0.150) return "ok" ("/continuous") def continuous ( ): global run_time print "Continuous run" run_time = 0 # sleep 100ms time.sleep (0.100) return "ok" ("/mid_run") def mid_run ( ): global run_time print "Mid run" run_time = 0.750 halt ( ) # sleep 100ms time.sleep (0.100) return "ok" ("/short_time") def short_time ( ): global run_time print "Short run" run_time = 0.300 halt ( ) # sleep 100ms time.sleep (0.100) return "ok" # # Motor drive functions # def go_forward ( ): global speed_offset if speed_offset != 42: roboclaw.write (chr (1 + speed_offset)) roboclaw.write (chr (128 + speed_offset)) else: roboclaw.write (chr (127 - speed_offset)) roboclaw.write (chr (255 - speed_offset)) def go_backward ( ): global speed_offset if speed_offset != 42: roboclaw.write (chr (127 - speed_offset)) roboclaw.write (chr (255 - speed_offset)) else: roboclaw.write (chr (1 + speed_offset)) roboclaw.write (chr (128 + speed_offset)) def go_left ( ): global speed_offset if speed_offset != 42: roboclaw.write (chr (127 - speed_offset)) roboclaw.write (chr (128 + speed_offset)) else: roboclaw.write (chr (1 + speed_offset)) roboclaw.write (chr (255 - speed_offset)) def go_right ( ): global speed_offset if speed_offset != 42: roboclaw.write (chr (1 + speed_offset)) roboclaw.write (chr (255 - speed_offset)) else: roboclaw.write (chr (127 - speed_offset)) roboclaw.write (chr (128 + speed_offset)) def halt ( ): roboclaw.write (chr (0)) if __name__ == "__main__" : app.run (host = '0.0.0.0', port = 80, debug = True)
Als u niet willen of nodig debug informatie uit kolf, stelt u foutopsporing op 'false' op de app.run-regel.
Als __name__ == "__main__":
app.run (host = '0.0.0.0', poort 80, = debug = False)
U kunt ook het wijzigen van de poort waarop de kolf http server hier ook luistert.