1
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
22 '''
23 Class that represents an infrared sensor.
24 '''
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:
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
77
79 return self.sensorState
80
82 self.sensorState = state
83
85 return self.sensorType
86
88 if self.activateCallback != None:
89 self.activateCallback(self.id)
90
92 if self.passivateCallback != None:
93 self.passivateCallback(self.id)
94
98