# 進階內容 ## Falcon 500 (TalonFX) 函式庫解析 ### Function * `getPosition().getValueAsDouble()`: 取得馬達旋轉角度 (Rotation) * `getVelocity().getValueAsDouble()`: 取得馬達速度 (RPS) * `get()`: 取得馬達速度百分比 (1 ~ -1) * `getAcceleration()` 取得馬達加速度 * `getDeviceID()` 取得馬達ID * `getMotorVoltage()` 取得馬達輸出電壓 ### Method * `set()`: 馬達運轉百分比 -1 ~ 1 * `setControl()`: 設定控制模式 * `setPosition()`: 設定馬達角度 * `setVoltage()`: 設定馬達輸入電壓 * `stopMotor()`: 停止馬達 ### Config #### 設定馬達函式 * `getConfigurator().apply(config)` #### Feedback * `withSensorToMechanismRatio()`: 馬達Encoder齒輪比轉換 #### MotorOutput * `withNeutralMode`: 設定 Brake & Coast * `withInverted`: 設定 正反轉 #### CurrentLimits * `withSupplyCurrentLimitEnable`: 是否限制馬達最大電流 * `withSupplyCurrentLimit`: 設定馬達最大電流 * `withStatorCurrentLimitEnable`: 是否限制馬達扭力輸出 * `withStatorCurrentLimit`: 設定馬達扭力輸出最大電流 ## SparkMax函式庫解析 ### Function * `get()`: 取得馬達速度百分比 (1 ~ -1) * `getBusVoltage()`: 取得SparkMax輸入的電壓 * `getDeviceId()`: 取得馬達ID * `getOutputCurrent()`: 取得馬達輸出電流 * `getEncoder().getPosition()`: 取得馬達角度 * `getEncoder().getVelocity()`: 取得馬達速度 ### Method * `set()`: 設定馬達運轉百分比 -1 ~ -1 * `setVoltage()`: 設定馬達輸出電壓 * `getEncoder().setPosition()`: 設定馬達角度 * `stopMotor()`: 停止馬達 ### Config #### 設定馬達函式 * `configure(driveConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters)` #### encoder * `positionConversionFactor` 轉換馬達經過齒輪比角度 * `velocityConversionFactor` 轉換馬達經過齒輪比速度 #### 其他 * `idleMode` 設定馬達Brake & Coast * `smartCurrentLimit` 設定最大電流 * `inverted` 設定正反轉 ## RelativeEncoder(Neo內建) 在宣告馬達時同時宣告一個 `RelativeEncoder` 並把此 Encoder 定義為 NEO 馬達內建的 Encoder ```java public class DriveMotorModule { private final SparkMax motor; private final RelativeEncoder encoder; public DriveMotorModule(int motorPort, boolean reverse) { this.motor = new SparkMax(motorPort, MotorType.kBrushless); this.encoder = this.motor.getEncoder(); } } ``` 創建 Neo 馬達的物件時ID後方必須填馬達類型, 使用的是 `MotorType.kBrushless` 無刷馬達 例: `CANSparkMax(ID, MotorType.kBrushless)` ## Rev Throug Encoder ![image13.wm](https://hackmd.io/_uploads/SyXibwlceg.png) 用 `DutyCycleEncoder` 當作encoder的物件 [詳細接線連結](https://www.revrobotics.com/rev-11-1271/) 範例: ```java public class DriveMotorModule { private final DutyCycleEncoder encoder; public DriveMotorModule() { this.encoder = new DutyCycleEncoder(0); } } ``` ### Function * `get()` 取得絕對角度 (Rotation) * `isConnected()` 是否有連接 ### Method * `setInverted()` 設定正反轉 * `setDutyCycleRange()` 設定角度範圍 ## CANCoder 範例: ```java public class DriveMotorModule { private final CANCoder canCoder; public DriveMotorModule(int motorPort, int encoderPort, boolean reverse) { this.canCoder = new CANCoder(encoderPort); } } ``` ### Function * `getPosition().getValueAsDouble()` 取得相對角度 * `getAbsolutePosition().getValueAsDouble()` 取得絕對角度 * `getVelocity().getValueAsDouble()` 取得速度 * `getDeviceID()` 取得Id ### Method * `setPosition` 設定角度 ## Commands指令類別 ### Command Group #### SequentialCommandGroup * (Command...) 1執行完執行2 指令依序執行 #### ParallelCommandGroup * (Command...) 1&2同時執行 #### ParallelRaceGroup * (Command...) 1&2同時執行 其中一個執行完終止全部指令 #### ParallelDeadlineGroup * (Deadline, Command...) Deadline結束 所有Command結束 ### Commands * `Commands.run(action, requiredments)` 要執行的動作、對應的subsystem (執行到被中斷) * `Commands.runEnd(run, runEnd, requirements) ` 要執行的動作、結束時執行的動作、對應的subsystem (運行命令直到中斷,接著運行第二個命令) * `Commands.runOnce(action, requirements)` 要執行的動作、對應的subsystem (只執行一次操作) ### WaitCommand * `new WaitCommand(seconds)` 不會執行任何操作,在指定持續時間後結束 ### WaitUntilCommand * `new WaitUilCommand(BooleanSupplier) 當Boolen為true 終止命令 ## 陀螺儀(Gyro) ### 宣告方法 創建物件 * `NavX-micro` 在創建AHRS物件時Port為 `SerialPort.Port.kUSB` * `NavX-MXP` Port為 `SPI.Port.kMXP` (一和二代都一樣) ```java public class DriveMotorSubsystem extends SubsystemBase { private final AHRS gyro; public DriveMotorSubsystem() { this.gyro = new AHRS(SPI.Port.kMXP); } } ``` ### 小知識 物體自由度,Roll(翻滾)、Pitch(俯仰)、Yaw(偏擺) ![image9.wm](https://hackmd.io/_uploads/BJdUzDl5gl.png) ### 函式 * `getYaw` 、 `getAngle` 取得Yaw(偏擺) * `getPitch` 取得Pitch(俯仰) * `getRoll` 取得Roll(翻滾) * `getRotation2d` 以Rotation2d形式傳回機器人航向 * `reset` 重置陀螺儀 * `getWorldLinearAccelX` 取得X軸線性加速度 * `getWorldLinearAccelY` 取得Y軸線性加速度 * `getWorldLinearAccelZ` 取得Z軸線性加速度