miércoles, 20 de mayo de 2015

Cuadricóptero + Processing

Bien les dejo este código ya viejo que hice hace ya mucho tiempo el objetivo era ver en "tiempo real" la aceleración que tendría cada uno de los motores de un cuadricóptero para la compensación de fuerzas y así conseguir una estabilización, el código esta comentado y des afortunadamente no encontré la parte que hacia la conexión con una placa Arduino para adquirir los datos del sensor, por ahora solo toma los datos del mouse pero ya con eso basta para que cualquiera pueda introducir datos en dichas variables provenientes de cualquier dispositivo con conexión a la PC.

Bueno sin más espero que le sirva por lo menos a alguien.



/**
 * Setup Quadcopter Simulation
 *
 * this code is for a mini quadcopter simulation and stabilization with accelerometer and microcontroller
 */

  /*
   line(300, 160, 340,160);  // top square mainframe quadcopter
 
   line(300, 200, 340,200); // bottom square mainframe quadcopter
 
   line(300, 160, 300,200); // this is the left side of mainframe
 
   line(340, 160, 340,200); // this is the right side of mainframe
*/

// The statements in the setup() function
// execute once when the program begins
float angle = 0.0;
float angle1 = 0.0;
float angle2 = 0.0;
float angle3 = 0.0;
float VM1=10;
float VM2=10;
float VM3=10;
float VM4=10;
void setup() {
  size(640, 320 );  // Size must be the first statement
  stroke(255);     // Set line drawing color to white
  frameRate(30);
}

void M1(){
  pushMatrix(); // Save the current position before the translate modifications.

  angle=angle+VM1;
  translate(270,130);
  rotate(angle);
  line(-35,0,35,0);
  fill(125, 0, 0);
  ellipse(-20,0,38,8);
  ellipse( 20,0,38,8);
  popMatrix();  //  Restore the original positions after the translate modifications.
}

void M2(){
  pushMatrix(); // Save the current position before the translate modifications.
  angle1=angle1+VM2;
  translate(370,130);
  rotate(angle1);
  line(-35,0,35,0);
  fill(125,0,0);
  ellipse(-20,0,38,8);
  ellipse( 20,0,38,8);
  popMatrix();  //  Restore the original positions after the translate modifications.

}

void M3(){
  pushMatrix(); // Save the current position before the translate modifications.
  angle2=angle2+VM3;
  translate(270,230);
  rotate(angle2);
  line(-35,0,35,0);
  fill(0);
  ellipse(-20,0,38,8);
  ellipse( 20,0,38,8);
  popMatrix();  //  Restore the original positions after the translate modifications.

}

void M4(){
  pushMatrix(); // Save the current position before the translate modifications.
  angle3=angle3+VM4;
  translate(370,230);
  rotate(angle2);
  line(-35,0,35,0);
  fill(0);
  ellipse(-20,0,38,8);
  ellipse( 20,0,38,8);
  popMatrix();  //  Restore the original positions after the translate modifications.

}
void protector(){
noFill();  strokeWeight(4);
      ellipse(270, 130, 100, 100); // M1 L1
      ellipse(370, 130, 100, 100); // M2 R1
      ellipse(270, 230, 100, 100); // M3 L2
      ellipse(370, 230, 100, 100);  strokeWeight(1); // M4 R2
      fill(255);
      ellipse(270, 130, 7, 7); // M1 L1
      ellipse(370, 130, 7, 7); // M2 R1
      ellipse(270, 230, 7, 7); // M3 L2
      ellipse(370, 230, 7, 7);
}

// The statements in draw() are executed until the
// program is stopped. Each statement is executed in
// sequence and after the last line is read, the first
// line is executed again.
void draw() {

  background(30);   // Set the background to black
VM1= map(mouseX, 0, width, 0, height);
VM2= map(mouseX, 0, width, 0, height);
VM3=10;
VM4=10;
    fill(0);
    strokeWeight(4);
    line(270,130,370,230);  // L1 and R2 Arm motor
    line(370,130,270,230);  // L2 and R1 Arm motor
    strokeWeight(1);
    ellipse(320, 180, 40, 40); // for the ellipse ( this is center XY, this is diameter)

  //line(320, 180, 320,180);    // center of mainframe quadcopter
     fill(50,0,40);
   ellipse(270, 130, 20, 20); // M1 L1
   ellipse(370, 130, 20, 20); // M2 R1
   ellipse(270, 230, 20, 20); // M3 L2
   ellipse(370, 230, 20, 20); // M4 R2
 
 

 

   text("SIMULATION OF QUADCOPTER STABILIZER  "+angle,10,40);
   text("M1: "+VM1,10,60);
   text("M2: "+VM2,10,70);
   text("M3: "+VM3,10,80);
   text("M4: "+VM4,10,90);  



M1();
M2();
M3();
M4();
protector();
}


No hay comentarios: