夏夜繁星 发表于 2015-10-5 20:05:59

Processing与arduino互动之4自由度桌面码垛机

   因为喜欢机器人,所以接触了arduino,因为喜欢arduino,所以接触了processing。某夜突发奇想,觉得可以用processing的key函数和mouse函数控制桌面码垛机的运动。嘿嘿,刚好利用国庆假期,用了5天实现自己的想法。
ps:网上关于processing与arduino互动的例子还真不多,有也基本是利用processing将arduino输出的数据可视化,
利用processing来控制arduino的基本没有哈。
pps:楼主发现淘宝上有商家直接拿楼主设计的桌面码垛机在卖,本想着共享设计文件是为了大家学习交流用的,没想到居然有人拿来盈利。找店主理论,他居然说是很久前在gradcad上下载的。希望各位网友在浏览淘宝是看到和我一样设计的4自由度机械臂是千万别买呀。http://www.tudou.com/programs/view/di9QwbJgYEs//**
*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;/**
*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);
}

夏夜繁星 发表于 2015-10-5 20:09:37

为何代码发不了???再试一次/**
*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;
}

自己一只 发表于 2015-10-5 21:30:07

楼主的机械臂也分享一下可行?

darkorigin 发表于 2015-10-5 21:51:35

不错啊 这代码深化下 可以考虑研发机械手了。。。
一些代码。。。。很好的东西~~

夏夜繁星 发表于 2015-10-5 22:20:25

darkorigin 发表于 2015-10-5 21:51 static/image/common/back.gif
不错啊 这代码深化下 可以考虑研发机械手了。。。
一些代码。。。。很好的东西~~

大神你说的一些代码是指那些????:$

wing 发表于 2015-10-5 22:47:33

虽然我不太明白为什么LZ这么反感别人用你已经公开源代码盈利,
不过这种操作界面的确很直观。
如果移植到触摸设备上应该会有更好的体验。
超赞的说{:soso_e179:}{:soso_e179:}{:soso_e179:}

I-robofan 发表于 2015-10-5 23:30:59

LZ,下面这段代码,稍微解释下呗,不懂;P41.void math_funtion()

42.{

43.A=sqrt(x*x+y*y);//数学方程

44.B=atan(y/x);

45.C=acos(A/300);

46.D=acos(1.0-A*A/45000);

47.D=D/pi*180;

48.posa=B+C;

49.pos1=posa/pi*180;

50.pos2=180-pos1-D;

51.}

henrypcw 发表于 2015-10-5 23:45:07

这个控制会直观点

http://img.app.meitudata.com/meitumv/mtplayer4/swf/mp4Player.swf?data=%7B%22id%22%3A418434594%2C%22source%22%3A%22embed%22%7D&vcastr_file=http%3A%2F%2Fmvvideo2.meitudata.com%2F5611199534aa23313.mp4&logoText=Ta%E7%9A%84%E7%BE%8E%E6%8B%8D&appDownloadURL=http%3A%2F%2Fwww.meipai.com%2Fuser%2F1040205060&shareEnabled=1&shareTitle=%E5%B0%8F%E4%BC%99%E4%BC%B4%E4%BB%AC%EF%BC%8C%E5%BF%AB%E6%9D%A5%E5%9B%B4%E8%A7%82%EF%BC%81&qzoneShareURL=http%3A%2F%2Fsns.qzone.qq.com%2Fcgi-bin%2Fqzshare%2Fcgi_qzshare_onekey%3Furl%3Dhttp%3A%2F%2Fwww.meipai.com%2Fmedia%2F418434594%26site%3D%E7%BE%8E%E6%8B%8D&qqShareURL=http%3A%2F%2Fconnect.qq.com%2Fwidget%2Fshareqq%2Findex.html%3Furl%3Dhttp%3A%2F%2Fwww.meipai.com%2Fmedia%2F418434594%26site%3D%E7%BE%8E%E6%8B%8D&weiboShareURL=http%3A%2F%2Fservice.weibo.com%2Fshare%2Fshare.php%3Fappkey%3D680740738%26ralateUid%3D2312920530%26sourceUrl%3Dhttp%3A%2F%2Fwww.meipai.com%2F%26content%3Dutf8%26searchPic%3Dfalse%26url%3Dhttp%3A%2F%2Fwww.meipai.com%2Fmedia%2F418434594

夏夜繁星 发表于 2015-10-6 00:01:44

henrypcw 发表于 2015-10-5 23:45 static/image/common/back.gif
这个控制会直观点

嗯嗯,比我那个好多了,我刚接触processing,以后会更新的。谢谢大神。

darkorigin 发表于 2015-10-6 01:35:57

夏夜繁星 发表于 2015-10-5 22:20 static/image/common/back.gif
大神你说的一些代码是指那些????

偶不是大神。。。只是论坛混的太久了。。。潜水员。。。

我是觉得你这些代码优化一下,比如抖动什么的去除一些

还有做成不一定非要processing作为上位机,。。。。
总之 比较零碎的想法

夏夜繁星 发表于 2015-10-6 07:26:26

I-robofan 发表于 2015-10-5 23:30
LZ,下面这段代码,稍微解释下呗,不懂

这是一个简单的运动方程来着。设小臂与夹持器的连接点的坐标为(x,y)。可以通过几何关系求出x和y与控制大小臂转动的舵机角度的关系。只改变x的值或只改变y的输入值,可以实现夹持器的水平或垂直运动。方便控制。PS(为何我一小时内只能回复一次呀)

夏夜繁星 发表于 2015-10-6 09:00:55

darkorigin 发表于 2015-10-6 01:35 static/image/common/back.gif
偶不是大神。。。只是论坛混的太久了。。。潜水员。。。

我是觉得你这些代码优化一下,比如抖动什么的 ...

嗯嗯,抖动是个大问题,http://www.tudou.com/programs/view/zpUtZ78B_w8/用ps2控制都没这么都,我觉得应该是processing与arduino之间的通信有问题。希望各位大神能指点指点。

Damn_intuition 发表于 2015-10-6 09:04:57

夏夜繁星 发表于 2015-10-6 09:00 static/image/common/back.gif
嗯嗯,抖动是个大问题,http://www.tudou.com/programs/view/zpUtZ78B_w8/用ps2控制都没这么都,我觉得应 ...

其实是代码问题,电机的控制应考虑负载的状况,根据负载惯性算出合适的加减速曲线。

夏夜繁星 发表于 2015-10-6 10:09:02

henrypcw 发表于 2015-10-5 23:45 static/image/common/back.gif
这个控制会直观点

大神,大神,你的processing程序和arduino程序能不能发给我,感激不尽呀呀呀呀
[email protected]

henrypcw 发表于 2015-10-6 10:22:22

夏夜繁星 发表于 2015-10-6 10:09
大神,大神,你的processing程序和arduino程序能不能发给我,感激不尽呀呀呀呀

这不是我的,是在网上看到的
页: [1] 2 3
查看完整版本: Processing与arduino互动之4自由度桌面码垛机