How to use sim.getScriptSimulationParameter and sim.setScriptSimulationParameter

Typically: "How do I... ", "How can I... " questions
Post Reply
e2718
Posts: 33
Joined: 15 Nov 2015, 05:39

How to use sim.getScriptSimulationParameter and sim.setScriptSimulationParameter

Post by e2718 »

Have been trying to do a simple test using the bubbleRob example in the tutorials folder. Have modified the source code in the bubbleRob to test setting and getting a test variable. Source code is below.

Code: Select all

function sysCall_init()
         
    -- This is executed exactly once, the first time this script is executed
    bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is bubbleRob's handle
    leftMotor=sim.getObjectHandle("bubbleRob_leftMotor") -- Handle of the left motor
    rightMotor=sim.getObjectHandle("bubbleRob_rightMotor") -- Handle of the right motor
    noseSensor=sim.getObjectHandle("bubbleRob_sensingNose") -- Handle of the proximity sensor
    minMaxSpeed={50*math.pi/180,300*math.pi/180} -- Min and max speeds for each motor
    backUntilTime=-1 -- Tells whether bubbleRob is in forward or backward mode
    -- Create the custom UI:
    xml = '<ui title="'..sim.getObjectName(bubbleRobBase)..' speed" closeable="false" resizeable="false" activate="false">'..[[
                <hslider minimum="0" maximum="100" on-change="speedChange_callback" id="1"/>
            <label text="" style="* {margin-left: 300px;}"/>
        </ui>
        ]]
    ui=simUI.create(xml)
    speed=(minMaxSpeed[1]+minMaxSpeed[2])*0.5
    simUI.setSliderValue(ui,1,100*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1]))
    
    ---------------------------------------------------------------------------
    -- this section is the test
    sim.setScriptSimulationParameter(sim.handle_self, "test_param", "test_value")
    test_value = sim.getScriptSimulationParameter(sim.handle_self, "test_param")
    print("the test_value should be test_value")
    print(test_value)
    ---------------------------------------------------------------------------

end
function speedChange_callback(ui,id,newVal)
    speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
end


function sysCall_actuation() 
    result=sim.readProximitySensor(noseSensor) -- Read the proximity sensor
    -- If we detected something, we set the backward mode:
    if (result>0) then backUntilTime=sim.getSimulationTime()+4 end 

    if (backUntilTime<sim.getSimulationTime()) then
        -- When in forward mode, we simply move forward at the desired speed
        sim.setJointTargetVelocity(leftMotor,speed)
        sim.setJointTargetVelocity(rightMotor,speed)
    else
        -- When in backward mode, we simply backup in a curve at reduced speed
        sim.setJointTargetVelocity(leftMotor,-speed/2)
        sim.setJointTargetVelocity(rightMotor,-speed/8)
    end
end

function sysCall_cleanup() 
    simUI.destroy(ui)
end 
I expect that when this runs it will print the value of the test_param (test_value). However, the value is nil, can somebody please explain what I am doing wrong and provide a fix? Thanks

Printed output:

Code: Select all

Warning: 'print()' now prints to the status bar, instead of the console (use 'print=printToConsole' to revert).
the test_value should be test_value


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

Re: How to use sim.getScriptSimulationParameter and sim.setScriptSimulationParameter

Post by coppelia »

Hello,

your script simulation parameter needs to be already in the script simulation parameter list. You can't create it programmatically.

Cheers

Post Reply