Distance calculationCoppeliaSim can measure the minimum distance between two measurable entities in a flexible way. The calculation is an exact minimum distance calculation. [Minimum distance calculation between two manipulators] Using the API function sim.checkDistance, one can easily calculate the minimum distance between entities, for instance between a robot and its environment, from within a simulation script, in each simulation step: #python
def sysCall_init():
sim = require('sim')
robotBase = sim.getObject('/robotModelAlias')
self.robotCollection = sim.createCollection(0)
sim.addItemToCollection(self.robotCollection, sim.handle_tree, robotBase, 0)
def sysCall_sensing():
result, distData, objectPair = sim.checkDistance(self.robotCollection, sim.handle_all)
if result > 0:
txt = 'Robot clearance is ' + str(distData[6]) + 'm, minimum distance object pair is ' + str(objectPair)
print(txt)
--lua
function sysCall_init()
sim = require('sim')
local robotBase = sim.getObject('/robotModelAlias')
robotCollection = sim.createCollection(0)
sim.addItemToCollection(robotCollection, sim.handle_tree, robotBase, 0)
end
function sysCall_sensing()
local result, distData, objectPair = sim.checkDistance(robotCollection, sim.handle_all)
if result > 0 then
local txt = 'Robot clearance is '..distData[7]
txt = txt..'m, minimum distance object pair is '..getAsString(objectPair)
print(txt)
end
end
One can also display the measured minimum distance, as a colored segment in the scene: #python
def sysCall_init():
sim = require('sim')
robotBase = sim.getObject('/robotModelAlias')
self.robotCollection = sim.createCollection(0)
sim.addItemToCollection(self.robotCollection, sim.handle_tree, robotBase, 0)
self.distanceSegment = sim.addDrawingObject(sim.drawing_lines, 4, 0, -1, 1, [0, 1, 0])
def sysCall_sensing():
result, distData, objectPair = sim.checkDistance(self.robotCollection, sim.handle_all)
if result > 0:
sim.addDrawingObjectItem(self.distanceSegment, None)
sim.addDrawingObjectItem(self.distanceSegment, distData)
--lua
function sysCall_init()
sim = require('sim')
local robotBase = sim.getObject('/robotModelAlias')
robotCollection = sim.createCollection(0)
sim.addItemToCollection(robotCollection, sim.handle_tree, robotBase, 0)
distanceSegment = sim.addDrawingObject(sim.drawing_lines, 4, 0, -1, 1, {0, 1, 0})
end
function sysCall_sensing()
local result, distData, objectPair = sim.checkDistance(robotCollection, sim.handle_all)
if result > 0 then
sim.addDrawingObjectItem(distanceSegment, nil)
sim.addDrawingObjectItem(distanceSegment, distData)
end
end
CoppeliaSim's distance calculation functionality is also available as stand-alone routines via the Coppelia geometric routines. |