Brains are where one specifies the major algorithms to drive the robot, either in the simulator or on the real robot. Brains should be valid python files, and gain their functionality by working with certain variables and procedures built into SoaR. They are normally specified in a file of their own, and then opened by SoaR for parsing.
Normally, they contain two methods:
- setup() gets run at the beginning of everything and is good for initializing variables. It is also where logging should be setup via the writeLog(filename) or readLog(filename) function calls, and if discrete movement is desired, set it up via the discrete(steptime) call.
- step() gets run each step of the program and should contain most of the logic code. It is where one would call motorOutput(v,w), sonarDistances() or pose(), or make use of the built-in list sonarInfo, and test for whether the robot's stall sensors have fired with stalled()
Beyond standard python, in a brain one also has access to the following variables and procedures:
- motorOutput(v,w) sets the robot's internal motors to produce a forward translational velocity of v (in meters/second) and a rotational velocity of w (in radians/second)
- pose() returns a tuple of three values describing the robot's current odometry readings for x, y and th (x and y in meters, th in radians)
- sonarDistances() returns a list describing the robot's current sonar readings, all in meters. each reading corresponds to the sonar located at the same entry in sonarInfo
- sonarInfo is a static list of tuples where each tuple describes one sonar in the form (x,y,th) where x and y (units in meters) are the location of the sonar on the robot with respect to its center and th is the direction it points (units in radians, zero is straight ahead)
- robot is a container object that starts out empty and persists for the duration of the program for use in any way the user pleases.
- stalled() returns True if the robot is stalled (i.e. stuck against a wall. the stall sensors on the real robot are not very sensitive so don't trust them to get you out of a stall)
- writeLog(logfile) and readLog(logfile) are useful for logging all sonar readings and odometry readings in a run to a file and then using them later for debugging purposes (readLog() is essentially like a hallucinatory flashback for the robot in terms of sonarDistances() and pose() to the log created when writeLog() was called)
- discrete(steptime) is a procedure that changes the way motorOutput() behaves. Instead of setting the robot's velocity for an indefinite amount of time, it makes it so motorOutput() only sets the velocity for the length of time specifed by the call to discrete(), and after that time is up, it stops the robot from moving.
Example brain file:
def setup():
robot.count = 0
def step():
print sonarDistances(), pose()
print robot.count
robot.count+=1
This brain will keep counting up and up from 0 each time step gets called, and will also output the current sonar readings and odometry readings each step.