# 簡單宣告 ## 一、導入 library :::info manage vendor library --> install new library offline --> ctre-phoenix / wpilib-new commands ::: ![](https://i.imgur.com/LgPyYbW.png) > What is WPILib? The WPI Robotics Library (WPILib) is the standard software library provided for teams to write code for their FRC® robots. A software library is a collection of code that can be imported into and used by other software. WPILib contains a set of useful classes and subroutines for interfacing with various parts of the FRC control system (such as sensors, motor controllers, and the driver station), as well as an assortment of other utility functions. ## 二、宣告馬達 ### 宣告四顆輪子 ```java= //宣告四顆輪子馬達// WPI_TalonFX leftFront = new WPI_TalonFX(1); WPI_TalonFX leftBack = new WPI_TalonFX(2); WPI_TalonFX rightFront = new WPI_TalonFX(4); WPI_TalonFX rightBack = new WPI_TalonFX(3); // WPI_TalonFX 名字可自己取 = new WPI_TalonFX( device ID of motor controller); ``` 上面的名字你想取什麼都可以,就跟平常打程式一樣,取什麼 cock 或 pornhub都可以,但為了好辨識,我們通常會取正常的名字,讓其他人在讀取和維修自己的程式時較為方便,較有可讀性。程式碼如果具備足夠的[可讀性](https://www.ithome.com.tw/node/73329),我們在開發上較容易維護及修改,也不容易犯錯而寫出有問題的程式碼,其他人能共同參與開發的難度也會降低。後面的那個數字是 device number,必須要看 phoneix tuner,之後會再講吧。 ![](https://i.imgur.com/lmNp224.png) 遇到紅色的波浪底線和數字,代表程式有錯,到時候再燒程式或是編毅時會跑不動。遇到黃色的波浪底線和數字,則是警告而已,可以不用理他。這麼會出現波浪底線和數字是因為我們尚未導入馬達的函式。使用 quick fix 再 import 馬達的函式則可消除紅色的波浪底線和數字。 ```java= import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX; ``` ### SpeedControllerGroup ```java= import edu.wpi.first.wpilibj.SpeedControllerGroup; //宣告左右兩顆馬達為一組// SpeedControllerGroup Left = new SpeedControllerGroup(leftFront, leftBack) ; SpeedControllerGroup Right = new SpeedControllerGroup(rightFront, rightBack) ; // SpeedControllerGroup 名字 = new SpeedControllerGroup(前面取名的馬達1, 前面取名的馬達2) ; ``` > Many FRC® drivetrains have more than 1 motor on each side. In order to use these with DifferentialDrive, the motors on each side have to be collected into a single SpeedController, using the SpeedControllerGroup class. The examples below show a 4 motor (2 per side) drivetrain. To extend to more motors, simply create the additional controllers and pass them all into the SpeedController group constructor (it takes an arbitrary number of inputs). ### DifferentialDrive ```java= import edu.wpi.first.wpilibj.drive.DifferentialDrive; DifferentialDrive Drive = new DifferentialDrive( Left , Right ) ; // DifferentialDrive 名字 = new DifferentialDrive( 前面的SpeedControllerGroup1 , 前面的SpeedControllerGroup2 ) ; ``` > WPILib provides separate Robot Drive classes for the most common drive train configurations (differential, mecanum, and Killough). The DifferentialDrive class handles the differential drivetrain configuration. These drive bases typically have two or more in-line traction or omni wheels per side (e.g., 6WD or 8WD) and may also be known as “skid-steer”, “tank drive”, or “West Coast Drive” (WCD). The Kit of Parts drivetrain is an example of a differential drive. There are methods to control the drive with 3 different styles (“Tank”, “Arcade”, or “Curvature”). ### 設定馬達正反轉 基本上馬達不管有沒有要反轉,我們都會把它寫出來。如此一來,以後在更改馬達轉動方向時可以更加一目瞭然,在寫程式時更加輕鬆。 ```java= leftFront.setInverted(false); leftBack.setInverted(false); rightFront.setInverted(false); rightBack.setInverted(false); ``` ### 設定搖桿 ```java= Joystick js1 = new Joystick(0); Joystick js2 = new Joystick(1); //Joystick 名字 =new Joystick(The port on the Driver Station that the js1 is plugged into.); ``` 宣告一個搖桿,後面的數字是 port,後面在看 driver station 時會講到吧~ ```java= import edu.wpi.first.wpilibj.drive.Joystick; ``` ### 設定按鈕 為了讓程式更有可讀性,我們替這些 joystick上的搖桿和按鈕取名字,後面的數字必須要看 driver station 才可得知,後面會講到吧。 :::danger :b: 大提醒 :b: 沒事不要用到 Shoulder! 那真的很破! ::: ```java= public final int leftStick_X = 0; public final int leftStick_Y = 1; public final int rightStick_X = 4; public final int rightStick_Y = 5; public final int trigger_L = 2; public final int trigger_R = 3; public final int Btn_A = 1; public final int Btn_B = 2; public final int Btn_X = 3; public final int Btn_Y = 4; public final int Btn_LB = 5; public final int Btn_RB = 6; public final int Btn_LS = 9; public final int Btn_RS = 10; ``` ![](https://i.imgur.com/d3bT71k.jpg) 資料來源:https://docs.wpilib.org/en/stable/docs/software/actuators/wpi-drive-classes.html ###### tags: `程式組教程`