FTC | Developing with VSCode - NicholsSchool/2023-Programming-Team-Wiki GitHub Wiki
Introduction
At Electric Mayhem, we use Visual Studio Code extensively for programming FRC Robots because of its versatility and lightweight nature. However, FTC has been stuck with two options. The primitive and unreliable OnBotJava, great for rapid prototyping, but lacking in features, and the relatively superior Android Studio, with more features, but more bloated and slow. We need a modern hybrid alternative, and here we present the solution. Using VS Code for building FTC classes effortlessly.
Prerequisites
Visual Studio Code Extensions
Gradle
The building process involves using Gradle, although it is not necessary, it may provide helpful features while developing.
Extension Pack for Java
Helpful features for programming in Java using VSCode, most likely already installed if you have programmed programmed Java in VSCode before. A package which includes Maven, Project Manager, IntelliSense, Refactoring, and Debugging.
Live Share
Real-time collaboration inside VS Code, similar to Google Docs. Useful for programming in groups, especially when not in-person. The process for collaborating is simple, you simply send a link to your partner and they are invited to your code space.
Java
JDK
This is a good to have tool for Java development and will prevent headaches in development and following this tutorial.
JRE
Same as JDK, good to have for Java development, keep up to date.
Android
This is the most important area as it deals with the tools needed to push code to the robots. All steps to installation will be detailed.
The tools here are for building the code only. You do not need these if you are working with someone who can already push code to the RC and you are uncomfortable with setting these up. These tools are heavily command line based through the install process, although afterwards they don't need to be meddled with manually.
Android Command Line Tools
The first step to installing the necessary Android tools to push code to the robots is, ironically, going to the Android Studio download page. Here, scroll down to the area "Command Line Tools Only" and download the latest version for Windows.
Once this is completed downloading, create a folder for your Android SDK. This should be in a memorable location, such as C:\android_sdk
. In this folder, create another folder called cmdline-tools
. In this folder, create one more folder called latest
. Finally, drop in the contents of the downloaded zip folder into the latest
folder, as detailed below:
Once you are done, verify that your file structure looks like this. It is VITAL that the Commandline Tools be in cmdline-tools > latest
.
We have now installed Android SDK Commandline Tools, which will allow us to install the platform tools necessary for pushing code to the robot.
Android Platform Tools
The next step in preparing your workstation for FTC Android Development is the installation of Android Platform Tools, which will allow for the code to be pushed to and installed on the robot, either OTA (Over-The-Air) or using a cable.
First, ensure you are in the cmdline-tools > latest > bin
directory. This directory should have these files:
Here, we need to open up a terminal (preferably Windows PowerShell). On Windows 11, you can simply right-click inside the directory and click Open in Terminal
.
In the terminal window, enter the command .\sdkmanager --install "platform-tools"
. This will install the Android SDK Platform Tools.
If you are encountering problems with this command, try the following:
-
Make sure you are in the correct directory.
-
Make sure that your JRE and JDK are up-to-date
-
Try running the command without the
.\
at the beginning. This is not necessary for some terminals and may cause problems.
You have now successfully installed everything you need to develop with Android and VS Code.
Development
Now it's time to use these new tools to develop some code for your robot. If you are used to using OnBotJava, this process may be a little bit different to set up, but it is easy to get used to.
Cloning the FtcRobotController code
The first step to creating an FTC project is cloning the FTC SDK, found at github.com/FIRST-Tech-Challenge/FtcRobotController. You will need to git clone
this into your project folder. This is made a lot easier with the GitHub extension, as you just need to use Control + Shift + P
to open the Command Palette, and then use the Git:Clone command.
Then, click "Clone from Github" and search for FtcRobotController. Make sure to select the repository from FIRST-Tech-Challenge.
Choose to clone into the folder for your project, then make sure you select "Open" or "Open in New Window" so that the cloned repository opens up in a new workspace.
Setting up the repository for your own use
The first step to using the FTC SDK is to make sure you are working in your own GitHub repository instead of the one you just cloned from. To do this, we will again use VS Code's Git interface. Go to the Source Control tab on the sidebar.
Here, make sure that the Git interface is initialized, then use the three dots to navigate to the Remove Remote
option as shown below.
Here, remove the remote to the FtcRobotController github.
Then, navigate to the Add Remote
option:
Click Add remote from GitHub
and then choose your project's repository. This should be a fresh repository made specifically for this project.
Be sure to name the remote origin
just like the one we removed.
Finally, click the Publish Branch
button on the Source Control panel, and your repository should now be populated with the FTC SDK Code.
You have now initialized the repository on GitHub. Now, let's get to developing.
Developing using the FTC SDK
If you are used to developing with OnBotJava, you may not be familiar with the complete SDK file structure. However, your team's code should only be stored inside the TeamCode\src\main\java\org\firstinspires\ftc\teamcode
folder, shown in VS Code like so:
Here, there should be only a README file, but you can add your TeleOp and Auto here, as well as any Constants and Subsystem classes.
The sample code for Iterative OpMode and Linear OpMode, as well as any others, are contained in the FtcRobotController\src\main\java\org\firstinspires\ftc\robotcontroller\external\samples\ folder, shown in VS Code as:
Pushing Code to a Robot
Pushing code to the robot is quite similar to how it is done on OnBotJava or Android Studio. Before we do so, we need to make sure that we point to our Android SDK in our project. This is vital for the build process. To do this, simply add a file to the root of the project (where the LICENCE and various gradle files are stored) called local.properties
as shown here:
Inside this file, you must point to the location of your Android SDK. To do this, type sdk.dir=
into the new file, and then input the directory where you have stored your SDK using the following syntax:
Now, open the TERMINAL using the menu bar at the top.
This should open at the bottom of your window. Confirm that the command line is set to your project's directory.
Finally, type in the command .\gradlew.bat InstallRelease
into the Windows PowerShell terminal. You will see the build process showing like this:
If your code is free of errors, you should see the BUILD SUCCESSFUL message.
If your build fails, check the terminal for compiler errors.
If the command fails to run, make sure you have inputted it correctly, the terminal is in the correct directory, the robot is connected, and the APK has an up-to-date version of platform-tools and its path is in local.properties.
Pushing Code to GitHub
Pushing code to GitHub is very easy thanks to the built-in source control tab. After you make some changes to your project, you can stage your changed files and add a commit message outlining your changes.
Then, click the Commit
button, and then the Sync Changes
button which replaces it.
You have now Committed and Pushed your changes to your repo. More advanced features can be found using the three-dots menu in the source control tab.
Conclusion
Congrats! You've just learned how to start programming FTC in VS Code. This will allow you to have more control over your development process, use source control with GitHub, collaborate in real-time, and push code over the air, all while remaining in a lightweight code environment. Furthermore, using VS Code and its various tools will help you in FRC programming as well, as they both use VS Code with Gradle for building and Git/GitHub for source control.
Happy Coding!