# Cancoder
###### tags: `程式組教程`
## 簡介
The CTR CANCoder is a rotary sensor that can be used to measure rotational position and velocity. The device senses the magnetic field(磁場) of a diametrically(直徑的) polarized magnet to determine rotational position with 12 bit precision(12位精度). The device is capable of providing a relative position measurement and an absolute position measurement simultaneously(同時) over the CAN bus.
## 燈號
| LED 顏色 | LED 亮度 | CAN 線的狀況 |
|:------------:|:--------:|:---------------------------------:|
| 緩慢紅色閃爍 | 亮 | CAN bus has been lost. |
| 快速紅色閃爍 | 暗 | CAN bus never detected since boot |
| 快速黃色閃爍 | 暗 | CAN bus never detected since boot |
| 快速綠色閃爍 | 暗 | CAN bus never detected since boot |
| 快速紅色閃爍 | 亮 | CAN bus present |
| 快速黃色閃爍 | 亮 | CAN bus present |
| 快速綠色閃爍 | 亮 | CAN bus present |

## Wiring
在 [CANCoder User’s Guide](https://store.ctr-electronics.com/content/user-manual/CANCoder%20User's%20Guide.pdf) 第 9 頁以後很多
自己看
## Programming
隨便啦 以下舉例一些
`.getPosition()` 取得 sensor 的位置
> Gets the position of the sensor. This may be relative or absolute depending on configuration. The units are determined by the coefficient and unit-string configuration params, default is degrees.
`.getVelocity()` 取得 sensor 的速度
> Gets the velocity of the sensor. The units are determined by the coefficient and unit-string configuration params, default is degrees per second.
`.setPosition()` 設定 sensor 的位置
> Sets the position of the sensor. The units are determined by the coefficient and unit-string configuration params, default is degrees.
`.setPositionToAbsolute() ` 將設定位置的型態改為 Absolute Sensor
> Sets the position of the sensor to match the magnet's "Absolute Sensor". The units are determined by the coefficient and unit-string configuration params, default is degrees.
`.configAbsoluteSensorRange() ` 設定回傳的數值類型
> Sets the signage and range of the "Absolute Position" signal. Choose unsigned for an absolute range of [0,+1) rotations, [0,360) deg, etc... Choose signed for an absolute range of [-0.5,+0.5) rotations, [-180,+180) deg, etc...
`.configFactoryDefault() ` 回歸原廠設定
> Configures all persistent settings to defaults.
`.configFeedbackCoefficient() ` 設定自己的單位 (可以乘上 Gear Ratio、CPR、Wheel Circumference 等數值)
> Choose what units you want the API to get/set. This also impacts the units displayed in Self-Test in Tuner. Depending on your mechanism, you may want to scale rotational units (deg, radians, rotations), or scale to a distance (inches, centimeters).
還有更多 但我累了先這樣
## 問題
我們在 Phoenix Tuner 上看不到 Cancoder -> 也看不到 id
類似的文章:[CANCoder Issue](https://www.chiefdelphi.com/t/cancoder-issue/398243)
1. 焊接問題
2. 要接在 12V 而不是 5V
3. Phoenix Tuner 版本
其他類似的文章:[CANCoders Not Working](https://www.chiefdelphi.com/t/cancoders-not-working/377253)
1. 要接在 12V 而不是 5V
:::spoiler 題外話
我看到別人做的東西 COOL
[FRC 5431 Titan Robotics - 2022 Build Thread](https://www.chiefdelphi.com/t/frc-5431-titan-robotics-2022-build-thread/400408)
:::
## 參考資料
- [CANCoder User’s Guide](https://store.ctr-electronics.com/content/user-manual/CANCoder%20User's%20Guide.pdf)
- [CANCoder Class Reference](https://store.ctr-electronics.com/content/api/java/html/classcom_1_1ctre_1_1phoenix_1_1sensors_1_1_c_a_n_coder.html)