ロボットプログラムの作成

すべてをインストールできたら、ロボットプログラムを作成することが出来ます。WPILibには複数のロボットプログラムテンプレートがあります。初心者にはこれらのテンプレートの利用をおすすめしますが、上級者はゼロからロボットのコードを書いても構いません。

基本クラスの選択

To start a project using one of the WPILib robot program templates, users must first choose a base class for their robot. Users subclass these base classes to create their primary Robot class, which controls the main flow of the robot program. There are various choices available for the base class:

TimedRobot

Documentation: Java - C++

Source: Java - C++

The TimedRobot class is the base class recommended for most users. It provides control of the robot program through a collection of init(), periodic(), and exit() methods, which are called by WPILib during specific robot states (e.g. autonomous or teleoperated). During these calls, your code typically polls each input device and acts according to the data it receives. For instance, you would typically determine the position of the joystick and state of the joystick buttons on each call and act accordingly. The TimedRobot class also provides an example of retrieving autonomous routines through SendableChooser (Java/ C++)

注釈

TimedRobot Skeleton テンプレートは、いくつかの有益なコメントとautonomousの例を取り除いたものです。すでに TimedRobot に慣れている場合は、これを使うことができます。以下に示す例は TimedRobot Skeleton のものです。

 7import edu.wpi.first.wpilibj.TimedRobot;
 8
 9/**
10 * The methods in this class are called automatically corresponding to each mode, as described in
11 * the TimedRobot documentation. If you change the name of this class or the package after creating
12 * this project, you must also update the Main.java file in the project.
13 */
14public class Robot extends TimedRobot {
15  /**
16   * This function is run when the robot is first started up and should be used for any
17   * initialization code.
18   */
19  public Robot() {}
20
21  @Override
22  public void robotPeriodic() {}
23
24  @Override
25  public void autonomousInit() {}
26
27  @Override
28  public void autonomousPeriodic() {}
29
30  @Override
31  public void teleopInit() {}
32
33  @Override
34  public void teleopPeriodic() {}
35
36  @Override
37  public void disabledInit() {}
38
39  @Override
40  public void disabledPeriodic() {}
41
42  @Override
43  public void testInit() {}
44
45  @Override
46  public void testPeriodic() {}
47
48  @Override
49  public void simulationInit() {}
50
51  @Override
52  public void simulationPeriodic() {}
53}
 5#include "Robot.h"
 6
 7Robot::Robot() {}
 8void Robot::RobotPeriodic() {}
 9
10void Robot::AutonomousInit() {}
11void Robot::AutonomousPeriodic() {}
12
13void Robot::TeleopInit() {}
14void Robot::TeleopPeriodic() {}
15
16void Robot::DisabledInit() {}
17void Robot::DisabledPeriodic() {}
18
19void Robot::TestInit() {}
20void Robot::TestPeriodic() {}
21
22void Robot::SimulationInit() {}
23void Robot::SimulationPeriodic() {}
24
25#ifndef RUNNING_FRC_TESTS
26int main() {
27  return frc::StartRobot<Robot>();
28}
29#endif

「Periodic」メソッドはデフォルトで20ミリ秒ごとに呼び出されます。これは、新しい更新レートを指定してスーパークラスのコンストラクタを呼び出すことで変更できます。

危険

Changing your robot rate can cause some unintended behavior (loop overruns). Teams can also use Notifiers to schedule methods at a custom rate.

public Robot() {
  super(0.03); // Periodic methods will now be called every 30 ms.
}
Robot() : frc::TimedRobot(30_ms) {}

Timeslice Robot

Documentation: Java - C++

Source: Java - C++

