Module HTColorSensor
[frames] | no frames]

Source Code for Module HTColorSensor

 1  # HTColorSensor.py 
 2   
 3  ''' 
 4   This software is part of the EV3BrickPi library. 
 5   It is Open Source Free Software, so you may 
 6   - run the code for any purpose 
 7   - study how the code works and adapt it to your needs 
 8   - integrate all or parts of the code in your own programs 
 9   - redistribute copies of the code 
10   - improve the code and release your improvements to the public 
11   However the use of the code is entirely your responsibility. 
12  ''' 
13   
14  from GenericSensor import GenericSensor 
15  import brickpi3 
16  import time 
17  from Tools import * 
18  from Color import Color 
19   
20 -class HTColorSensor(GenericSensor):
21 ''' 22 Color names from brickpi3 library. 23 ''' 24 colorNames = ["BLACK", "BLUE", "GREEN", "YELLOW", "RED", "WHITE"] 25
26 - def __init__(self, port):
27 ''' 28 Abstraction of the HiTechnics Color Sensor. 29 ''' 30 GenericSensor.__init__(self, port) 31 self.bp.set_sensor_type(self._getPort(), self.bp.SENSOR_TYPE.NXT_COLOR_FULL) 32 time.sleep(2) # wait until setup is complete
33
34 - def getValue(self):
35 ''' 36 Returns list of [color id, red light reflected, green light reflected, blue light reflected, ambient light] 37 or None, if detection fails. 38 ''' 39 try: 40 value = self.bp.get_sensor(self._getPort()) 41 if not (len(value) == 5 and value[0] >= 1 and value[0] <= 6): # not successfully read the values 42 return None 43 except brickpi3.SensorError as error: 44 return None 45 Tools.debug("NxtColorSensor.getValue() returned: " + str(value)) 46 return value
47
48 - def getColor(self):
49 ''' 50 Returns reference of the detected color or None, if detection fails. 51 ''' 52 value = self.getValue() 53 if value != None: 54 return Color(value[1:5]) 55 else: 56 return None
57
58 - def getColorName(self):
59 ''' 60 Returns standard color name from brickpi3 library. 61 ''' 62 v = self.getValue() 63 if v == None: 64 return "UNDEFINED" 65 else: 66 return HTColorSensor.colorNames[v[0] - 1]
67