Page 1 of 1

Car with force sensor simulation

Posted: 23 Jun 2024, 13:13
by bal_my
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

Re: Car with force sensor simulation

Posted: 24 Jun 2024, 06:09
by coppelia
Hello,

difficult to say without seeing the scene. What are the readings of the force sensor in the first few simulation steps? Also, your car is not supposed to drive backwards, only forwards or turn. If it runs backwards then maybe your joints are mounted in reverse, and you should feed them negative velocities instead?

Cheers

Re: Car with force sensor simulation

Posted: 24 Jun 2024, 19:37
by bal_my
Thanks for the reply, i have figure out the problem regarding backward movement of the car. However, how do i set up the force sensor to make contact with obstacle? As i run the simulation, it stopped and says:
module 'sim' was implicitly loaded.
[/Body@childScript:error] 1308: in sim._getObject: object does not exist, or alias/path is ill formatted.
stack traceback:
[C]: in function 'sim._getObject'
...rogram Files/CoppeliaRobotics/CoppeliaSimEdu/lua/sim.lua:1308: in function 'sim.getObject'
[string "/Body@childScript"]:3: in function 'sysCall_init'
[sandboxScript:info] Simulation suspended.

Here is the link to the scene (https://drive.google.com/drive/folders/ ... sp=sharing). Thanks in advance.

Re: Car with force sensor simulation

Posted: 25 Jun 2024, 06:17
by coppelia
Make sure to carefully read this page about accessing objects from code.

Cheers

Re: Car with force sensor simulation

Posted: 01 Jul 2024, 20:10
by bal_my
Thanks for the answer earlier, i want to insert graph for force and torque however i could not find some graph option that i can choose. Any solution?

Re: Car with force sensor simulation

Posted: 05 Jul 2024, 12:29
by coppelia
If you want to add a graph and plot a curve, have a look at the graph section in the user manual.

Cheers