Car with force sensor simulation
Posted: 23 Jun 2024, 13:13
Hello, i intended to create a car attached with a force sensor to detect obstacle then change its direction. When running simulation, the force sensor did not make any contact and the car was moving backwards. What is the issue? This is the Lua code that I used:
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