def main() : options=parse_commandline() resetdata=get_resetdata() dev=connecttoarm()
These statements, do the following:
- Gather the command line options work out what they are meant to do and store the details in options. Its does this by calling the parse_commandline() function.
- Get the contents of the resetarm.dat file (by calling the get_resetdata() function). Resetarm.dat file is explained a bit here, but is basically a little cheat that allows us to "remember" where the arm is. The file is not particularly human readable because the pickle function isn't intended to be easy to read. However the pickle function is effectively storing a python dictionary who's keys are each of the motor names and who's values are the time in seconds that the motor has been running (with positive values in one direction and negative in the other). The data is stored within the program in the "dictionary" resetdata
- Calls the connect to arm function, finds our device and allocated the details to the "handle" dev - so we can refer to it elsewhere.
if options.filename!=None : filehandle=csv.reader(open(options.filename,'rb'), delimiter=',') # opens the resetarm file to read for row in filehandle: rownumeric= for each in row[:6]: rownumeric.append(int(each)) command=buildcommand(*rownumeric[:6]) if sendcommand(dev,command)<>3 : print "Possible error sending data" rownumeric.append(float(row)) time.sleep(rownumeric) ## need to increment reset file positions! sendcommand(dev) # create dictionary from values datadict=dict(zip(['shoulder','elbow','wrist','grip','rotate'],rownumeric[:6])) resetdata=store_reset_values(datadict, resetdata, rownumeric)
This group of commands are only run if the command line options passed in a filename. If there is a filename, the file is opened (using the csv function). Then for each row in the file, we read it in, convert the values to integers (they should be 0,1 or 2 for off, up, down etc on the motor). The command in the csv file should be in the correct order (shoulder, elbow, wrist, grip, rotate, light and time (time being a floating point value)) so there is no need for any "clever" dictionary type stuff. These first 5 parameters are passed to "build command" which turns each 0,1,2 value into the 3 bytes which are sent to the arm to control it. This command is then sent to our device. If the command is successfully sent it returns "3" for the number of bytes written; there is a basic error trap here in case of failure. The software then waits for the time defined in the final parameter on that line of the csv file. Then sends a "0" command to the device to stop all the motors. The "zip" function is then used to zip the "keys" and the "values" together, these are then added to the "resetdata" so that it can be stored for later recall.
elif options.reset_to_origin==True : for key in resetdata : if resetdata[key]<0 : move_to_reset(dev, key, 1, abs(resetdata[key])) elif resetdata[key]>0 : move_to_reset(dev, key, 2, abs(resetdata[key])) zeroreset()
These commands are only processed if the -o option is used on the command line. (As its an elif statement it is not run if the file option has been run). They read in the data stored in resetarm.dat file, and then move each of the motors in the right direction to bring it back to the origin. Then it sets the resetarm.dat values to zero (by calling our zeroreset() function).
elif options.zero==True : zeroreset() # zero the values in the reset file.
If the -z command line option was used (and -f or -o were not) then it resets the values in resetarm.dat to zero.
If none of the z/o/f command options were used then this code is run instead:
else : command=buildcommand( shoulder=options.shoulder, elbow=options.elbow, wrist=options.wrist, grip=options.grip, rotate=options.rotate, light=options.light) if sendcommand(dev,command)<>3 : print "Possible error sending data" resetdata=store_reset_values(options.__dict__, resetdata, options.timedelay) time.sleep(options.timedelay) sendcommand(dev) #turn off! dev.reset() #reset USB port (no need to release?)These commands essentially do the same as the data read in from the file but using the data provided at the command prompt - this makes them slightly more complicated as they aren't necessarily supplied in the "correct" order to feed directly to the "build command" function based on their position and so have to be passed with their names.
That lot has taken a bit longer to write than I expected so I don't think I'll detail each function at this stage - as they are all subject to change.