Clone this wiki locally
The RaspiRobotBoard has an accompanying Python library that simplifies the controlling of motors and the RaspiRobotBoard's other inputs and outputs.
For instructions on installing the library, and an overview of what you can do with it, please see Tutorial 1 - Getting Started.
The library implements a class called RaspiRobot. This is only available for Python 2 and any Python programs that you write that use the libaray must be run as a super user. I.e.
su python myProgram.py
To import the library and create an instance of the class, put this at the top of your Python program.
from raspirobotboard import * rr = RaspiRobot()
The rest is pretty straightforward, there are just a load of useful methods on the class that you can use.
There are two LEDs built-in to the RaspiRobotBoard, called LED1 and LED2. Both of these can be turned on and off using the following methods:
To turn LED1 on just do:
To turn it off again do:
To control LED2 just do the same thing but using setLED2.
The RaspiRobotBoard has two switch inputs that can be connected to the board.
The sw1_closed() and sw2_closed() functions return true if the contacts for that switch are closed. By default, the switches are open. You can test out closing the switch by shorting the two contacts with a screwdriver.
The following test program will show you the state of each of the switch contacts.
import RPi.GPIO as GPIO from raspirobotboard import * rr = RaspiRobot() while True: print("SW1=" + str(rr.sw1_closed()) + " SW2=" + str(rr.sw2_closed())) raw_input("check again")
Open Collector Outputs
The RaspiRobotBoard has two low-power open collector outputs. These can each source up to 25mA and so are suitable for driving LEDs with series resistors. They can also be used to drive transistors and other external electronics. They are buffered and therefore protect the Raspberry Pi' s processor.
To turn the Open Collector OC1 output on just do:
To turn it off again do:
To control OC2, substitute oc2 in place of oc1.
Motor (High Level Interface)
There are two levels of command for controlling the motors. There is a high level interface that assumes that the motors are connected to wheels on a rover. These commands are forward, reverse, left, right and stop.
... will start both motors running in the same direction to move the robot rover forwards. They will continue in this direction until another command is issued.
If you want to move forward for a certain amount of time, you can specify a number of seconds as an argument:
The commands left, right and reverse all work in the same way.
The stop command stops all the motors.
Motor (Low Level Interface)
The low level interface is intended for control of the motors directly. It allows you to turn each motor on or off and its direction independently.
The method for this (set_motors) takes four arguments: left_go, left_dir, right_go, right_dir.
So to set both motors going forward, you would just use the following:
rr.set_motors(1, 0, 1, 0)
To set the left motor forward and the right motor in reverse you would use:
rr.set_motors(1, 0, 1, 1)