I'm working on a robot, more precisely on his head which have to track an human face.
I'm using a RPI which will detect the face and then send to the Arduino the angles for both servomotors.
I wrote a python script which will take 2 parameters and send them to the Arduino, this script will open the serial connection and send the command.
python script:
#!/usr/bin/env python
import serial
import time
import sys
ser = serial.Serial("/dev/ttyACM0", 9600)
time.sleep(2)
coords = [str(sys.argv[1]), str(sys.argv[2])]
commande = 'X' + coords[0] + 'Y' + coords[1] + 'V'
ser.write(commande)
ser.close()
The control of servos is working well on the Arduino but the problem is that each time I execute my script, the opening of the serial connection firstly reset both servos and once the Arduino receive the command both servos will go to the correct angles.
Arduino sketch:
//Bibliothèque :
#include <Servo.h> //Ajoute la librairie Servo
Servo servo1; //servo 180°
Servo servo2; //servo 360°
int pos1 = 0; //Position initiale du servo1
int pos2 = 0; //Position initiale du servo2
int maxHaut = 90;
int maxBas = -35;
int maxGauche = 90;
int maxDroite = -85;
/**
* Déclarations variables commandes
*/
String commande = "";
int coords[] = {0,0};
int newCoords[] = {0,0};
int indexX = 0;
int indexY = 0;
int indexFinCommande = 0;
int result = 0;
void setup() {
servo1.attach(9); //servo 180° sur pin 9
servo1.write(35);
servo2.attach(10); //servo 180° sur pin 10
servo2.write(85);
Serial.begin(9600);
}
void loop() {
//pos1 = servo1.read();
//pos2 = servo2.read();
commande = "";
if(Serial.available())
{
//Serial.println("Traitement de la commande..");
commande = Serial.readString();
//Serial.println(commande);
traitementCommande(commande);
}
}
void traitementCommande(String uneCommande)
{
Serial.println(uneCommande);
indexFinCommande = uneCommande.indexOf('V');
indexX = uneCommande.indexOf('X');
indexY = uneCommande.indexOf('Y');
if((indexFinCommande==-1) || (indexX==-1) || (indexY==-1))
{
Serial.println("Commande non reconnue..");
goto commandNotRecognized;
}
newCoords[0] = uneCommande.substring(indexX+1,indexY).toInt();
newCoords[1] = uneCommande.substring(indexY+1, indexFinCommande).toInt();
if((newCoords[0] < maxBas) || (newCoords[0] > maxHaut) || (newCoords[1] < maxDroite) || (newCoords[1] > maxGauche))
{
Serial.println("Coordonees invalides..");
goto commandNotRecognized;
}
coords[0] = newCoords[0];
coords[1] = newCoords[1];
pos1 = coords[0] + 35;
pos2 = coords[1] + 85;
servo1.write(pos1);
servo2.write(pos2);
Serial.println("OK");
commandNotRecognized:
Serial.print(coords[0]);
Serial.print(" ");
Serial.println(coords[1]);
}
I want to know if there is an alternative to keep the serial connection open or if you guys had a solution for my problem.
I hope you will understand my request. Sorry for the fews french word used in my code, if something seems strange to you just ask me to explain it ;) And I'm also sorry for my english that should not be perfect..
Thank you for your time and your help.