collapse

* Posts Recentes

O que é isto ? por SerraCabo
[12 de Abril de 2024, 14:20]


Amplificador - Rockboard HA 1 In-Ear por almamater
[11 de Abril de 2024, 20:46]


Emulador NES em ESP32 por dropes
[10 de Abril de 2024, 15:30]


Meu novo robô por josecarlos
[29 de Março de 2024, 18:30]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]


Laser Engraver - Alguém tem? por almamater
[16 de Dezembro de 2023, 14:23]

Autor Tópico: Processing ajuda  (Lida 2018 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline TheDeadLives

  • Mini Robot
  • *
  • Mensagens: 398
Processing ajuda
« em: 17 de Agosto de 2011, 16:38 »
Estive a desenvolver um projecto, para controlar cinco motores apartir do arduino e utilizei o processing ( foi uma grande complicação porque não o entendo muito bem) para os controlar.

O meu código do arduino é o seguinte:

Código: [Seleccione]
int baseMotorEnablePin = 2;
int baseMotorPin1 = 3;
int baseMotorPin2 = 4;
int shoulderMotorEnablePin = 14;
int shoulderMotorPin1 = 15;
int shoulderMotorPin2 = 16;
int elbowMotorEnablePin = 8;
int elbowMotorPin1 = 9;
int elbowMotorPin2 = 10;
int wristMotorEnablePin = 5;
int wristMotorPin1 = 6;
int wristMotorPin2 = 7;
int handMotorEnablePin = 11;
int handMotorPin1 = 17;
int handMotorPin2 = 18;
int incomingByte;
 
void setup() {
  pinMode(baseMotorPin1, OUTPUT);
  pinMode(baseMotorPin2, OUTPUT);
  pinMode(baseMotorEnablePin, OUTPUT);
  digitalWrite(baseMotorEnablePin, HIGH);
  pinMode(shoulderMotorPin1, OUTPUT);
  pinMode(shoulderMotorPin2, OUTPUT);
  pinMode(shoulderMotorEnablePin, OUTPUT);
  digitalWrite(shoulderMotorEnablePin, HIGH);
  pinMode(elbowMotorPin1, OUTPUT);
  pinMode(elbowMotorPin2, OUTPUT);
  pinMode(elbowMotorEnablePin, OUTPUT);
  digitalWrite(elbowMotorEnablePin, HIGH);
  pinMode(wristMotorPin1, OUTPUT);
  pinMode(wristMotorPin2, OUTPUT);
  pinMode(wristMotorEnablePin, OUTPUT);
  digitalWrite(wristMotorEnablePin, HIGH);
  pinMode(handMotorPin1, OUTPUT);
  pinMode(handMotorPin2, OUTPUT);
  pinMode(handMotorEnablePin, OUTPUT);
  digitalWrite(handMotorEnablePin, HIGH);
  Serial.begin(9600);
}
 
void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'Q' ) {
    digitalWrite(baseMotorPin1, LOW);
    digitalWrite(baseMotorPin2, HIGH);
    }
    if (incomingByte == 'W') {
    digitalWrite(baseMotorPin1, HIGH);
    digitalWrite(baseMotorPin2, LOW);
    }
    if (incomingByte == 'E') {
    digitalWrite(shoulderMotorPin1, LOW);
    digitalWrite(shoulderMotorPin2, HIGH);
    }
    if (incomingByte == 'R') {
    digitalWrite(shoulderMotorPin1, HIGH);
    digitalWrite(shoulderMotorPin2, LOW);
    }
    if (incomingByte == 'T') {
    digitalWrite(elbowMotorPin1, LOW);
    digitalWrite(elbowMotorPin2, HIGH);
    }
    if (incomingByte == 'y' || incomingByte == 'Y') {
    digitalWrite(elbowMotorPin1, HIGH);
    digitalWrite(elbowMotorPin2, LOW);
    }
    if (incomingByte == 'U') {
    digitalWrite(wristMotorPin1, LOW);
    digitalWrite(wristMotorPin2, HIGH);
    }
    if (incomingByte == 'I') {
    digitalWrite(wristMotorPin1, HIGH);
    digitalWrite(wristMotorPin2, LOW);
    }
    if (incomingByte == 'O') {
    digitalWrite(handMotorPin1, LOW);
    digitalWrite(handMotorPin2, HIGH);
    }
    if (incomingByte == 'P') {
    digitalWrite(handMotorPin1, HIGH);
    digitalWrite(handMotorPin2, LOW);
    }

    if (incomingByte == 'G') {
    digitalWrite(baseMotorPin1, LOW);
    digitalWrite(baseMotorPin2, LOW);
    digitalWrite(shoulderMotorPin1, LOW);
    digitalWrite(shoulderMotorPin2, LOW);
    digitalWrite(elbowMotorPin1, LOW);
    digitalWrite(elbowMotorPin2, LOW);
    digitalWrite(wristMotorPin1, LOW);
    digitalWrite(wristMotorPin2, LOW);
    digitalWrite(handMotorPin1, LOW);
    digitalWrite(handMotorPin2, LOW);
    }
  }
}


E o meu código do processing é o seguinte:


Código: [Seleccione]
import processing.serial.*;

Serial port;

int M1LX, M1RX, M2LX, M2RX, M3LX, M3RX, M4LX, M4RX, M5LX, M5RX;
int M1LY, M1RY, M2LY, M2RY, M3LY, M3RY, M4LY, M4RY, M5LY, M5RY;

