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?
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
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.
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.
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).
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.
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
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?
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.