-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcolorsensor.patch
More file actions
76 lines (73 loc) · 2.87 KB
/
Copy pathcolorsensor.patch
File metadata and controls
76 lines (73 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--- src/nxt/sensor.py 2010-01-14 10:03:48.000000000 -0800
+++ sensor-new.py 2010-06-06 19:45:15.208091177 -0700
@@ -35,6 +35,13 @@
CUSTOM = 0x09
LOW_SPEED = 0x0A
LOW_SPEED_9V = 0x0B # Low-speed I2C (Ultrasonic sensor)
+ HIGH_SPEED = 0x0C #found this in another spec ans thought it should be included
+ COLORFULL = 0x0D #NXT 2.0 color sensor in full color mode (color sensor mode)
+ COLORRED = 0x0E #NXT 2.0 color sensor with red light on (light sensor mode)
+ COLORGREEN = 0x0F #NXT 2.0 color sensor with green light on (light sensor mode)
+ COLORBLUE = 0x10 #NXT 2.0 color sensor in with blue light on (light sensor mode)
+ COLORNONE = 0x11 #NXT 2.0 color sensor in with light off (light sensor mode)
+ COLOREXIT =0x12 #NXT 2.0 color sensor internal state (not sure what this is for yet)
class Mode(object):
'Namespace for enumeration of the mode of sensor'
@@ -227,6 +234,59 @@
self.sensor_type = Type.LIGHT_INACTIVE
self.set_input_mode()
+class ColorSensor(AnalogSensor):
+ 'Object for color sensors'
+# this is a class for the lego NXT 2.0 RGB color sensor
+# not to be confused with the hitechnic color sensor
+# the color sensor can run in two modes:
+# a light sensor which returns the reflected light from the lamp that is
+# currently on (red, green, blue, off/ambient) on a scale of 1-1023
+# a color sensor that returns a 1-6 decimal value corresponding to
+# (black, blue, green, yellow, red, white) unfortunately the RGB values
+# are not sent over the wire
+
+# TODO: calibration
+
+#note: if you create a new object everytime you make a call the light
+# will flash on an off because each time the object is created the light
+# color is set to none
+
+ def __init__(self, brick, port):
+ super(ColorSensor, self).__init__(brick, port)
+ self.set_light_color(None)
+
+ def get_input_values(self):
+ values = self.brick.get_input_values(self.port)
+ (self.port, self.valid, self.calibrated, self.sensor_type,
+ self.mode, self.raw_ad_value, self.normalized_ad_value,
+ self.scaled_value, self.calibrated_value) = values
+ return values
+
+ def set_light_color(self, color):
+ if color == 'red':
+ self.sensor_type = Type.COLORRED
+ elif color == 'green':
+ self.sensor_type = Type.COLORGREEN
+ elif color == 'blue':
+ self.sensor_type = Type.COLORBLUE
+ elif color == 'full':
+ self.sensor_type = Type.COLORFULL
+ elif color == 'off':
+ self.sensor_type = Type.COLORNONE
+ else:
+ self.sensor_type = Type.COLORNONE
+ self.set_input_mode()
+
+ def get_color(self):
+ self.set_light_color('full')
+ self.get_input_values()
+ return self.scaled_value
+
+ def get_reflected_light(self, color):
+ self.set_light_color(color)
+ self.get_input_values()
+ return self.scaled_value
+
class SoundSensor(AnalogSensor):
'Object for sound sensors'