sexta-feira, 21 de agosto de 2009

Controle de Servo Motor

Usando Arduino-0017 e Processing e servo motor de parabólica

Internface gráfica a partir do exemplo da biblioteca G4P
http://www.lagers.org.uk/g4p/distribution/web/index.html

Util
Openprocessing
http://openprocessing.org/visuals/?visualID=2362


Código Fonte
-----------------------------------------8<----------------------------

import guicomponents.*;
import processing.serial.*;

Serial myPort; // Create object from Serial class

// G4P components for main window
GPanel pnlControls;
GLabel lblSomeString, lblAlpha, lblAction, lblCursor;
GTextField txfSomeText;
GCombo cboColor, cboFont;
GHorzSlider sdrAlpha;
GActivityBar acyBar;
GTimer tmrTimer;
GButton btnTimer;
GCheckbox cbxBusy, cbxMouseOver;
GOptionGroup opgMouseOver;
GOption optHand, optXhair, optMove, optText,optWait;

// G4P components for second window
GButton btnControl; // Used to create controller window
GWindow windControl;
GHorzSlider sdrHorzPos;
GVertSlider sdrVertPos;

PImage imgBgImage;

int count = 0;

int pX = 10;
int pY = 30;
int pHeight = 280;
int pWidth = 600;

void setup(){
//size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
//
String portName = Serial.list()[1];
// println(Serial.list());
myPort = new Serial(this, portName, 9600);

size(700,340);
// Load the background image
imgBgImage = loadImage("bground.jpg");
// Set the colour scheme
G4P.setColorScheme(this, GCScheme.GREEN_SCHEME);
G4P.messagesEnabled(false);

// Create 2D GUI
createPanelAndStatusBar();
createTransparencySlider();


// Enable mouse over image changes
G4P.setMouseOverEnabled(true);

// Create a second control window
// btnControl = new GButton(this, "Open Panel Position Window", 10, pHeight - 100,200,30);
pnlControls.add(btnControl);
}



public void createPanelAndStatusBar(){
pnlControls = new GPanel(this,"Panel Tab Text (drag to move : click to open/close)",pX,pY,pWidth,pHeight);
pnlControls.setOpaque(true);
pnlControls.setCollapsed(false);
lblAction = new GLabel(this, "USER ACTION FEEDBACK DISPLAYED HERE!", 0, pHeight-20, pWidth, 20);
lblAction.setBorder(1);
lblAction.setOpaque(true);
lblAction.setColorScheme(GCScheme.RED_SCHEME);
pnlControls.add(lblAction);
}


public void createTransparencySlider(){
lblAlpha = new GLabel(this,"Adjustar rotação (0-180º) ->",0,pHeight-40,150);
lblAlpha.setFont("Arial", 14);
sdrAlpha = new GHorzSlider(this,pWidth-400,pHeight-40,299,19);
sdrAlpha.setBorder(2);
// sdrAlpha.setLimits(255, 128, 255);
sdrAlpha.setLimits(179, 0, 179);
pnlControls.add(lblAlpha);
pnlControls.add(sdrAlpha);
}


public void handleSliderEvents(GSlider slider){
if(sdrAlpha == slider){
//pnlControls.setAlpha(sdrAlpha.getValue());
// lblAction.setText("Panel transparency is " + pnlControls.getAlpha());
lblAction.setText("Ângulo do Servo " + sdrAlpha.getValue());
//myPort.write(100);
myPort.write(sdrAlpha.getValue());
}
if(sdrHorzPos == slider){
pnlControls.setX(sdrHorzPos.getValue());
}
if(sdrVertPos == slider){
pnlControls.setY(sdrVertPos.getValue());
}
}


public void handlePanelEvents(GPanel panel){
if(pnlControls == panel){
switch (pnlControls.getEventType()){
case GPanel.DRAGGED:
if(sdrHorzPos != null && sdrVertPos != null){
sdrHorzPos.setValue(pnlControls.getX());
sdrVertPos.setValue(pnlControls.getY());
}
}
}
}


void draw(){
pushMatrix();
background(imgBgImage);
popMatrix();
}

public void drawController(GWinApplet appc, GWinData data){
appc.stroke(255,255,0);
appc.strokeWeight(1);
appc.fill(130,130,0);
appc.rect(pnlControls.getX()/4, (pnlControls.getY() - pnlControls.getTabHeight())/4,
pnlControls.getWidth()/4, (pnlControls.getHeight()+ pnlControls.getTabHeight())/4);
}


/* ----------- P R O G R A M A D O A R D U I N O -------------------------------------
Servo ligado no pino 10


#include

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // Data received from the serial port


void setup()
{
myservo.attach(10); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop()
{
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
val = map(val, 0, 179, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

*/

Nenhum comentário: