El de procesing:
/*
CODIGO DE CONTROL DE MULTIROTOR ARDUINO
POR: Miguel Garnacho Vélez
FECHA: 24/12/2014
VERSION: Alpha 0.7
PROXIMO CAMBIOS:
Modos 2 y 3 (VERSION Alpha 1.0)
*/
import controlP5.*;
import org.firmata.*;
//Configuracion
int XBEE = 1; // Xbee
int CONT = 2; // Joystick
int Modo = 1;
float factorMod = 1.8;
float factorRes = 200;
int maxMod = 10;
int count;
int frecuencia = 10;
int mod;
int pin, TI, TF;
/*
1 - Control por Joystick
2 - Por teclado
3 - Por interfaz
4 - TeleTest de rotores (A traves de Serial) NO OPERATIVo HASTA VERSION 1.1
5 - Modo calibracion de giroscopio (Poner el arduino como modo 6) NO OPERATIVO HASTA VERSION 1.5
6 - Debug del GPS NO OPERATIVO HASTA VERSION 2.5
*/
import procontroll.*;
import net.java.games.input.*;
//import class to set up serial connection with wiring board
import processing.serial.*;
Serial xbee;
ControllIO controllIO;
ControllDevice joypad;
ControllSlider slider1; //X
ControllSlider slider2; //Y
ControllSlider potencia;
int Nspeed;
int Sspeed;
int Wspeed;
int Espeed;
String strNspeed;
String strSspeed;
String strWspeed;
String strEspeed;
int restaX;
int restaY;
int pot;
int ejeX;
int ejeY;
boolean modo = false;
color verde = color(0, 200, 0);
color rojo = color(200, 0, 0);
color colorR = rojo;
void setup()
{
size(640, 400);
background(color(204));
println(Serial.list());
String portName = Serial.list()[XBEE];
xbee = new Serial(this, portName, 9600);
// Joystick
controllIO = ControllIO.getInstance(this);
controllIO.printDevices();
joypad = controllIO.getDevice(CONT);
joypad.plug(this, "handleButton1Press", ControllIO.ON_PRESS, 3);
joypad.plug(this, "handleButton2Press", ControllIO.ON_PRESS, 6);
slider1 = joypad.getSlider(4); //Eje X (4)
slider1.setMultiplier(128);
slider1.setTolerance(0.06f);
println(slider1.getName());
slider2 = joypad.getSlider(3); //Eje Y (3)
slider2.setTolerance(0.06f);
slider2.setMultiplier(128);
println(slider2.getName());
potencia = joypad.getSlider(0); //Potencia (0)
potencia.setTolerance(0.06f);
potencia.setMultiplier(90);
println(potencia.getName());
//Creación de la interfaz
//Variables
float transX;
float transY;
}
void draw()
{
switch (Modo) //Eliges un modo
{
case 1: //Por Joystick
ModoJoystick();
break;
case 2:
teclado();
break;
case 3:
interfaz();
break;
case 4:
TeleTest();
break;
case 5:
Calib();
break;
case 6:
DebugGPS();
break;
}
// Manda la velocidad de los rotores a Arduino
if (modo) {mandar();} //Si has pulsado el modo, se manda los datos
} //Draw
void mousePressed()
{ // Botones
if ( mouseX >= 220 && mouseX <= 300 && mouseY >= 340 && mouseY <= 360 ) // chekea si pulsa modo
{
if (modo) {modo = false; colorR = rojo;}
else {modo = true; colorR = verde;}
}
if ( mouseX >= 340 && mouseX <= 420 && mouseY >= 340 && mouseY <= 360 ) // lo mismo con aterrizar
{
//xbee.write('A'); //Aterrizaje
println("Aterrizaje");
}
}
void handleButton1Press() //Modo
{
if (modo) {modo = false; colorR = rojo;}
else {modo = true; colorR = verde;}
}
void handleButton2Press() //Aterrizar
{println("Aterrizaje");}
//---------------------------------------------------------FUNCIONES DE TECLADO-----------------------------------------------------------
void DrawJoystick()
{
background(0);
stroke(0);
fill(0,255,0);
rect(220, 340, 80, 20, 7); // FeedBack
rect(340, 340, 80, 20, 7); // Aterrizar
fill(0);
text("Modo", 230, 340, 80, 20);
text("Aterrizar",350, 340, 80, 20);
fill(colorR); //Piloto del modo
rect(600,350,20,20);
fill(0);
stroke(0,255,0);
rect(50, 50, 256, 256); // Panel del jostick
stroke(0,255,0); // Ejes X y Y
line(50, 178, 306, 178); // X
line(178, 50, 178, 306); // Y
fill(0,255,0);
ellipse( 178, 178, 5, 5); // Puntero
fill(0,255,0);
stroke(0,255,0);
rect(520, 60, 80, 20, 7); // E
rect(360, 250, 80, 20, 7); // W
rect(360, 60, 80, 20, 7); // N
rect(520, 250, 80, 20, 7); // S
rect(500, 300, 80 , 20, 7); //Potencia
fill(0);
line(400, 70, 560, 260); //Ejes
line(560, 70, 400, 260); //Ejes
fill(0,255,0);
text("Interfaz del DronArduino", 10, 10, 100, 80); // Título
text("X: Y:", 60, 310, 140, 100); //Coordenadas de los ejes
}
void DrawTeclado()
{}
//------------------------------------------------------------MODOS-------------------------------------------------------------//
void ModoJoystick()
{
DrawJoystick();
// Puntero
fill(255,0,0);
ellipse(slider1.getValue() + 178, slider2.getValue() + 178, 5, 5);
fill(0,255,0);
text(" " + int(slider1.getValue()) + " " + int(-slider2.getValue()), 60, 310, 140, 100); //Coordenadas de los ejes
fill(0);
text(int(potencia.getValue() + 90), 515, 315); //Potencia
// Calculo
ejeX = int(slider1.getValue());
ejeY = -int(slider2.getValue());
mod = int(sqrt(sq(ejeX) + sq(ejeY))/factorMod)
pot = int(potencia.getValue() + 90);
restaX = int(abs(ejeX * pot / factorRes));
restaY = int(abs(ejeY * pot / factorRes));
//println(mod);
if (ejeX > 0 && ejeY > 0) // 1
{
Espeed = pot - mod;
Nspeed = pot - restaY;
Sspeed = pot - restaX;
Wspeed = pot;
}
if (ejeX < 0 && ejeY > 0) // 2
{
Nspeed = pot - mod;
Espeed = pot - restaY;
Wspeed = pot - restaX;
Sspeed = pot;
}
if (ejeX < 0 && ejeY < 0) // 3
{
Wspeed = pot - mod;
Nspeed = pot - restaY;
Sspeed = pot - restaX;
Espeed = pot;
}
if (ejeX > 0 && ejeY < 0) // 4
{
Sspeed = pot - mod;
Espeed = pot - restaY;
Wspeed = pot - restaX;
Nspeed = pot;
}
if (ejeX == 0) //Se mueve por el eje Y
{
if (ejeY > 0)
{Nspeed = pot - restaY;Espeed = pot - restaY;Wspeed = pot;Sspeed = pot;}
else
{Wspeed = pot - restaY;Sspeed = pot - restaY;Nspeed = pot;Espeed = pot;}
}
if (ejeY == 0) //Se mueve por el eje X
{
if (ejeX > 0)
{Espeed = pot - restaX;Sspeed = pot - restaX;Nspeed = pot;Wspeed = pot;}
else
{Wspeed = pot - restaX;Nspeed = pot - restaX;Sspeed = pot;Espeed = pot;}
}
if (ejeX == 0 && ejeY == 0) //Origen
{Nspeed = pot;Sspeed = pot;Wspeed = pot;Espeed = pot;}
// Finaliza con constrain
Nspeed = constrain(Nspeed,0,200);
Sspeed = constrain(Sspeed,0,200);
Wspeed = constrain(Wspeed,0,200);
Espeed = constrain(Espeed,0,200);
// Display rotores
text(Nspeed, 395, 75); //N
text(Sspeed, 555, 265); //S
text(Wspeed, 395, 265); //W
text(Espeed, 555, 75); //E
}
void teclado()
{
//Dibujar el teclado
DrawTeclado();
if (keyPressed)
{
if (key == 'w' || key == 'W') {if (Nspeed >= 15) {Nspeed =- 15;}} else {Sspeed = pot;}
if (key == 'a' || key == 'A') {if (Wspeed >= 15) {Wspeed =- 15;}} else {Espeed = pot;}
if (key == 's' || key == 'S') {if (Sspeed >= 15) {Sspeed =- 15;}} else {Nspeed = pot;}
if (key == 'd' || key == 'D') {if (Espeed >= 15) {Espeed =- 15;}} else {Wspeed = pot;}
if (key == CODED)
{
if (keyCode == UP) {if (pot < 180) {pot =+ 5;}}
if (keyCode == DOWN) {if (pot > 0) {pot =- 5;}}
}
}
}
void mandar()
{
count++;
if (count == frecuencia)
{
count = 0;
TI = millis();
//Pasa la velocidad a una string
strNspeed = str(Nspeed);
strSspeed = str(Sspeed);
strWspeed = str(Wspeed);
strEspeed = str(Espeed);
xbee.write('%');
xbee.write(strNspeed + ",");
xbee.write(strSspeed + ",");
xbee.write(strWspeed + ",");
xbee.write(strEspeed);
xbee.write('\n');
TF = millis();
medirPin(TI, TF);
}
}
void interfaz()
{}
void TeleTest()
{}
void Calib()
{}
void DebugGPS()
{}
void medirPin(int ti, int tf)
{
pin = tf - ti;
println("Pin: " + pin);
}
/*
if (slider1.getValue() == 0) // X
{
Wspeed = pot;
Espeed = pot;
}
if (slider2.getValue() == 0) // Y
{
Nspeed = pot;
Sspeed = pot;
}
*/