Skip to content

Commit

Permalink
- Adding MPCBarrierScheduledRobotController and MPCHumanoidRobotContr…
Browse files Browse the repository at this point in the history
…olTask.

- Changing the parents class for MPCControllerTaks, MPCEstimatorTaks, MPCStepGeneratorTask and MPCWholeBodyControllerCoreTask to MPCHumanoidRobotControlTask.
- boolean of wholebodycontrollercoreRan is added to HumanoidRobotMPCContextData.
  • Loading branch information
Beomyeong committed Dec 13, 2024
1 parent c092e3d commit 50a7cd6
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package us.ihmc.avatar;

import us.ihmc.avatar.factory.HumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotContextData;
import us.ihmc.avatar.factory.MPCHumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.commonWalkingControlModules.controllerCore.command.CrossRobotCommandResolver;
import us.ihmc.robotModels.FullHumanoidRobotModel;
import us.ihmc.robotics.time.ThreadTimer;
Expand All @@ -10,7 +10,7 @@
import java.util.ArrayList;
import java.util.List;

public class MPCControllerTask extends HumanoidRobotControlTask
public class MPCControllerTask extends MPCHumanoidRobotControlTask
{
private final CrossRobotCommandResolver controllerResolver;
private final CrossRobotCommandResolver masterResolver;
Expand All @@ -24,7 +24,11 @@ public class MPCControllerTask extends HumanoidRobotControlTask
protected final List<Runnable> postControllerCallbacks = new ArrayList<>();
protected final List<Runnable> schedulerThreadRunnables = new ArrayList<>();

public MPCControllerTask(String prefix, AvatarControllerThreadInterface controllerThread, long divisor, double schedulerDt, FullHumanoidRobotModel masterFullRobotModel)
public MPCControllerTask(String prefix,
AvatarControllerThreadInterface controllerThread,
long divisor,
double schedulerDt,
FullHumanoidRobotModel masterFullRobotModel)
{
super(divisor);
this.divisor = divisor;
Expand All @@ -33,7 +37,7 @@ public MPCControllerTask(String prefix, AvatarControllerThreadInterface controll
controllerResolver = new CrossRobotCommandResolver(controllerThread.getFullRobotModel());
masterResolver = new CrossRobotCommandResolver(masterFullRobotModel);

// String prefix = "Controller";
// String prefix = "Controller";
timer = new ThreadTimer(prefix, schedulerDt * divisor, controllerThread.getYoVariableRegistry());
ticksBehindScheduled = new YoLong(prefix + "TicksBehindScheduled", controllerThread.getYoVariableRegistry());
}
Expand All @@ -58,15 +62,13 @@ protected void execute()
timer.stop();
}

@Override
protected void updateMasterContext(HumanoidRobotContextData masterContext)
protected void updateMasterContext(HumanoidRobotMPCContextData masterContext)
{
runAll(schedulerThreadRunnables);
masterResolver.resolveHumanoidRobotContextDataController(controllerThread.getHumanoidRobotContextData(), masterContext);
}

@Override
protected void updateLocalContext(HumanoidRobotContextData masterContext)
protected void updateLocalContext(HumanoidRobotMPCContextData masterContext)
{
controllerResolver.resolveHumanoidRobotContextDataScheduler(masterContext, controllerThread.getHumanoidRobotContextData());
controllerResolver.resolveHumanoidRobotContextDataEstimator(masterContext, controllerThread.getHumanoidRobotContextData());
Expand All @@ -83,5 +85,4 @@ public void addRunnableOnSchedulerThread(Runnable runnable)
{
schedulerThreadRunnables.add(runnable);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package us.ihmc.avatar;

import us.ihmc.avatar.factory.HumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotContextData;
import us.ihmc.avatar.factory.MPCHumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.commonWalkingControlModules.controllerCore.command.CrossRobotCommandResolver;
import us.ihmc.robotModels.FullHumanoidRobotModel;
import us.ihmc.robotics.time.ThreadTimer;
Expand All @@ -10,7 +10,7 @@
import java.util.ArrayList;
import java.util.List;

public class MPCEstimatorTask extends HumanoidRobotControlTask
public class MPCEstimatorTask extends MPCHumanoidRobotControlTask
{
private final CrossRobotCommandResolver estimatorResolver;
private final CrossRobotCommandResolver masterResolver;
Expand Down Expand Up @@ -66,16 +66,14 @@ protected void execute()
timer.stop();
}

@Override
protected void updateMasterContext(HumanoidRobotContextData masterContext)
protected void updateMasterContext(HumanoidRobotMPCContextData masterContext)
{
runAll(schedulerThreadRunnables);
masterResolver.resolveHumanoidRobotContextDataEstimator(estimatorThread.getHumanoidRobotContextData(), masterContext);
masterContextUpdated = true;
}

@Override
protected void updateLocalContext(HumanoidRobotContextData masterContext)
protected void updateLocalContext(HumanoidRobotMPCContextData masterContext)
{
estimatorResolver.resolveHumanoidRobotContextDataScheduler(masterContext, estimatorThread.getHumanoidRobotContextData());
estimatorResolver.resolveHumanoidRobotContextDataController(masterContext, estimatorThread.getHumanoidRobotContextData());
Expand All @@ -98,5 +96,4 @@ public void addRunnableOnSchedulerThread(Runnable runnable)
{
schedulerThreadRunnables.add(runnable);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package us.ihmc.avatar;

import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotContextData;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.robotModels.FullHumanoidRobotModel;

public class MPCStepGeneratorTask extends MPCControllerTask
{
public MPCStepGeneratorTask(String prefix, AvatarControllerThreadInterface controllerThread, long divisor, double schedulerDt, FullHumanoidRobotModel masterFullRobotModel)
public MPCStepGeneratorTask(String prefix,
AvatarControllerThreadInterface controllerThread,
long divisor,
double schedulerDt,
FullHumanoidRobotModel masterFullRobotModel)
{
super(prefix, controllerThread, divisor, schedulerDt, masterFullRobotModel);
}

@Override
protected void updateMasterContext(HumanoidRobotContextData masterContext)
protected void updateMasterContext(HumanoidRobotMPCContextData masterContext)
{
runAll(schedulerThreadRunnables);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package us.ihmc.avatar;

import us.ihmc.avatar.factory.HumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotContextData;
import us.ihmc.avatar.factory.MPCHumanoidRobotControlTask;
import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.commonWalkingControlModules.controllerCore.command.CrossRobotCommandResolver;
import us.ihmc.robotModels.FullHumanoidRobotModel;
import us.ihmc.robotics.time.ThreadTimer;
Expand All @@ -10,7 +10,7 @@
import java.util.ArrayList;
import java.util.List;

public class MPCWholeBodyControllerCoreTask extends HumanoidRobotControlTask
public class MPCWholeBodyControllerCoreTask extends MPCHumanoidRobotControlTask
{
private final CrossRobotCommandResolver wholeBodyControllerCoreResolver;
private final CrossRobotCommandResolver masterResolver;
Expand All @@ -22,7 +22,6 @@ public class MPCWholeBodyControllerCoreTask extends HumanoidRobotControlTask

protected final List<Runnable> postWholeBodyControllerCoreCallbacks = new ArrayList<>();


public MPCWholeBodyControllerCoreTask(String prefix,
AvatarControllerThreadInterface wholeBodyControllerCoreThread,
long divisor,
Expand All @@ -47,6 +46,7 @@ protected boolean initialize()
ticksBehindScheduled.set(0);
return super.initialize();
}

@Override
protected void execute()
{
Expand All @@ -58,17 +58,16 @@ protected void execute()
timer.stop();
}

@Override
protected void updateMasterContext(HumanoidRobotContextData context)
protected void updateMasterContext(HumanoidRobotMPCContextData context)
{

}

@Override
protected void updateLocalContext(HumanoidRobotContextData context)
protected void updateLocalContext(HumanoidRobotMPCContextData context)
{

}

@Override
public void addCallbackPostTask(Runnable runnable)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package us.ihmc.avatar.factory;

import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.concurrent.runtime.barrierScheduler.implicitContext.BarrierScheduler;
import us.ihmc.concurrent.runtime.barrierScheduler.implicitContext.BarrierScheduler.TaskOverrunBehavior;
import us.ihmc.robotics.time.ThreadTimer;
import us.ihmc.yoVariables.registry.YoRegistry;

import java.util.List;

public class MPCBarrierScheduledRobotController implements DisposableRobotController
{
private final YoRegistry registry;
private final BarrierScheduler<HumanoidRobotMPCContextData> barrierScheduler;
private final HumanoidRobotMPCContextData masterContext;

private final ThreadTimer timer;
private final List<MPCHumanoidRobotControlTask> tasks;

public MPCBarrierScheduledRobotController(String name,
List<MPCHumanoidRobotControlTask> tasks,
HumanoidRobotMPCContextData masterContext,
TaskOverrunBehavior overrunBehavior,
double schedulerDt)
{
this.tasks = tasks;
this.masterContext = masterContext;

barrierScheduler = new BarrierScheduler<>(tasks, masterContext, overrunBehavior);
registry = new YoRegistry(name);

timer = new ThreadTimer("Scheduler", schedulerDt, registry);
}

@Override
public void initialize()
{
// Reset the internal counter to observe when the tasks get triggered.
timer.reset();
// Reset the scheduler counter so the threads restart as if we started for the first time.
barrierScheduler.reset();
for (int i = 0; i < tasks.size(); i++)
{
tasks.get(i).initialize();
}
masterContext.setControllerRan(false);
masterContext.setEstimatorRan(false);
masterContext.setWholeBodyControllerCoreRan(false);
}

@Override
public YoRegistry getYoRegistry()
{
return registry;
}

@Override
public void doControl()
{
timer.start();
masterContext.setSchedulerTick(timer.getTickCount());
barrierScheduler.run();
timer.stop();
}

public void waitUntilTasksDone()
{
try
{
barrierScheduler.waitUntilTasksDone();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

@Override
public void dispose()
{
barrierScheduler.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package us.ihmc.avatar.factory;

import us.ihmc.commonWalkingControlModules.barrierScheduler.context.HumanoidRobotMPCContextData;
import us.ihmc.concurrent.runtime.barrierScheduler.implicitContext.Task;

import java.util.List;

public abstract class MPCHumanoidRobotControlTask extends Task<HumanoidRobotMPCContextData>
{
public MPCHumanoidRobotControlTask(long divisor)
{
super(divisor);
}

@Override
protected boolean initialize()
{
return true;
}

@Override
protected void cleanup()
{
}

/**
* The given callback will be run on the task thread right <b>before</b> each control tick.
*/
public void addCallbackPreTask(Runnable callback)
{
throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support this operation.");
}

/**
* The given callback will be run on the task thread right <b>after</b> each control tick.
*/
public void addCallbackPostTask(Runnable callback)
{
throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support this operation.");
}

/**
* This will cause the provided runnable to be executed on the scheduler thread before the task is
* released. All runnables provided here will be executed periodically in the order they were
* provided.
*
* @param runnable
*/
public void addRunnableOnSchedulerThread(Runnable runnable)
{
throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support this operation.");
}

protected static void runAll(List<Runnable> runnables)
{
for (int i = 0; i < runnables.size(); i++)
runnables.get(i).run();
}
}
Loading

0 comments on commit 50a7cd6

Please sign in to comment.