How to write data from SmartDashboard to a file?

Hello There,
I am questioning on why I am getting this error. I am trying to get the data off of the Shuffleboard, and write it to a file. Here is what I have so far.

@Overide

public void execute() {

try {

  writer = new FileWriter("data.txt");

  int len = arr.length;

  int getCurrentIndex = (int) arr[0];



  while (running) {

    for(int i = getCurrentIndex; i < len; i++) {

      getCurrentIndex = (int) arr[i];

      writer.write(arr[i] + "\n" + "");

    }

    writer.close();

  }

} catch(Exception e) {

  e.printStackTrace();

}

}

The getCurrentIndex variable is there so that the next time it writes, it will continue from the previous index. Unsure though if this is necessary. If their is another way to do this, please let ne know. Oh!, and here is the error:

java.lang.NullPointerException
at frc.robot.commands.GetDriveInput.execute(GetDriveInput.java:42)
at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:281)
at frc.robot.Robot.robotPeriodic(Robot.java:56)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:270)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:117)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:335)
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:387)
at java.base/java.lang.Thread.run(Thread.java:834)
java.lang.NullPointerException
at frc.robot.commands.GetDriveInput.execute(GetDriveInput.java:42)
at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:281)
at frc.robot.Robot.robotPeriodic(Robot.java:56)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:270)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:117)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:335)
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:387)
at java.base/java.lang.Thread.run(Thread.java:834)

Which line is line 42?

1 Like

int len = arr.length;

I don’t think this is the issue though.

you close writer inside the while loop, but then still try to write to it.

that’s sort of what I’m going for, but I’m unsure if that is the correct approach

If you close the file, you can no longer write to it. You could either only close the file after the loop is complete, or re open the file before writing to it every time. What exactly are you going for? There may be another option.

If that is line 42 then arr is not initialized. Thus the NullPointerException

Ok, the idea is to write to a file from the Shuffleboard. I want to get the input data from a controller and put that line by line onto a file. My idea is to manually record the data from telop mode and play back the inputs on Autonomous. This is for the Galactic Search portion of the @Home Challenge. If there is a way, please let me know. The code that I wrote was me just thinking, I figured it would not work.

Not to discourage you, but unless your robot doesn’t have encoders and a gyro, you’re probably better off investing in trajectory following. It’s more optimized than most anything you can prerecord, and has a ton of support. Of course I don’t know if that’s actually viable for you.

ok, well that was just an idea I had. And yes, we will be using encoders and possibly a gyro.

If you have the feedback use the feedback. Controller input playback is the fancy version of timed-based driving. If you bump a power cell and roll slightly off course, you’re unable to correct.

2 Likes

Ok, did not know that. Thank You! for that insight, I think I’ll take that advice and try to implement the PID controller.

You’ll want this.

1 Like