|
| 1 | +--- |
| 2 | +title: Analog Inputs |
| 3 | +description: Reading raw and normalized voltage with NextAnalogInput |
| 4 | +sidebar: |
| 5 | + order: 6 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 9 | + |
| 10 | +`NextAnalogInput` is a wrapper around an analog input that gives you convenient access to different readings. |
| 11 | + |
| 12 | +It inherits from `AnalogFeedback`, so any `NextAnalogInput` can also be passed directly as the feedback source for a [feedback servo](/hardware/actuators/feedback-servos). |
| 13 | + |
| 14 | +## Creating a NextAnalogInput |
| 15 | + |
| 16 | +<Tabs syncKey="language"> |
| 17 | +<TabItem label="Kotlin"> |
| 18 | +```kotlin |
| 19 | +val input = NextAnalogInput(RobotController.controlHub, 0) // By Lynx Module and channel, recommended |
| 20 | + |
| 21 | +val input = NextAnalogInput("armEncoder") // Using your configuration name |
| 22 | +``` |
| 23 | +</TabItem> |
| 24 | +<TabItem label="Java"> |
| 25 | +```java |
| 26 | +NextAnalogInput input = new NextAnalogInput(RobotController.getControlHub(), 0); // By Lynx Module and channel, recommended |
| 27 | + |
| 28 | +NextAnalogInput input = new NextAnalogInput("armEncoder"); // Using your configuration name |
| 29 | +``` |
| 30 | +</TabItem> |
| 31 | +</Tabs> |
| 32 | + |
| 33 | +Both constructors accept two optional parameters: |
| 34 | + |
| 35 | +| Parameter | Type | Default | Description | |
| 36 | +|---|---|---|---| |
| 37 | +| `customTransformation` | `(Double) -> Double` | Value Unchanged | An optional transformation applied to the normalized voltage ratio. | |
| 38 | +| `maxVoltage` | `Voltage` | `3.3.volts` | The voltage that corresponds to a normalized value of `1.0`. | |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### `rawVoltage` |
| 43 | + |
| 44 | +`Voltage` this is the raw, untransformed voltage currently read from the analog input. |
| 45 | + |
| 46 | +<Tabs syncKey="language"> |
| 47 | +<TabItem label="Kotlin"> |
| 48 | +```kotlin |
| 49 | +val voltage = input.rawVoltage |
| 50 | +``` |
| 51 | +</TabItem> |
| 52 | +<TabItem label="Java"> |
| 53 | +```java |
| 54 | +Voltage voltage = input.getRawVoltage(); |
| 55 | +``` |
| 56 | +</TabItem> |
| 57 | +</Tabs> |
| 58 | + |
| 59 | +### `value` |
| 60 | + |
| 61 | +`Double` the normalized reading, starting as `rawVoltage` then divided by your `maxVoltage` and passed through your `customTransformation`. |
| 62 | + |
| 63 | +<Tabs syncKey="language"> |
| 64 | +<TabItem label="Kotlin"> |
| 65 | +```kotlin |
| 66 | +val normalized = input.value |
| 67 | +``` |
| 68 | +</TabItem> |
| 69 | +<TabItem label="Java"> |
| 70 | +```java |
| 71 | +double normalized = input.getValue(); |
| 72 | +``` |
| 73 | +</TabItem> |
| 74 | +</Tabs> |
0 commit comments