Programming

From FRCpedia
Jump to navigation Jump to search

Coding. Love it or hate it, without code, the robot won't do anything. Sometimes even with code, the robot doesn't do anything. Hopefully, after reading this guide your code will at least do something. These guides will be assuming a Java-based robot because that is what is currently used by Team 2035. It's useful to at least consider other languages because many teams have success with C++, Labview, et cetera. This guide is broken down into several distinct areas of robot code for ease of reference.

Introduction

OK well programming, you may be asking yourself what exactly is it and does it really involve people mashing at keyboards all day. In order to understand programming, it is essential that you understand what a programming language is. According to formal definition, a programming language is a vocabulary and set of grammatical rules for instructing a computer or computing device to perform specific tasks. In this case, the device that we will be programming will be the robot, and we will do that by using a specific type of programming language. The key is using the right programming language for the right situation. This guide will be specifically geared towards java, however.


Here are some programming languages that are popularly used in FRC and are fully supported by FRC tutorials and programs:

1200px-ISO C++ Logo.svg.png


C++ is a widely used programming language that offers a lot more control over base process than other languages. It is harder to learn than most other languages due to its complicated syntax and difficult structure. However, it is once of the most resource efficient programming languages when learned well. FRC offers a lot of tutorials on how to code your robot in c++ if desired.


Java logo 640.jpg


Java is another widely used programming language. It is decently efficient and has pretty simple syntax for a programming language. As far as programming languages go, java is pretty much in the middle ground. It is not too difficult, but it is challenging enough to give users a variety of options to use in their programming endeavors. This is the language that we will cover and use during this wiki, but you are free to try the other programming languages if you think that they will be better.

Labview.jpeg



Lab-view is another language used for FRC programming. To be honest, lab-view was not really a language that we went into. Lab-view offers GUI coding templates and blocks for beginner programmers. It is probably the easier language to learn but does not offer the most options. If you want to learn more about lab-view, look it up here.

That's basically the end of this section. Hopefully now you have an idea of what programming language you want to use for your robot. Remember, we are going to cover java in this tutorial. If you want to use any other language then that then I recommend that you go to the actual FRC website and look at the help pages for that specific programming language. Onwards!

Class Breakdown

This guide is going to assume that you were able to successfully installed FRC VS Code(a specific FRC coding framework program design for visual studios) and have a general understanding of java. If you don't have either of these things, it is going to be difficult for you to follow along. The reason why we don't explain any of these things here is because it would take a huge amount of time to explain these things and there are plenty of tutorials out there on java or setting of FRC VS Code already. Check of this doc that we made if you need additional help: https://docs.google.com/document/d/1u8_LrAfO6XVPyn7fh2uA1ldmiwVRVrZO-xOsBdCmuu0/edit.

