Crash in while loop

Typically: "How do I... ", "How can I... " questions
jgouveia
Posts: 4
Joined: 25 Apr 2023, 16:59

Crash in while loop

Post by jgouveia »

Hello, i am trying to use a while loop in a function to correct the robot's angular position. The purpose is to always make the robot stay at the same initial angle that is pre-defined, rotating clockwise or counterclockwise. But always when i try to use a while loop to peform this task the program crash, there's any solution or workaraound for this?

Code: Select all

function correctAngle()
    while(true) do 
        if (compassData[3] > (initial_angle + 0.025)) then
            rotate(-1)
        elseif (compassData[3] < (initial_angle - 0.025)) then
                rotate(1)
        elseif (compassData[3] < (initial_angle + 0.025)) and (compassData[3] > (initial_angle - 0.025)) and (isSpining())  then
                stop()
                break
        end
    end
end

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Crash in while loop

Post by coppelia »

Hello,

when you say that the program crashes, does that mean CoppeliaSim crashes? Or is there any error message?

Cheers

jgouveia
Posts: 4
Joined: 25 Apr 2023, 16:59

Re: Crash in while loop

Post by jgouveia »

The simulation stops and a message appears saying "abort execution". In the terminal it shows a message "[/body@childScript:error] ?: script execution was terminated externally.".
I was thinking maybe it's a memory issue.

Thank you for your attention.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Crash in while loop

Post by coppelia »

No, that happens is you have an infinite loop or a very long operation in a non-threaded context. Something is wrong with your algorithm.

Cheers

juyeon
Posts: 12
Joined: 27 Mar 2023, 07:11

Re: Crash in while loop

Post by juyeon »

coppelia wrote: 02 May 2023, 14:00 No, that happens is you have an infinite loop or a very long operation in a non-threaded context. Something is wrong with your algorithm.

Cheers
I ran into the same problem. I put a while function in sysCall_actuation() so that when it didn’t rotate to the desired angle, it kept turning, but it would crash.

Code: Select all

	ang_arrive_flag=false 
        goal_matrix = sim.getObjectMatrix(goal,model)
            print('start:',goal_matrix)
        goal_matrix[6]=math.cos(recive_ang)
        goal_matrix[11]=math.cos(recive_ang)
        goal_matrix[7]=-math.sin(recive_ang)
        goal_matrix[10]=math.sin(recive_ang)
            
            print('after:',goal_matrix)
        sim.setObjectMatrix(goal,model,goal_matrix)
        --print('goal_matrix',goal_matrix)
        
        
        while ang_arrive_flag==false and recive_ang >0 do
                --print(ang_arrive_flag)
                res=sim.setJointTargetVelocity(rightJointHandle,100*math.pi/180)        --turn left
                print(res)
                sim.setJointTargetVelocity(leftJointHandle,50*math.random()*math.pi/180)
                model_matirx=sim.getObjectMatrix(model,goal)
                print('model_matrix',model_matirx)
                theta = math.acos(model_matirx[6])
                print(theta)
                a=sim.getJointTargetVelocity(rightJointHandle)
                print('velocity',a)
                    if math.abs(theta)<0.05  then
                        print('DDDDD')
                        ang_arrive_flag=true
                        update_flag=true
                        model_matirx[1]=1
                        model_matirx[2]=0
                        model_matirx[3]=0
                        
                        model_matirx[5]=0
                        model_matirx[6]=1
                        model_matirx[7]=0
                            
                        model_matirx[9]=0
                        model_matirx[10]=0
                        model_matirx[11]=1
                        sim.setObjectMatrix(goal,model,model_matirx)
                        recive_ang=0
                        break
                    
                    end
        end
  
I don't know what's wrong,please take a look for me.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Crash in while loop

Post by coppelia »

Did it now really crash, or simply hang like last time?!

If it hangs, then you are probably using a long loop in a non-coroutine section. You didn't show your whole code, so difficult to say

Cheers

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: Crash in while loop

Post by fferri »

juyeon wrote: 19 May 2023, 05:43

