Lost Program Source

  • Post author:
  • Post category:FTC

At the Port Moody FTC scrimmage we had one team lose the source code to an OnBot Java program. The program was still listed in the Driver Station, but the Java source file only had a couple of blank lines.

Summary

This might have been an accidental cut and paste error trying to copy the program. The problem is if you cut the text instead of copy, and then go to a new tab to paste the text, OnBot Java will save the file in the old tab. The text is gone from the old program when you go back.

OnBot Java automatically creates backups. We probably could have recovered the program, except the backups are hidden, and we didn’t know they existed. 

Neither Blocks nor OnBot Java keep versions that you can easily go back to. A more likely problem is you make a big change that doesn’t end up working. There’s no undo at that point.

At an event, if you have to change code, you should copy the source code and then make the change. Make use of the OnBot Java File Copy method of making a copy.

Manually backing up your source code (Java or Blocks) is recommended insurance against losing code for any reason. A backup you create also lets you go back to a version you know works.

I’m not going to discuss Blocks programs, other than to say you can make copies to change and backup and restore Blocks programs (but there is no automatic backup like OnBot Java).

In this Article

Possible Cause

We don’t know what happened. There isn’t anything quite like this in the FTC forum posts. We should have taken a look at the robot controller log file to see if there were any messages. However, if this was a problem with OnBot Java there should be more reports of problems.

One possibility is an unintentional cut and paste of the source code instead of a copy and paste. Or perhaps an inadvertent selection of the text and replacing it with a space or tab. 

This is a Reddit post from three years ago that mentions missing code, but not how it happened.

My team uses OnBotJava for TeleOp. Whenever you try to edit the file it looks like the code deleted itself somehow… but the robot is still running the code so it must be somewhere in the control hub. Any ideas how to get it out so that it can be edited again?

There was mention on how to recover the file if you had NOT clicked the build everything icon, but that did not work for me. I did NOT try the reverse compiler option mentioned.

Prevention

Be cautious when copying, cutting, pasting and deleting things.  I think a more likely problem that anyone can encounter is that you make a big change to a program but it doesn’t work. Then what do you do?

In OnBot Java the source code is stored on the robot controller. You use a browser to create and edit the program. The program is NOT stored in the browser or on the browsing device. The program is only on the robot controller.

If you do notice your Java code is blank, if you immediately do a Ctrl Z – Undo (or click the Undo Icon) the text should reappear. But if you go to another program in OnBot java and come back the undo won’t work. If it was due to a cut paste error, and you haven’t copied anything else yet, you could paste the text back into the old program.

If you’re changing anything significant you should make a file copy your old program and then make the change. That way if it doesn’t work you’ll still have the old code handy.

This would be very important when changing code at an event. The last thing you want to do is break something and not have a working program in time for the next match. However, be sure the drive team knows which program to run.

Control Hubs are subject to both mechanical and electrical stress. They can be damaged and stop working. If you have your source code backed up, you can replace the hub and restore your programs easily.

Your best insurance is to make copies of your programs and to also backup your programs. 

Make Program File Copies

OnBot Java allows you to copy an existing java file to make a new program. You make a copy of the original source and give it a new name. In OnBot Java you should do this by using the File copy method, not a copy and paste of the text into a blank opMode.

Right click on the file in the Project Files list and select Copy.

OnBot File Copy

Right click anywhere in the Project Files list and select Paste to create a copy.

OnBot File Paste

OnBot will warn you it is creating a new file and displays the new file name.

OnBot new file pop up dialogue showing the new file name and OK and Cancel buttons.
New file warning, click the OK button to continue

There will be a new file in your list of programs.

The copied file appears in the Project Files list.
New File Nullop_copy2.java

Note: in this example I already had copied the Nullop.java file previously, so OnBot created a Nullop_copy2.java file.

One naming convention might be to consider the copy to be the backup. Each time you copy the original it will add a number to the copy. The file names look like MyTeleOp_copy, MyTeleOp_copy2, etc. In this case, MyTeleOp is the current version of the program that you would change and the highest numbered copy is the most recent backup. This assumes you download to the the same folder every time.

If you want to edit the copy, you might want to rename the copy and add a number. E.g. MyTeleOp, MyTeleOp1, MyTeleOp2, etc. Then the highest number is the current version and that is the file you change.

Use whatever naming convention works for you.

Warning: be careful when you start to edit the program. If you have the old and new programs both open in OnBot Java it’s easy to accidentally started modifying the wrong one. Ensure you only have one copy of the program open and that it’s the one you want to change.

Note: these program copies are not true backups because they are still stored only on the robot controller. However, they are the easiest way to not lose code and also allow you to go back if a change doesn’t work.

You might also need to update the name attribute of the @TeleOp or @Auto annotation in the Java source. Name changes what is shown in the driver station list of programs, two programs should not have the same name. The Java sample programs all have name/group attributes.

@TeleOp(name="Basic: Linear OpMode", group="Linear OpMode")

The name and group attributes are optional. I sometimes delete these when I copy the sample programs and just use @TeleOp. However, name/group might be useful if you have a lot of programs. You might want your commonly used programs in a group that is listed first on the driver station.

Disable the Old Programs

If you start making program copies, you’ll end up with a long list of programs in the driver station. For both OnBot Java and Blocks you can mark programs you don’t need as Disabled. That prevents them from showing up in the Driver Station’s list of programs. Handy if you end up with a program with six versions. In Java you can see this in the sample programs where they add the @Disabled annotation. The program will still be compiled when you build, it just won’t be visible in the driver station.

