Software
Programming – part two of three to your ultimate robot. Read the tips and advice below to get off to a great start!
Quick Links:
Overall Process
Labview
C++
Java
Overall Tips for Any Language
It can be difficult at times to organize your programming process, especially if you have multiple programmers writing code at one time. How do you best organize your programmers? How should you organize your code? Where should you start?
Pseudo code. Before you even touch your computer, make a plan on paper of what you want to do. This goes for any programming language. This can start out as simple as listing the things you want the robot to execute. (example: “Move when the joystick moves”) Then you can get more detailed and start to list the steps it must do to complete this certain task. (1. Read value off joystick. 2. scale value. 3. send to motor). Continue to add detail before actually coding your program. (value = joystick -> get y axis; scaledValue = value times 12; etc.)
There is a proper way to pseudo code, and you can learn it if you want, but the more important part is just walking through what you want to happen and how you can get this to happen. After you know what you want to do, you just have syntax to worry about.
Keep your code organized. Get rid of old parts you don’t need and format your code so its easy to read (Netbeans has a feature where you can highlight your code, right click, and format it properly). When you start getting crazy with nested loops, having the code indent helps keep your code straight. Don’t be afraid to put in a return every once in a while. Walls of code can be off-setting and pressing enter an extra time after a section of code can make your code easier on the eyes and brain.
Come up with coding standards specific to your team. This means that all of your team’s coders follow a set of rules so that all of the code is consistent. For example, something like “every if statement must have a corresponding else statement”, “all statements must be commented”, or “variables must have meaningful names”. This will especially help you when your team has multiple programmers, making the code easy to understand and quicker to debug for people who may not have originally written it.
Repository. Your team should create a place to store your code, such as a central server that tracks different code revisions and histories. Some repositories have features that do ‘conflict resolution’ when multiple versions of the same file are checked in. They also serve as a place to keep code in case something happens to your local computer. There are free online ‘subversion repositories’ available, but rookie teams may even want to use google docs as an initial step.
For subversion, you can set up a free site on Unfuddle (which can be used for svn or git). Subversion is built into netbeans (meaning you can check things in and out through netbeans). However, if you want to check things in and out of the repository through Windows Explorer, you can download Tortoise SVN. Tortoise SVN is just a client, where the Unfuddle site would be the server where your files are actually stored. If you’re unfamiliar with subversion – go here.
Anyone can setup an svn, git, or cvs repository by installing a free server of that type on a computer that would act as your dedicated server. Then you can download corresponding client versions to add files to the repository on that server, or update them, or check them out. Then your repository would just be the address of that computer, assuming it’s networked.
Debugging. Always do basic debugging before you start messing with your code, especially when its the first time you run it. Making sure that everything is turned on and plugged in will fix a lot of problems. If your code is giving a really weird error, try restarting everything. This will fix a lot of problems, too. Also, if your having trouble downloading code, make sure you have the right IP address and that your Ethernet cable is plugged in to your cRIO correctly.
LabVIEW
Benefits:
- Graphical programming. LabVIEW allows better visualization of how the program is structured and how data moves through the program.
- Easier to learn. Experience shows that LabVIEW is easier to learn for those with no prior programming experience. If some of your team members have experience in either of the other languages, however, you may be ahead to use those.
- Implicit parallelism. Parts of the code that are not interdependent are run in parallel without any extra code or directives from the programmer.
- Extensive visual libraries. Existing libraries for image and signal processing and control systems are applications that LabVIEW was originally created for, so support for them is very good.
- Example autonomous code. For the last couple of years, FIRST has supplied example code for that year’s autonomous mode.
- Graphical, built-in debugging. Turn on debugging mode with the click of a button (no need to recompile and/or redeploy or run separate tools) which graphically shows how data is flowing through the program in real time. Probes display the values in the program.
- VCS Software. LabVIEW can be configured to work with certain VCS software, and offers a tool called LVMerge which can be used with some VCS software to merge LabVIEW files at the sub-file level.
Cons:
- Harder to find mentors. Laview is not as commonly used as C++ or Java, so mentors may have to learn the language as well.
- Skills are not transferable. The number of jobs available for LabVIEW programmers are much less that those for C++ or Java programmers, for those looking to directly bring skills learned in FRC into the workforce.
- Program code is not textual. LabVIEW needs to be installed to be able to edit a program. Also, version control utilities can only work on a while-file level, while text-based languages can be merged line by line.
- Program ordering is not implicitly deterministic. Implicit parallelism can also lead to some unanticipated behavior, but language features exist to establish sequential relationships.
The best set of online FIRST tutorials for LabVIEW are here. Watch these videos to learn more about FIRST-specific LabVIEW programming.
Also, view this PDF Document made by National Instruments (the company that makes LabVIEW and the cRIO) on FIRST-specific coding using the LabVIEW programming language.
Questions about LabVIEW? Ask us here.
C++
Benefits:
- Slightly faster than Java. The loop time for C++ programming is slightly higher than for Java, which, if used right, means the robots will have a slight advantage over robots programmed in Java.
- Real world applications. C++ is used in the programming work force for things like applications (such as Microsoft Windows) and video games, just to name a few. This means that programmers that learn C++ will be able to use C++ in their future education and career.
- WPI Library. WPI has created an immense FIRST library for C++, which means that many of the functions that rookie teams will use are easy to call and use in their code.
Cons:
- Harder to learn. C++ has a steep learning curve and can often take even experienced programmers time to get used to.
- C++ is operating system dependent. If you want to use C++ on different operating systems (like Mac and Windows), you’ll need to recompile your program for each platform you want to use before running it.
Watch this tutorial above made by FIRST Team #1503 on the basics of C++. To learn more in depth about C++ syntax and programming, click here to download a document also made by FIRST Team #1503.
Questions about C++? Ask us here.
Java
Benefits:
- Easier to learn. The time in which it takes to learn Java is considerably less than C++ programming. The learning curve is less steep when familiarizing yourself with the syntax.
- Often taught at schools. Most AP Computer Science courses and other programming courses taught at high schools are taught in Java, which means that students on a team can have supplementary education in the language that mentors do not have to teach.
- Useful for real-world programming situations. Java is used constantly in the programming work world, especially to code apps and other computer-based programs. This means that programmers that use Java will be learning a language that can prove useful to them later in their educations or careers.
- Very versatile. The WPI FIRST-specific Java library is immense and constantly growing. This means that not only are there pre-made functions that you can use for your code, but you can also make your own using similar techniques that were used for the WPI library. This means that Java is both simple while allowing complexity, if you choose.
- Easy to switch languages later. Java is very practical for students to learn in terms of it being similar to the languages that are used in real-world applications.
- Automatic Garbage. Java relieves programmers from having to deal with explicitly managing the memory in the code.
- Java runs independent of the operating system. This means that Java can run on multiple platforms (Mac, Windows, Linux, etc.). In other words, Java is cross-platform.
Cons:
- Slightly slower than C++.
- Less memory efficient than C++. Java does not allow programmers the flexibility to control their own “garbage”, which means that Java generally uses more memory that C++.
Read this tutorial below made by FIRST Team #3504 on the basics of Java. This is part of a three part series – click on the links for Part 2: Programming Your First FIRST Robot and Part 3: Syntax of Programming.
Also, this document explains how to introduce conceptual Java programming to people who have never seen programming before. This is an excellent exercise for teams to do for any language before the Build Season starts.
Getting started with FIRST Java. Use this website to become comfortable to FIRST Java, reading WPI’s descriptions of how to use this particular language successfully in FIRST.
Questions about Java? Ask us here.
