# 程式101
###### tags: `程式` `函式庫安裝`
## 安裝環境
有(at least)三包東西要安裝
### 1. FRC game tool
由NI(National Instrument)提供
對機器通訊用的程式們,例如Driver Station
(以及LabVIEW編寫FRC機器用的library [^LabVIEWLib])
https://www.ni.com/zh-tw/support/downloads/drivers/download.frc-game-tools.html#440024
[^LabVIEWLib]: 類似於java要裝WPILib這個程式庫才能寫FRC的程式,LabVIEW要裝這些library才能寫FRC,總之不是我們要用的東西
### 2. WPILib
由[WPI](https://zh.wikipedia.org/zh-tw/%E4%BC%8D%E6%96%AF%E7%89%B9%E7%90%86%E5%B7%A5%E5%AD%A6%E9%99%A2)提供
讓C++/java可以寫FRC機器程式的library,以及code裡面寫說在`edu.wpi.first.wpilibj`package底下的所有東西
內含VSCode、Smart dashboard等,一堆很厲害的東西
https://github.com/wpilibsuite/allwpilib/releases
### 3. [3rd Party libraries](https://docs.wpilib.org/en/stable/docs/software/vscode-overview/3rd-party-libraries.html)
#### [線上安裝方法(Online Installation)](https://docs.wpilib.org/en/latest/docs/software/vscode-overview/3rd-party-libraries.html#adding-offline-libraries)
* 打開 WPILib VS Code 並點擊右上角的 WPILib 圖示
* 選擇Manage Vendor Libraries
* 按一下Install new libraries (online)
* 貼上URL並按下Enter
#### 1.REV Robotics REVLib
搭配SPARK MAX和Color Sensor V3
*Online Installation URL:*
https://software-metadata.revrobotics.com/REVLib-2024.json
*Offline Installation*
* 下載REVLib壓縮檔
* 解壓縮後將所有資料夾複製到”C:\Users\Public\wpilib\2024”中
* 完成安裝
https://docs.revrobotics.com/brushless/revlib/revlib-overview#c-and-java-installation
#### 2.[CTRE Phoenix Framework](https://store.ctr-electronics.com/software/)
由CTRE(Victor SPX, Talon SRX馬達控制器和CANcoder, CANifier, Pigeon IMU,
Pigeon 2.0等CAN裝置的製造商)提供
要操作這兩種馬達控制器用的,還有馬達控制器的很多特異功能
還有[Pheonix tuner X](https://v6.docs.ctr-electronics.com/en/stable/docs/tuner/index.html)
*Online Installation URL:*
Phoenix 6: https://maven.ctr-electronics.com/release/com/ctre/phoenix6/latest/Phoenix6-frc2024-latest.json
*Offline Installation*
* 選擇FRC Offline Installer
* 在FRC Libraries中勾選"C++/Java"
* 等待安裝完成
https://docs.ctr-electronics.com/
ref: https://docs.wpilib.org/en/stable/docs/software/can-devices/third-party-devices.html
#### 3.[Kauai Labs](https://pdocs.kauailabs.com/navx-mxp/software/roborio-libraries/java/)
搭配NavX-MXP使用,離線安裝的檔案目前沒有更新,建議使用線上安裝
*Online Installation URL:*
https://dev.studica.com/releases/2024/NavX.json
```
//宣告
m_ahrs = new AHRS(SerialPort.Port.kMXP);
```
---
## 創建新專案
1. 打開VSCode
2. ctrl+shift+P -> "WPILib: create a new project"
3. (GUI表單) 選擇語言(C++/Java), 框架(Timed/Command), 資料夾, 隊號
4. ctrl+shift+P -> "WPILib: Manage Vendor Libraries" 或 檔案列表build.gradle按右鍵"Manage Vendor Libraries"
5. -> "install new libraries" -> "CTRE-Pheonix"
(1,2,3 ref: https://docs.wpilib.org/en/latest/docs/zero-to-robot/step-4/creating-benchtop-test-program-cpp-java.html)
(1,2,3 ref: https://docs.wpilib.org/en/stable/docs/software/vscode-overview/creating-robot-program.html)
(4,5 ref: https://docs.wpilib.org/en/latest/docs/software/vscode-overview/3rd-party-libraries.html)
---
## 編譯上傳
ctrl+shift+P -> "WPILib: Deploy Robot Code" 或 檔案列表build.gradle按右鍵"Deploy Robot Code"
---
## Timed Robot 基本架構
`src\main\java\frc\robot\Robot.java`
```java
public void robotInit() {
// 機器剛開機執行一次
// 用來初始化各個元件(搖桿&馬達)
}
public void teleopInit() {
// 剛進入teleOp模式時執行一次
// 通常留空,或者設定一些狀態例如變速檔位,以及機器初始狀態所有馬達靜止
}
public void teleopPeriodic() {
// 在teleOp模式時每20ms一直重複執行
// 大部分事情寫在這裡
}
// 自走模式的init和periodic
public void autonomousInit() {
}
public void autonomousPeriodic() {
}
```
---
## 基本元件
### XboxController
`XboxController` extends `GenericHID`
https://docs.wpilib.org/en/stable/docs/software/basic-programming/joystick.html#xboxcontroller-class
https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/XboxController.html
```java
import edu.wpi.first.wpilibj.XboxController;
...
XboxController con;
...
con = new XboxController(0);
...
con.getAButton(); // 被按著
con.getAButtonPressed(); // 按一次
con.getAButtonReleased(); // 放開(一次)
...
con.getLeftX(); // 左蘑菇頭的X軸
...
// methods inherit from GenericHID
con.getPOV(0) // 第0個POV被按下的角度
con.getPOV() // 預設(第0個)POV被按下的角度
/* Don't use these:
con.getRawButton(6) // 第6顆按鈕被按著
con.getRawButtonPressed(6) // 第6顆按鈕被按下過
con.getRawButtonReleased(6) // 第6顆按鈕被放開過
// 按鈕編號從1開始
con.getRawAxis(3) // 第0個軸
// 軸編號從0開始
*/
```
### WPI_VictorSPX
`WPI_VictorSPX` extends `VictorSPX`
https://api.ctr-electronics.com/phoenix/release/java/com/ctre/phoenix/motorcontrol/can/WPI_VictorSPX.html
```java
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
...
WPI_VictorSPX motor1;
...
motor1 = new WPI_VictorSPX(8);
...
motor1.set(-0.4)
```
---
## Smart Dashboard
https://docs.wpilib.org/en/stable/docs/software/dashboards/shuffleboard/getting-started/shuffleboard-tour.html
https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.html
```java
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
...
SmartDashboard.putNumber("Car_Speed", 0);
...
carspeed=SmartDashboard.getNumber("Car_Speed", 0);
```
---
## Example code
```java
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.XboxController;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to
* each mode, as described in the TimedRobot documentation. If you change the
* name of this class or
* the package after creating this project, you must also update the
* build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
private XboxController con;
private WPI_VictorSPX motor1;
/**
* This function is run when the robot is first started up and should be used
* for any
* initialization code.
*/
@Override
public void robotInit() {
con = new XboxController(0);
motor1 = new WPI_VictorSPX(8);
}
/**
* This function is called every 20 ms, no matter the mode. Use this for items
* like diagnostics
* that you want ran during disabled, autonomous, teleoperated and test.
*
* <p>
* This runs after the mode specific periodic functions, but before LiveWindow
* and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
}
/**
* This autonomous (along with the chooser code above) shows how to select
* between different
* autonomous modes using the dashboard. The sendable chooser code works with
* the Java
* SmartDashboard. If you prefer the LabVIEW Dashboard, remove all of the
* chooser code and
* uncomment the getString line to get the auto name from the text box below the
* Gyro
*
* <p>
* You can add additional auto modes by adding additional comparisons to the
* switch structure
* below with additional strings. If using the SendableChooser make sure to add
* them to the
* chooser code above as well.
*/
@Override
public void autonomousInit() {
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
}
/** This function is called once when teleop is enabled. */
@Override
public void teleopInit() {
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {
if (con.getAButton()) {
motor1.set(0.3);
} else {
motor1.set(0.0);
}
}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {
}
/** This function is called periodically when disabled. */
@Override
public void disabledPeriodic() {
}
/** This function is called once when test mode is enabled. */
@Override
public void testInit() {
}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {
}
/** This function is called once when the robot is first started up. */
@Override
public void simulationInit() {
}
/** This function is called periodically whilst in simulation. */
@Override
public void simulationPeriodic() {
}
}
```
```java
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to
* each mode, as described in the TimedRobot documentation. If you change the
* name of this class or
* the package after creating this project, you must also update the
* build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
/**
* This function is run when the robot is first started up and should be used
* for any
* initialization code.
*/
@Override
public void robotInit() {
}
/**
* This function is called every 20 ms, no matter the mode. Use this for items
* like diagnostics
* that you want ran during disabled, autonomous, teleoperated and test.
*
* <p>
* This runs after the mode specific periodic functions, but before LiveWindow
* and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
}
/**
* This autonomous (along with the chooser code above) shows how to select
* between different
* autonomous modes using the dashboard. The sendable chooser code works with
* the Java
* SmartDashboard. If you prefer the LabVIEW Dashboard, remove all of the
* chooser code and
* uncomment the getString line to get the auto name from the text box below the
* Gyro
*
* <p>
* You can add additional auto modes by adding additional comparisons to the
* switch structure
* below with additional strings. If using the SendableChooser make sure to add
* them to the
* chooser code above as well.
*/
@Override
public void autonomousInit() {
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
}
/** This function is called once when teleop is enabled. */
@Override
public void teleopInit() {
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {
}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {
}
/** This function is called periodically when disabled. */
@Override
public void disabledPeriodic() {
}
/** This function is called once when test mode is enabled. */
@Override
public void testInit() {
}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {
}
/** This function is called once when the robot is first started up. */
@Override
public void simulationInit() {
}
/** This function is called periodically whilst in simulation. */
@Override
public void simulationPeriodic() {
}
}
```