因为喜欢机器人,所以接触了arduino,因为喜欢arduino,所以接触了processing。某夜突发奇想,觉得可以用processing的key函数和mouse函数控制桌面码垛机的运动。嘿嘿,刚好利用国庆假期,用了5天实现自己的想法。
ps:网上关于processing与arduino互动的例子还真不多,有也基本是利用processing将arduino输出的数据可视化,
利用processing来控制arduino的基本没有哈。
pps:楼主发现淘宝上有商家直接拿楼主设计的桌面码垛机在卖,本想着共享设计文件是为了大家学习交流用的,没想到居然有人拿来盈利。找店主理论,他居然说是很久前在gradcad上下载的。希望各位网友在浏览淘宝是看到和我一样设计的4自由度机械臂是千万别买呀。- [code]/**
- *processing control a 4-DOF mechanical arm
- *by jorneryChen
- *
- *my emial address is [email protected]
- */
- #include<Servo.h>
- #include<LiquidCrystal.h>
- LiquidCrystal lcd(12,11,10,9,8,7);
- Servo myservo;[pre lang="processing" line="1"]/**
- *processing control a 4-DOF mechanical arm
- *by jorneryChen
- *
- *my emial address is [email protected]
- */
- import processing.serial.*;
- Serial port;
- boolean serial;
- //输出参数
- float x=150;
- float y=0;
- float pos1,pos2;
- int pos=72;
- int pos3=90;
- int a=2;
- float posa;
- float A;
- float B;
- float C;
- float D;
- float pi=acos(-1.0);
- //界面参数
- PFont font;
- void setup()
- {
- size(500,500);
- frameRate(53);
- port=new Serial(this,"COM3",9600);
- font=loadFont("LilyUPCBold-48.vlw");
- smooth();
- }
- void draw()
- {
- math_funtion();
- inter_face();
- picture();
- control();
- }
- void math_funtion()
- {
- A=sqrt(x*x+y*y);//数学方程
- B=atan(y/x);
- C=acos(A/300);
- D=acos(1.0-A*A/45000);
- D=D/pi*180;
- posa=B+C;
- pos1=posa/pi*180;
- pos2=180-pos1-D;
- }
- void inter_face()
- {
- background(255);
- textSize(40);
- fill(245,12,32);
- text("C",5,40);
- fill(38,245,12);
- text("L",35,40);
- fill(12,91,245);
- text("Y",55,40);
- fill(66,204,227);
- textSize(40);
- text("made by yao",120,40);
- textSize(30);
- fill(211,191,0);
- text("this interface shows data only",5,80);
- fill(0);
- textSize(20);
- text("X=",5,110);
- text(int(x),35,110);
- text("Y=",85,110);
- text(int(y),115,110);
- text("POS=",5,140);
- text(int(pos),60,140);
- text("POS1=",105,140);
- text(int(pos1),175,140);
- text("POS2=",225,140);
- text(int(pos2),295,140);
- text("POS3=",345,140);
- text(int(pos3),415,140);
- if((keyPressed==true)&&(key=='i'))
- {
- fill(0,211,58);
- text("opening",250,110);
- }
- else if((keyPressed==true)&&(key=='k'))
- {
- fill(0,211,58);
- text("closing",250,110);
- }
- }
- void picture()
- {
- strokeWeight(4);
- fill(255);
- rect(5,160,490,320);
- ellipse(170,320,300,300);
- stroke(0);
- line(415,170,415,470);
- float n=map(pos3,0,180,300,40);
- float m=map(x,0,300,450,190);
- fill(0,0,150);
- ellipse(n,m,30,30);
- float l=map(y,-150,150,170,470);
- l=constrain(l,170,470);
- ellipse(415,l,30,30);
- }
- void control()
- {
- if((keyPressed==true)&&(key=='a'))
- {
- x-=a;
- serial=true;
- }
- else if((keyPressed==true)&&(key=='d'))
- {
- x+=a;
- serial=true;
- }
- else if((keyPressed==true)&&(key=='w'))
- {
- y-=a;
- serial=true;
- }
- else if((keyPressed==true)&&(key=='s'))
- {
- y+=a;
- serial=true;
- }
- else if((keyPressed==true)&&(key=='i'))
- {
- pos+=a;
- pos=constrain(pos,72,160);
- serial=true;
- }
- else if((keyPressed==true)&&(key=='k'))
- {
- pos-=a;
- pos=constrain(pos,72,160);
- serial=true;
- }
- else if((keyPressed==true)&&(key=='j'))
- {
- pos3+=1;
- serial=true;
- }
- else if((keyPressed==true)&&(key=='l'))
- {
- pos3-=1;
- serial=true;
- }
- if(serial){
- send_data();
- }
- }
- void send_data()
- {
- print("pos=");
- print(int(pos));
- print(',');
- print("pos1=");
- print(int(pos1));
- print(',');
- print("pos2=");
- print(int(pos2));
- print(',');
- print("pos3=");
- println(pos3);
- port.write('%');
- port.write(int(pos));
- // port.write(';');
- port.write(int(pos1));
- // port.write(';');
- port.write(int(pos2));
- // port.write(';');
- port.write(int(pos3));
- serial=false;
- }
复制代码
Servo myservo1;
Servo myservo2;
Servo myservo3;
int servo =3; //定义舵机信号线接口
int servo1=4;//小臂
int servo2=5;//大臂
int servo3=6;
int pos;
int pos1;
int pos2;
int pos3;
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
myservo.attach(servo);
myservo1.attach(servo1);
myservo2.attach(servo2);
myservo3.attach(servo3);
pos=72;
pos1=60;
pos2=60;
pos3=90;
updateServo();
}
void loop()
{
recv_data();
updateServo();
}
void recv_data()
{
while(Serial.available()>=8)
{
char data=Serial.read();
if(data=='%')
{
pos=Serial.read();
pos1=Serial.read();
pos2=Serial.read();
pos3=Serial.read();
show_data();
}
}
}
void show_data()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("pos=");
lcd.print(pos);
lcd.print(',');
lcd.print("pos1=");
lcd.print(pos1);
lcd.setCursor(0,1);
lcd.print("pos2=");
lcd.print(pos2);
lcd.print(',');
lcd.print("pos3=");
lcd.print(pos3);
}
void updateServo()
{
myservo.write(pos);
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
delay(8);
}[/code][/code] |