@TeleOp(name="Basic: Linear OpMode", group="Linear OpMode")
@Disabled
public class BasicOpMode_Linear extends LinearOpMode {

Backup Programs

You should also backup your programs to another device. It’s possible to lose or modify programs by mistake and not have the original to go back to. Also Control hubs can be damaged and need to be replaced, that’s rare, but has happened. 

Ideally, anytime after you’ve tested your programs you should backup the source code somewhere safe. That way you know that version of the program was working as expected.

There are a few different ways to backup your programs.

Automatic OnBot Java Backups

A “new” feature for OnBot Java is that it now automatically creates a backup of the OnBot java source code every time you build. Possibly useful if you know where they are, but you can’t access them from OnBot Java. The nice part is you don’t have to do anything special, they are just there.

The OnBot Java source backups are in the folder: FIRST/java/srcBackups on the robot controller.

I think this was first mentioned in an FTC Tech Tip email Week of 01/08/2024 “OnBot Java Backups”. The tip is recorded here:

https://ftc-docs.firstinspires.org/en/latest/tech_tips/tech-tips.html?highlight=backups

This week’s Tech Tip of the Week is for all those who program in OnBot Java.

Have you ever been worried that your OnBot Java programs could suddenly magically vanish? Has it ever happened to you? 

One lesser-known feature of OnBot Java is automatic backups – each time you “compile all” in OnBot Java the system saves a copy of all source code, up to 30 compilations deep. In order to find these backups, you must connect to the Control Hub via USB from a Windows machine and navigate to the “FIRST” folder on the device’s internal storage. In this folder you’ll find a “java” folder, and within that is the “srcBackups” folder. Here you’ll find zip files containing each backup with a time/date stamp. Happy Programming!

An issue with these automated copies is that they are created every time you build. It may be hard to locate a particular version. About 30 or so backups are kept, older ones are deleted. Depending on how often you build the OnBot Java programs you might not be able to go back very far. Though likely you only want something recent, like from the last programming session.

This would have been a way to recover from the blank java file that happened at the Port Moody event.

Recover OnBot Java Automatic Backups

If you connect a laptop to your control hub with a USB cable in Windows, it will likely open this window:

Window that appears when you connect a control hub with a USB cable.

Open the Internal shared storage folder and you’ll see the First folder:

List of folders including the FIRST folder.

Open the FIRST folder, then the Java folder and then the srcBackups folder and you should see something like:

A list of backup Zip files in the srcBackups folder.

You can see in the file names there’s a sequential number plus a date and timestamp.

You can copy those files to your laptop. 

If you unzip one of the backup files you’ll see a couple of folders. Open the org, firstinspires, ftc, and teamcode folders and you’ll finally see a list of your OnBot Java programs.

List of Java files in the teamcode folder of the backup zip file.

You can then copy that program somewhere and then upload it to OnBot Java to recreate the program. Check first if you already have a program with that file name. You may need to adjust the file name and class name prior to uploading.

Manually Backup A Program

In OnBot Java you can also download the Java source file from the list of programs in the Project Files section. Select the program and right click to bring up the file menu and select Download.

Select a file and then open the file menu to select Download.

The advantage of this over the automated backups is you can save them whenever you want. E.g. just prior to a competition. But you have to remember to actually do the backup and find a place to save the files.

You also have to manage multiple copies of the program. You can end up with file names like MyProgram.java then MyProgram (1).java, MyProgram (2).java if you save into the same folder. They are usually placed in whatever Downloads folder your browser uses. 

You might wish to rename them to something useful like MyProgram-Nov10-scrimmage.java. Try to indicate where or why the change was made in the file name. You might want to move them into another folder. You could create a Backups folder, with sub-folders by date or event and copy all your programs into those folders. This provides useful organization to your backups.

REV Hardware Client Backups

The REV Hardware Client can also back up your entire control hub. This have to be done manually. These backups include Blocks source, OnBot Java source, and the robot configuration files. Note: this is Windows only software. This is the same software that can be used to update the firmware and FTC software on the Control Hub and Driver Hub.

With a Control Hub connected via a USB cable, you can go to the Backup and Restore tab.

REV Hardware Client Backup and Restore screen.

If you create a backup it will indicate what the backup file name is. That is also a link which if you click it will take you to the folder where all the backups are saved on your computer. Note that these are stored in the AppData folder. That’s normally hidden and the easiest way to access them is clicking the saved as link in the REV hardware client backups window.

REV Hardware Client showing backed up zip files.

Note how the Robot Controller name 9999-RC is part of the folder name and zip file names. Inside the zip will be the robot configuration files, as well as the Blocks and OnBot Java source files. 

I suspect The REV Hardware Client will keep as many copies as you’re willing to make. You might want to make notes somewhere about why each backup was made with the date and time.  Notes will come in handy if you try to restore an older version. Though it’s likely you’ll only use the most recent.

Restore Program

Regardless of what backup method you use, the result is that a file was saved somewhere.

Note: if you use the Rev Hardware client, and have a failed control hub, you can restore everything with the Rev Backup and Restore screen. This is more useful if you have to replace your control hub.

Assuming you only want to restore one program then you need to locate the file you want to restore. You may need to unzip the backup first. Likely you’ll want to copy that program somewhere so you can work on it.

If the file was renamed as part of the downloading process you probably should copy the file somewhere and rename as needed.

In OnBot Java click the Upload Files icon (the one with the upward pointing arrow).

OnBot Java Upload Files icon

That will bring up a select file pop up window. It will probably show the Downloads folder of your browser. Change folders if needed and select the file to upload.

If there is already a file with that filename the upload will fail with a message saying there is already a file with that name. You can select and delete the file in the Project Files folder if that is what you want and then redo the upload. Or rename the file on your computer (and the Java class) and then redo the upload.