int boxSize = 64;
arrow myRightArrow;
int[]rightArrowxpoints={30,54,30,30,0,0,30};
int[]rightArrowypoints={0,27,54,40,40,15,15};
arrow myLeftArrow;
int[]leftArrowxpoints={0,24,24,54,54,24,24};
int[]leftArrowypoints={27,0,15,15,40,40,54};
PFont myFont;

void setup()
{size(145, 455);
M1LX = 5;
M1LY = 25;
M1RX = 75;
M1RY = 25;
M2LX = 5;
M2LY = 115;
M2RX = 75;
M2RY = 115;
M3LX = 5;
M3LY = 205;
M3RX = 75;
M3RY = 205;
M4LX = 5;
M4LY = 295;
M4RX = 75;
M4RY = 295;
M5LX = 5;
M5LY = 385;
M5RX = 75;
M5RY = 385;

println(Serial.list());
myFont = createFont("verdana", 12);
textFont(myFont);
port = new Serial(this, Serial.list()[1], 9600);
myRightArrow = new arrow(rightArrowxpoints,rightArrowypoints,7);
myLeftArrow = new arrow(leftArrowxpoints,leftArrowypoints,7);
}

void draw()
{
background(0);
noStroke();fill(150);
text("Base Motor (q|Q/w|W)", 5, 5, 200, 75);
text("Shoulder Motor (e|E/r|R)", 5, 95, 200, 75);
text("Elbow Motor (t|T/y|y)", 5, 185, 200, 75);
text("Wrist Motor (u|u/i|I)", 5, 275, 200, 75);
text("Hand Motor (o|O/p|P)", 5, 365, 200, 75);

if(keyPressed)
{
if (key == 'q' || key == 'Q')
{
port.write('Q');
}
if (key == 'w' || key == 'W')
{
port.write('W');
}
if (key == 'e' || key == 'E')
{
port.write('E');
}
if (key == 'r' || key == 'R')
{
port.write('R');
}
if (key == 't' || key == 'T')
{
port.write('T');
}
if (key == 'y' || key == 'y')
{
port.write('Y');
}
if (key == 'u' || key == 'U')
{
port.write('U');
}
if (key == 'i' || key == 'I')
{
port.write('I');
}
if (key == 'O' || key == 'O')
{
port.write('Z');
}
if (key == 'p' || key == 'P')
{
port.write('P');
}
if (key == 'o' || key == 'O')
{
port.write('O');
}
}
else if (mousePressed == true) {
if (mouseX > M1LX-boxSize && mouseX < M1LX+boxSize &&
mouseY > M1LY-boxSize && mouseY < M1LY+boxSize) {
port.write('Q');
}
else if(mouseX > M1RX-boxSize && mouseX < M1RX+boxSize &&
mouseY > M1RY-boxSize && mouseY < M1RY+boxSize) {
port.write('W');
}
else if(mouseX > M2LX-boxSize && mouseX < M2LX+boxSize &&
mouseY > M2LY-boxSize && mouseY < M2LY+boxSize) {
port.write('E');
}
else if(mouseX > M2RX-boxSize && mouseX < M2RX+boxSize &&
mouseY > M2RY-boxSize && mouseY < M2RY+boxSize) {
port.write('R');
}
else if(mouseX > M3LX-boxSize && mouseX < M3LX+boxSize &&
mouseY > M3LY-boxSize && mouseY < M3LY+boxSize) {
port.write('T');
}
else if(mouseX > M3RX-boxSize && mouseX < M3RX+boxSize &&
mouseY > M3RY-boxSize && mouseY < M3RY+boxSize) {fill(200);
port.write('Y');
}
else if (mouseX > M4LX-boxSize && mouseX < M4LX+boxSize &&
mouseY > M4LY-boxSize && mouseY < M4LY+boxSize) {
port.write('U');
}
else if(mouseX > M4RX-boxSize && mouseX < M4RX+boxSize &&
mouseY > M4RY-boxSize && mouseY < M4RY+boxSize) {
port.write('I');
}
else if (mouseX > M5LX-boxSize && mouseX < M5LX+boxSize &&
mouseY > M5LY-boxSize && mouseY < M5LY+boxSize) {
port.write('O');
}
else if(mouseX > M5RX-boxSize && mouseX < M5RX+boxSize &&
mouseY > M5RY-boxSize && mouseY < M5RY+boxSize) {
port.write('P');
}
else {
port.write('G');
}
}

myRightArrow.drawArrow(80,30);
myRightArrow.drawArrow(80,120);
myRightArrow.drawArrow(80,210);
myRightArrow.drawArrow(80,300);
myRightArrow.drawArrow(80,390);
myLeftArrow.drawArrow(10,30);
myLeftArrow.drawArrow(10,120);
myLeftArrow.drawArrow(10,210);
myLeftArrow.drawArrow(10,300);
myLeftArrow.drawArrow(10,390);
}

class arrow extends java.awt.Polygon {
public arrow(int[] xpoints,int[] ypoints, int npoints) {
super(xpoints,ypoints,npoints);

}
void drawArrow(int xOffset, int yOffset){
fill(150);
rect(xOffset-5, yOffset-5, boxSize, boxSize);
fill(255);
beginShape();
for(int i=0;i<npoints;i++){
vertex(xpoints[i]+xOffset,ypoints[i]+yOffset);
}
endShape();
}
}

Só que infelizmente o código do processing está a dar erro e não sei porque, visto, como já referi anteriormente, não o entendo muito bem (mesmo sabendo que é semelhante ao software do arduino).

Se me pudessem ajudar ficaria-vos muito grato.