Robot Commands Python Script

This script makes use of the curses library to take control of the screen and the serial library for sending data over the UART serial port.

It waits for a key press and then decides either to send a command over the serial port, or to take a picture using the raspistill command.

It‘s a bit crude but proves the concept of sending commands over the serial port.

        
#! /usr/bin/env python

import curses
import serial
from subprocess import call

def command(cmd):
    ser = serial.Serial('/dev/ttyAMA0', 9600)
    ser.write(cmd + "\n")
    ser.close

def picture():
    call("/usr/bin/raspistill -o /home/pi/brianpic.jpg", shell=True)
    command("look_forward")

myscreen = curses.initscr()

done = False
while not done:

    myscreen.border(0)
    myscreen.addstr(12, 25, "MyRobot. Enter command:")
    myscreen.refresh()

    key = myscreen.getch()

    if key == ord('1'):
        command("led_on")
    elif key == ord('0'):
        command("led_off")
    elif key == ord('l'):
        command("turn_left")
    elif key == ord('r'):
        command("turn_right")
    elif key == ord('f'):
        command("forward")
    elif key == ord('b'):
        command("reverse")
    elif key == ord('c'):
        command("look_forward")
    elif key == ord('z'):
        command("look_left")
    elif key == ord('x'):
        command("look_right")
    elif key == ord('h'):
        command("halt")
    elif key == ord('p'):
        picture()
    elif key == ord('q'):
        done = True

curses.endwin()


        
comments powered by Disqus