ExBot机器人 PC控制调试端程序(基于Java,可嵌入到网页中)
/*
Created 12 Dec. 2010
by JinTao Liu
Email; top1944@163.com
WEB: blog/ednchina.com/exbot
*/
import processing.serial.*;
Serial port; // Create object from Serial class
class ArduinoData{
public
int sonic1;
int sonic2;
int ultra;
int rhumb;
int counter;
int servo_pos;
int PWM_val;
int current;//current monit
};
int Width=900;
int Height=600;
void setup() {
size(Width, Height);
frameRate(30);
smooth();
println(Serial.list());
String arduinoPort = Serial.list()[1];
port = new Serial(this, arduinoPort, 115200);
background(0);
exbotlogo(0,0);
}
///////////////////////////////////////////////////////////////////////
// draw
///////////////////////////////////////////////////////////////////////
int i=0;
int loopNum=0 ,penguinNum=0;
float val; // Data received from the serial port
int x;
int[] arduino = new int[8];
float easing=0.1;
float easingHead=0;
int headTem;
//ArduinoData ard=new ArduinoData();
////////////////////////////////////////////////////////////
void draw() {
if ( port.available() > 14)
// If data is available,
{
val = readSerial(arduino);// read it and store it in val
movePenguin();
}
sonicLine(arduino[0],arduino[1],arduino[2]);
exbotDirect(450, 150,arduino[3]); //exbotDirect(x,y,head)
}
int readSerial(int a[])
{
int i, val,valLow;
int num2read=8; //8 is the mumber of datas to read
while(port.available() >0) // If data is available,
{
val = port.read(); // read it and store it in val
if (val !='A') continue;
val = port.read();
if (val !='R') continue;
val = port.read();
if (val !='D') continue;
for(i = 0; i<num2read; i ++)//num2read is the mumber of datas to read
{
val = port.read(); //low byte first. o is low byte
valLow=val;
val = port.read(); //high byte first. o is high byte
a=valLow+(val<<8);
}
for(i = 0; i<num2read; i ++)//num2read is the mumber of datas to read
{print(i);print(" = ");print(a);print(" ");}
println();
return 1;//Finish receiving
}
return 0;//No data
}
void exbotlogo(int x, int y)
{
PImage img,penguin;
penguin=loadImage("penguin.jpg");//Before that,Add the image to the sketch鈥檚 data folder.(Sketch/Add file)
img = loadImage("exbot.png");//Before that,Add the image to the sketch鈥檚 data folder.(Sketch/Add file)
image(penguin, x+10, y+100);
image(img, x, y);
// Devide Line //
stroke(0,255,0);
line(0, Height/2, Width, Height/2);
//grid on
stroke(0,100,0);
for(i=1;i<Height;i=i+50)
line(0, Height/2+i, Width, Height/2+i);
stroke(0,100,0);
for(i=1;i<Width;i=i+50)
line(i, Height/2, i, Height);
// text //
textSize(20);
fill(255,0,0);
text("Sonic_1", 10, Height/2-60);
fill(0,255,0);
text("Sonic_2", 10, Height/2-40);
fill(0,0,255);
text("Ultra", 10, Height/2-20);
//video window
rect(605,5,290,290);
img = loadImage("demo.jpg");//Before that,Add the image to the sketch鈥檚 data folder.(Sketch/Add file)
image(img, 610, 10);
}
void exbotDirect(int x1, int y1,int head) //(x1,y1) is the center .'head' is the degree of head
{
//////////////////
// filter
if(head>360) head=headTem;
headTem=head;
//print("headTem="); print(headTem);
////////////////////////////
// easing //
easingHead += (head - easingHead) * easing;
head=int(easingHead);
//print("head="); print(head);
////////////////////////////
PImage img;
int radius=150;//radius of circular
fill(255);
stroke(0);
ellipse(x1, y1, 2*radius, 2*radius);
line(x1-radius,y1,x1+radius,y1);line(x1,y1-radius,x1,y1+radius);
img=loadImage("carSmall.jpg");//Before that,Add the image to the sketch鈥檚 data folder.(Sketch/Add file)
pushMatrix();
translate(x1,y1);
rotate(head*PI/180);
imageMode(CENTER);
image(img,0,0,30,50);
stroke(0,255,0);
line(0,0,0,-radius);//direction
popMatrix();
}
int sonicLine(int a1, int a2,int a3)
{
float Top=300;
float Bottom=600;
float BiggestUint=4000 ;//Biggest Uint is 65535.But the farest distance is lee than 4000.
int i;
// data line //
stroke(255,0,0);
point(loopNum+1, map(a1, 0, BiggestUint, Bottom, Top));// White point
stroke(0,255,0);
point(loopNum+1, map(a2, 0, BiggestUint, Bottom, Top));// White point
stroke(0,0,255);
point(loopNum+1, map(a3, 0, BiggestUint, Bottom, Top));// White point
loopNum++;
if (loopNum > width) {
loopNum = 0;
fill(0);
stroke(0,255,0);
rect(0,Height/2,Width,Height/2);
// Devide Line //
stroke(0,255,0);
line(0, Height/2, Width, Height/2);
//grid on
stroke(0,100,0);
for(i=1;i<Height;i=i+50)
line(0, Height/2+i, Width, Height/2+i);
stroke(0,100,0);
for(i=1;i<Width;i=i+50)
line(i, Height/2, i, Height);
}
return loopNum;
}
void movePenguin()
{
int totalNum=4;//Number of interval
PImage imgPg_1,imgPg_2;
imgPg_1=loadImage("penguinYB-1.png");
imgPg_2=loadImage("penguinYB-2.png");
imageMode(CORNER);
penguinNum++;
if(penguinNum>totalNum)
{
//fill(0,255,0);
rect(10, 100, 139, 104);
image(imgPg_1,10,100);
}
else
{
rect(10, 100, 139, 104);
image(imgPg_2,10,100);
}
if (penguinNum>totalNum*2)
penguinNum=0;
// println(penguinNum);
}
文章评论(0条评论)
登录后参与讨论