How can i use the SimulationTime for calculation ?

Typically: "How do I... ", "How can I... " questions
Post Reply
jianye
Posts: 46
Joined: 16 Oct 2019, 13:58

How can i use the SimulationTime for calculation ?

Post by jianye »

hello,

i want to use the SimulationTime for the calculation and i call the Function get.simSimulationTime().

the code as follows:

Code: Select all

    if distance_1 < 0.9 then
        time_1 = sim.getSimulationTime()
        print(time_1)
    end
    if distance_2 < 0.9 then
        time_2 = sim.getSimulationTime()
        print(time_2)
    end
    if time_1 - time_2 > 0 then
        sim.setStringSignal("SafetyStop","1")
    end
and i get the problem that: attempt to perform arithmetic on global 'time_1' (a nil value)
stack traceback

my question is: how can i tranform the data type of simulationtime that i can use it for calculation ?

thank you so much.

Jian

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

Re: How can i use the SimulationTime for calculation ?

Post by fferri »

time_1 is nil because the condition distance_1 < 0.9 is false.

Fix the logic of your program. For example check that both variables are not nil:

Code: Select all

if time_1 ~= nil and time_2 ~= nil and time_1 - time_2 > 0 then
    sim.setStringSignal("SafetyStop","1")
end

Post Reply