1
2
3
4 '''
5 This software is part of the raspibrick module.
6 It is Open Source Free Software, so you may
7 - run the code for any purpose
8 - study how the code works and adapt it to your needs
9 - integrate all or parts of the code in your own programs
10 - redistribute copies of the code
11 - improve the code and release your improvements to the public
12 However the use of the code is entirely your responsibility.
13 '''
14
15 from RobotInstance import RobotInstance
16 from Tools import Tools
17
19 '''
20 Class that represents an infrared sensor.
21 '''
23 '''
24 Creates an infrared sensor at given port.
25 For the Pi2Go the following infrared sensors are used:
26 id = 0: front center; id = 1: front left; id = 2: front right;
27 id = 3: line left; id = 4: line right. The following global constants are defined:
28 IR_CENTER = 0, IR_LEFT = 1, IR_RIGHT = 2, IR_LINE_LEFT = 3, IR_LINE_RIGHT = 4
29 @param id: sensor identifier
30 '''
31 self.id = id
32 self.device = "irs" + str(id)
33 self.sensorState = "PASSIVATED"
34 self.sensorType = "InfraredSensor"
35 self.activateCallback = None
36 self.passivateCallback = None
37 for key in kwargs:
38 if key == "activated":
39 self.activateCallback = kwargs[key]
40 elif key == "passivated":
41 self.passivateCallback = kwargs[key]
42 robot = RobotInstance.getRobot()
43 if robot == None:
44 RobotInstance._partsToRegister.append(self)
45 else:
46 self._setup(robot)
47 Tools.debug("InfraredSensor instance with ID " + str(id) + " created")
48
50 robot.sendCommand(self.device + ".create")
51 if self.activateCallback != None or self.passivateCallback != None:
52 robot.registerSensor(self)
53
63
65 return self.sensorState
66
68 self.sensorState = state
69
71 return self.sensorType
72
74 if self.activateCallback != None:
75 self.activateCallback(self.id)
76
78 if self.passivateCallback != None:
79 self.passivateCallback(self.id)
80
84