sim.getObjectOrientation() returning values in steps

Typically: "How do I... ", "How can I... " questions
Post Reply
HinayM
Posts: 2
Joined: 13 Jan 2021, 15:18

sim.getObjectOrientation() returning values in steps

Post by HinayM »

Hello, I am trying to rotate a mobile robot (Pionner_p3dx), by a certain specified angle, for example, 90 degrees. I am using a while loop which basically, detects orientation angle in every pass, and if that value about Z-axis is less than or equal to pi/2, then the loop is terminated. I also tried to directly use equation involving trackwidth and tyre radius, but time according to it is not correct, so I am going very fundamental first. Then also, the orientation values returned are in steps, and changing by 0.03 radians or 1.7 degrees suddenly. I don't understand why the value returned is in steps and not continuous. I have attached code and some output values of orienataion[3] for reference.

Code: Select all

function sysCall_threadmain()
    --simRemoteApi.start(19999)
    base=sim.getObjectHandle('Cuboid0')
    joint=sim.getObjectHandle('Revolute_joint')
    angle=sim.getObjectOrientation(base,-1)[3]
    sim.setJointTargetVelocity(joint,0.5)     --rad/s
    while (angle<math.pi/2) do
        angle=sim.getObjectOrientation(base,-1)[3]
        print(angle)
    end
    sim.setJointTargetVelocity(joint,0)
end

function sysCall_cleanup()
    -- Put some clean-up code here
end
Output values:
0.96741753816605
0.96741753816605
0.99241989850998
0.99241989850998
0.99241989850998
1.0174136161804
1.0174136161804
1.0424164533615
1.0674144029617
1.0924071073532
1.0924071073532
1.1174054145813
1.1174054145813
1.1424080133438
1.1674021482468
1.1674021482468
1.192400097847
1.192400097847
1.192400097847
1.2173956632614
1.2173956632614
1.2424006462097
1.2424006462097
1.2674024105072
1.2923972606659
1.2923972606659

I would really appreciate if anyone can point out any error present in code. Thank You.

Regards,
HinayM

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

Re: sim.getObjectOrientation() returning values in steps

Post by coppelia »

Hello,

since you operate from within a thread, you can read the same value several times, in the same simulation step. If you want to be sure to execute some code in next simulation step, call sim.switchThread:

Code: Select all

    while (angle<math.pi/2) do
        angle=sim.getObjectOrientation(base,-1)[3]
        print(angle)
        sim.switchThread()
    end
Best is to do it like:

Code: Select all

function sysCall_threadmain()
    --simRemoteApi.start(19999)
    sim.setThreadAutomaticSwitch(false) -- do not allow the system to interrupt
    base=sim.getObjectHandle('Cuboid0')
    joint=sim.getObjectHandle('Revolute_joint')
    angle=sim.getObjectOrientation(base,-1)[3]
    sim.setJointTargetVelocity(joint,0.5)     --rad/s
    while (angle<math.pi/2) do
        angle=sim.getObjectOrientation(base,-1)[3]
        print(angle)
        sim.switchThread() -- explicitely interrupt this thread and resume in next simulation step
    end
    sim.setJointTargetVelocity(joint,0)
end
Cheers

Post Reply