Script Crashing, Trying to rotate robot per orientation

Typically: "How do I... ", "How can I... " questions
Post Reply
hardluker
Posts: 1
Joined: 10 May 2024, 16:04

Script Crashing, Trying to rotate robot per orientation

Post by hardluker »

Hello,
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

fferri
Posts: 1255
Joined: 09 Sep 2013, 19:28

Re: Script Crashing, Trying to rotate robot per orientation

Post by fferri »

The problem is that you have a situation like:

Code: Select all

function sysCall_sensing()
    while ... do
        ...
    end
end
which can't possibly work, because code in sysCalls must not block. Your code doesn't return until a condition becomes true, which would happens several steps in the future, but simulation time can't progress until the sysCall returns.

You should change your logic to return immediately (e.g. keeping some global state variables), and check again in the next step if the condition has became true, or (much simpler) use a threaded script.

Post Reply