## Set random direction to avoid other ePucks on the scene

Typically: "How do I... ", "How can I... " questions
DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

### Set random direction to avoid other ePucks on the scene

Hello,
I am coding a scene with two ePucks (the next step is going to have more ePucks) and the main purpose is the following:

When the ePucks are going to be closer than a certain distance (threshold has been fixed before) they have to change direction: in my alghoritm they turn of 90 degrees and then go streight along the new direction.
I would like that the direction of turning is going to be random and to do that I am using the following code:

ePuck One:

Code: Select all

local m=simGetObjectMatrix(ePuck,-1)
local m2=simBuildMatrix({0,0,0},{0,90*math.pi/180,90*math.pi/180})
m=simMultiplyMatrices(m,m2)
local e=simGetEulerAnglesFromMatrix(m)
p = simGetObjectPosition(ePuck,-1)
p1 = simGetObjectPosition(ePuck1,-1)

a = (p[1] - p1[1])^2
b = (p[2] - p1[2])^2
d = math.sqrt(a + b)

simWriteCustomDataBlock(ePuck1,'position',simPackFloatTable({d}))

rand1 = math.random(-1,1)
rand2 = math.random(-1,1)

if (math.sign(rand1)== math.sign(rand2)) then
rand1 = -rand1
end

if ( d < 0.3 ) then
while (e[3] >= 0) and (e[3] < math.pi/2) do
simSetJointTargetVelocity(leftMotor,maxVel*rand1)
simSetJointTargetVelocity(rightMotor,maxVel*rand2)
local m=simGetObjectMatrix(ePuck,-1)
local m2=simBuildMatrix({0,0,0},{0,90*math.pi/180,90*math.pi/180})
m=simMultiplyMatrices(m,m2)
e=simGetEulerAnglesFromMatrix(m)
end
end


ePuck Two:

Code: Select all

local m=simGetObjectMatrix(ePuck1,-1)
local m2=simBuildMatrix({0,0,0},{0,90*math.pi/180,90*math.pi/180})
m=simMultiplyMatrices(m,m2)
local e=simGetEulerAnglesFromMatrix(m)

local d = simUnpackFloatTable(data)

rand1 = math.random(-1,1)
rand2 = math.random(-1,1)

if (math.sign(rand1)== math.sign(rand2)) then
rand1 = -rand1
end

if (d[1] < 0.3) then
while (e[3] >=-math.pi ) and (e[3] <=-math.pi/2) do
simSetJointTargetVelocity(leftMotor,maxVel*rand1)
simSetJointTargetVelocity(rightMotor,maxVel*rand2)
local m=simGetObjectMatrix(ePuck1,-1)
local m2=simBuildMatrix({0,0,0},{0,90*math.pi/180,90*math.pi/180})
m=simMultiplyMatrices(m,m2)
e=simGetEulerAnglesFromMatrix(m)
end
end

The problem is that the statement:

Code: Select all

 if (math.sign(rand1)== math.sign(rand2)) then
rand1 = -rand1
end

Doesn't work as desired, the error returned is the following:

Lua runtime error: [string "SCRIPT ePuck#0"]:67: attempt to call field 'sign' (a nil value)

The purpose of this statement is to not allow the variables maxVel*rand1 and maxVel*rand2 to have the same sign otherwise the ePuck doesn't turn.

Can anyone explain to me how to write the code with the purpose explained above without retrive error?

Davide

DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

### Re: Set random direction to avoid other ePucks on the scene

Fix it.

Code: Select all

rand1 = math.random(-1,1)
rand2 = math.random(-1,1)

if ((rand1 > 0) and (rand2 > 0)) or ((rand1 < 0) and (rand2 < 0)) then
rand1 = -rand1
end