-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/joystick rough terrain (#433)
* Added a feature to use the joystick to walk in multiple directions with Continuous Hiking, and a bunch of cleanup * Bunch of changes, started looking at adding a ContinuousHikingLogger to log to a file to try and determine when things are going wrong * Added ContinuousHikingLogger to log to the files and only keep a certain amount of logs so we don't blow up storage * Fixed null pointer exception * Removed the backward parameter, we aren't ready for it * Cleanup and document the ContinuousPlannerTools * Put things back where they belong * Added comment for the logging class * More logging information * Cleanup on the turning joystick walking, fixed a z height issue, and increased the lattice radius options * Remove unused import * Changes that need to go through in another PR
- Loading branch information
1 parent
b52806c
commit b9245f4
Showing
21 changed files
with
390 additions
and
211 deletions.
There are no files selected for viewing
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
134 changes: 134 additions & 0 deletions
134
...level-behaviors/src/main/java/us/ihmc/behaviors/activeMapping/ContinuousHikingLogger.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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package us.ihmc.behaviors.activeMapping; | ||
|
||
import us.ihmc.commons.exception.DefaultExceptionHandler; | ||
import us.ihmc.commons.nio.FileTools; | ||
import us.ihmc.commons.nio.WriteOption; | ||
import us.ihmc.log.LogTools; | ||
import us.ihmc.tools.IHMCCommonPaths; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Comparator; | ||
import java.util.Date; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class is meant to provide a way to log print statements to a file. | ||
* It's used throughout the Continuous Hiking process to help with debugging. | ||
* By logging to a file often the details of the process, we can attempt to determine how things are going wrong. | ||
* Writing unit tests for a UI is tricky and when this class was written, a testing structure didn't exist, so this was the next best option | ||
*/ | ||
public class ContinuousHikingLogger | ||
{ | ||
private static final int NUMBER_OF_LOGS_TO_KEEP = 100; | ||
private File file; | ||
|
||
private static final String CONTINUOUS_HIKING_FILE_SUFFIX = "ContinuousHikingLog.txt"; | ||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); | ||
private static final String logFileName = dateFormat.format(new Date()) + "_" + CONTINUOUS_HIKING_FILE_SUFFIX; | ||
private static final String filePath = IHMCCommonPaths.CONTINUOUS_HIKING_DIRECTORY.resolve(logFileName).toString(); | ||
|
||
StringBuilder additionalString = new StringBuilder(); | ||
|
||
public ContinuousHikingLogger() | ||
{ | ||
FileTools.ensureDirectoryExists(IHMCCommonPaths.CONTINUOUS_HIKING_DIRECTORY, DefaultExceptionHandler.MESSAGE_AND_STACKTRACE); | ||
deleteOldLogs(); | ||
|
||
try | ||
{ | ||
if(!Files.exists(IHMCCommonPaths.CONTINUOUS_HIKING_DIRECTORY)) | ||
{ | ||
Files.createDirectory(IHMCCommonPaths.CONTINUOUS_HIKING_DIRECTORY); | ||
} | ||
if (!Files.exists(IHMCCommonPaths.TERRAIN_MAP_DIRECTORY.resolve(logFileName))) | ||
{ | ||
Files.createFile(Paths.get(filePath)); | ||
file = new File(filePath); | ||
} | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void logToFile(boolean logToFile, boolean printToConsole) | ||
{ | ||
if (logToFile || printToConsole) | ||
{ | ||
if (printToConsole) | ||
System.out.println(this); | ||
|
||
if (logToFile) | ||
{ | ||
FileTools.write(file.getAbsoluteFile().toPath(), toString().getBytes(), WriteOption.APPEND, DefaultExceptionHandler.MESSAGE_AND_STACKTRACE); | ||
} | ||
|
||
additionalString.setLength(0); | ||
} | ||
} | ||
|
||
public String toString() | ||
{ | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("\n"); | ||
|
||
builder.append("]\n"); | ||
builder.append(additionalString.toString()).append("\n"); | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public void appendString(String string) | ||
{ | ||
additionalString.append(String.format("[%s]: (", new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()))); | ||
additionalString.append(string); | ||
additionalString.append(")\n"); | ||
} | ||
|
||
/** Keeps around the recommended number of logs. */ | ||
public static void deleteOldLogs() | ||
{ | ||
deleteOldLogs(NUMBER_OF_LOGS_TO_KEEP, IHMCCommonPaths.CONTINUOUS_HIKING_DIRECTORY.toString()); | ||
} | ||
|
||
/** | ||
* It's recommended to leave quite a few logs around, otherwise, we diminish the usefulness of the logging. | ||
* This method expects the folder to exist or it will throw an exception | ||
*/ | ||
public static void deleteOldLogs(int numberOfLogsToKeep, String directory) | ||
{ | ||
SortedSet<Path> sortedLogFolderPaths = new TreeSet<>(Comparator.comparing(path1 -> path1.getFileName().toString())); | ||
|
||
try (Stream<Path> paths = Files.walk(Path.of(directory))) | ||
{ | ||
paths.filter(Files::isRegularFile).forEach(dir -> | ||
{ | ||
if (dir.getFileName().toString().endsWith(ContinuousHikingLogger.CONTINUOUS_HIKING_FILE_SUFFIX)) | ||
{ | ||
sortedLogFolderPaths.add(dir); | ||
} | ||
}); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
|
||
while (sortedLogFolderPaths.size() > numberOfLogsToKeep) | ||
{ | ||
Path earliestLogDirectory = sortedLogFolderPaths.first(); | ||
LogTools.warn("Deleting old log {}", earliestLogDirectory); | ||
FileTools.deleteQuietly(earliestLogDirectory); | ||
sortedLogFolderPaths.remove(earliestLogDirectory); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.