Displaying trajectory

Typically: "How do I... ", "How can I... " questions
Post Reply
TomDelaney
Posts: 22
Joined: 31 Jul 2022, 19:48

Displaying trajectory

Post by TomDelaney »

Hello,

I am trying to show a trajectory and 2 other effected trajectories in coppeliasim. Therefore I want to just draw lines connecting the points inside of each of these trajectories. The 3 trajectories are stored in text files with each row having the points x,y and z coordinates.

The code of the script is as follows:

Code: Select all

function readCSV(filename)
    local data = {}  -- Initialize the vector of vectors
    
    file = io.open(filename, "r")  -- Open the file in read mode
    
    if not file then
        print("Error opening file: " .. filename)
        return nil
    end
    
      -- Iterate through the lines of the file
    for line in file:lines() do

        -- Use string.gmatch to iterate through elements separated by ","
        for element in string.gmatch(line, "([^,]+)") do
            table.insert(data, tonumber(element))  -- Assuming the elements are numbers, convert and add to the table
        end
    end

    file:close()  -- Close the file

    return data
end

function sysCall_init()
    -- do some initialization here
    simRemoteApi.start(19999)
    
    -- variables for lines
    line_size = 5
    
    Imu_trace = sim.addDrawingObject(sim.drawing_points, line_size, 0 ,-1, 1000, {0,0,0})
    Tremor_trace = sim.addDrawingObject(sim.drawing_points, line_size, 0 ,-1, 1000, {1,0,0})
    Suppression_trace = sim.addDrawingObject(sim.drawing_points, line_size, 0 ,-1, 1000, {0,0,1})
    
    -- load and iterate through the text file positions
    --load
    file_name_imu_pos = "positions/end_effector_positions_0.txt"
    file_name_tremor_pos = "positions/tremor_cause_positions_0.txt"
    file_name_sup_pos = "positions/suppressed_positions_0.txt"
    
    Imu_pos = readCSV(file_name_imu_pos)
    tremor_pos = readCSV(file_name_tremor_pos)
    sup_pos = readCSV(file_name_sup_pos)
            
    sim.addDrawingObjectItem(Imu_trace, Imu_pos)
    sim.addDrawingObjectItem(Tremor_trace, tremor_pos)
    sim.addDrawingObjectItem(Suppression_trace, sup_pos)
    
    -- move the arm to that position
    
    
end

function sysCall_actuation()
    -- put your actuation code here
end

function sysCall_sensing()
    -- put your sensing code here
end

function sysCall_cleanup()
    -- do some clean-up here
end


The code runs without any issues and the the values are all accurately read into the data vector, but the lines are still not drawing. What is the issue and how to fix this?

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

Re: Displaying trajectory

Post by coppelia »

Hello,

to add multiple drawing objects at once, combine the drawing object handle with sim.handleflag_addmultiple, when calling sim.addDrawingObjectItem

cheers

TomDelaney
Posts: 22
Joined: 31 Jul 2022, 19:48

Re: Displaying trajectory

Post by TomDelaney »

Hi,

Could you provide your answer in code, because the following combining of the flags with sim.addDrawingObjectItem, still yields no results.

Code: Select all

 sim.addDrawingObjectItem(Imu_trace|sim.handleflag_addmultiple, Imu_pos)
 sim.addDrawingObjectItem(Tremor_trace|sim.handleflag_addmultiple, tremor_pos)
 sim.addDrawingObjectItem(Suppression_trace|sim.handleflag_addmultiple, sup_pos)
Thanks!

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

Re: Displaying trajectory

Post by coppelia »

Your code now looks good. Are you sure you have anything in Imu_pos, tremor_pos or sup_pos? Try printing those values with a simple print, and show us the output of those prints.

Cheers

TomDelaney
Posts: 22
Joined: 31 Jul 2022, 19:48

Re: Displaying trajectory

Post by TomDelaney »

Here you go these are the output values for one of the position vectors:

