|
| 1 | +--- |
| 2 | +title: Color Sensors |
| 3 | +description: Reading color and distance with NextColorDistanceSensor |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 9 | + |
| 10 | +`NextColorDistanceSensor` combines a color sensor and an optional distance sensor into one class. |
| 11 | + |
| 12 | +## Creating a NextColorDistanceSensor |
| 13 | + |
| 14 | +<Tabs syncKey="language"> |
| 15 | +<TabItem label="Kotlin"> |
| 16 | +```kotlin |
| 17 | +val sensor = NextColorDistanceSensor(RobotController.controlHub, 0) // By Lynx Module and I2C bus, recommended |
| 18 | + |
| 19 | +val sensor = NextColorDistanceSensor("colorSensor") // Using your configuration name |
| 20 | +``` |
| 21 | +</TabItem> |
| 22 | +<TabItem label="Java"> |
| 23 | +```java |
| 24 | +NextColorDistanceSensor sensor = new NextColorDistanceSensor(RobotController.getControlHub(), 0); // By Lynx Module and I2C bus, recommended |
| 25 | + |
| 26 | +NextColorDistanceSensor sensor = new NextColorDistanceSensor("colorSensor"); // Using your configuration name |
| 27 | +``` |
| 28 | +</TabItem> |
| 29 | +</Tabs> |
| 30 | + |
| 31 | +Both constructors accept an optional `hasDistance` parameter: |
| 32 | + |
| 33 | +| Parameter | Type | Default | Description | |
| 34 | +|---|---|---|---| |
| 35 | +| `hasDistance` | `Boolean` | `false` | Whether to also create a distance sensor from the same sensor. | |
| 36 | + |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +### `update()` |
| 41 | + |
| 42 | +Reads the color sensor (and distance sensor, if present) and refreshes the cache. Call this once per loop, before reading anything. It's recommended to put this in your `periodic` for your `Mechanism`.. |
| 43 | + |
| 44 | +<Tabs syncKey="language"> |
| 45 | +<TabItem label="Kotlin"> |
| 46 | +```kotlin |
| 47 | +sensor.update() |
| 48 | +``` |
| 49 | +</TabItem> |
| 50 | +<TabItem label="Java"> |
| 51 | +```java |
| 52 | +sensor.update(); |
| 53 | +``` |
| 54 | +</TabItem> |
| 55 | +</Tabs> |
| 56 | + |
| 57 | +### `isWithinDistance(threshold, unit)` |
| 58 | + |
| 59 | +True if the distance sensor senses an object within `threshold`, in the given unit. |
| 60 | + |
| 61 | +<Tabs syncKey="language"> |
| 62 | +<TabItem label="Kotlin"> |
| 63 | +```kotlin |
| 64 | +if (sensor.isWithinDistance(4.0)) { ... } //defaults to CM |
| 65 | +``` |
| 66 | +</TabItem> |
| 67 | +<TabItem label="Java"> |
| 68 | +```java |
| 69 | +if (sensor.isWithinDistance(4.0)) { ... } //defaults to CM |
| 70 | +``` |
| 71 | +</TabItem> |
| 72 | +</Tabs> |
| 73 | + |
| 74 | +### `isColor(profile)` |
| 75 | + |
| 76 | +True if the cached color reading matches the given `ColorProfile`. |
| 77 | + |
| 78 | +<Tabs syncKey="language"> |
| 79 | +<TabItem label="Kotlin"> |
| 80 | +```kotlin |
| 81 | +if (sensor.isColor(green)) { ... } |
| 82 | +``` |
| 83 | +</TabItem> |
| 84 | +<TabItem label="Java"> |
| 85 | +```java |
| 86 | +if (sensor.isColor(green)) { ... } |
| 87 | +``` |
| 88 | +</TabItem> |
| 89 | +</Tabs> |
| 90 | + |
| 91 | +### `isColorWithinDistance(profile, threshold, unit)` |
| 92 | + |
| 93 | +True if the cached color reading matches `profile` and an object is within `threshold`, in the given unit. |
| 94 | + |
| 95 | +<Tabs syncKey="language"> |
| 96 | +<TabItem label="Kotlin"> |
| 97 | +```kotlin |
| 98 | +if (sensor.isColorWithinDistance(green, 4.0)) { ... } //defaults to CM |
| 99 | +``` |
| 100 | +</TabItem> |
| 101 | +<TabItem label="Java"> |
| 102 | +```java |
| 103 | +if (sensor.isColorWithinDistance(green, 4.0)) { ... } //defaults to CM |
| 104 | +``` |
| 105 | +</TabItem> |
| 106 | +</Tabs> |
| 107 | + |
| 108 | +### `debug()` |
| 109 | + |
| 110 | +Single-line telemetry string showing the current RGB, HSV, and distance readings. Useful for calibrating `ColorProfile`s. |
| 111 | + |
| 112 | +<Tabs syncKey="language"> |
| 113 | +<TabItem label="Kotlin"> |
| 114 | +```kotlin |
| 115 | +telemetry.addLine(sensor.debug()) |
| 116 | +``` |
| 117 | +</TabItem> |
| 118 | +<TabItem label="Java"> |
| 119 | +```java |
| 120 | +telemetry.addLine(sensor.debug()); |
| 121 | +``` |
| 122 | +</TabItem> |
| 123 | +</Tabs> |
| 124 | + |
| 125 | +## NextColor |
| 126 | + |
| 127 | +`NextColor` stores a color, either as RGB or HSV. HSV is recommended for most cases, since it's more stable under different lighting conditions. |
| 128 | + |
| 129 | +<Tabs syncKey="language"> |
| 130 | +<TabItem label="Kotlin"> |
| 131 | +```kotlin |
| 132 | +val lime = NextColor.hsv(100f, 0.9f, 0.85f) |
| 133 | + |
| 134 | +val sameColor = NextColor.rgb(lime.red, lime.green, lime.blue) |
| 135 | +``` |
| 136 | +</TabItem> |
| 137 | +<TabItem label="Java"> |
| 138 | +```java |
| 139 | +NextColor lime = NextColor.hsv(100f, 0.9f, 0.85f); |
| 140 | + |
| 141 | +NextColor sameColor = NextColor.rgb(lime.getRed(), lime.getGreen(), lime.getBlue()); |
| 142 | +``` |
| 143 | +</TabItem> |
| 144 | +</Tabs> |
| 145 | + |
| 146 | +## ColorProfile |
| 147 | + |
| 148 | +`ColorProfile` describes a target color and the tolerances, to match your readings. These can be used for `isColor()` or `isColorWithinDistance()`. |
| 149 | + |
| 150 | +<Tabs syncKey="language"> |
| 151 | +<TabItem label="Kotlin"> |
| 152 | +```kotlin |
| 153 | +val green = ColorProfile( |
| 154 | + space = ColorSpace.HSV, |
| 155 | + color = NextColor.hsv(130f, 0.7f, 0.6f), |
| 156 | + tolerance = NextColor.hsv(20f, 0.3f, 1f), |
| 157 | +) |
| 158 | +``` |
| 159 | +</TabItem> |
| 160 | +<TabItem label="Java"> |
| 161 | +```java |
| 162 | +ColorProfile green = new ColorProfile( |
| 163 | + ColorSpace.HSV, |
| 164 | + NextColor.hsv(130f, 0.7f, 0.6f), |
| 165 | + NextColor.hsv(20f, 0.3f, 1f) |
| 166 | +); |
| 167 | +``` |
| 168 | +</TabItem> |
| 169 | +</Tabs> |
| 170 | + |
| 171 | +| Parameter | Type | Description | |
| 172 | +|---|---|---| |
| 173 | +| `space` | `ColorSpace` | The color space to compare in: `RGB` or `HSV`. | |
| 174 | +| `color` | `NextColor` | The target color to match against. | |
| 175 | +| `tolerance` | `NextColor` | How far each channel can deviate from `color` and still count as a match. | |
0 commit comments