Control A Servo through Arduino by controling a joint in Coppelia Sim

Typically: "How do I... ", "How can I... " questions
coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by coppelia »

if your client is expecting a number in text format, then simply send, e.g. "45.0\n"

Unfortunately we do not have the corresponding Arduino code...

Cheers

bsakal
Posts: 10
Joined: 29 Apr 2022, 00:34

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by bsakal »

Eureka! That works, thanks a lot.

But now I want to send the joint position, p1=sim.getJointPosition(joint), and how can I add this '/n' message with this variable?

Also for this code

Code: Select all

sim.serialSend(serialPortHandle,sim.packInt32Table({number})) -- send as coded int32 
is there tutorial or example that I can follow to understand how it works?

Thank you very much for your time.
Sincerely,

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

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by coppelia »

Then your client must be accepting angles in degrees, as strings.

So you can do, e.g.:

Code: Select all

local stringToSend=tostring(sim.getJointPosition(joint))..'\n'
Cheers

bsakal
Posts: 10
Joined: 29 Apr 2022, 00:34

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by bsakal »

Thanks a lot, once again everything works now. However, simulation send data so fast that the Arduino bottlenecks and gets unstable also the servo cannot reach that fast.

Is there a way to slow down the data send speed or maybe something like delay(500) to delay data send after each one.
Such as

Send Data
wait 0,5 secs
Send Data
wait 0,5 secs

Also, for this kind of codes should use a threaded script instead of the non-threaded?

Sincerely,
BS

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

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by coppelia »

With a threaded script, you can use sim.wait.
With a non-threaded script, simply count how often you've sent data, and skip sensing if too frequent, e.g.:

Code: Select all

function sysCall_init()
    lastTime=sim.getSystemTimeInMs(-1)
end

function sysCall_actuation()
    currentTime=sim.getSystemTimeInMs(-1)
    if currentTime-lastTime>500 then

        ...send data here...

        lastTime=currentTime
    end
end
Cheers

bsakal
Posts: 10
Joined: 29 Apr 2022, 00:34

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by bsakal »

Hello Mr. Admin,

Thank you very much for your contribution, your suggestions work like magic.

If a student would have a similar situation like mine - I want to write some hints.
AS I understood Coppelia Sim with basic serialsend code send to Arduino as chars and here this example 4 code works great with the task. It takes it and converts into integer.

I was not able to work with

Code: Select all

sim.serialSend(serialPortHandle,sim.packInt32Table({number})) -- send as coded int32
code, I will try to do it later on.

bsakal
Posts: 10
Joined: 29 Apr 2022, 00:34

Re: Control A Servo through Arduino by controling a joint in Coppelia Sim

Post by bsakal »

Another question from me is:

I need to control several actuators from Simulation and I wonder

-How can I send several serial messages with maybe different headings so that Arduino can understand which code is which code? How can I achieve this, could you please guide me?

Post Reply