Module LightSensor
[hide private]
[frames] | no frames]

Source Code for Module LightSensor

  1  # LightSensor.java 
  2   
  3  ''' 
  4  Class that represents a light sensor. 
  5   
  6   This software is part of the raspibrick module. 
  7   It is Open Source Free Software, so you may 
  8   - run the code for any purpose 
  9   - study how the code works and adapt it to your needs 
 10   - integrate all or parts of the code in your own programs 
 11   - redistribute copies of the code777 
 12   - improve the code and release your improvements to the public 
 13   However the use of the code is entirely your responsibility. 
 14   ''' 
 15   
 16  from Tools import Tools 
 17  from RobotInstance import RobotInstance 
 18  import SharedConstants 
 19   
20 -class LightSensor():
21 ''' 22 Class that represents an light sensor. 23 '''
24 - def __init__(self, id, **kwargs):
25 ''' 26 Creates a light sensor instance with given id. 27 IDs: 0: front left, 1: front right, 2: rear left, 3: rear right 28 The following global constants are defined: 29 LS_FRONT_LEFT = 0, LS_FRONT_RIGHT = 1, LS_REAR_LEFT = 2, LS_REAR_RIGHT = 3. 30 @param id: the LightSensor identifier 31 ''' 32 self.id = id 33 self.sensorState = "DARK" 34 self.sensorType = "LightSensor" 35 self.triggerLevel = 500 36 self.brightCallback = None 37 self.darkCallback = None 38 self.isRegistered = False 39 for key in kwargs: 40 if key == "bright": 41 self.brightCallback = kwargs[key] 42 elif key == "dark": 43 self.darkCallback = kwargs[key] 44 robot = RobotInstance.getRobot() 45 if robot == None: # deferred registering, because Robot not yet created 46 RobotInstance._sensorsToRegister.append(self) 47 else: 48 if self.brightCallback != None or self.darkCallback != None: 49 robot.registerSensor(self) 50 Tools.debug("LightSensor instance with ID " + str(id) + " created")
51
52 - def getValue(self):
53 ''' 54 Returns the current intensity value (0..1000). 55 @return: the measured light intensity 56 @rtype: int 57 ''' 58 self._checkRobot() 59 Tools.delay(1) 60 nb = self.id 61 if nb == 0: 62 nb = 1 63 elif nb == 1: 64 nb = 0 65 robot = RobotInstance.getRobot() 66 return int(self._readADC(nb) / 255.0 * 1000 + 0.5)
67
68 - def getTriggerLevel(self):
69 return self.triggerLevel
70
71 - def setTriggerLevel(self, level):
72 self.triggerLevel = level
73
74 - def getSensorState(self):
75 return self.sensorState
76
77 - def setSensorState(self, state):
78 self.sensorState = state
79
80 - def getSensorType(self):
81 return self.sensorType
82
83 - def onBright(self, v):
84 if self.brightCallback != None: 85 self.brightCallback(self.id, v)
86
87 - def onDark(self, v):
88 if self.darkCallback != None: 89 self.darkCallback(self.id, v)
90
91 - def _readADC(self, channel):
92 robot = RobotInstance.getRobot() 93 try: 94 robot._bus.write_byte(SharedConstants.ADC_I2C_ADDRESS, channel) 95 robot._bus.read_byte(SharedConstants.ADC_I2C_ADDRESS) # ignore first byte 96 data = robot._bus.read_byte(SharedConstants.ADC_I2C_ADDRESS) 97 except: 98 raise Exception("Exception while accessing PCF8591P I2C Expander. channel: " + str(channel)) 99 data = 0 100 return data
101
102 - def _checkRobot(self):
103 if RobotInstance.getRobot() == None: 104 raise Exception("Create Robot instance first")
105