Pathfinder - including trajectory CSV files into jar

Now that WPILib bundles anything in the src directory into the jar file, I found that you could include Trajectory CSV files into the jar. This means that any CSV you generate (for example with vannaka’s Motion Profile Generator could be included in your jar file and automatically downloaded to the robot by the eclipse plugins. This also helps with version control, because the same file you commit is guaranteed to be on the robot.

Just put the csv files in the same src folder as this example class, and pass it the name of the trajectory to load. For example if you have paths named “crossauto_left_detailed.csv” and “crossauto_right_detailed.csv” use nameOfPath as crossauto.

The trick was that Pathfinder is calling C code, so it couldn’t directly use the file from the jar, so instead the file is copied out of the jar onto the filesystem so Pathfinder can see it.

	public PathfinderDrive(String nameOfPath) {
		
		File leftFile = new File("/home/lvuser/" + nameOfPath) + "_left_detailed.csv");
		
		File rightFile = new File("/home/lvuser/" + nameOfPath) + "_right_detailed.csv");

		try {
		Files.copy(getClass().getResource(nameOfPath) + "_left_detailed.csv").openStream(), leftFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		Files.copy(getClass().getResource(nameOfPath) + "_right_detailed.csv").openStream(), rightFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		leftTraj = Pathfinder.readFromCSV(leftFile);
		rightTraj = Pathfinder.readFromCSV(rightFile);
...

We went with a similar idea this year…

The original source for our paths are several classes that contain profile settings (max accel, etc) and the waypoints. We have a utility class that generates custom CSV files that include the settings, waypoints, and the path. During build we have a unit test that checks each CSV to make sure it’s settings and waypoints match the ones in code, and abort the build if not.

As a last ditch effort, when we run the auto we run that same comparison and will recreate the path at runtime if there’s a mismatch.

This allows there to be one, authoritative, location that defines the paths, so we aren’t trying to version CSV files and figure out what changed from version to version, or worse, lose track of what is supposed to be, and is, running on the bot.