terminal.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004   ArbotiX Terminal - command line terminal to interact with an ArbotiX
00005   Copyright (c) 2008-2011 Vanadium Labs LLC.  All right reserved.
00006 
00007   Redistribution and use in source and binary forms, with or without
00008   modification, are permitted provided that the following conditions are met:
00009       * Redistributions of source code must retain the above copyright
00010         notice, this list of conditions and the following disclaimer.
00011       * Redistributions in binary form must reproduce the above copyright
00012         notice, this list of conditions and the following disclaimer in the
00013         documentation and/or other materials provided with the distribution.
00014       * Neither the name of Vanadium Labs LLC nor the names of its 
00015         contributors may be used to endorse or promote products derived 
00016         from this software without specific prior written permission.
00017   
00018   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021   DISCLAIMED. IN NO EVENT SHALL VANADIUM LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024   OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00026   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 """
00029 
00030 import roslib; roslib.load_manifest('arbotix_python')
00031 import sys
00032 
00033 from arbotix_python.arbotix import ArbotiX # does this look ridiculous to anyone else?
00034 from arbotix_python.ax12 import *
00035 
00036 # help phrases
00037 help = ["ArbotiX Terminal V0.1",
00038 "",
00039 "valid commands:",
00040 " ls [i b]- list the servos found on the bus. Optional parameters: i - highest ID to query, b - baudrate to query at.",
00041 " mv id id2 - rename any servo with ID=id, to id2",
00042 " baud b - set baud rate of bus to b",
00043 " get param id - get a parameter value from a servo",
00044 " set param id val - set parameter on servo ID=id to val",
00045 "",
00046 "valid parameters",
00047 " pos - current position of a servo, 0-1023",
00048 " baud - baud rate",
00049 " temp - current temperature, degrees C, READ ONLY"]
00050 
00051 
00052 class Terminal(ArbotiX):
00053     OKBLUE = '\033[94m'
00054     OKGREEN = '\033[92m'
00055     WARNING = '\033[93m'
00056     FAIL = '\033[91m'
00057     ENDC = '\033[0m'
00058 
00059     def __init__(self, port = "/dev/ttyUSB0", baud = 115200):
00060         # start
00061         ArbotiX.__init__(self, port, baud)  
00062         print "ArbotiX Terminal --- Version 0.1"
00063         print "Copyright 2011 Vanadium Labs LLC"
00064 
00065         # loop
00066         while True:
00067             print ">> ",
00068             kmd = raw_input().split(" ")
00069             try:
00070                 if kmd[0] == "help":        # display help data
00071                     if len(kmd) > 1:        # for a particular command
00072                         if kmd[1] == "ls":   
00073                             print help[3]
00074                         elif kmd[1] == "mv":
00075                             print help[4]
00076                         elif kmd[1] == "baud":
00077                             print help[5]
00078                         elif kmd[1] == "get":
00079                             print help[6]
00080                         elif kmd[1] == "set":   
00081                             print help[7]
00082                         else:
00083                             print "help: unrecognized command"
00084                     else:
00085                         for h in help:
00086                             print h
00087 
00088                 elif kmd[0] == "ls":       # list servos
00089                     self._ser.timeout = 0.25
00090                     if len(kmd) > 2:
00091                         self.write(253, P_BAUD_RATE, [self.convertBaud(int(kmd[1]))])
00092                         self.query()
00093                     self.query()
00094 
00095                 elif kmd[0] == "mv":         # rename a servo
00096                     if self.write( int(kmd[1]), P_ID, [int(kmd[2]),] ) == 0:
00097                         print self.OKBLUE+"OK"+self.ENDC
00098 
00099                 elif kmd[0] == "baud":      # set bus baud rate
00100                     self.write(253, P_BAUD_RATE, [self.convertBaud(int(kmd[1]))])
00101                     print self.OKBLUE+"OK"+self.ENDC
00102 
00103                 elif kmd[0] == "set":
00104                     if kmd[1] == "baud":
00105                         self.write( int(kmd[2]), P_BAUD_RATE, [self.convertBaud(int(kmd[3]))] )
00106                         print self.OKBLUE+"OK"+self.ENDC
00107                     elif kmd[1] == "pos" or kmd[1] == "position":
00108                         self.setPosition( int(kmd[2]), int(kmd[3]) )
00109                         print self.OKBLUE+"OK"+self.ENDC
00110 
00111                 elif kmd[0] == "get":
00112                     if kmd[1] == "temp":
00113                         value = self.getTemperature(int(kmd[2]))
00114                         if value >= 60 or value < 0:
00115                             print self.FAIL+str(value)+self.ENDC
00116                         elif value > 40:
00117                             print self.WARNING+str(value)+self.ENDC
00118                         else:
00119                             print self.OKGREEN+str(value)+self.ENDC
00120                     elif kmd[1] == "pos" or kmd[1] == "position":
00121                         value = self.getPosition(int(kmd[2]))
00122                         if value >= 0:
00123                             print self.OKGREEN+str(value)+self.ENDC
00124                         else:
00125                             print self.FAIL+str(value)+self.ENDC
00126         
00127             except Exception as e:
00128                 print "error...", e
00129 
00130     def query(self, max_id = 18, baud = 1000000):
00131         k = 0                   # how many id's have we printed
00132         for i in range(max_id):
00133             if self.getPosition(i+1) != -1:
00134                 if k > 8:
00135                     k = 0
00136                     print ""
00137                 print repr(i+1).rjust(4),
00138                 k = k + 1
00139             else:
00140                 if k > 8:
00141                     k = 0
00142                     print ""
00143                 print "....",
00144                 k = k + 1
00145             sys.stdout.flush()
00146         print ""
00147 
00148     def convertBaud(self, b):
00149         if b == 500000:
00150             return 3
00151         elif b == 400000:
00152             return 4
00153         elif b == 250000:
00154             return 7
00155         elif b == 200000:
00156             return 9
00157         elif b == 115200:
00158             return 16
00159         elif b == 57600:
00160             return 34
00161         elif b == 19200:    
00162             return 103
00163         elif b == 9600:
00164             return 207
00165         else:
00166             return 1    # default to 1Mbps
00167 
00168 
00169 if __name__ == "__main__":
00170     try:
00171         if len(sys.argv) > 2:
00172             t = Terminal(sys.argv[1], int(sys.argv[2]))
00173         elif len(sys.argv) > 1:
00174             t = Terminal(sys.argv[1])
00175         else:
00176             t = Terminal()
00177     except KeyboardInterrupt:
00178         print "\nExiting..."
00179 
00180 


corobot_arm
Author(s):
autogenerated on Wed Aug 26 2015 11:09:52