Skip to content

Commit

Permalink
[Gym] Generic locomotion envs - PART IV (#198)
Browse files Browse the repository at this point in the history
* [core] Add ControllerFunctor default value 'None' in Python. Take advantage of it for EngineAsync. 
* [python] Improve PEP8 compliance and typing.

Co-authored-by: Alexis Duburcq <alexis.duburcq@wandercraft.eu>
  • Loading branch information
duburcqa and Alexis Duburcq authored Sep 19, 2020
1 parent c359d9f commit 222eb84
Show file tree
Hide file tree
Showing 14 changed files with 1,349 additions and 1,150 deletions.
10 changes: 5 additions & 5 deletions core/include/jiminy/core/control/ControllerFunctor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ namespace jiminy
/// \brief Constructor
///
/// \remark A valid 'callable' is a function pointer, functor or lambda with signature:
/// void(float64_t const & t,
/// vectorN_t const & q,
/// vectorN_t const & v,
/// matrixN_t const & sensorsData[I]...,
/// vectorN_t & u)
/// void(float64_t const & t,
/// vectorN_t const & q,
/// vectorN_t const & v,
/// sensorsDataMap_t const & sensorsData,
/// vectorN_t & u)
/// where I is range(n), with n the number of different type of sensor.
///
/// \param[in] commandFct 'Callable' computing the command
Expand Down
9 changes: 6 additions & 3 deletions core/src/engine/EngineMultiRobot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1548,10 +1548,13 @@ namespace jiminy
float64_t const & controllerUpdatePeriod =
boost::get<float64_t>(stepperOptions.at("controllerUpdatePeriod"));
if ((EPS < sensorsUpdatePeriod && sensorsUpdatePeriod < SIMULATION_MIN_TIMESTEP)
|| (EPS < controllerUpdatePeriod && controllerUpdatePeriod < SIMULATION_MIN_TIMESTEP))
|| sensorsUpdatePeriod > SIMULATION_MAX_TIMESTEP
|| (EPS < controllerUpdatePeriod && controllerUpdatePeriod < SIMULATION_MIN_TIMESTEP)
|| controllerUpdatePeriod > SIMULATION_MAX_TIMESTEP)
{
std::cout << "Error - EngineMultiRobot::setOptions - Cannot simulate a discrete system with period smaller than";
std::cout << SIMULATION_MIN_TIMESTEP << "s. Increase period or switch to continuous mode by setting period to zero." << std::endl;
std::cout << "Error - EngineMultiRobot::setOptions - Cannot simulate a discrete system with update period smaller than"
<< SIMULATION_MIN_TIMESTEP << "s or larger than" << SIMULATION_MAX_TIMESTEP << "s. "
<< "Increase period or switch to continuous mode by setting period to zero." << std::endl;
return hresult_t::ERROR_BAD_INPUT;
}
// Verify that, if both values are set above sensorsUpdatePeriod, they are multiple of each other:
Expand Down
55 changes: 55 additions & 0 deletions python/jiminy_py/src/jiminy_py/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## @file src/jiminy_py/controller.py
import numpy as np
from typing import Callable

from . import core as jiminy
from .robot import BaseJiminyRobot


class BaseJiminyController(jiminy.ControllerFunctor):
"""
@brief Base class to instantiate a Jiminy controller based on a
callable function.
@details This class is primarily helpful for those who want to
implement a custom internal dynamics with hysteresis for
prototyping.
"""
def __init__(self, compute_command: Callable):
"""
@brief TODO
"""
self.__robot = None
super().__init__(compute_command, self.internal_dynamics)

def initialize(self, robot: BaseJiminyRobot):
"""
@brief TODO
"""
self.__robot = robot
return_code = super().initialize(self.__robot)

if return_code == jiminy.hresult_t.SUCCESS:
raise ValueError("Impossible to instantiate the controller. "
"There is something wrong with the robot.")

def internal_dynamics(self,
t: float,
q: np.ndarray,
v: np.ndarray,
sensors_data: jiminy.sensorsData,
u_command: np.ndarray):
"""
@brief Internal dynamics of the robot.
@details Overload this method to implement a custom internal dynamics
for the robot. Note that is results in an overhead of about
100% of the simulation in most cases, which is not often not
acceptable in production, but still useful for prototyping.
One way to solve this issue would be to compile it using
CPython.
@remark This method is time-continuous as it is designed to implement
physical laws.
"""
pass
Loading

0 comments on commit 222eb84

Please sign in to comment.