Search code examples
pythonwebots

Changing spring constants after importing robot causes issues


I have to run an optimization on tendon stiffnesses in a modelled leg. Changing the spring constants manually causes no issues and the physical model seems to work well. The issue presents itself when I attempt to optimize for these values.

Since this is an optimization problem, I have to run the simulation for 1.5 seconds and then reset the robot to its initial state, with slightly different spring constants. To do this I have the robot model saved as a "Robot.wbo" file. At the end of each optimization iteration, I remove the existing robot node and use "def importMFNode" to create a new robot node, which creates the robot in the initial state. At the start of each new iteration, I then set new spring constants and setting these causes the model to behave unexpectedly. I have pinned down the issue to these two lines.

knee_spring_constant.setSFFloat(int(x[2]))
heel_spring_constant.setSFFloat(int(x[3]))

All these two lines do is change the spring constants of two slider joints. If I do it using these two commands, the model fails. If I do it manually, it works.

Instead of the model behaving normally, it just behaves erratically and not like it is supposed to. However, if I manually make a new copy of the robot node right after it is imported by the supervisor code, and delete the old one, the robot behaves as it is supposed to.


Solution

  • I'm not sure why this is not working, maybe a global physics reset is missing.

    But for sure, your method to unload/reload the robot is heavy and can be simplified easily. There are several solutions to reset a robot which are more simple and so which will potentially work better.

    1. You could reset the robot position and its physics at the end of an iteration. Changing the spring constants before calling the physics reset function seems to be a safe idea.
    2. You could reset the entire simulation and change the spring constants.
    3. You could keep your method as-is, but change the springs inside Robot.wbo before loading it (with a basic template engine, a simple search and replace could do the job).