NXTBrick20.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 import nxt.locator
00006 from nxt.sensor import *
00007 from nxt.motor import *
00008 import time
00009 
00010 class NXTBrick:
00011   def __init__(self, bsock=None):
00012     """
00013     Ctor
00014     Connecting to NXT brick and creating motor object, sensor object
00015     and so on. Motor encoders will be reset.
00016     """
00017     if bsock:
00018       self.sock = bsock
00019     else:
00020       self.sock = nxt.locator.find_one_brick()
00021 
00022     self.motors = [Motor(self.sock, PORT_A),
00023                    Motor(self.sock, PORT_B),
00024                    Motor(self.sock, PORT_C)]
00025             
00026     self.sensors = [Touch(self.sock, PORT_1),
00027                     Sound(self.sock, PORT_2),
00028                     Light(self.sock, PORT_3),
00029                     Ultrasonic(self.sock, PORT_4)]
00030     self.resetPosition()
00031 
00032   def close(self):
00033     """
00034     Finalizing connection with NXT brick.
00035     """
00036     self.sock.close()
00037 
00038   def resetPosition(self, relative = 0):
00039     """
00040     Resetting encoders of NXT motors
00041     """
00042     for m in self.motors:
00043       m.reset_position(relative)
00044 
00045   def setMotors(self, vels):
00046     """
00047     This operation receives array and set them as motor power.  If the
00048     number of vels items does not match with the number of motors,
00049     smaller number of them will be taken and set respectively.
00050     """
00051     for i, v in enumerate(vels[:min(len(vels),len(self.motors))]):
00052       self.motors[i].sync = 1
00053       self.motors[i].run(max(min(v,127),-127))
00054 
00055   def getMotors(self):
00056     """
00057     Getting motors' angle (degrees)
00058     """
00059     state = []
00060     for m in self.motors:
00061       stat = None
00062       for i in range(3):
00063         try:
00064           stat = m.get_tacho().tacho_count
00065           break
00066         except:
00067           time.sleep(0.01)
00068           continue
00069 
00070       if stat == None:
00071         import sys
00072         print "Unknown motor encoder error"
00073         print sys.exc_info()[1]
00074       state.append(stat)
00075 
00076     return state
00077 
00078 
00079   def getSensors(self):
00080     """
00081     Getting sensors' values. Data will be returned as array.
00082     """
00083     state = []
00084     for s in self.sensors:
00085       stat = None
00086       for i in range(3):
00087         try:
00088           stat = s.get_sample()
00089           break
00090         except:
00091           time.sleep(0.01)
00092           continue
00093       if stat == None:
00094         import sys
00095         print "Unknown sensor error"
00096         print sys.exc_info()[1]
00097       state.append(stat)
00098 
00099     return state
00100 
00101 
00102 """
00103 Test program
00104 It gives appropriate values to motors, and angles of motors are
00105 obtained and shown.  Sensor data are also obtained and shown.
00106 """
00107 if __name__ == "__main__":
00108   import time
00109   nxt = NXTBrick()
00110   print "connected"
00111     
00112   # Testing motors
00113   for i in range(0):
00114     nxt.setMotors([80,-80,80])
00115     print "Motor: "
00116     mstat = nxt.getMotors()
00117     for i, m in enumerate(mstat):
00118       print "(" , i, "): ", m
00119     time.sleep(0.1)
00120   nxt.setMotors([0,0,0])
00121 
00122   # Testing sensors
00123   for i in range(100):
00124     sensors = ["Touch", "Sound", "Light", "USonic"]
00125     sval = nxt.getSensors()
00126     print sval
00127     time.sleep(0.1)


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28