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

Source Code for Module InfraredSensor

 1  # InfraredSensor.java 
 2   
 3  ''' 
 4  Class that represents an infrared 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 code 
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  import RPi.GPIO as GPIO 
18  import SharedConstants 
19  from RobotInstance import RobotInstance 
20   
21 -class InfraredSensor():
22 ''' 23 Class that represents an infrared sensor. 24 '''
25 - def __init__(self, id, **kwargs):
26 ''' 27 Creates an infrared sensor at given port. 28 For the Pi2Go the following infrared sensors are used: 29 id = 0: front center; id = 1: front left; id = 2: front right; 30 id = 3: line left; id = 4: line right. The following global constants are defined: 31 IR_CENTER = 0, IR_LEFT = 1, IR_RIGHT = 2, IR_LINE_LEFT = 3, IR_LINE_RIGHT = 4 32 @param id: sensor identifier 33 ''' 34 self.id = id 35 self.sensorState = "PASSIVATED" 36 self.sensorType = "InfraredSensor" 37 self.activateCallback = None 38 self.passivateCallback = None 39 for key in kwargs: 40 if key == "activated": 41 self.activateCallback = kwargs[key] 42 elif key == "passivated": 43 self.passivateCallback = 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.activateCallback != None or self.passivateCallback != None: 49 robot.registerSensor(self) 50 Tools.debug("InfraredSensor instance with ID " + str(id) + " created")
51
52 - def getValue(self):
53 ''' 54 Checks, if reflected light is detected. 55 @return: 1, if the sensor detects reflected light; otherwise 0 56 @rtype: int 57 ''' 58 Tools.delay(1) 59 self._checkRobot() 60 if self.id == SharedConstants.IR_CENTER and \ 61 GPIO.input(SharedConstants.P_FRONT_CENTER) == GPIO.LOW: 62 return 1 63 elif self.id == SharedConstants.IR_LEFT and \ 64 GPIO.input(SharedConstants.P_FRONT_LEFT) == GPIO.LOW: 65 return 1 66 elif self.id == SharedConstants.IR_RIGHT and \ 67 GPIO.input(SharedConstants.P_FRONT_RIGHT) == GPIO.LOW: 68 return 1 69 elif self.id == SharedConstants.IR_LINE_LEFT and \ 70 GPIO.input(SharedConstants.P_LINE_LEFT) == GPIO.LOW: 71 return 1 72 elif self.id == SharedConstants.IR_LINE_RIGHT and \ 73 GPIO.input(SharedConstants.P_LINE_RIGHT) == GPIO.LOW: 74 return 1 75 else: 76 return 0
77
78 - def getSensorState(self):
79 return self.sensorState
80
81 - def setSensorState(self, state):
82 self.sensorState = state
83
84 - def getSensorType(self):
85 return self.sensorType
86
87 - def onActivated(self):
88 if self.activateCallback != None: 89 self.activateCallback(self.id)
90
91 - def onPassivated(self):
92 if self.passivateCallback != None: 93 self.passivateCallback(self.id)
94
95 - def _checkRobot(self):
96 if RobotInstance.getRobot() == None: 97 raise Exception("Create Robot instance first")
98