I am trying to rotate my robot until the orientation value reaches a certain value.
However, the program just freezes and I have to kill the script.
The robot never starts turning even though the rotateClockwise() function is working just fine.
The issue seems to be in the turnRight() function and the while loop.
Here is the script
Code: Select all
-- Imports for utilizing simulation-related functions
sim=require'sim'
simUI=require'simUI'
-- Initialization function upon creation of the object
function sysCall_init()
-- Defining the parts of the bot
disasterBotBase=sim.getObject('.')
leftMotor=sim.getObject("./leftMotor")
rightMotor=sim.getObject("./rightMotor")
noseSensor=sim.getObject("./sensingNose")
-- Defining the X,Y,Z coords of the bot
botPos=sim.getObjectPosition(disasterBotBase)
botPosX=botPos[1]
botPosY=botPos[2]
botPosZ=botPos[3]
-- Creating an array of people sensors
peopleSensors = {}
peopleSensors[1] = sim.getObject("./peopleSensor[0]")
peopleSensors[2] = sim.getObject("./peopleSensor[1]")
-- Creating an array for the human found flag initializing all to false
humanFound = {}
for i = 1, #peopleSensors do
humanFound[i] = false
end
-- Declaring backup timer and a backup amount
backupTimer=0
backupAmount=6
-- Speed of the robot
speed=6
end
-- Function for defining sensing logic
-- This function runs continually throughout the simulation
function sysCall_sensing()
turnRight()
print(getOrientation())
detectHumans()
end
-- Function for detecting humans with the people sensors
function detectHumans()
-- Iterating through the sensors checking if humans are found.
for i = 1, #peopleSensors do
-- Gathering further info from peopleSensor to get the obj that is detected.
res, dist, point, obj, n = sim.readProximitySensor(peopleSensors[i])
if (res > 0 and not humanFound[i]) then
objAlias = sim.getObjectAlias(obj)
if (objAlias == "human") then
humanFound[i] = true
print(string.format("Human Found! Sensor: %d", i))
end
elseif (res <= 0) then
humanFound[i] = false
end
end
end
-- Function for moving the robot forward
function moveForward()
sim.setJointTargetVelocity(leftMotor,speed)
sim.setJointTargetVelocity(rightMotor,speed)
end
--Function for turning the robot to the right
function turnRight()
orient=getOrientation()
while orient ~= -1.5707 do
rotateClockwise()
orient=getOrientation()
end
end
-- Function to rotate robot clockwise.
function rotateClockwise()
sim.setJointTargetVelocity(leftMotor,speed)
sim.setJointTargetVelocity(rightMotor,-speed)
end
-- Function to rotate the robot counter-clockwise
function rotateCounterClockwise()
sim.setJointTargetVelocity(leftMotor,-speed)
sim.setJointTargetVelocity(rightMotor,speed)
end
-- Function for returning the rotation degree of the robot.
function getOrientation()
orient=sim.getObjectOrientation(disasterBotBase)
return orient[3]
end