-
Notifications
You must be signed in to change notification settings - Fork 0
66-logging-util Initial Implementation #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KTruntaev
wants to merge
7
commits into
tank-bot
Choose a base branch
from
66-logging-util
base: tank-bot
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f780dc8
Implemented Data Logging
KTruntaev b3ca771
Minor Fixes
KTruntaev 674e254
Minor Fixes
KTruntaev f4a560c
Merge branch 'tank-bot' into 66-logging-util
KTruntaev ca6ab22
Minor Fixes
KTruntaev 1966ed9
Merge branch '66-logging-util' of https://github.com/BadRobots1014/Ba…
KTruntaev 86fb5d9
Update Robot.java
KTruntaev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| package frc.robot.util; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| public class DynamicField extends StaticField { | ||
|
|
||
| private Supplier<Object> supplier; | ||
|
|
||
| /** | ||
| * A supplier "broker". Stores the name of the measurement, latest value, and the supplier of the values. | ||
| * Uses Object as a flexible data type. | ||
| * | ||
| * @param n field name | ||
| * @param v initial value | ||
| * @param s value supplier/source | ||
| */ | ||
| public DynamicField(String n, Object v, Supplier<Object> s) { | ||
| super(n,v); | ||
|
|
||
| supplier = s; | ||
| } | ||
|
|
||
| public void update() { | ||
| super.setValue(supplier.get()); | ||
| } | ||
| } |
This file contains hidden or 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,153 @@ | ||
| package frc.robot.util; | ||
|
|
||
| import java.io.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.StringJoiner; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Responsible for recording the sensor data | ||
| */ | ||
| public class Logger { | ||
|
|
||
| private String path; | ||
| private File file; | ||
|
|
||
| List<DynamicField> dynamicFields; | ||
|
|
||
| // TODO: Look into the datalog tool to be able to automatically pull the log file off the robot | ||
| // TODO: Use the RobotSimulation to test the logging utility | ||
| // TODO: Track Autonomous and Teleop Status | ||
| public Logger(String p, String name) { | ||
| path = p; | ||
| try { | ||
| file = new File(path + name + ".csv"); | ||
| if (file.createNewFile()) { | ||
| System.out.println("File Created: " + file.getName()); | ||
| } else { | ||
| System.out.println("File already exists."); | ||
| } | ||
|
|
||
| // Wipes the log if it already contains text | ||
| FileWriter myWriter = new FileWriter(file,false); | ||
| myWriter.close(); | ||
|
|
||
| } catch (IOException e) { | ||
| System.out.println("Unable to create a file. "); | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| dynamicFields = new ArrayList<>(); | ||
| } | ||
|
|
||
| public void log() { | ||
| try { | ||
| // TODO: Do not close the writer after each write | ||
| FileWriter myWriter = new FileWriter(file,true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can accomplish this by simply initializing a private instance variable in your constructor. |
||
|
|
||
| myWriter.write("\n"); | ||
|
|
||
| List<String> values = new ArrayList<>(); | ||
|
|
||
| for(DynamicField dF : dynamicFields) { | ||
| values.add(dF.getValue().toString()); | ||
| } | ||
|
|
||
| myWriter.write(String.join(",", values)); | ||
| myWriter.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Could not write to file."); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets up the Dynamic Fields | ||
| */ | ||
| public void setup() { | ||
| try { | ||
| FileWriter myWriter = new FileWriter(file,true); | ||
|
|
||
| //myWriter.write("Created using FREZ Log"); | ||
|
|
||
| myWriter.write("---\n"); | ||
|
|
||
| List<String> variableNames = new ArrayList<>(); | ||
|
|
||
| for(DynamicField dF : dynamicFields) { | ||
| variableNames.add(dF.getName()); | ||
| } | ||
|
|
||
| myWriter.write(String.join(",", variableNames)); | ||
|
|
||
| myWriter.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Could not write to file."); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public void createDynamicFieldDouble(String n, Double v, Supplier<Double> s){ | ||
| // This causes a runtime error which can prevent logging a value of a wrong type | ||
| createDynamicField(n, v, () -> s.get()); | ||
| } | ||
|
|
||
| public void createDynamicFieldString(String n, Object v, Supplier<String> s){ | ||
| // This causes a runtime error which can prevent logging a value of a wrong type | ||
| createDynamicField(n, v, () -> s.get()); | ||
| } | ||
|
|
||
| public void createDynamicFieldBoolean(String n, Boolean v, Supplier<Boolean> s){ | ||
| // This causes a runtime error which can prevent logging a value of a wrong type | ||
| createDynamicField(n, booleanToInt(v), () -> booleanToInt(s.get())); | ||
| } | ||
|
|
||
| private void createDynamicField(String n, Object v, Supplier<Object> s) { | ||
| DynamicField newField = new DynamicField(n, v, s); | ||
| dynamicFields.add(newField); | ||
| } | ||
|
|
||
| public void createStaticField(String n, Object v) { | ||
| StaticField newField = new StaticField(n, v); | ||
| writeStaticField(n, v); | ||
| } | ||
|
|
||
| /** | ||
| * Records the Frequency of the Logs (does not determine how often they are actually created) | ||
| * | ||
| * @param timeUnit - decimal of a second that represents how often the logs are updated | ||
| */ | ||
| public void recordFrequency(double timeUnit) { | ||
| // t? is the property tag to signify time unit | ||
| createStaticField("t?Time Unit", timeUnit); | ||
| } | ||
|
|
||
| public void writeStaticField(String n, Object v) { | ||
| try { | ||
| FileWriter myWriter = new FileWriter(file,true); | ||
|
|
||
| myWriter.write(n+","+v+"\n"); | ||
|
|
||
| myWriter.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Could not write to file."); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public void updateFields() { | ||
| for(DynamicField field : dynamicFields) { | ||
| field.update(); | ||
| } | ||
|
|
||
| System.out.println("Updated Dynamic Fields."); | ||
| } | ||
|
|
||
| public int booleanToInt(boolean bool) { | ||
| if(bool) | ||
| return 1; | ||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or 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,30 @@ | ||
| package frc.robot.util; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Struct for storing the sensor data field | ||
| */ | ||
|
|
||
| public class StaticField{ | ||
| private String name; | ||
| private Object value; | ||
|
|
||
| public StaticField(String n, Object v) { | ||
| name = n; | ||
| value = v; | ||
| } | ||
|
|
||
| public Object getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public void setValue(Object v) { | ||
| value = v; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than incrementing this by a fixed
20here, should we increment by the elapsed time since the last execution?