Skip to content

Commit

Permalink
Merge Drive Motor feed-forward from Dev
Browse files Browse the repository at this point in the history
Finished XML config storage for our custom interpolation class
  • Loading branch information
catr1xLiu authored Jul 11, 2024
2 parents ac6fde5 + 300f89b commit fb1779a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<InterpolatedMotorFeedForward>
<info>
<interpolatedVariablesCount type="int">1</interpolatedVariablesCount>
<length type="int">2</length>
</info>

<variable0>
<name type="string">motorVelocity</name>
<sample0 type="double">0</sample0>
<sample1 type="double">4.8</sample1>
</variable0>

<variable1>
<name type="string">motorPower</name>
<sample0 type="double">0</sample0>
<sample1 type="double">1</sample1>
</variable1>
</InterpolatedMotorFeedForward>
1 change: 0 additions & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package frc.robot;

import edu.wpi.first.units.Unit;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import frc.robot.subsystems.MapleSubsystem;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.pathplanner.lib.auto.AutoBuilder;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.units.Unit;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
Expand Down Expand Up @@ -55,7 +54,7 @@ public RobotContainer(String chassisName) {
switch (Robot.CURRENT_ROBOT_MODE) {
case REAL -> {
// Real robot, instantiate hardware IO implementations
// drive = new Drive(
// drive = new SwerveDrive(
// new GyroIOPigeon2(),
// new ModuleIOSparkMax(0),
// new ModuleIOSparkMax(1),
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/frc/robot/subsystems/drive/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public SwerveModule(ModuleIO io, String name) {
this.io = io;
this.name = name;

driveOpenLoop = new InterpolatedMotorFeedForward(
"DriveWheelOpenLoop",
new double[] {0, 1},
new double[] {0, 5.2}
);
driveOpenLoop = InterpolatedMotorFeedForward.fromDeployedDirectory("DrivingMotorOpenLoop");
turnCloseLoop = new MapleSimplePIDController(Constants.SwerveModuleConfigs.steerHeadingCloseLoopConfig, 0);

CommandScheduler.getInstance().unregisterSubsystem(this);
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/frc/robot/tests/InterpolationTableTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package frc.robot.tests;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.utils.Config.MapleConfigFile;
import frc.robot.utils.Config.MapleInterpolationTable;

import java.io.IOException;

public class InterpolationTableTest implements UnitTest {
private final MapleInterpolationTable testTable;
public InterpolationTableTest() {
testTable = new MapleInterpolationTable(
"testTable",
new MapleInterpolationTable.Variable("x", 1, 2, 3, 4),
new MapleInterpolationTable.Variable("y", 2, 3, 4, 5)
);
try {
testTable = MapleInterpolationTable.fromConfigFile(MapleConfigFile.fromDeployedConfig("InterpolatedMotorFeedForward", "DrivingMotorOpenLoop"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void testStart() {
SmartDashboard.putNumber("InterpolationTables/testValueX", 1.0);
SmartDashboard.putNumber("InterpolationTables/testValueVelocity", 1.0);
}

@Override
public void testPeriodic() {
final double x = SmartDashboard.getNumber("InterpolationTables/testValueX", 1.0);
assert testTable != null;
final double vel = SmartDashboard.getNumber("InterpolationTables/testValueVelocity", 1.0);
SmartDashboard.putNumber(
"InterpolationTables/testOutPutY",
testTable.interpolateVariable("y", x)
"InterpolationTables/testOutPutPower",
testTable.interpolateVariable("motorPower", vel)
);
}
}
4 changes: 2 additions & 2 deletions src/main/java/frc/robot/utils/Config/MapleConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.w3c.dom.NodeList;

public class MapleConfigFile {
private final String configType;
private final String configName;
public final String configType;
public final String configName;

public static final class ConfigBlock {
private final String blockName;
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/frc/robot/utils/Config/MapleInterpolationTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MapleInterpolationTable {
private final String tableName;
public final String tableName;
private final Variable independentVariable;
private final Map<String, Variable> interpolatedVariables;

Expand All @@ -27,21 +26,35 @@ public Variable(String name, double... values) {
}

public void initializeTuningPanelOnDashboard() {
// if (Objects.equals(tableName, "Unknown"))
// return;
// SmartDashboard.putNumberArray("InterpolationTables/" + tableName + "/" + variableName, values);
if (Objects.equals(tableName, "Unknown"))
return;
SmartDashboard.putNumberArray("InterpolationTables/" + tableName + "/" + variableName, values);
}

public void updateValuesFromDashboard() {
if (Objects.equals(tableName, "Unknown"))
return;

final double[] updatedValues = SmartDashboard.getNumberArray("InterpolationTables/" + tableName + "/" + variableName, values);
final double[] updatedValues = SmartDashboard.getNumberArray("InterpolationTables/" + tableName + "/" + variableName, new double[]{});
if (updatedValues.length != values.length)
return;

System.arraycopy(updatedValues, 0, values, 0, values.length);
}

public void toBlock(MapleConfigFile.ConfigBlock block) {
block.putStringConfig("name", variableName);
for (int i = 0; i < values.length; i++)
block.putDoubleConfig("sample"+i, values[i]);
}

public static Variable fromBlock(MapleConfigFile.ConfigBlock block, int length) {
final String name = block.getStringConfig("name");
final double[] values = new double[length];
for (int i = 0; i < length; i++)
values[i] = block.getDoubleConfig("sample"+i);

return new Variable(name, values);
}
}
public MapleInterpolationTable(String name, Variable independentVariable, Variable... interpolatedVariables) {
this.tableName = name;
Expand Down Expand Up @@ -92,12 +105,27 @@ public double interpolateVariable(String interpolatedVariableName, double indepe

public MapleConfigFile toConfigFile(String configType) {
final MapleConfigFile configFile = new MapleConfigFile(configType, tableName);
// TODO write this part

final MapleConfigFile.ConfigBlock infoBlock = configFile.getBlock("info");
infoBlock.putIntConfig("interpolatedVariablesCount", interpolatedVariables.size());
infoBlock.putIntConfig("length", independentVariable.values.length);
independentVariable.toBlock(configFile.getBlock("variable0"));
int i = 1;
for (Variable interpolatedVariable:interpolatedVariables.values())
interpolatedVariable.toBlock(configFile.getBlock("variable" + (i++)));

return configFile;
}

public static MapleInterpolationTable fromDeployDirectory(String configType, String name) throws IOException {
final MapleConfigFile configFile = MapleConfigFile.fromDeployedConfig(configType, name);
return new MapleInterpolationTable(name, null); // TODO: write this part
public static MapleInterpolationTable fromConfigFile(MapleConfigFile configFile) {
final MapleConfigFile.ConfigBlock infoBlock = configFile.getBlock("info");
final int interpolatedVariablesCount = infoBlock.getIntConfig("interpolatedVariablesCount"),
length = infoBlock.getIntConfig("length");

final Variable independentVariable = Variable.fromBlock(configFile.getBlock("variable0"), length);
final Variable[] interpolateVariables = new Variable[interpolatedVariablesCount];
for (int i = 0; i < interpolatedVariablesCount; i++)
interpolateVariables[i] = Variable.fromBlock(configFile.getBlock("variable"+(i+1)), length);
return new MapleInterpolationTable(configFile.configName, independentVariable, interpolateVariables); // TODO: write this part
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import frc.robot.utils.Config.MapleConfigFile;
import frc.robot.utils.Config.MapleInterpolationTable;
import org.littletonrobotics.junction.Logger;

import java.io.IOException;

public class InterpolatedMotorFeedForward extends SimpleMotorFeedforward {
private final String name;
private final MapleInterpolationTable interpolationTable;
public InterpolatedMotorFeedForward(String name, double[] motorPower, double[] velocityMeasured) {
super(0, 0);
this.name = name;
this.interpolationTable = new MapleInterpolationTable(
this(new MapleInterpolationTable(
name,
new MapleInterpolationTable.Variable("motorVelocity", velocityMeasured),
new MapleInterpolationTable.Variable("motorPower", motorPower)
);
));
}

protected InterpolatedMotorFeedForward(MapleInterpolationTable interpolationTable) {
super(0, 0);
this.name = interpolationTable.tableName;
this.interpolationTable = interpolationTable;
}

@Override
Expand All @@ -33,4 +38,17 @@ public void saveFeedForwardConfigToUSB() {
final MapleConfigFile configFile = interpolationTable.toConfigFile("InterpolatedMotorFeedForward");
configFile.saveConfigToUSBSafe();
}

public static InterpolatedMotorFeedForward fromDeployedDirectory(String name) {
final MapleInterpolationTable interpolationTable;
try {
interpolationTable = MapleInterpolationTable.fromConfigFile(MapleConfigFile.fromDeployedConfig(
"InterpolatedMotorFeedForward", name
));
} catch (IOException e) {
throw new RuntimeException(e);
}

return new InterpolatedMotorFeedForward(interpolationTable);
}
}

0 comments on commit fb1779a

Please sign in to comment.