Skip to content

Commit e6851a6

Browse files
committed
distance and color sensor added
1 parent 493da12 commit e6851a6

4 files changed

Lines changed: 256 additions & 5 deletions

File tree

.idea/workspace.xml

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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. |

src/content/docs/hardware/sensors/digital-sensor.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Digital Sensor
2+
title: Digital Sensors
33
description: Reading digital sensors with NextDigitalSensor
44
sidebar:
55
order: 3
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Distance Sensors
3+
description: Reading distance with NextDistanceSensor
4+
sidebar:
5+
order: 2
6+
---
7+
8+
import { Tabs, TabItem } from '@astrojs/starlight/components';
9+
10+
`NextDistanceSensor` is a lightweight wrapper for a distance sensor that caches the last reading.
11+
12+
## Creating a NextDistanceSensor
13+
14+
<Tabs syncKey="language">
15+
<TabItem label="Kotlin">
16+
```kotlin
17+
val sensor = NextDistanceSensor(RobotController.controlHub, 0) // By Lynx Module and I2C bus, recommended
18+
19+
val sensor = NextDistanceSensor("distanceSensor") // Using your configuration name
20+
```
21+
</TabItem>
22+
<TabItem label="Java">
23+
```java
24+
NextDistanceSensor sensor = new NextDistanceSensor(RobotController.getControlHub(), 0); // By Lynx Module and I2C bus, recommended
25+
26+
NextDistanceSensor sensor = new NextDistanceSensor("distanceSensor"); // Using your configuration name
27+
```
28+
</TabItem>
29+
</Tabs>
30+
31+
## Usage
32+
33+
### `update()`
34+
35+
Reads the distance at that moment and refreshes the cache. Call this once per loop, before reading anything. It's recommended to put this in your `periodic` for your `Mechanism`.
36+
37+
<Tabs syncKey="language">
38+
<TabItem label="Kotlin">
39+
```kotlin
40+
sensor.update()
41+
```
42+
</TabItem>
43+
<TabItem label="Java">
44+
```java
45+
sensor.update();
46+
```
47+
</TabItem>
48+
</Tabs>
49+
50+
### `isWithinDistance(threshold, unit)`
51+
52+
True if the distance sensor senses an object within `threshold`, in the given unit.
53+
54+
<Tabs syncKey="language">
55+
<TabItem label="Kotlin">
56+
```kotlin
57+
if (sensor.isWithinDistance(4.0)) { ... } //defaults to CM
58+
```
59+
</TabItem>
60+
<TabItem label="Java">
61+
```java
62+
if (sensor.isWithinDistance(4.0)) { ... } //defaults to CM
63+
```
64+
</TabItem>
65+
</Tabs>

0 commit comments

Comments
 (0)