From a4f7282ecc251adf5fa9fa8fe038995e75a34b8e Mon Sep 17 00:00:00 2001 From: nathanrsxtn Date: Fri, 25 Mar 2022 09:03:50 -0600 Subject: [PATCH] Try to take ownership of CSV file with tuner --- .../frc4388/robot/commands/ShooterTuner.java | 30 +++++++++++++------ .../frc4388/robot/subsystems/BoomBoom.java | 1 - 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/frc4388/robot/commands/ShooterTuner.java b/src/main/java/frc4388/robot/commands/ShooterTuner.java index 3cc1bb4..d4b2887 100644 --- a/src/main/java/frc4388/robot/commands/ShooterTuner.java +++ b/src/main/java/frc4388/robot/commands/ShooterTuner.java @@ -1,25 +1,33 @@ package frc4388.robot.commands; import java.io.File; +import java.io.IOException; import java.io.OutputStream; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.FileOwnerAttributeView; +import java.nio.file.attribute.UserPrincipal; +import java.nio.file.attribute.UserPrincipalLookupService; import java.util.Arrays; -import java.util.Objects; import com.diffplug.common.base.Errors; import edu.wpi.first.util.sendable.Sendable; import edu.wpi.first.util.sendable.SendableBuilder; import edu.wpi.first.wpilibj.Filesystem; +import edu.wpi.first.wpilibj.RobotBase; import edu.wpi.first.wpilibj.shuffleboard.BuiltInLayouts; import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.wpilibj2.command.CommandBase; import edu.wpi.first.wpilibj2.command.InstantCommand; import edu.wpi.first.wpilibj2.command.Subsystem; +import frc4388.robot.Robot; import frc4388.robot.subsystems.BoomBoom; -import static frc4388.robot.subsystems.BoomBoom.ShooterTableEntry; +import frc4388.robot.subsystems.BoomBoom.ShooterTableEntry; public class ShooterTuner extends CommandBase { private final BoomBoom m_boomBoom; @@ -36,11 +44,18 @@ public class ShooterTuner extends CommandBase { m_shotEditor = new ShotEditor(); m_shotCsvAppender = new PersistentInstantCommand(this::appendCsv).withName("Append"); m_shooterTableView = new ShooterTableEditor(); - // m_shooterTableUpdater = new PersistentInstantCommand(m_boomBoom::loadShooterTable).withName("Reload"); + // m_shooterTableUpdater = new + // PersistentInstantCommand(m_boomBoom::loadShooterTable).withName("Reload"); } private OutputStream getCsvOutputStream() { - return Objects.requireNonNullElseGet(csvOutputStream, () -> Errors.rethrow().get(() -> Files.newOutputStream(new File(Filesystem.getDeployDirectory(), "ShooterData.csv").toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND))); + if (csvOutputStream == null) { + Path path = new File(Filesystem.getDeployDirectory(), "ShooterData.csv").toPath(); + if (RobotBase.isReal()) + Errors.log().run(() -> Files.getFileAttributeView(path, FileOwnerAttributeView.class).setOwner(FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName("admin"))); + csvOutputStream = Errors.rethrow().get(() -> Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.APPEND)); + } + return csvOutputStream; } private class ShotEditor implements Sendable { @@ -76,11 +91,7 @@ public class ShooterTuner extends CommandBase { } private void appendCsv() { - String s = String.format("%s,%s,%s%n", - m_boomBoom.m_shooterTable[0].distance, - m_boomBoom.m_shooterTable[0].hoodExt, - m_boomBoom.m_shooterTable[0].drumVelocity - ); + String s = String.format("%s,%s,%s%n", m_boomBoom.m_shooterTable[0].distance, m_boomBoom.m_shooterTable[0].hoodExt, m_boomBoom.m_shooterTable[0].drumVelocity); byte[] b = s.getBytes(); Errors.log().run(() -> getCsvOutputStream().write(b)); } @@ -113,6 +124,7 @@ public class ShooterTuner extends CommandBase { Errors.log().run(getCsvOutputStream()::close); m_boomBoom.loadShooterTable(); } + @Override public boolean runsWhenDisabled() { return true; diff --git a/src/main/java/frc4388/robot/subsystems/BoomBoom.java b/src/main/java/frc4388/robot/subsystems/BoomBoom.java index 0e81dfc..e6f1f75 100644 --- a/src/main/java/frc4388/robot/subsystems/BoomBoom.java +++ b/src/main/java/frc4388/robot/subsystems/BoomBoom.java @@ -141,7 +141,6 @@ public class BoomBoom extends SubsystemBase { } public void loadShooterTable() { - try { // This is a helper class that allows us to read a CSV file into a Java array. CSV csv = new CSV<>(ShooterTableEntry::new) {