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

Typically: "How do I... ", "How can I... " questions
bsakal
Posts: 10
Joined: 29 Apr 2022, 00:34

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

Post by bsakal »

Hello,

I am a Robotic Student from Germany, and I am trying to control a servo by sending the angle data of a join in the simulation. I checked the Esplora.ttt and some other examples on the on the internet.

However, even though I achieved a communication between Arduino and Coppelia Sim. It doesn't seem that it send the correct data and the Servo doesn't respond it correctly. I am sharing my Arduino and Childscript Code below. It could be great if someone could guide me or show my mistake.


As I understood, the Coppelia works with radians for the angles and it sends the radian data to Arduino.

Thank you very much.

Child Script

Code: Select all

function sysCall_init()
    -- do some initialization here
cons=sim.auxiliaryConsoleOpen("Let's See",100,1)
   portString=[[\\.\COM8]]
   baudrate=9600
   serial=sim.serialOpen(portString,baudrate)
   joint=sim.getObject('/Revolute_joint') 
   a=120*math.pi/180
end

function sysCall_actuation()
    -- put your actuation code here
    sim.setJointTargetPosition(joint,a)
    p1=sim.getJointPosition(joint)
	res=sim.auxiliaryConsolePrint(cons,"\n")
	res=sim.auxiliaryConsolePrint(cons,p1)
    sim.setFloatSignal('poss',p1)
    ser=sim.serialSend(serial,p1)

    
end

function sysCall_sensing()
    -- put your sensing code here
end

function sysCall_cleanup()
    -- do some clean-up here
    sim.serialClose(serial)
    res=sim.auxiliaryConsoleClose(cons)

end
Arduino Code

Code: Select all

float data;
float pos=0;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
//while (!Serial);
}

void loop() {
  
while (Serial.available())
{
delay(3);
data = Serial.parseFloat();
pos= data*180/3.14159265359;
 myservo.write(pos);                  // sets the servo position according to the scaled value
 delay(3);                           // waits for the servo to get there

Serial.println(pos,DEC);
Serial.println(data,DEC);

}
}

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

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

Post by coppelia »

Hello,

it is difficult to say what is wrong in a situation that involves hardware. When you say "It doesn't seem that it send the correct data...", is there a way to verify this?
You have the possibility to use a socket inteface?

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 »

Since the Serial Port is busy by Simulation, I cannot see the data from the serial monitor of arduino.

What kind of data type is sent by ser=sim.serialSend(serial,p1) code? (HEX, DEC etc.)

Or is there another or an example I could follow in the forum? The most of I have seen was sending data from Arduino to Simulation but not the other way.

Thank you very much for your reply.

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 »

The issue is hardware is working, I can command Servo through serial monitor with the same code.
So only thing I want the simulation to do send the Angle data in Radian as 1, 1.2, 2.1 etc. But when I start the simulation it seems like it send something as data but the servo does not understand the angle.

That's why I am asking what kind of data it send. Maybe I should do a conversation of the data type at first.

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

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

Post by coppelia »

The serial port functions operate with binary data, i.e. buffers of bytes. Have a look at following example:

Code: Select all

local number=1.23456
sim.serialSend(serialPortHandle,sim.packFloatTable({number})) -- this effectively sends 4 bytes (a coded float)

sim.serialSend(serialPortHandle,tostring(number)) -- this effectively sends 7 bytes (the string "1.23456")
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 »

I tried to use the codes you shared but the result is the same.

I just found out by only blinking that the problem is the Arduino code side. If I use Serial.parseInt it doesn't work; however, if I use Serial.Read it does work. The difference is

ParseInt() reads as many characters as it can before it times out or hits a non-digit character. It will assemble those ascii characters into a number and convert from ascii and give you back an actual int.

read() just reads one character from the serial buffer and returns it (promoted to an int) or -1 if there's nothing to read. read will not convert anything from ascii so if you send a '1' from the serial monitor it will read 49 (the ascii code for '1').

I come from a mechanical engineering base, and I don't understand data types that much. I don't understand the exact problem here.

Now my only problem is I cannot read for example 45 degree, the Serial.read function reads it as 4 and 5 as 2 characters.
If someone could suggest me a way I would be really grateful.

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

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

Post by coppelia »

you can send 45 degreen in various ways, not sure what the Arduino expects:

Code: Select all

local number
if arduinoExpectsAnglesInDegrees then
    number=45
else
    number=45*math.pi/180
end

sim.serialSend(serialPortHandle,sim.packDoubleTable({number})) -- send as coded double
sim.serialSend(serialPortHandle,sim.packFloatTable({number})) -- send as coded float
sim.serialSend(serialPortHandle,sim.packInt32Table({number})) -- send as coded int32
sim.serialSend(serialPortHandle,sim.packUInt32Table({number})) -- send as coded unsigned int32
sim.serialSend(serialPortHandle,sim.packUInt16Table({number})) -- send as coded unsigned int16
sim.serialSend(serialPortHandle,sim.packUInt8Table({number})) -- send as coded unsigned int8
sim.serialSend(serialPortHandle,tostring(number)) -- send as string
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 »

Dear admin,

Thank you very much for trying to help me. I really appreciate your support.

I have 2 questions,

1- Do I need to write any other code to send the data as Int let's say. My code is such below

Code: Select all

function sysCall_init()
    cons=sim.auxiliaryConsoleOpen("Let's See",100,1)
	portString=[[\\.\COM8]]
	baudrate=9600
	serial=sim.serialOpen(portString,baudrate)
    h=sim.getObjectHandle('Cuboid')
end

function sysCall_actuation()
	local number
    number=45
    send=sim.serialSend(serial,sim.packInt32Table({number})) -- send as coded int32
	res=sim.auxiliaryConsolePrint(cons,"\n")
	res=sim.auxiliaryConsolePrint(cons,number)	
end

function sysCall_sensing()
    -- put your sensing code here
end

function sysCall_cleanup()
    -- do some clean-up here
    sim.serialClose(serial)
    res=sim.auxiliaryConsoleClose(cons)
end

It says the packint32 function "packs a table of int32 numbers into a string".
Do I need to unpack this table in the Arduino/C++ side? or does it directly send it as 45 as in code?

Or I cannot find it anymore on internet, is there way that I can find esplora.ttt example and the arduino code of it?

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

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

Post by coppelia »

You can find the deprecated model of the Arduino Esplora here

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 »

-Is it possible the find the Arduino code that control Esplora too?

I feel that I am having the problem with the Arduino Code.

-Another most probably a simple question is, each time a send a number from the serial port can I send a " '/n' " message such as:

Code: Select all

45
/n
143
/n
5
/n
Thanks a lot!

Post Reply