"Imu pos",{2.2697763705065e-06, -0.39997769523957, -0.40001975319722, 3.7415501802182e-06, -0.39996260077727, -0.40002976536356, 3.3556498339746e-06, -0.39994593469643, -0.4000485260661, 2.9298789208755e-06, -0.39992699703493, -0.40006846543452, 2.288649854187e-06, -0.3999046357435, -0.40009402326368, 1.4120852052005e-06, -0.39988048887304, -0.40011947542786, 6.1228725160156e-07, -0.39985312159594, -0.40014682449022, -4.0557926097627e-07, -0.39982494190562, -0.40017498151434, -1.4166747416489e-06, -0.39979534578117, -0.40020454956149, -2.6642423069999e-06, -0.39976196971849, -0.40023788871082, -4.2614123462482e-06, -0.39972392561534, -0.40027588395069, -6.0446342297211e-06, -0.39968577537966, -0.4003139779256, -7.9357905149242e-06, -0.39964596611683, -0.40035372073152, -1.0023724321627e-05, -0.39960396452215, -0.40039564362978, -1.2528279223562e-05, -0.39955662154278, -0.40044288734395, -1.4860027377719e-05, -0.39951011694652, -0.40048928354771, -1.7557168148939e-05, -0.39946106867484, -0.40053820579927, -2.0691895611553e-05, -0.39940584461083, -0.40059327361122, -2.3816895086354e-05, -0.39934823400219, -0.40065070501884, -2.6544097580223e-05, -0.39929270755033, -0.40070604311956, -3.0320083363615e-05, -0.39922828000787, -0.40076650024846, -3.3754778083632e-05, -0.39915847543553, -0.40082869551566, -3.7443003412992e-05, -0.39908783195744, -0.40089276272528, -4.1180427810595e-05, -0.39901545385701, -0.40096081571882, -4.5195525475348e-05, -0.39893994750151, -0.40103307279468, -4.9478057453103e-05, -0.39886686481303, -0.4011021216596, -5.3559611996152e-05, -0.39879024225685, -0.40117267781804, -5.7121676399845e-05, -0.39870941015901, -0.4012490530353, -5.9771114708994e-05, -0.39863454601757, -0.40132503449217, -6.316564937478e-05, -0.39855843056453, -0.4014001006135, -6.6842726536468e-05, -0.39847940788666, -0.40147807318353, -6.9299340221108e-05, -0.39840059385101, -0.40155751476004, -7.0470075997221e-05, -0.39832229423704, -0.40163949605334, -7.295316650171e-05, -0.3982401217866, -0.40172126860535, -7.6111868949462e-05, -0.39815930141639, -0.40180249681522, -7.6686559326815e-05, -0.39807567933084, -0.40187822960043, -7.8646009954942e-05, -0.39798977366673, -0.40195858697492, -7.826063204059e-05, -0.39790212072462, -0.40204454133063, -7.8338923412042e-05, -0.39781199697201, -0.40213730979099, -7.8302320127503e-05, -0.39772041731108, -0.40222966669289, -7.8691654047566e-05, -0.39762858338898, -0.40232108406192, -8.0300360136961e-05, -0.39753646573071, -0.40241682326658, -8.0264819268224e-05, -0.39744260870532, -0.40251057573722, -8.0946810940511e-05, -0.39734695261725, -0.40260223018897, -8.1100840256395e-05, -0.39725113077743, -0.4026956038686, -8.0818003305534e-05, -0.39715765145866, -0.40279055923205, -7.926226842598e-05, -0.39705760958951, -0.40288769280508, -7.8341553406219e-05, -0.39695977335228, -0.40298173924985, -7.6848913325068e-05, -0.39686107937018, -0.40307941165985, -7.5402634424146e-05, -0.39676253735097, -0.40317735874633, -7.4200242661184e-05, -0.39665959016209, -0.40327598181294, -7.2493525881973e-05, -0.39656222736219, -0.40337113395567, -7.0779073257552e-05, -0.39645904127146, -0.40346537538373, -6.6301870707767e-05, -0.39635680035236, -0.40356360104871, -6.07062620059e-05, -0.3962522434592, -0.40366675028021, -5.6266891377496e-05, -0.39614865537178, -0.40376855715324, -5.3092773056657e-05, -0.39604884063512, -0.40386831403702, -4.8089840458094e-05, -0.39594539431106, -0.40396869724706, -4.2729007297167e-05, -0.39584287938386, -0.40406574466271, -4.0776917085249e-05, -0.39574773519194, -0.40416128005387, -3.7988679245571e-05, -0.3956515031123, -0.40425778713973, -3.359522352304e-05, -0.3955523806109, -0.40435402865759, -2.9555105226366e-05, -0.39545044296173, -0.40445021037332, -2.5626522844438e-05, -0.39534900721468, -0.4045496902698, -2.0317063456227e-05, -0.39524653002292, -0.40465001947741, -1.582286048604e-05, -0.39514374263839, -0.40474988409168, -1.0596446680598e-05, -0.39504577499728, -0.4048442650238, -5.9738437337143e-06, -0.3949443767216, -0.40493900517365, 2.070187242072e-07, -0.3948427425654, -0.40503773197719, 7.0828306880727e-06, -0.39474685889794, -0.40512697137225, 1.1917805538402e-05, -0.39464846191033, -0.40522033206241, 1.8054285649392e-05, -0.39455263940919, -0.40531329649844, 2.5479834259654e-05, -0.39445616459235, -0.40540246292269, 3.0718683422854e-05, -0.39436218322763, -0.40549284007607, 3.70466702692e-05, -0.39426961706484, -0.4055798987788, 4.3955326976209e-05, -0.39417413061726, -0.40566991825061, 5.0787258240174e-05, -0.39408285806359, -0.40575666682898, 5.747577936517e-05, -0.39399044739129, -0.4058422876567, 6.5602516688799e-05, -0.39389833691058, -0.40592890020358, 7.2351671184931e-05, -0.39380934499591, -0.40601044849337, 7.6948281978696e-05, -0.39372200324134, -0.40609157713557, 8.5404563417707e-05, -0.393634693033, -0.40617430243278, 9.4569937722709e-05, -0.39354805884083, -0.40625536935973, 0.00010128775988293, -0.39346096511473, -0.40633525767176, 0.00010838001062334, -0.39337579508756, -0.40641473358258, 0.00011665101547422, -0.39329159021005, -0.40649300158708, 0.0001248036351801, -0.39321127849459, -0.40657002266725, 0.00013322447241886, -0.39312840107335, -0.40664441916125, 0.00014054258916218, -0.39304812300228, -0.40671953405361, 0.00015062403685808, -0.39297073775059, -0.40679191289827, 0.00016259214978788, -0.39289401374016, -0.4068641437991, 0.00017504864295541, -0.39281343763835, -0.40694015728859, 0.00018458840426052, -0.39273760082314, -0.40701016037467, 0.00018846670194899, -0.3926660850125, -0.40707709067826, 0.00019586877363258, -0.39259027313807, -0.40714344453138, 0.0002085931620026, -0.39251650450329, -0.40721565028398, 0.00021734982218297, -0.39244498220986, -0.40728173996193, 0.00022698155329477, -0.39237261583663, -0.40734924224988, 0.0002383261217138, -0.39230291479104, -0.4074132335017, 0.00024593432261459, -0.3922382079175, -0.40747583057461}

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

Re: Displaying trajectory

Post by coppelia »

All your 100 points lie very close to each other, and on a line. Try to display random points with coordinates between 0 and 1:

Code: Select all

-- lua

function sysCall_init()
    sim = require('sim')
    line_size = 5
    Imu_trace = sim.addDrawingObject(sim.drawing_points, line_size, 0 ,-1, 1000, {0,0,0})
    local Imu_pos = {}
    for i=1, 300, 1 do
        Imu_pos[#Imu_pos+1] = math.random()
    end
    sim.addDrawingObjectItem(Imu_trace|sim.handleflag_addmultiple, Imu_pos)
end
Cheers

TomDelaney
Posts: 22
Joined: 31 Jul 2022, 19:48

Re: Displaying trajectory

Post by TomDelaney »

Thank you got it, just had to reshape the vector to contain three long elements representing the x,y and z coordinates. Thank you for your help!

Post Reply