Tutorial: Run GRIP from a CPP, Java, or LabVIEW FRC program - PatrickPenguinTurtle/GRIPWiki GitHub Wiki
To use GRIP as part of an FRC robot project, it may be convenient to automatically run it as a subprocess of your FRCUserProgram. This can be done in a couple lines of language-dependent code. Then, you can use NetworkTables to retrieve values published from GRIP.
First, make sure you've deployed the project to the roboRIO, then add this code to your robot project.
Note: These examples assume you have a Publish Contours step in your pipeline that publishes areas to a subtable called "targets", as shown below. You can adjust the examples based on what you're actually publishing.
import java.io.IOException;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
public class MyRobot extends IterativeRobot {
private final NetworkTable grip = NetworkTable.getTable("grip");
@Override
public void robotInit() {
/* Run GRIP in a new process */
try {
new ProcessBuilder("/home/lvuser/grip").inheritIO().start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void autonomousPeriodic() {
/* Get published values from GRIP using NetworkTables */
for (double area : grip.getNumberArray("targets/area", new double[0])) {
System.out.println("Got contour with area=" + area);
}
}
}
If your robot program is written in C++, you still must have the JRE (Java Runtime Environment) installed on the roboRIO to run GRIP. If you haven't done this yet, see the ScreenSteps instructions.
#include <unistd.h>
#include <stdio.h>
#include "WPILib.h"
class MyRobot: public IterativeRobot {
public:
void RobotInit() override {
/* Run GRIP in a new process */
if (fork() == 0) {
system("/home/lvuser/grip &");
}
}
void AutonomousPeriodic() override {
auto grip = NetworkTable::GetTable("grip");
/* Get published values from GRIP using NetworkTables */
auto areas = grip->GetNumberArray("targets/area", llvm::ArrayRef<double>());
for (auto area : areas) {
std::cout << "Got contour with area=" << area << std::endl;
}
}
};
START_ROBOT_CLASS(MyRobot)
GRIP will publish its data to the network table hosted by the RoboRIO Network Table server under /GRIP/. On the GRIP GUI, find the name of the variable you want to read. Since the other tutorials use Publish ContoursReport, here's an example of how to use it.
To access the variables, use the "NT Read Numeric Array.vi" under WPI Robotics Library > Dashboard > Read Value.
The Read Value can be changed to Read Numeric Array once placed on the block diagram. In the previous GRIP Publish ContoursReport pane, the subtable name was left to the default name, "myContoursReport".
Using that subtable name, you can build a path string in LabVIEW. In order to build up the contours report as an array of contour report objects, you'll need to package it into a contour report cluster as shown below.