The TimesliceRobot class extends the TimedRobot framework to provide more control over the scheduling of periodic functions. It allows users to allocate specific time slices to different robot operations, running them sequentially within a defined period (typically shorter than TimedRobot's default 20ms). This class is recommended for users who need more precise timing control and consistent starting times for their robot's periodic functions.

TimesliceRobot provides the same init(), periodic(), and exit() methods as TimedRobot, but adds the ability to schedule additional periodic functions with custom allocations. This allows for more efficient use of processing time and can lead to improved performance which can make odometry and estimators more accurate and controller outputs change more consistently.

RobotBase

Documentation: Java - C++

Source: Java - C++

RobotBase クラスは最も最小限の基本クラスであり、一般的に直接使用することは推奨されません。ロボットの制御フローはユーザーのために処理されないので、 startCompetition() メソッドの中でゼロからすべてを書かなければなりません。デフォルトでは、このテンプレートは異なるオペレーションモード(teleop、autoなど)の処理方法をを紹介しています。

注釈

RobotBase Skeleton テンプレートが用意されており、空白の startCompetition() メソッドを提供します。

コマンド・ロボット

Command Robot フレームワークは、自動的に入力を取得し、生の入力データをイベントに変換することで、 Timed Robot の基本的な機能を拡張します。 これらのイベントは、イベントがトリガーされたときに実行されるユーザーコードに関連付けられています。例えば、ボタンが押されると、そのボタンが押されたことに関連するコードが自動的に呼び出され、そのボタンの状態を直接取得したり追跡したりする必要はありません。 コマンドロボットフレームワークは、複雑な動作をするコンパクトで読みやすいコードを書くことを容易にしますが、コマンドロボットフレームワークがどのように動作するかを理解するために、プログラマーに追加の先行時間投資時間が要求されます。

Command Robot を使用しているチームは、 Command-Based Programmingというチュートリアル を参照してください。

Romi

Romi を使用しているチームは Romi - Timed または Romi - Command Bot テンプレートを使用してください。

Romi - Timed

Romi - Timed テンプレートは、 arcadeDrive(double xaxisSpeed, double zaxisRotate) メソッドを持つ RomiDrivetrain クラスを提供します。このarcadeDrive関数を実装するかどうかはユーザー次第です。

このクラスはまた、Romi のオンボードエンコーダーを取得し、リセットするための関数も提供します。

Romi - Command Bot

Romi - Command Bot テンプレートは、 arcadeDrive(double xaxisSpeed, double zaxisRotate) メソッドを持つ RomiDrivetrain サブシステムを提供します。この arcadeDrive 関数を使うかどうかはユーザー次第です。

このサブシステムはまた、Romiのオンボード・エンコーダーを取得したり、リセットしたりする機能も備えています。

基本クラスを使用しない場合

必要であれば、ユーザは基本クラスを完全に省略し、他のプログラムと同じように main() メソッドでプログラムを書くこともできます。 これは非常に推奨されませんが(ユーザーはロボットコードを書くときに「車輪の再発明」をすべきではありません)、プログラムの流れを完全にコントロールしたい人のためにサポートされています。

警告

ユーザーは、自分が何をやっているのか絶対に確信が持てない限り、ロボットプログラムの main() メソッドを変更 すべきではありません

新規WPILibプロジェクトの作成

基本クラスが決まったら、新しいロボットプロジェクトを作成することが出来ます。 Ctrl+Shift+P でVisual Studio Codeのコマンドパレットを表示します。そしてプロンプトに 「WPILib」と入力します。 そうすると、WPILibのVS Codeコマンドの一覧が出ます。その中から、 Create a new project を選びます:

「WPILib: Create a new project」コマンドがハイライトされる。

すると、「New Project Creator」ウィンドウが表示されます。

新しいプロジェクト作成画面。

New Project Creator ウィンドウのそれぞれの要素の説明は以下です。

  1. Project Type: 作成したいプロジェクトの種類。 これはサンプルプロジェクト、またはWPILibが提供するプロジェクトテンプレートの1つです。 テンプレートはロボットの基本クラスごとに存在します。 さらに、 Command-based プロジェクト用のテンプレートも存在します。これは TimedRobot 基本クラスをベースに構築されていますが、多くの追加機能を含んでいます。この種のロボットプログラムは、新しいチームに強く推奨されます。

  2. Language: プロジェクトで利用する言語 (C++またはJava)。

  3. Base Folder: テンプレートプロジェクトの場合、使用するテンプレートの種類を指定します。

  4. Project Location: ロボットプロジェクトを配置するフォルダ。

  5. Project Name: プロジェクト名。「Create New Folder」と書かれたチェックボックスを選択した場合は、プロジェクトのフォルダの名前にもなります。

  6. Create a New Folder: これをチェックすると、以前に指定したフォルダー内にプロジェクトを保存するための新しいフォルダーが作成されます。 チェックされていない 場合、プロジェクトは以前に指定されたフォルダに直接配置されます。 フォルダが空でなく、このオプションがチェックされていない場合は、エラーが発生します。

  7. Team Number: プロジェクト用のチーム番号。プロジェクトのパッケージ名やロボットにデプロイするために利用されます。

  8. Enable Desktop Support: Enables unit test and simulation support (see Introduction to Robot Simulation).

    • Java: This option has no effect and can be left checked or unchecked

    • C++: Checking this option enables desktop compilation, which is required for simulation and unit tests

    • Romi/XRP: This option has no effect for Romi and XRP templates and can be left checked or unchecked

    注釈

    While WPILib fully supports desktop builds, some third-party vendor libraries may not. If a library doesn't support desktop compilation, your C++ code may not compile or may crash when running simulation.

上記項目を設定したら、「Generate Project」をクリックします。そうするとロボットのプロジェクトが作成されます。

注釈

プロジェクト作成のエラーは、画面の右下隅に表示されます。

警告

OneDriveのキャッシュがビルドシステムに干渉するため、OneDrive上でのプロジェクトの作成はサポートされていません。Windowsのインストールによっては、デフォルトでOneDriveに「ドキュメント」と「デスクトップ」フォルダが置かれています。

すべての設定を選択した後の例を以下に表示します。

新規プロジェクト作成画面が完成しました。

新しいプロジェクトを開く

プロジェクトの作成後、VS Codeは以下のようにプロジェクトを開くかどうかの選択肢を表示します。 Ctrl+K を押してから Ctrl+O を押し(macOSでは単に Command+O ) 、プロジェクトを保存したフォルダを選択することで、すぐやるか後でやるかを選択することができます。

VS Codeでプロジェクトを開くポップアップ

プロジェクトを開くと、左の方にプロジェクト階層が表示されます。ファイルをダブルクリックするとエディタにてファイルを表示できます。

VS Codeエディタでファイルを開く。

C++の設定 (C++のみ)

C++プロジェクトでは、IntelliSenseをセットアップしなければなりません。プロジェクトを開くと、右下隅にC++の設定をリフレッシュするかを聞くポップアップが出現します。「Yes」をクリックしてIntelliSenseをセットアップします。

C++の設定をリフレッシュするよう求められたときに「はい」を選択する様子。