Code: Select all
function sysCall_init()
-- Get handles for the wheels
leftMotor = sim.getObjectHandle("FrontLeftWheel")
rightMotor = sim.getObjectHandle("FrontRightWheel")
-- Get handle for the force sensor
forceSensor = sim.getObjectHandle("ForceSensor")
-- Initialize sensor reading mode
sim.readForceSensor(forceSensor)
-- Movement parameters
forwardVelocity = 2 -- speed for moving forward
turnVelocity = 1 -- speed for turning
end
function sysCall_actuation()
-- Read the force sensor
result, forceVector, _ = sim.readForceSensor(forceSensor)
-- Check if an object is detected (using a threshold for the force value)
objectDetected = result == 1 and forceVector[3] > 10 -- Adjust the threshold as necessary
if objectDetected then
-- Object detected, turn the car
sim.setJointTargetVelocity(FrontLeftWheel, -turnVelocity)
sim.setJointTargetVelocity(FrontRightWheel, turnVelocity)
else
-- No object detected, move forward
sim.setJointTargetVelocity(leftMotor, forwardVelocity)
sim.setJointTargetVelocity(rightMotor, forwardVelocity)
end
end