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
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);
}
}