Code: Select all

        goal_matrix = sim.getObjectMatrix(goal,model)
            print('start:',goal_matrix)
        goal_matrix[6]=math.cos(recive_ang)
        goal_matrix[11]=math.cos(recive_ang)
        goal_matrix[7]=-math.sin(recive_ang)
        goal_matrix[10]=math.sin(recive_ang)
            
            print('after:',goal_matrix)
        sim.setObjectMatrix(goal,model,goal_matrix)
  
that's a terrible way to operate on a transform matrix.

Try sim.rotateAroundAxis or some function from matrix.lua, e.g. Matrix3x3:fromaxisangle, Matrix3x3:rotx, Matrix3x3:roty, Matrix3x3:rotz (use then Matrix4x4:fromrotation or Matrix4x4:fromrt to create a full 4x4 matrix).

juyeon
Posts: 12
Joined: 27 Mar 2023, 07:11

Re: Crash in while loop

Post by juyeon »

fferri wrote: 19 May 2023, 11:24
that's a terrible way to operate on a transform matrix.

Try sim.rotateAroundAxis or some function from matrix.lua, e.g. Matrix3x3:fromaxisangle, Matrix3x3:rotx, Matrix3x3:roty, Matrix3x3:rotz (use then Matrix4x4:fromrotation or Matrix4x4:fromrt to create a full 4x4 matrix).
Thank you for your reply, this function seems to be useful to me, but I don't quite understand the axis variable in sim.rotateAroundAxis()

I use the dr12 model .

Please help me to look at my modified code, this rotation angle is not the angle I want, and it will crash when I set recive_ang=0.5, it will not crash when I set 1.5. I think it should be the reason why I don't quite understand this variable.

Code: Select all

	model_matrix = sim.getObjectMatrix(model,-1)
        print(model_matrix)
        x = {model_matrix[1],model_matrix[5],model_matrix[9]}

        pos = {model_matrix[4],model_matrix[8],model_matrix[12]}
        goal_matrix = sim.rotateAroundAxis(model_matrix,x,pos,recive_ang)
        print(goal_matrix)
        
        
        while recive_ang>0 do
            model_matrix = sim.getObjectMatrix(model,-1)

    
            sim.setJointTargetVelocity(rightJointHandle,100*math.pi/180)        --turn left
            sim.setJointTargetVelocity(leftJointHandle,50*math.random()*math.pi/180)
            
            res,rota = sim.getRotationAxis(model_matrix,goal_matrix)
            print(rota)
            
            if rota == recive_ang then
                recive_ang=0
                break
            end
            
   
        end
        

juyeon
Posts: 12
Joined: 27 Mar 2023, 07:11

Re: Crash in while loop

Post by juyeon »

coppelia wrote: 19 May 2023, 09:38 Did it now really crash, or simply hang like last time?!

If it hangs, then you are probably using a long loop in a non-coroutine section. You didn't show your whole code, so difficult to say

Cheers
The strange thing is that I wrote the same loop function in sysCall_actuation(), sim.setJointTargetVelocity() didn't seem to work and caused me to be in the loop for a long time, so I was prompted to interrupt manually, but I wrote it in coroutineMain(), no Interruption can be executed normally, but the model shakes very badly, which will cause the stop condition I set to be shaken and other behaviors to appear.
I thought these two functions were just different ways of writing the system execution function. Is there any difference between these two functions?

juyeon
Posts: 12
Joined: 27 Mar 2023, 07:11

Re: Crash in while loop

Post by juyeon »

Code: Select all

function sysCall_actuation()

    if recive_ang ~=0  then

        model_matrix = sim.getObjectMatrix(model,-1)
        
        z = {0,0,1}
        
        pos={0,0,0}
        
        goal_matrix = sim.rotateAroundAxis(model_matrix,z,pos,recive_ang)
        
        
        while recive_ang>0 do
            model_matrix = sim.getObjectMatrix(model,-1)

    
            sim.setJointTargetVelocity(rightJointHandle,100*math.pi/180)        --turn left
            sim.setJointTargetVelocity(leftJointHandle,50*math.random()*math.pi/180)
            
            res,rota = sim.getRotationAxis(model_matrix,goal_matrix)
            print(rota)
            
            if rota == recive_ang then

                recive_ang=0
                break
            end
  
        end
      
    end

end 
I tried it again,but still failed.
This is my full code, i want to cotrol the model(dr12.ttm),Rotate to the angle I give and stop.

Post Reply