@@ -35,6 +35,13 @@ class Type(object):
3535 CUSTOM = 0x09
3636 LOW_SPEED = 0x0A
3737 LOW_SPEED_9V = 0x0B # Low-speed I2C (Ultrasonic sensor)
38+ HIGH_SPEED = 0x0C #Possible other mode for I2C; may be used by future sensors.
39+ COLORFULL = 0x0D #NXT 2.0 color sensor in full color mode (color sensor mode)
40+ COLORRED = 0x0E #NXT 2.0 color sensor with red light on (light sensor mode)
41+ COLORGREEN = 0x0F #NXT 2.0 color sensor with green light on (light sensor mode)
42+ COLORBLUE = 0x10 #NXT 2.0 color sensor in with blue light on (light sensor mode)
43+ COLORNONE = 0x11 #NXT 2.0 color sensor in with light off (light sensor mode)
44+ COLOREXIT = 0x12 #NXT 2.0 color sensor internal state (not sure what this is for yet)
3845
3946class Mode (object ):
4047 'Namespace for enumeration of the mode of sensor'
@@ -233,6 +240,59 @@ def set_illuminated(self, active):
233240 self .sensor_type = Type .LIGHT_INACTIVE
234241 self .set_input_mode ()
235242
243+ class ColorSensor (AnalogSensor ):
244+ 'Object for color sensors'
245+ # this is a class for the lego NXT 2.0 RGB color sensor
246+ # not to be confused with the hitechnic color sensor
247+ # the color sensor can run in two modes:
248+ # a light sensor which returns the reflected light from the lamp that is
249+ # currently on (red, green, blue, off/ambient) on a scale of 1-1023
250+ # a color sensor that returns a 1-6 decimal value corresponding to
251+ # (black, blue, green, yellow, red, white) unfortunately the RGB values
252+ # are not sent over the wire
253+
254+ # TODO: calibration
255+
256+ #note: if you create a new object everytime you make a call the light
257+ # will flash on an off because each time the object is created the light
258+ # color is set to none
259+
260+ def __init__ (self , brick , port ):
261+ super (ColorSensor , self ).__init__ (brick , port )
262+ self .set_light_color (None )
263+
264+ def get_input_values (self ):
265+ values = self .brick .get_input_values (self .port )
266+ (self .port , self .valid , self .calibrated , self .sensor_type ,
267+ self .mode , self .raw_ad_value , self .normalized_ad_value ,
268+ self .scaled_value , self .calibrated_value ) = values
269+ return values
270+
271+ def set_light_color (self , color ):
272+ if color == 'red' :
273+ self .sensor_type = Type .COLORRED
274+ elif color == 'green' :
275+ self .sensor_type = Type .COLORGREEN
276+ elif color == 'blue' :
277+ self .sensor_type = Type .COLORBLUE
278+ elif color == 'full' :
279+ self .sensor_type = Type .COLORFULL
280+ elif color == 'off' :
281+ self .sensor_type = Type .COLORNONE
282+ else :
283+ self .sensor_type = Type .COLORNONE
284+ self .set_input_mode ()
285+
286+ def get_color (self ):
287+ self .set_light_color ('full' )
288+ self .get_input_values ()
289+ return self .scaled_value
290+
291+ def get_reflected_light (self , color ):
292+ self .set_light_color (color )
293+ self .get_input_values ()
294+ return self .scaled_value
295+
236296class SoundSensor (AnalogSensor ):
237297 'Object for sound sensors'
238298
@@ -308,3 +368,28 @@ def get_sample(self):
308368
309369 return self .xval , self .yval , self .zval
310370
371+
372+ class GyroSensor (AnalogSensor ):
373+ 'Object for gyro sensors'
374+ #This class is for the hitechnic gryo accelerometer. When the gryo is
375+ #not moving there will be a constant offset that will change with
376+ #temperature and other ambient factors. It might be appropriate to
377+ #write a calibration function to account for this offset.
378+ #
379+ #TODO: calibration
380+
381+ def __init__ (self , brick , port ):
382+ super (GyroSensor , self ).__init__ (brick , port )
383+ self .sensor_type = Type .ANGLE
384+ self .set_input_mode ()
385+
386+ def get_input_values (self ):
387+ values = self .brick .get_input_values (self .port )
388+ (self .port , self .valid , self .calibrated , self .sensor_type ,
389+ self .mode , self .raw_ad_value , self .normalized_ad_value ,
390+ self .scaled_value , self .calibrated_value ) = values
391+ return values
392+
393+ def get_sample (self ):
394+ self .get_input_values ()
395+ return self .scaled_value
0 commit comments