forked from strongback/strongback-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request strongback#25 from Team3132/interfaceNetworkTable
Create a way to tune PID values on all motors using network tables.
- Loading branch information
Showing
9 changed files
with
154 additions
and
94 deletions.
There are no files selected for viewing
36 changes: 29 additions & 7 deletions
36
src/main/java/frc/robot/interfaces/NetworkTableHelperInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,36 @@ | ||
/*----------------------------------------------------------------------------*/ | ||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */ | ||
/* Open Source Software - may be modified and shared by FRC teams. The code */ | ||
/* must be accompanied by the FIRST BSD license file in the root directory of */ | ||
/* the project. */ | ||
/*----------------------------------------------------------------------------*/ | ||
|
||
package frc.robot.interfaces; | ||
|
||
|
||
public interface NetworkTableHelperInterface extends DashboardUpdater { | ||
/** | ||
* Gets a named double value from the network tables. If not found | ||
* the defaultValue is returned. | ||
* @param key the name to look up in the table | ||
* @param defaultValue will be returned if key not found. | ||
* @return the value in the table if found, defaultValue otherwise. | ||
*/ | ||
public double get(String key, double defaultValue); | ||
/** | ||
* Gets a named Boolean value from the network tables. If not found | ||
* the defaultValue is returned. | ||
* @param key the name to look up in the table | ||
* @param defaultValue will be returned if key not found. | ||
* @return the value in the table if found, defaultValue otherwise. | ||
*/ | ||
public boolean get(String key, boolean defaultValue); | ||
/** | ||
* Gets a named String value from the network tables. If not found | ||
* the defaultValue is returned. | ||
* @param key the name to look up in the table | ||
* @param defaultValue will be returned if key not found. | ||
* @return the value in the table if found, defaultValue otherwise. | ||
*/ | ||
public String get(String key, String defaultValue); | ||
|
||
/** | ||
* Sets a named double value from the network tables. | ||
* @param key the name to look up in the table. | ||
* @param value the value set to the network table. | ||
*/ | ||
public void set(String key, double value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package frc.robot.lib; | ||
|
||
import org.strongback.Executable; | ||
import org.strongback.Executor.Priority; | ||
import org.strongback.Strongback; | ||
import org.strongback.components.Motor; | ||
|
||
import frc.robot.Constants; | ||
import frc.robot.interfaces.NetworkTableHelperInterface; | ||
|
||
/** | ||
* Whenever a motor is created, networkTable for the motor PIDF values are implemented from here. | ||
* The PIDF values are initiliased on the network table and and the motors are updated from the PIDF | ||
* values in the network tables once per second. This allows the values to be updated on the fly by changing | ||
* the values in the network tables. | ||
*/ | ||
|
||
class TunableMotor implements Executable { | ||
private final Motor motor; | ||
private final NetworkTableHelperInterface networkTable; | ||
private double lastUpdateSec = 0; | ||
|
||
public static void tuneMotor(Motor motor, int id, double p, double i , double d , double f, NetworkTablesHelper networkTable) { | ||
var tunable = new TunableMotor(motor, id, p, i, d, f, networkTable); | ||
networkTable.set("p", p); | ||
networkTable.set("i", i); | ||
networkTable.set("d", d); | ||
networkTable.set("f", f); | ||
Strongback.executor().register(tunable, Priority.LOW); | ||
} | ||
|
||
public TunableMotor(Motor motor, int id, double p, double i, double d, double f, NetworkTablesHelper networkTable) { | ||
this.motor = motor; | ||
this.networkTable = networkTable; | ||
} | ||
|
||
// Executes the command 1 time every 1 second. | ||
@Override | ||
public void execute(long timeInMillis) { | ||
double now = Strongback.timeSystem().currentTime(); | ||
if (now < lastUpdateSec + Constants.DASHBOARD_UPDATE_INTERVAL_SEC) | ||
return; | ||
update(); | ||
lastUpdateSec = now; | ||
} | ||
|
||
private void update() { | ||
double p = networkTable.get("p", Constants.DRIVE_P); | ||
double i = networkTable.get("i", Constants.DRIVE_I); | ||
double d = networkTable.get("d", Constants.DRIVE_D); | ||
double f = networkTable.get("f", Constants.DRIVE_F); | ||
motor.setPIDF(0, p, i, d, f); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,4 +180,4 @@ public void updateDashboard() { | |
dashboard.putNumber(name + " ball count", getCount()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.