Data visualization/outputData in CoppeliaSim can be visualized or output/logged in various ways: Following example illustrates how to display a joint angle in a simple custom user interface: #python
import math
def sysCall_init():
sim = require('sim')
simUI = require('simUI')
self.jointHandle = sim.getObject('/Joint')
xml = """
<ui title="Example" closeable="false" layout="form">
<label text="joint angle:" />
<label id="1" text="-" />
</ui>
"""
self.ui = simUI.create(xml)
def sysCall_sensing():
v = 180 * sim.getJointPosition(self.jointHandle) / math.pi
simUI.setLabelText(self.ui, "1", '{:.3f}'.format(v))
def sysCall_cleanup():
simUI.destroy(self.ui)
--lua
function sysCall_init()
sim = require('sim')
simUI = require('simUI')
jointHandle = sim.getObject('/Joint')
local xml =[[<ui title="Example" closeable="false" layout="form">
<label text="joint angle:" />
<label id="1" text="-" />
</ui>]]
ui = simUI.create(xml)
end
function sysCall_sensing()
local v = 180 * sim.getJointPosition(jointHandle) / math.pi
simUI.setLabelText(ui, 1, string.format('%.3f', v))
end
function sysCall_cleanup()
simUI.destroy(ui)
end
Augmenting the scene with visual itemsSimple visual items such as points, lines, spheres or individual triangles can easily be added to the scene via API functions sim.addDrawingObject and sim.addDrawingObjectItem, for example: #python
import math
def sysCall_init():
sim = require('sim')
drawingObject = sim.addDrawingObject(sim.drawing_spherepts, 0.005, 0, -1, 1000, [1, 0, 0])
for i in range(360):
coord = [math.cos(i * math.pi / 180), math.sin(i * math.pi / 180), 0.01]
sim.addDrawingObjectItem(drawingObject, coord)
--lua
function sysCall_init()
sim = require('sim')
local drawingObject = sim.addDrawingObject(sim.drawing_spherepts, 0.005, 0, -1, 1000, {1, 0, 0})
for i = 0, 359, 1 do
local coord = {math.cos(i * math.pi / 180), math.sin(i * math.pi / 180), 0.01}
sim.addDrawingObjectItem(drawingObject, coord)
end
end
More complex items, such as random meshes can be created on the fly via sim.loadModel, simAssimp.importShapes, sim.createPrimitiveShape, sim.createShape and/or sim.copyPasteObjects. To avoid surcharching the scene with too many additional objects, make sure to group them with sim.groupShapes. For example: #python
import math
def sysCall_init():
sim = require('sim')
individualShapes = []
for i in range(360):
shape = sim.createPrimitiveShape(sim.primitiveshape_cylinder, [0.005, 0.005, 0.02])
coord = [math.cos(math.radians(i)), math.sin(math.radians(i)), 0.01]
sim.setObjectPosition(shape, coord)
sim.setShapeColor(shape, None, sim.colorcomponent_ambient_diffuse, [1, 0, 0])
individualShapes.append(shape)
# Group individual shapes:
masterShape = sim.groupShapes(individualShapes, True)
# Make it invisible to collision detection, sensors, etc.:
sim.setObjectSpecialProperty(masterShape, 0)
--lua
function sysCall_init()
sim = require('sim')
local individualShapes = {}
for i = 0, 359, 1 do
local shape = sim.createPrimitiveShape(sim.primitiveshape_cylinder, {0.005, 0.005, 0.02})
local coord = {math.cos(i * math.pi / 180), math.sin(i * math.pi / 180), 0.01}
sim.setObjectPosition(shape, coord)
sim.setShapeColor(shape, nil, sim.colorcomponent_ambient_diffuse, {1, 0, 0})
individualShapes[i + 1] = shape
end
-- Group individual shapes:
local masterShape = sim.groupShapes(individualShapes, true)
-- Make it invisible to collision detection, sensors, etc.:
sim.setObjectSpecialProperty(masterShape, 0)
end
Logging data to a fileFollowing example illustrates how to log a joint angle to a file: #python
import math
def sysCall_init():
sim = require('sim')
self.jointHandle = sim.getObject('/Joint')
self.file = open('jointAngles.txt', 'w+')
self.file.write('Joint angles for each simulation step:\n\n')
def sysCall_sensing():
v = 180 * sim.getJointPosition(self.jointHandle) / math.pi
self.file.write('time: {:.3f} [s]'.format(sim.getSimulationTime() + sim.getSimulationTimeStep()))
self.file.write(', joint angle: {:.1f} [deg]\n'.format(v))
def sysCall_cleanup():
self.file.close()
--lua
function sysCall_init()
sim = require('sim')
jointHandle = sim.getObject('/Joint')
file = io.open('jointAngles.txt', 'w+')
file:write('Joint angles for each simulation step:\n\n')
end
function sysCall_sensing()
local v = 180 * sim.getJointPosition(jointHandle) / math.pi
file:write(string.format('time: %.3f [s]', sim.getSimulationTime() + sim.getSimulationTimeStep()))
file:write(string.format(', joint angle: %.1f [deg]\n', v))
end
function sysCall_cleanup()
file:close()
end
|