Hello, I’m developing a project, and although I already made some progress I don’t know If I’m going in the right direction.
So the project is simple, I have a log file with the position values that I want my servo motors (mg995) to ‘read’ and move accordingly at the rate of 1/8khz (0.008 seconds)
So far I’ve made a serial port connection and with a python shield I’m able to read the file and send it to the Arduino
In the Arduino I have to process the value and give a little adjust, and then write to the servos.
My difficulties right now is to make the motors act in ‘real time’. What is happening is that the Arduino is not reading the values at the same time that the python is sending it to him.
I want it to move at the speed of 0.008 seconds, but when I send the value at that speed the motors don-t even move, I have to slow it down to 0.8 seconds minimum so they can move accordingly .
I’m using a time.sleep() function in python to make the interval between the loop.
So, is my approach good or should I do it in some other way? what is blocking the speed of communication/reaction between reading the values and the servos movement ?
bellow is a sample of what i’m using to read and move the ‘X’ motor, although the idea is to do this for all 3 motors at the same time.
A sample of my arduino code to move ‘X’ motor
void loop()
void serialEvent()
{
while(Serial.available()) {
char ch = Serial.read();
Serial.write(ch);
if(index < MaxChars && ch != ",") {
strValue[index++] = ch;
} else {
strValue[index] = 0;
newAngle=atoi(strValue)+90;
X.write(newAngle);
index = 0;
angle = newAngle;
angleX = newAngle;
value = 0;
}
}
a Sample of The Python code to read the file and send the values to arduino thru the serial port
file = open('Xvalues.txt')
while 1:
line = file.readline()
if not line:
break
ser.write(str.encode(line))
time.sleep(0.8)
file.close
A sample of the file with the values for the X motor
22,
22,
22,
23,
24,
24,
24,
24,
25,
thank you all