# RigidBodySim.jl and disturbances

**URL:** <https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [October 20, 2018, 5:59pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576 "2018-10-20T17:59:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)\
**Post date:** [October 20, 2018, 5:59pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/1 "2018-10-20T17:59:28Z")

</div>

Hi, is there any way to add friction or other disturbance to the model as the controller is added? Something like:

`closed_loop_dynamics = Dynamics(robot, control!, disturbance!)`

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 20, 2018, 6:20pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/2 "2018-10-20T18:20:03Z")

</div>

Perhaps `control!` is a bit of a misnomer; it’s really just a torque source. The `control!` function you pass in could first call the actual controller to get the actuator torque vector, and then add additional torques (e.g., your disturbance torques).

Perhaps it would be nice to have a convenience type that stores a bunch of `control!` functions, satisfies the standard controller call signature, and does the addition of torques for you. @rdeits and I were talking about that at some point but never got around to it. PR’s definitely welcome.

---

<div class="post-metadata">

**Author:** ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)\
**Post date:** [October 21, 2018, 6:14pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/3 "2018-10-21T18:14:46Z")

</div>

In analog controller simulations it is possible to add directly to the function, the problem is that in my case my disturbance function would act as a “controller in the loop” and my controller would be simulated as a digital controller. It is possible to create two callbacks but both would modify the torque value and this would not give the effect of continuous disturbance on the system. I will do some tests by adding ‘τdcache’ to the Dynamics type and modifying the function used in ODE.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 21, 2018, 8:20pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/4 "2018-10-21T20:20:32Z")

</div>

You can still do this with a `PeriodicController`, you just have to be careful with the associated `PeriodicCallback` that ensures that the simulation is stopped at the appropriate times for the controller to be run. If you tried calling a `PeriodicController` from another `control!` function, you may have gotten this error message:

[https://github.com/JuliaRobotics/RigidBodySim.jl/blob/e01174abfcf9877bdeba3cf74f4f9b760d1f12ba/src/control.jl#L132-L137](https://github.com/JuliaRobotics/RigidBodySim.jl/blob/e01174abfcf9877bdeba3cf74f4f9b760d1f12ba/src/control.jl#L132-L137)

The last part describes how to make this work; let me know if this is not clear enough.

Here’s an example that implements this (sorry I didn’t think of this earlier):

[https://github.com/tkoolen/HumanoidLCMSim.jl/blob/c57fd594c78975f0b0f86420281a8506f10fdba8/src/atlassim.jl#L90-L103](https://github.com/tkoolen/HumanoidLCMSim.jl/blob/c57fd594c78975f0b0f86420281a8506f10fdba8/src/atlassim.jl#L90-L103)

(the `let` block is to work around [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)).

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 21, 2018, 8:52pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/5 "2018-10-21T20:52:13Z")

</div>

By the way, if instead of a simple closure you’re making a callable named type, you can overload `controlcallback` for that type:

[https://github.com/JuliaRobotics/RigidBodySim.jl/blob/e01174abfcf9877bdeba3cf74f4f9b760d1f12ba/src/core.jl#L96-L99](https://github.com/JuliaRobotics/RigidBodySim.jl/blob/e01174abfcf9877bdeba3cf74f4f9b760d1f12ba/src/core.jl#L96-L99)

to avoid having to explicitly create the callback and pass it into the `ODEProblem` constructor.

---

<div class="post-metadata">

**Author:** ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)\
**Post date:** [October 26, 2018, 1:57pm UTC](https://discourse.julialang.org/t/rigidbodysim-jl-and-disturbances/16576/6 "2018-10-26T13:57:29Z")

</div>

Thank you! I will try to do this.
