raisimTech / raisimLib

Visit www.raisim.com
http://www.raisim.com
Other
335 stars 90 forks source link

inserting actuators models give me segmentation fault #566

Open JcSack opened 4 months ago

JcSack commented 4 months ago

Hello, i was trying to implement a custom sim-to-real but I needed to include the actuator model inside the simulation. I'm trying in this way, considering only the mechanical differential equation of a SEA:

$$B\ddot{\theta} + K(\theta - q) = \tau$$

So I implemented backward Euler in this way:

float step(const Eigen::Ref<EigenVec>& action) final {

      //Code related to operations on the actions

    if(!sea_included_)
        myRobot->setPdTarget(pTarget_, vTarget_);
    else{
        motorTorque = 18*(pTarget3_ - theta) - 0.2*dotTheta;

        dotTheta += control_dt_*B_inverse*(motorTorque - motorSpring_*(theta - gc_.tail(3)));
        theta += control_dt_*dotTheta;

        theta(2) = gc_(9);
        dotTheta(2) = gv_(8);

        linkTorque_.tail(3) << motorSpring_*(theta - gc_.tail(3));

        myRobot->setGeneralizedForce(linkTorque_);
    }

            for(int i=0; i< int(control_dt_ / simulation_dt_ + 1e-10); i++){
                if(server_) server_->lockVisualizationServerMutex();
                world_->integrate();
                if(server_) server_->unlockVisualizationServerMutex();
            }

            updateObservation(); 

            //Define reward functions
            return reward.sum();

When I run, after a random number of episode, it returns me nan and then segmentation fault. I tried to reduce the learning rate, the number of neurons but nothing helped.

Here theta, dotTheta and motorTorque are Eigen::Vector3d. I've checked thousands of time that I'm not exceding the dimension of the Eigen vector used, so I was wondering if the problem were due to how and where i was performing the integration. Raisim solves the dynamic of the rigid bodies inside that for loop with world_->integrate() right? Am I supposed to put any integrator inside the for loop or it's okay how I did?

But in general, there are smarter ways to include motor dynamic into the simulator?

Thanks a lot =)

JcSack commented 4 months ago

The segmentation fault is appearing less often. I made a mistake with the sign of the controller. However I still have doubt related to the actuator dynamic. I am inserting it correctly? I mean, should the model be inserted before the world_->integrate() or inside its for loop?

Moreover, I am noticing a change of performance in my controller setting "PD_PLUS_FEEDFORWARD_TORQUE" with $K_p=K_D=0$ and "FORCE_AND_TORQUE" mode. However, I read that the PD_PLUS doesn't take into account the joint limits, doesn't it? Anyway, if I set: $\tau = K_pe + K_d\dot{e}$ And then:

setGeneralizedForce(tau)

is the same thing of do:

setPdGains(Kp, Kd)

isn't it?

jhwangbo commented 4 months ago

No they are different. The PD controller is more stable because it is updated in an implicit manner. Its behavior is similar to having an infinitely small time step.

Check the states before it segfaults. If they are exloding, that means that your controller is not stable. Otherwise, try Valgrind following the instructions in the manual

JcSack commented 4 months ago

Okay, I am figuring out that maybe I should use an explicit solver to improve sim-to-real.

Anyway, I am debuggin with valgrind as you suggested. It raises this error: Screenshot from 2024-03-10 16-18-30

Does it depend still from my code?

jhwangbo commented 4 months ago

That looks like an another issue. I'll look at it. But conditional jump should not trigger a segfault. There must be another error message, possibly related to the memory