Car with force sensor simulation

Typically: "How do I... ", "How can I... " questions
Post Reply
bal_my
Posts: 3
Joined: 22 Jun 2024, 11:07

Car with force sensor simulation

Post 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

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: Car with force sensor simulation

Post 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

bal_my
Posts: 3
Joined: 22 Jun 2024, 11:07

Re: Car with force sensor simulation

Post 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.

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: Car with force sensor simulation

Post by coppelia »

Make sure to carefully read this page about accessing objects from code.

Cheers

bal_my
Posts: 3
Joined: 22 Jun 2024, 11:07

Re: Car with force sensor simulation

Post 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?

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: Car with force sensor simulation

Post 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

Post Reply