bonjour ,
je travail sur un projet de tourelle lance projectile a detection automatique de la couleur rouge. il y a une tourelle avec une web cam dessus et deux servos pour l'axe horizontal et verticale.
le code processing detecter la couleur rouge la plus forte sur se que voi la web cam et cree un rectangle rouge dessus, le centre de se carre est decrit par Xmoy et Ymoy, on veut pouvoir dire au servo de bouger pour avoir le carre rouge au mileu de l'ecran ( la tourelle se retrouvant en face de la cible rouge) aisni de mettre Xmoy et Ymoy au milieu de l'ecran.
Le code pour detecter une couleur rouge fonctionne mais le probleme est que je ne parviens pas a envoier la position que doive adopter les servos en fonction de se que voi la webcam.
j'ai essaier de m'aider du code qui permet de bouger les servos avec la souris de l'ordi, mais sa ne fonctionne pas.
voila les code :
CODE PROCESSING
import processing.video.*;
import processing.serial.*;
Serial port;// port pour transmettre a arduino
int numPixels;
Capture video;
void setup() {
size(640, 480);
// buffer = createImage(640,480,HSB);
//image(buffer,639,0);
// détection de la webcam par defaut
video = new Capture(this, 640, 480);
numPixels = video.width * video.height;
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[0], 19200);
}
void draw() {
if (video.available()) {
colorMode(HSB, 100, 100, 100);
//background(0,0,99);
// buffer.loadPixels();
video.read();
video.loadPixels();
image(video, 0, 0); // initialisation position video
//initialisation
float Xm=0;
float Ym=0;
float Xmoy=0;
float Ymoy=0;
float coef=0;
color noir = color (0,0,0);
// int PixCop = 0;
//Parcours tout les pixels de l'image
for (int x = 0; x < 639; x++)
{
for(int y = 0; y < 479;y++)
{
int loc = x+y*640;
color ColorPix = video.pixels[loc];
if( hue(ColorPix)>93 && saturation(ColorPix)>60 && brightness(ColorPix)>30)
{
Xm=Xm + (x*brightness(ColorPix));
Ym=Ym + (y*brightness(ColorPix));
coef = coef + brightness(ColorPix);
set(x,y,noir);
}
}
}
Xmoy = Xm / coef;
Ymoy = Ym/coef;
fill(0,100,100);
rect(Xmoy-20,Ymoy-20,40,40);
}
}
int z=-1;
if(Xmoy > 319) ;{
//Output the servo position ( from 0 to 180)
port.write("h"+z);
}
if(Xmoy < 319) ;{
port.write("h"+1);
}
//if(Xmoy > 319) valH = val-1;
//if(Xmoy < 319) valH = val+1;
//if(Ymoy > 239) valV = val-1;
//if(Ymoy > 239) valV = val+1;
}
CODE ARDUINO
#include <Servo.h> //bibliothéque pour les servomoteurs
Servo servoH; // Crée un serveur pour les déplacement Horizontal
Servo servoV; // Crée un serveur pour les déplacement Vertical
//int potpinH = 0;// analog pin for H used to connect the potentiometer
//int potpinV = 0; // analog pin used for V to connect the potentiometer
//int valH; // variable to read the value from the analog pin
//int valV; // variable to read the value from the analog pin
void setup()
{
servoH.attach(14); // attaches the servo on pin 9 to the servo object
servoV.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop() {
//valH = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//valV = analogRead(potpin);
//valH = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
//valV = map(val, 0, 1023, 0, 179);
static int v= 0;
if(Serial.available()){
char ch =Serial.read();
switch(ch) {
case 'h':
servoH.write(v);
v=0;
break;
/* servoH.write(valH); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
servoV.write(valV); // sets the servo position according to the scaled value
delay(15);
*/
}
}
}
william
Inscrit(e) (le) 21 avril 2010Déconnecté Dernière activité mai 14 2010 12:55