When you create a new java robot project(select command based as that's what we will be working in), the first thing that you will notice is that there are some classes automatically created for you and a huge list of directories and sub directories that are created for you.

The two main directories that you see right now are subsystems and commands. Outside those directories are RobotMap, Robot, OI, and main. We are going to go through each of these and explain how these work in the conception of a robot.


Robot: This is the main class that we will be working in. All of the subsystems, commands, and OI created will be declared in this file. The most important things to look for in the robot class are robot init and robot periodic. The robot init method is called once when the robot is turned on and basically initializes all of the values that need to be initialized once. Things like oi.initalize go into this file. The robot periodic method is where most of the methods go that need to be called repeatedly in order to check some value or do some function. Methods that constantly update the drivetrain according to the position of the joystick go into this method.

Subsystems: For those that don't know, a subsystem is classified as a smaller part of a working whole(the working whole being the robot and the smaller part being a specific part if the robot). A drive train is an example of a possible subsystem. So is an arm mechanism for picking up cube objects. The basic outline of a subsystem is simple, really. If you look in the example subsystem class, you will see a mostly empty class with a constructor. To create a subsystem, pretty much all you have to do is create local public variables before the constructor(these can be specific motors used in the subsystem or solenoids), construct them within the constructor, and create methods within the class that use those variables(such as a method to tell a victor speed controller to move a motor forward). Methods created in the subsystem class will be called in specific commands as desired.

Commands: This is the folder where you will create your command classes. For those that don't know what a command is, a command is basically a way of organizing a series of methods that will be triggered at the press of a button(joystick button). If you click on the example command class, you will notice that there are pre-created methods for you there. The most important methods to notice are the Start command method and the End command method. The start command method will be activated when the joystick button is pressed and the end method will be called after the start method putting an end to whatever you want to do in the start method. It is important to notice that this is regulated by the isFinished method which will call the end method if it returns true. Take some time to fully understand how these command methods work and interact with each other. It will save you a lot of frustration in the future.

OI: This stands for Object Interface. This is the class where you actually create the buttons that will call the commands that you looked at earlier. The key to this class is to keep yourself organized when it comes to creating the button objects, as they can get cluttered if you are not careful. It is also important to note that buttons cannot be created with the parameter value of 0. Button mapping always starts at 1 because joysticks don't have a button 0. Make sure to read the green readme text provided in the OI class before working with this class. It will give you useful tips for working with the class.

RobotMap: This is the file where you will organize all of your variables via static reference. The variables declared in the robot map class are static in order for other classes to access them using the reference RobotMap.variableName. The variables used in RobotMap are usually final ints(PWM ports), joysticks, joystick buttons, numeric values for subsystems, and whatever else you may need to put in an accessible static class.

Main: This is an interesting file that was added with the addition of FRC VS Code. You do not need to make any changes to this file but you have to make sure that the file is present otherwise the code won't compile.

Subsystems

Github and Collaboration

      Rockin Guide To Git 
           && Github

What is Github? Github is a development program that allows coders to collaborate on a single project easily. You can create a repository here (a storage base for all of the teams files) that can be accessed through Eclipse . This does not have to be for eclipse necessarily as other developers use it as well. But for us we will be using eclipse.

What is Git? Git is a software program that can be downloaded that helps you link your github repository to Eclipse.

Brief view on how it works:

A repository is created in Github through an account.

In Eclipse, there is a Git plugin that is GUI operated. This is good because this means that you don’t have to do the work of connecting people’s work through Git Bash(a Git command prompt).

Through this GUI you can link your github repository to Eclipse.

Through this GUI you can also create branches. A branch consists of a specific individual’s version of the main project.

You can push branches to the main branch.

You can merge branches created by different people into a single branch which is the branch you will use and put on the roborio.

You can push the finished branch to your github repository Steps on how to Set up a Github account:

Go to the Github website https://github.com/ Create a Github account Find the section titled Your Repositories and select New Repository. Put in a repository name and select create repository. Congratulations, your repository is created. Note, if you want more features your team should plan on creating an organization in github. This will make it easier for team members to collaborate.

Connecting your Github with Eclipse and creating Master Branch

Download Git here https://git-scm.com/downloads Open Eclipse Go to Windows---Show---View---Other---Git and select the views under it. Create a new java project In Eclipse, select Clone a Git Repository, copy and paste the url from your git repository and click finish Right click on your java project, go to team---share project and under repository, select your git repository and then select finish. Now go to Git staging and put the unstaged changes into the stage changes. Put a commit message and select Commit and Push When it asks you for a username and password, use your github account Now by your java project in brackets it should say projectname[master]. From this project you will be able to push the final project to Github by going to team---Push to Upstream. To grab data from the pushed branches use team---fetch to get data from remote tracking To merge branches into the master branch, go to team---merge and select the branch you want to merge with it. Note: if there is a conflict when merging, it will let you know. 14. Finally, commit your final project to github by going into the master branch and going to team---Push to Upstream.


How to create a Branch(Not master):

Create a new java project In Eclipse, select Clone a Git Repository, copy and paste the url from your git repository and click finish Right click on your java project, go to team---share project and under repository, select your git repository and then select finish.

To create a new branch, go to team---switch to---new branch and put in a branch name. After you created the branch it will automatically check out the branch. This means that you will be in the new branch not the older one.

From this branch, you can commit and push the branch’s version of the project to the master branch. Go to Team---Push and push this new branch. Now the branch should be created.


How to open a branch: Go to Team---Fetch from Upstream Go to File---Open Projects From Filesystem Open the needed branch. It should now appear in the Package Explorer.





Quick Tips:

Team---Push to Upstream - This uploads your project to your github repository.

Commit and Push -This will push the branch that you are in to a database that can be fetched through the master branch..

Team---switch to---new branch - This creates a new branch(another duplicate of the branch it was created from).

Team---merge - This merges two branches together. From this an overlapped project is created.

Team---fetch--- This grabs data from remote tracking. Use it to grab data from another branch that was committed and pushed.

Autonomous

Creating a full working autonomous system takes a lot of setup time and patience and trial and error. After working on an autonomous system for 2 years, we finally created a semi-working one.

Why is Autonomous Useful? In most of the FIRST robotics competitions, there is a period where the driver cannot manually control functions of the robot. During this time, the robot will need to be performing tasks on its own without the guidance of the driver. With simple autonomous systems, you can have the robot move to a certain spot that it needs to go during autonomous. With more complex systems and sensors, the robot can shoot goals on its own and have more complex positioning. All of this is essential for giving teams a head start in the beginning of a game.

How to Do Autonomous The first step is to gather all of the sensors that your robot will use in order to make Autonomous possible. This could involve ultrasonic sensors(or other distance sensors), encoders in various places, and cameras for vision processing. All of these will be used to help the robot navigate through the terrain in the game without the users control.

The next step involves creating structures in a

Vision Processing

Normally, robots can't see. Vision processing aims to fix that. A vision processing system is any system which enables the robot to process visual data. The term "vision processing" is a broad one, with a wide variety of possible approaches; it can be as simple as streaming a USB camera to the driver station, or as complicated as using machine learning to detect game pieces. By implementing a vision system, a team can have vastly improved autonomous function, and as shown by Destination: Deep Space, simple vision may be necessary for competing effectively.