74
74
75
75
pi = pigpio .pi ()
76
76
77
+
77
78
class Color :
79
+
78
80
def __init__ (self , red : float , green : float , blue : float ):
79
81
self .red = red
80
82
self .green = green
81
83
self .blue = blue
82
84
85
+
83
86
class RawColor :
87
+
84
88
def __init__ (self , red : int , green : int , blue : int , ir : int ):
85
89
self .red = red
86
90
self .green = green
87
91
self .blue = blue
88
92
self .ir = ir
89
93
94
+
90
95
class CIEColor :
96
+
91
97
def __init__ (self , x : float , y : float , z : float ):
92
98
self .x = x
93
99
self .y = y
94
100
self .z = z
95
101
102
+
96
103
class ColorSensorV3 :
97
104
"""REV Robotics Color Sensor V3"""
98
105
@@ -193,8 +200,7 @@ class ColorSensorMeasurementRate(enum.IntEnum):
193
200
kColorRate2000ms = 7
194
201
195
202
def configureProximitySensorLED (self , freq : LEDPulseFrequency ,
196
- curr : LEDCurrent ,
197
- pulses : int ):
203
+ curr : LEDCurrent , pulses : int ):
198
204
"""
199
205
Configure the the IR LED used by the proximity sensor.
200
206
@@ -213,7 +219,7 @@ def configureProximitySensorLED(self, freq: LEDPulseFrequency,
213
219
self ._write8 (self .Register .kProximitySensorPulses , pulses )
214
220
215
221
def configureProximitySensor (self , res : ProximitySensorResolution ,
216
- rate : ProximitySensorMeasurementRate ):
222
+ rate : ProximitySensorMeasurementRate ):
217
223
"""
218
224
Configure the proximity sensor.
219
225
@@ -228,8 +234,8 @@ def configureProximitySensor(self, res: ProximitySensorResolution,
228
234
self ._write8 (self .Register .kProximitySensorRate , res | rate )
229
235
230
236
def configureColorSensor (self , res : ColorSensorResolution ,
231
- rate : ColorSensorMeasurementRate ,
232
- gain : GainFactor ):
237
+ rate : ColorSensorMeasurementRate ,
238
+ gain : GainFactor ):
233
239
"""
234
240
Configure the color sensor.
235
241
@@ -275,7 +281,8 @@ def getRawColor(self) -> RawColor:
275
281
276
282
Returns Color containing red, green, blue and IR values
277
283
"""
278
- return RawColor (self .getRed (), self .getGreen (), self .getBlue (), self .getIR ())
284
+ return RawColor (self .getRed (), self .getGreen (), self .getBlue (),
285
+ self .getIR ())
279
286
280
287
def getRed (self ) -> int :
281
288
"""
@@ -312,9 +319,8 @@ def getIR(self) -> int:
312
319
# This is a transformation matrix given by the chip
313
320
# manufacturer to transform the raw RGB to CIE XYZ
314
321
_Cmatrix = [
315
- 0.048112847 , 0.289453437 , - 0.084950826 ,
316
- - 0.030754752 , 0.339680186 , - 0.071569905 ,
317
- - 0.093947499 , 0.072838494 , 0.34024948
322
+ 0.048112847 , 0.289453437 , - 0.084950826 , - 0.030754752 , 0.339680186 ,
323
+ - 0.071569905 , - 0.093947499 , 0.072838494 , 0.34024948
318
324
]
319
325
320
326
def getCIEColor (self ) -> CIEColor :
@@ -327,9 +333,12 @@ def getCIEColor(self) -> CIEColor:
327
333
Returns CIEColor value from sensor
328
334
"""
329
335
raw = self .getRawColor ()
330
- return CIEColor (self ._Cmatrix [0 ] * raw .red + self ._Cmatrix [1 ] * raw .green + self ._Cmatrix [2 ] * raw .blue ,
331
- self ._Cmatrix [3 ] * raw .red + self ._Cmatrix [4 ] * raw .green + self ._Cmatrix [5 ] * raw .blue ,
332
- self ._Cmatrix [6 ] * raw .red + self ._Cmatrix [7 ] * raw .green + self ._Cmatrix [8 ] * raw .blue )
336
+ return CIEColor (
337
+ self ._Cmatrix [0 ] * raw .red + self ._Cmatrix [1 ] * raw .green +
338
+ self ._Cmatrix [2 ] * raw .blue , self ._Cmatrix [3 ] * raw .red +
339
+ self ._Cmatrix [4 ] * raw .green + self ._Cmatrix [5 ] * raw .blue ,
340
+ self ._Cmatrix [6 ] * raw .red + self ._Cmatrix [7 ] * raw .green +
341
+ self ._Cmatrix [8 ] * raw .blue )
333
342
334
343
def hasReset (self ) -> bool :
335
344
"""
@@ -352,18 +361,20 @@ def _checkDeviceID(self) -> bool:
352
361
raw = pi .i2c_read_byte_data (self .i2c , self .Register .kPartID )
353
362
354
363
if self .kPartID != raw :
355
- print ("Unknown device found with same I2C addres as REV color sensor" )
364
+ print (
365
+ "Unknown device found with same I2C addres as REV color sensor" )
356
366
return False
357
367
358
368
return True
359
369
360
370
def _initializeDevice (self ):
361
- self ._write8 (self . Register . kMainCtrl ,
362
- self .MainControl . kRGBMode |
363
- self .MainControl .kLightSensorEnable |
371
+ self ._write8 (
372
+ self .Register . kMainCtrl ,
373
+ self .MainControl .kRGBMode | self . MainControl . kLightSensorEnable |
364
374
self .MainControl .kProximitySensorEnable )
365
375
366
- self ._write8 (self .Register .kProximitySensorRate ,
376
+ self ._write8 (
377
+ self .Register .kProximitySensorRate ,
367
378
self .ProximitySensorResolution .kProxRes11bit |
368
379
self .ProximitySensorMeasurementRate .kProxRate100ms )
369
380
@@ -407,7 +418,8 @@ def readConfig():
407
418
with open (configFile , "rt" , encoding = "utf-8" ) as f :
408
419
j = json .load (f )
409
420
except OSError as err :
410
- print ("could not open '{}': {}" .format (configFile , err ), file = sys .stderr )
421
+ print ("could not open '{}': {}" .format (configFile , err ),
422
+ file = sys .stderr )
411
423
return False
412
424
413
425
# top level must be an object
@@ -469,7 +481,8 @@ def readConfig():
469
481
# read sensor and send to NT
470
482
rawcolor = sensor1 .getRawColor ()
471
483
prox = sensor1 .getProximity ()
472
- colorEntry1 .setDoubleArray ([rawcolor .red , rawcolor .green , rawcolor .blue , rawcolor .ir ])
484
+ colorEntry1 .setDoubleArray (
485
+ [rawcolor .red , rawcolor .green , rawcolor .blue , rawcolor .ir ])
473
486
proxEntry1 .setDouble (prox )
474
487
475
488
# flush NT
0 commit comments