Skip to content

Commit

Permalink
Feature/joystick rough terrain (#433)
Browse files Browse the repository at this point in the history
* 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
PotatoPeeler3000 authored Nov 15, 2024
1 parent b52806c commit b9245f4
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void computeSwingWaypoints(HeightMapData heightMapData,
SwingPlannerType swingPlannerType)
{
swingTrajectories.clear();

if (heightMapData == null || heightMapData.isEmpty())
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void update(TerrainMapData terrainMapData, HeightMapData heightMapData)
{
remotePropertySets.setPropertyChanged();

// When running on the process we don't want to create the parameters locally, this gets done on the remote side
// When running on the process, we don't want to create the parameters locally, this gets done on the remote side
if (runningLocally)
{
ros2PropertySetGroup.update();
Expand Down Expand Up @@ -300,27 +300,32 @@ public void onMonteCarloPlanReceived(FootstepDataListMessage message)

private void publishInputCommandMessage()
{
Controller currentController = Controllers.getCurrent();
boolean currentJoystickControllerConnected = currentController != null;

boolean walkingEnabled = ImGui.getIO().getKeyCtrl();
double forwardJoystickValue = 0.0;
// Check to see if a controller is plugged into the computer
Controller joystickController = Controllers.getCurrent();
// Here we check against null rather then .isConnected() because if the controller is unplugged that method won't work
boolean controllerConnected = joystickController != null;

// Setup a bunch of variables to be published in the message
boolean walkWithKeyboard = ImGui.getIO().getKeyCtrl();
boolean walkWithController = false;
double lateralJoystickValue = 0.0;
double forwardJoystickValue = 0.0;
double turningJoystickValue = 0.0;

if (currentJoystickControllerConnected)
if (controllerConnected)
{
walkingEnabled |= currentController.getButton(currentController.getMapping().buttonR1);
forwardJoystickValue = -currentController.getAxis(currentController.getMapping().axisLeftY);
lateralJoystickValue = -currentController.getAxis(currentController.getMapping().axisLeftX);
turningJoystickValue = -currentController.getAxis(currentController.getMapping().axisRightX);
walkWithController = joystickController.getButton(joystickController.getMapping().buttonA);
forwardJoystickValue = -joystickController.getAxis(joystickController.getMapping().axisLeftY);
lateralJoystickValue = -joystickController.getAxis(joystickController.getMapping().axisLeftX);
turningJoystickValue = -joystickController.getAxis(joystickController.getMapping().axisRightX);
}

// Only allow Continuous Walking if the CTRL key is held and the checkbox is checked
// We publish this all the time to prevent any of the values from staying true all the time
if (continuousHikingParameters.getEnableContinuousHiking())
{
commandMessage.setEnableContinuousWalking(walkingEnabled);
commandMessage.setPublishToController(ImGui.getIO().getKeyAlt());
commandMessage.setEnableContinuousHikingWithKeyboard(walkWithKeyboard);
commandMessage.setEnableContinuousHikingWithJoystickController(walkWithController);
commandMessage.setForwardValue(forwardJoystickValue);
commandMessage.setLateralValue(lateralJoystickValue);
commandMessage.setTurningValue(turningJoystickValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ContinuousHikingParameters extends StoredPropertySet implements Con
public static final DoubleStoredPropertyKey goalPoseUpDistance = keys.addDoubleKey("Goal pose up distance");
public static final DoubleStoredPropertyKey swingTime = keys.addDoubleKey("Swing time");
public static final DoubleStoredPropertyKey transferTime = keys.addDoubleKey("Transfer time");
public static final DoubleStoredPropertyKey plannerTimeoutFraction = keys.addDoubleKey("Planner timeout fraction");
public static final DoubleStoredPropertyKey planningTimeoutAsAFractionOfTheStepDuration = keys.addDoubleKey("Planning timeout as a fraction of the step duration");
public static final DoubleStoredPropertyKey planningWithoutReferenceTimeout = keys.addDoubleKey("Planning without reference timeout");
public static final DoubleStoredPropertyKey percentThroughSwingToPlanTo = keys.addDoubleKey("Percent through swing to plan to");
public static final BooleanStoredPropertyKey logFootstepPlans = keys.addBooleanKey("Log footstep plans");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ default void setTransferTime(double transferTime)
set(ContinuousHikingParameters.transferTime, transferTime);
}

default void setPlannerTimeoutFraction(double plannerTimeoutFraction)
default void setPlanningTimeoutAsAFractionOfTheStepDuration(double planningTimeoutAsAFractionOfTheStepDuration)
{
set(ContinuousHikingParameters.plannerTimeoutFraction, plannerTimeoutFraction);
set(ContinuousHikingParameters.planningTimeoutAsAFractionOfTheStepDuration, planningTimeoutAsAFractionOfTheStepDuration);
}

default void setPlanningWithoutReferenceTimeout(double planningWithoutReferenceTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ default double getTransferTime()
return get(transferTime);
}

default double getPlannerTimeoutFraction()
default double getPlanningTimeoutAsAFractionOfTheStepDuration()
{
return get(plannerTimeoutFraction);
return get(planningTimeoutAsAFractionOfTheStepDuration);
}

default double getPlanningWithoutReferenceTimeout()
Expand Down
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);
}
}

}
Loading

0 comments on commit b9245f4

Please sign in to comment.