Continuous-time nonlinear control

Discussion in 'Simulation' started by sytham, Apr 26, 2017.

  1. Hi, I'm running simulations of continuous-time nonlinear controllers on Mujoco models, specifically of the form
    dh/dt = f(x,h)
    u = g(h),
    where x is sensor state, usually (qpos, qvel) but may include additional sensor readings such as contacts, h is the internal state of the controller, f is some nonlinear function, g is a linear transform and u is the control.

    Q1. Now, I think from this post I understood that in this case I have no choice but to integrate dh/dt in my own code in parallel to Mujoco. What I would really like is to be able to calculate dh/dt in a Mujoco callback to include it in the Mujoco integration, but do I understand correctly that this is not possible? E.g. while mjcb_control is called for every RK4 step, it's expected to not maintain its own internal state and be time-independent, correct? (Even assuming I just make use of qpos, qvel.)

    Q2. Assuming this is not possible, what is the recommended way to do this? It seems I shouldn't use mjcb_control then, since it'll lead to trouble with e.g. RK4, so I should use the mj_step1 -> my_control_function -> mj_step2 approach? And then my timestep should be tiny to minimize time discrepancies between my integration and the Mujoco integration. On the other hand, tiny timestep with very many simulation steps might lead to accumulation of roundoff errors...any guidelines?
     
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    You can declare actuators with internal state (stored in mjData.act) which is integrated by RK4 along with the (qpos, qvel) dynamics. You can also create custom actuators that generate zero force but still have a state, and then use that state however you want (it is a hack but will not interfere with MuJoCo). In that case you have to provide the callback mjcb_act_dyn which returns the time-derivative of these activation states.

    Note that RK4 does not appear to have advantages over Euler in the context of contact dynamics. If you are working with a system where the uncontrolled dynamics are energy-conserving, RK4 has a bigger advantage. The models distributed with MuJoCo use the Euler integrator, and the time steps do not need to be too small.

    So you have two options:

    1. hack the custom actuators to store your variables, and install both mjcb_control and mjcb_act_dyn

    2. use mj_step1/2, with Euler integrator.

    I would try (2) first, unless there is empirical evidence that RK4 is needed.
     
  3. Thanks, the dynamics are dissipative, so I tried option 2 and this seems to work well enough.