Student Robotics has written a module —
sr.robot — which is used to interface with the hardware.
It handles all the low-level interactions so you don’t have to.
To set the output power of output 0 of the first motor board to -30%, for example, you would simply write:
R.motors.m0.power = -30
-30 would be backwards (depending upon which way you wired up the motor) — 30% power in reverse.
To gain access to all of this functionality, all you need to do is write:
from sr.robot import *
…at the top of your code (before you use any of its functionality, basically). This imports the Student Robotics module that we’ve written to interface with our hardware.
Then, within the
sr.robot module, there is a
Robot class that should be instantiated, as follows:
from sr.robot import * R = Robot()
R in this case), you then have access to the following attributes:
They can be used in your code just like the example above.
servos are Python lists, and so should be accessed as such.
Here are some examples:
R.motors.m0.power = 50 # WILL work, if motor 0 exists R.motors.m0.power = -20 # WILL work, if motor 1 exists R.motors.m0.power = 42 # WON'T WORK # the above is similar to the situation for 'ruggeduinos' and 'servos'
A number of examples in the documentation will assume you’ve instantiated the required
Robot class and have called it
From here in, if you see a
R.something, the requirement of the
sr.robot import line and the instantiation of
R is implicit.
As well as the attributes listed above, the Robot class also has the following attributes, which you may find useful:
- The number of the zone that the robot is associated with. An integer from 0 to 3 inclusive.
- Either “comp” or “dev”. When in a competition match, this will be “comp”, and at all other times this will be “dev”.
- The path to the USB memory stick.
Your code is unzipped and run from a temporary directory, therefore files you create will be lost when the kit is turned off.
You can use this to easily read from and write to files on the stick itself.
Note that the USB memory stick is mounted synchronously, so any writes to it will block until complete and may slow down your code.
An example of how the
usbkeyattribute might be used:
from sr.robot import * import os R = Robot() print "The path to the USB key is:", R.usbkey print "My file on the USB contains:" f = open(os.path.join(R.usbkey, "my-file.txt"), "r") print f.read() f.close()
Normally the Robot object is initialised with the following:
R = Robot()
However if you want to:
- customise your Ruggeduino firmware
- initialise some hardware or software before the start button is pressed
Then Robot initialisation can be broken up as follows (this example is equivalent to the previous code excerpt):
R = Robot.setup() # Setup phase. # Here you can configure hardware enumeration R.init() # Initialisation phase. # Here you can perform hardware/software initialisation before start R.wait_start()
During the setup phase, the Robot hardware is inaccessible.
R.motors is unavailable since enumerations occurs in the
In this phase you can configure how the Robot finds and configures hardware.
init call, all hardware is accessible.
If you have any hardware which must be initialised before the start button is pressed,
the initialisation phase is the time to do so.