1 of 28

C2.6 Programming Practices

2 of 28

Style

3 of 28

Style Guide

  • Tab size = 4 spaces
  • Use VScode’s Format Document command to format the document when possible
  • Put “//Spectrum 3847” at the top of every file
    • If the code is based on another team’s work put “//Based on team#### code” you can add a year if you want
  • Follow this style guide for coding conventions - https://medium.com/geekculture/java-coding-standards-68ba6c426794
    • A lot of our code doesn’t currently fit this guide, fixing files to meet it is good practice.

4 of 28

Package Names

“It should be an in lowercase ASCII letter such as java, lang. If the name contains multiple words, it should be separated by dots (.) such as java.util, java. lang.”

Examples:

package frc.robot;

package frc.robot.commands.ballpath;”

5 of 28

Class and Interface name:

“It should start with the uppercase letter. Use words, instead of acronyms. “

It should be a noun such Robot, Climber, ShuffleboardTab, etc. Use words, instead of acronyms

Examples:

class Robot

class Swerve

class Launcher extends SubsystemBase

6 of 28

Method name

“Usually, the method name should either be a verb or verb-noun combination starting with the lower letter. If it contains multiple words then every inner word should start with uppercase.”

Examples:

public void periodic()

public void setVelocity( double velocity)

public static Command getAutonomousCommand()

7 of 28

Variable name:

“i) Instance variable: Usually variable name should be a noun starting with a lowercase letter. If it contains multiple words then every inner word should start with uppercase.”

Examples: motor; speed; velocityRPM;

“ii) Constants (final variables):- Usually, a constant name should be a noun. It should contain the only uppercase If it contains multiple words then words are separated with the ( _ ) underscore symbol. Usually, we declare constants with public static and final modifiers.”

Examples:- CAN_CONFIG_TIMEOUT, MIN_BATTERY_VOLTAGE, PIGEON_ID

8 of 28

Variable name:

“iii) boolean variables:- It uses “is” as a prefix before the variable name. Negated boolean variable names must be avoided.”

Example:- boolean isAngleEnableCurrentLimit boolean isDriveMotorInvert

Don’t Use:- boolean isNotDriveMotorInvert

“iv) Private variable:- It uses underscore ( _ ) at end of the variable name.”

Example:- private double rpm_; private double kP_, kI_, kD_, kF_;

9 of 28

Importing Classes

“ Imported classes must always be listed explicitly. Importing classes explicitly gives an excellent documentation value for the class at hand and makes the class easier to comprehend and maintain.”

Example:- import frc.robot.commands.ResetGyro; import frc.robot.commands.swerve.TurnToAngle;

Don’t Use:- import frc.robot.commands.*;

10 of 28

Method/Class Modifiers

“Modifiers should be given in the following order:

<access> static abstract synchronized <unusual> final native

The <access> modifier (if present) must be the first modifier.”

Examples:- public static XboxGamepad driver

Don’t Use:- static public XboxGamepad operator

11 of 28

Git / Github

12 of 28

Git VScode Integration

  • Allows you to easily commit and sync changes.
  • Allows you to easily switch branches in repos

13 of 28

Github Pull Requests and Issues Extension

  • Extension Website
  • Lets you add issues to code and create/approve pull requests from inside VScode

14 of 28

Github Desktop App

  • https://desktop.github.com/
  • This is a very easy to use tool to commit and sync repository changes. It may be easier to use than the integration inside of VScode for some tasks.

15 of 28

Git Branches

  • Each programmer should create a branch of the code.
    • Merge the changes from main into your branch often.
    • You can also merge changes from other branches to work with code that other team members are working on.
  • When using team laptop make sure you switch to your branch
  • You are free to write code and test ideas in your branch of the repository.
  • Commit and Sync your branch often.
  • Write useful descriptions of each commit whenever possible.

16 of 28

Pull Requests

  • To get changes from your branch into main you need to create a pull request
  • Pull Request = a request for the lead programmer to pull your changes into “main” branch.
  • Don’t make a pull request to “main” unless it’s working in the simulator.

17 of 28

Github Tags/Releases

  • Before and after each event we should create a Tag/Release of our code on Github, this way we can easily download it and deploy to the robot if we need to.

  • It’s good practice to make other releases as well if we have big milestones or know big changes are coming

18 of 28

Git Tips

19 of 28

Folders

20 of 28

Top Level Folders

  • Lib = Files that are generic and we will reuse from year to year
  • Robot = files that are specific to each robot

21 of 28

Lib

  • Drivers = classes that allow certain hardware to work or wrappers for other WPIlib hardware classes
  • Gamepads = classes that allow us to better use gamepads such as button classes, filtering joystick access classes, etc.
  • Math = classes that help perform math
  • Sim = classes that help with simulation
  • Swerve = classes that help setup and run the swerve drive
  • Telemetry = classes that help with data sent from robot to driverstation, so shuffleboard, dashboard, etc
  • Util = other library classes.

22 of 28

robot

  • commands = holds all the commands for that act on subsystems
    • Subfolders for each subsystem or groups of subsystems
  • constants = holds classes for all our constant files
    • Each subsystem has a constants file
  • subsystems = holds all our subsystems for each mechanism on the robot
  • telemetry = holds all the classes for shuffleboard, smartdashboard, network tables, and logging

23 of 28

Classes

24 of 28

Subsystems

  • Should contain each action the subsystem/mechanism can perform
    • Such as an intake being able to control it’s motor,
    • Mechanism that may need to operated at different times can have subclass subsystems
      • Such as the intake’s pneumatic cylinder, it would need it’s own default command.
  • Should contain any sensors that are part of the subsystem/mechanism

25 of 28

Subsystem Creation

  • Create a new file Subsystem.java
  • Create the subsystem variables
    • motor(s)
    • solenoids(s)
    • sensors(s)
  • Create a constructor Subsystem()
    • Instantiate all of our subsystem variables
      • motor = new TalonFX(CanIDs.subsystem);
    • Default Command should be added for every subsystem

26 of 28

Subsystem - Default Commands

  • Default command should put subsystem in it’s safest state.
  • Motors should all be commanded to stop or controlled by joysticks.
  • Pneumatics may be commanded to a specific state if it makes sense
    • Such as the intake going up when no command is controlling it.

27 of 28

Constants

  • CanIDs and Hardware ports should go in the main constants file these don’t change often
  • Each subsystem should have it’s own constants file for all of their tunable settings.

28 of 28

Commands

  • For many subsystems a main Commands document that contains multiple static methods that return commands is likely the easiest way to create them.
    • Using RunCommand, StartandEndCommand and others are easier than full Command classes.