slitaz-arm rev 188

Add support for PiBorg (A PiCy will be used at Paris)
author Christophe Lincoln <pankso@slitaz.org>
date Thu May 15 18:57:11 2014 +0200 (2014-05-15)
parents abffc259e9cb
children 8d67f2277f55
files rpi/data/piborg-rkey.desktop rpi/data/piborg.png rpi/piborg/RemoteKeyBorgC.py rpi/piborg/RemoteKeyBorgS.py rpi/piborg/TurtleBorg.py rpi/piborg/piborg
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rpi/data/piborg-rkey.desktop	Thu May 15 18:57:11 2014 +0200
     1.3 @@ -0,0 +1,6 @@
     1.4 +[Desktop Entry]
     1.5 +Type=Application
     1.6 +Name=PiBorg Remote Keyboard
     1.7 +Icon=piborg.png
     1.8 +Exec=RemoteKeyBorgC.py
     1.9 +Categories=Utility;
     2.1 Binary file rpi/data/piborg.png has changed
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rpi/piborg/RemoteKeyBorgC.py	Thu May 15 18:57:11 2014 +0200
     3.3 @@ -0,0 +1,118 @@
     3.4 +#!/usr/bin/env python
     3.5 +# coding: Latin-1
     3.6 +
     3.7 +# Load library functions we want
     3.8 +import socket
     3.9 +import time
    3.10 +import pygame
    3.11 +
    3.12 +# Settings for the RemoteKeyBorg client
    3.13 +broadcastIP = '192.168.0.255'           # IP address to send to, 255 in one or more positions is a broadcast / wild-card
    3.14 +broadcastPort = 9038                    # What message number to send with (LEDB on an LCD)
    3.15 +leftDrive = 1                           # Drive number for left motor
    3.16 +rightDrive = 4                          # Drive number for right motor
    3.17 +interval = 0.1                          # Time between keyboard updates in seconds, smaller responds faster but uses more processor time
    3.18 +regularUpdate = True                    # If True we send a command at a regular interval, if False we only send commands when keys are pressed or released
    3.19 +
    3.20 +# Setup the connection for sending on
    3.21 +sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)       # Create the socket
    3.22 +sender.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)                        # Enable broadcasting (sending to many IPs based on wild-cards)
    3.23 +sender.bind(('0.0.0.0', 0))                                                         # Set the IP and port number to use locally, IP 0.0.0.0 means all connections and port 0 means assign a number for us (do not care)
    3.24 +
    3.25 +# Setup pygame and key states
    3.26 +global hadEvent
    3.27 +global moveUp
    3.28 +global moveDown
    3.29 +global moveLeft
    3.30 +global moveRighte
    3.31 +global moveQuit
    3.32 +hadEvent = True
    3.33 +moveUp = False
    3.34 +moveDown = False
    3.35 +moveLeft = False
    3.36 +moveRight = False
    3.37 +moveQuit = False
    3.38 +pygame.init()
    3.39 +screen = pygame.display.set_mode([300,300])
    3.40 +pygame.display.set_caption("RemoteKeyBorg - Press [ESC] to quit")
    3.41 +
    3.42 +# Function to handle pygame events
    3.43 +def PygameHandler(events):
    3.44 +    # Variables accessible outside this function
    3.45 +    global hadEvent
    3.46 +    global moveUp
    3.47 +    global moveDown
    3.48 +    global moveLeft
    3.49 +    global moveRight
    3.50 +    global moveQuit
    3.51 +    # Handle each event individually
    3.52 +    for event in events:
    3.53 +        if event.type == pygame.QUIT:
    3.54 +            # User exit
    3.55 +            hadEvent = True
    3.56 +            moveQuit = True
    3.57 +        elif event.type == pygame.KEYDOWN:
    3.58 +            # A key has been pressed, see if it is one we want
    3.59 +            hadEvent = True
    3.60 +            if event.key == pygame.K_UP:
    3.61 +                moveUp = True
    3.62 +            elif event.key == pygame.K_DOWN:
    3.63 +                moveDown = True
    3.64 +            elif event.key == pygame.K_LEFT:
    3.65 +                moveLeft = True
    3.66 +            elif event.key == pygame.K_RIGHT:
    3.67 +                moveRight = True
    3.68 +            elif event.key == pygame.K_ESCAPE:
    3.69 +                moveQuit = True
    3.70 +        elif event.type == pygame.KEYUP:
    3.71 +            # A key has been released, see if it is one we want
    3.72 +            hadEvent = True
    3.73 +            if event.key == pygame.K_UP:
    3.74 +                moveUp = False
    3.75 +            elif event.key == pygame.K_DOWN:
    3.76 +                moveDown = False
    3.77 +            elif event.key == pygame.K_LEFT:
    3.78 +                moveLeft = False
    3.79 +            elif event.key == pygame.K_RIGHT:
    3.80 +                moveRight = False
    3.81 +            elif event.key == pygame.K_ESCAPE:
    3.82 +                moveQuit = False
    3.83 +
    3.84 +try:
    3.85 +    print 'Press [ESC] to quit'
    3.86 +    # Loop indefinitely
    3.87 +    while True:
    3.88 +        # Get the currently pressed keys on the keyboard
    3.89 +        PygameHandler(pygame.event.get())
    3.90 +        if hadEvent or regularUpdate:
    3.91 +            # Keys have changed, generate the command list based on keys
    3.92 +            hadEvent = False
    3.93 +            driveCommands = ['X', 'X', 'X', 'X']                    # Default to do not change
    3.94 +            if moveQuit:
    3.95 +                break
    3.96 +            elif moveLeft:
    3.97 +                driveCommands[leftDrive - 1] = 'OFF'
    3.98 +                driveCommands[rightDrive - 1] = 'ON'
    3.99 +            elif moveRight:
   3.100 +                driveCommands[leftDrive - 1] = 'ON'
   3.101 +                driveCommands[rightDrive - 1] = 'OFF'
   3.102 +            elif moveUp:
   3.103 +                driveCommands[leftDrive - 1] = 'ON'
   3.104 +                driveCommands[rightDrive - 1] = 'ON'
   3.105 +            else:
   3.106 +                # None of our expected keys, stop
   3.107 +                driveCommands[leftDrive - 1] = 'OFF'
   3.108 +                driveCommands[rightDrive - 1] = 'OFF'
   3.109 +            # Send the drive commands
   3.110 +            command = ''
   3.111 +            for driveCommand in driveCommands:
   3.112 +                command += driveCommand + ','
   3.113 +            command = command[:-1]                                  # Strip the trailing comma
   3.114 +            sender.sendto(command, (broadcastIP, broadcastPort))
   3.115 +        # Wait for the interval period
   3.116 +        time.sleep(interval)
   3.117 +    # Inform the server to stop
   3.118 +    sender.sendto('ALLOFF', (broadcastIP, broadcastPort))
   3.119 +except KeyboardInterrupt:
   3.120 +    # CTRL+C exit, inform the server to stop
   3.121 +    sender.sendto('ALLOFF', (broadcastIP, broadcastPort))
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/rpi/piborg/RemoteKeyBorgS.py	Thu May 15 18:57:11 2014 +0200
     4.3 @@ -0,0 +1,99 @@
     4.4 +#!/usr/bin/env python
     4.5 +# coding: Latin-1
     4.6 +
     4.7 +# Load library functions we want
     4.8 +import SocketServer
     4.9 +import RPi.GPIO as GPIO
    4.10 +GPIO.setmode(GPIO.BCM)
    4.11 +GPIO.setwarnings(False)
    4.12 +
    4.13 +# Set which GPIO pins the drive outputs are connected to
    4.14 +DRIVE_1 = 4
    4.15 +DRIVE_2 = 18
    4.16 +DRIVE_3 = 8
    4.17 +DRIVE_4 = 7
    4.18 +
    4.19 +# Set all of the drive pins as output pins
    4.20 +GPIO.setup(DRIVE_1, GPIO.OUT)
    4.21 +GPIO.setup(DRIVE_2, GPIO.OUT)
    4.22 +GPIO.setup(DRIVE_3, GPIO.OUT)
    4.23 +GPIO.setup(DRIVE_4, GPIO.OUT)
    4.24 +
    4.25 +# Map of drives to pins
    4.26 +lDrives = [DRIVE_1, DRIVE_2, DRIVE_3, DRIVE_4]
    4.27 +
    4.28 +# Function to set all drives off
    4.29 +def MotorOff():
    4.30 +    GPIO.output(DRIVE_1, GPIO.LOW)
    4.31 +    GPIO.output(DRIVE_2, GPIO.LOW)
    4.32 +    GPIO.output(DRIVE_3, GPIO.LOW)
    4.33 +    GPIO.output(DRIVE_4, GPIO.LOW)
    4.34 +
    4.35 +# Settings for the RemoteKeyBorg server
    4.36 +portListen = 9038                       # What messages to listen for (LEDB on an LCD)
    4.37 +
    4.38 +# Class used to handle UDP messages
    4.39 +class PicoBorgHandler(SocketServer.BaseRequestHandler):
    4.40 +    # Function called when a new message has been received
    4.41 +    def handle(self):
    4.42 +        global isRunning
    4.43 +
    4.44 +        request, socket = self.request          # Read who spoke to us and what they said
    4.45 +        request = request.upper()               # Convert command to upper case
    4.46 +        driveCommands = request.split(',')      # Separate the command into individual drives
    4.47 +        if len(driveCommands) == 1:
    4.48 +            # Special commands
    4.49 +            if request == 'ALLOFF':
    4.50 +                # Turn all drives off
    4.51 +                MotorOff()
    4.52 +                print 'All drives off'
    4.53 +            elif request == 'EXIT':
    4.54 +                # Exit the program
    4.55 +                isRunning = False
    4.56 +            else:
    4.57 +                # Unknown command
    4.58 +                print 'Special command "%s" not recognised' % (request)
    4.59 +        elif len(driveCommands) == len(lDrives):
    4.60 +            # For each drive we check the command
    4.61 +            for driveNo in range(len(driveCommands)):
    4.62 +                command = driveCommands[driveNo]
    4.63 +                if command == 'ON':
    4.64 +                    # Set drive on
    4.65 +                    GPIO.output(lDrives[driveNo], GPIO.HIGH)
    4.66 +                elif command == 'OFF':
    4.67 +                    # Set drive off
    4.68 +                    GPIO.output(lDrives[driveNo], GPIO.LOW)
    4.69 +                elif command == 'X':
    4.70 +                    # No command for this drive
    4.71 +                    pass
    4.72 +                else:
    4.73 +                    # Unknown command
    4.74 +                    print 'Drive %d command "%s" not recognised!' % (driveNo, command)
    4.75 +        else:
    4.76 +            # Did not get the right number of drive commands
    4.77 +            print 'Command "%s" did not have %d parts!' % (request, len(lDrives))
    4.78 +
    4.79 +try:
    4.80 +    global isRunning
    4.81 +
    4.82 +    # Start by turning all drives off
    4.83 +    MotorOff()
    4.84 +    print '\nStarting remote keyborg...'
    4.85 +    #raw_input('You can now turn on the power, press ENTER to continue')
    4.86 +    # Setup the UDP listener
    4.87 +    remoteKeyBorgServer = SocketServer.UDPServer(('', portListen), PicoBorgHandler)
    4.88 +    # Loop until terminated remotely
    4.89 +    isRunning = True
    4.90 +    while isRunning:
    4.91 +        remoteKeyBorgServer.handle_request()
    4.92 +    # Turn off the drives and release the GPIO pins
    4.93 +    print 'Finished'
    4.94 +    MotorOff()
    4.95 +    #raw_input('Turn the power off now, press ENTER to continue')
    4.96 +    GPIO.cleanup()
    4.97 +except KeyboardInterrupt:
    4.98 +    # CTRL+C exit, turn off the drives and release the GPIO pins
    4.99 +    print 'Terminated'
   4.100 +    MotorOff()
   4.101 +    raw_input('Turn the power off now, press ENTER to continue')
   4.102 +    GPIO.cleanup()
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/rpi/piborg/TurtleBorg.py	Thu May 15 18:57:11 2014 +0200
     5.3 @@ -0,0 +1,111 @@
     5.4 +#!/usr/bin/env python
     5.5 +# coding: Latin-1
     5.6 +
     5.7 +# Load library functions we want
     5.8 +import sys
     5.9 +import time
    5.10 +import RPi.GPIO as GPIO
    5.11 +GPIO.setmode(GPIO.BCM)
    5.12 +GPIO.setwarnings(False)
    5.13 +
    5.14 +# Set which GPIO pins the drive outputs are connected to
    5.15 +DRIVE_1 = 4
    5.16 +DRIVE_2 = 18
    5.17 +DRIVE_3 = 8
    5.18 +DRIVE_4 = 7
    5.19 +
    5.20 +# Set all of the drive pins as output pins
    5.21 +GPIO.setup(DRIVE_1, GPIO.OUT)
    5.22 +GPIO.setup(DRIVE_2, GPIO.OUT)
    5.23 +GPIO.setup(DRIVE_3, GPIO.OUT)
    5.24 +GPIO.setup(DRIVE_4, GPIO.OUT)
    5.25 +
    5.26 +# Map of functions to drive pins
    5.27 +leftDrive = DRIVE_1                     # Drive number for left motor
    5.28 +rightDrive = DRIVE_4                    # Drive number for right motor
    5.29 +penDrive = DRIVE_3                      # Drive number for pen solenoid
    5.30 +
    5.31 +# Functions for the robot to perform
    5.32 +def MoveForward(n):
    5.33 +    """Move forward for 'n' seconds"""
    5.34 +    GPIO.output(leftDrive, GPIO.HIGH)
    5.35 +    GPIO.output(rightDrive, GPIO.HIGH)
    5.36 +    time.sleep(n)
    5.37 +    GPIO.output(leftDrive, GPIO.LOW)
    5.38 +    GPIO.output(rightDrive, GPIO.LOW)
    5.39 +
    5.40 +def MoveLeft(n):
    5.41 +    """Move left for 'n' seconds"""
    5.42 +    GPIO.output(leftDrive, GPIO.HIGH)
    5.43 +    GPIO.output(rightDrive, GPIO.LOW)
    5.44 +    time.sleep(n)
    5.45 +    GPIO.output(leftDrive, GPIO.LOW)
    5.46 +
    5.47 +def MoveRight(n):
    5.48 +    """Move right for 'n' seconds"""
    5.49 +    GPIO.output(leftDrive, GPIO.LOW)
    5.50 +    GPIO.output(rightDrive, GPIO.HIGH)
    5.51 +    time.sleep(n)
    5.52 +    GPIO.output(rightDrive, GPIO.LOW)
    5.53 +
    5.54 +def PenUp(n):
    5.55 +    """Lift the pen up"""
    5.56 +    GPIO.output(penDrive, GPIO.LOW)
    5.57 +
    5.58 +def PenDown(n):
    5.59 +    """Place the pen down"""
    5.60 +    GPIO.output(penDrive, GPIO.HIGH)
    5.61 +
    5.62 +def HelpMessage(n):
    5.63 +    """Display a list of available commands"""
    5.64 +    print ''
    5.65 +    print 'Available commands:'
    5.66 +    commands = dCommands.keys()
    5.67 +    commands.sort()
    5.68 +    for command in commands:
    5.69 +        print '% 10s - %s, %s' % (command, dCommands[command].func_name, dCommands[command].__doc__)
    5.70 +    print ''
    5.71 +
    5.72 +# Map of command names to functions
    5.73 +dCommands = {
    5.74 +        'FORWARD':MoveForward,
    5.75 +        'FD':MoveForward,
    5.76 +        'LEFT':MoveLeft,
    5.77 +        'LT':MoveLeft,
    5.78 +        'RIGHT':MoveRight,
    5.79 +        'RT':MoveRight,
    5.80 +        'PENUP':PenUp,
    5.81 +        'PU':PenUp,
    5.82 +        'PENDOWN':PenDown,
    5.83 +        'PD':PenDown,
    5.84 +        'HELP':HelpMessage,
    5.85 +        '?':HelpMessage
    5.86 +        }
    5.87 +
    5.88 +# If we have been run directly then look at command line
    5.89 +if __name__ == "__main__":
    5.90 +    # Process command
    5.91 +    if len(sys.argv) > 1:
    5.92 +        # Extract the command name and value (if there is any)
    5.93 +        command = sys.argv[1].upper()
    5.94 +        if len(sys.argv) > 2:
    5.95 +            sValue = sys.argv[2].upper()
    5.96 +        else:
    5.97 +            sValue = '0'
    5.98 +        try:
    5.99 +            fValue = float(sValue)
   5.100 +        except:
   5.101 +            fValue = 0.0
   5.102 +    
   5.103 +        # Select the appropriate function and call it
   5.104 +        if dCommands.has_key(command):
   5.105 +            dCommands[command](fValue)
   5.106 +        else:
   5.107 +            print 'Command "%s" not recognised' % (command)
   5.108 +            HelpMessage(fValue)
   5.109 +    else:
   5.110 +        # No command, display the help message
   5.111 +        print 'Usage: %s command [n]' % (sys.argv[0])
   5.112 +        HelpMessage(0)
   5.113 +
   5.114 +    
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/rpi/piborg/piborg	Thu May 15 18:57:11 2014 +0200
     6.3 @@ -0,0 +1,45 @@
     6.4 +#!/bin/sh
     6.5 +#
     6.6 +# SliTaz Raspberry PiBorg tiny utility
     6.7 +#
     6.8 +. /lib/libtaz.sh
     6.9 +check_root
    6.10 +
    6.11 +usage() {
    6.12 +	cat << EOT
    6.13 +
    6.14 +$(boldify 'Usage:') $(basename $0) [command] [movement] [seconds]
    6.15 +
    6.16 +$(boldify 'Commands:')
    6.17 +  testsuite   Test PiBorg/PiCy motors and movements
    6.18 +  turtle      Move PiCy like a turtle from scripts!
    6.19 +  rkey        Remote control PiBorg with a keyboard
    6.20 +
    6.21 +$(boldify 'Turtle movements:')
    6.22 +  fd          Move forward N seconds
    6.23 +  lt          Move left N seconds
    6.24 +  rt          Move right N seconds
    6.25 +
    6.26 +EOT
    6.27 +}
    6.28 +
    6.29 +case "$1" in
    6.30 +	
    6.31 +	turtle)
    6.32 +		move="$2"
    6.33 +		time="$3"
    6.34 +		TurtleBorg.py ${move} ${time} ;;
    6.35 +	
    6.36 +	rkey)
    6.37 +		RemoteKeyBorgS.py & ;;
    6.38 +	
    6.39 +	testsuite)
    6.40 +		TurtleBorg.py fd 0.5
    6.41 +		TurtleBorg.py lt 0.5
    6.42 +		TurtleBorg.py rt 0.5 ;;
    6.43 +	
    6.44 +	*)
    6.45 +		usage ;;
    6.46 +		
    6.47 +esac
    6.48 +exit 0