极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 47192|回复: 38

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

[复制链接]
发表于 2015-10-5 20:05:59 | 显示全部楼层 |阅读模式
     因为喜欢机器人,所以接触了arduino,因为喜欢arduino,所以接触了processing。某夜突发奇想,觉得可以用processing的key函数和mouse函数控制桌面码垛机的运动。嘿嘿,刚好利用国庆假期,用了5天实现自己的想法。
ps:网上关于processing与arduino互动的例子还真不多,有也基本是利用processing将arduino输出的数据可视化,
利用processing来控制arduino的基本没有哈。
pps:楼主发现淘宝上有商家直接拿楼主设计的桌面码垛机在卖,本想着共享设计文件是为了大家学习交流用的,没想到居然有人拿来盈利。找店主理论,他居然说是很久前在gradcad上下载的。希望各位网友在浏览淘宝是看到和我一样设计的4自由度机械臂是千万别买呀。
  1. [code]/**
  2. *processing control a 4-DOF mechanical arm
  3. *by jorneryChen
  4. *
  5. *my emial address is [email protected]
  6. */
  7. #include<Servo.h>

  8. #include<LiquidCrystal.h>

  9. LiquidCrystal lcd(12,11,10,9,8,7);

  10. Servo myservo;[pre lang="processing" line="1"]/**
  11. *processing control a 4-DOF mechanical arm
  12. *by jorneryChen
  13. *
  14. *my emial address is [email protected]
  15. */

  16. import processing.serial.*;
  17. Serial port;
  18. boolean serial;
  19. //输出参数
  20. float x=150;
  21. float y=0;
  22. float pos1,pos2;
  23. int pos=72;
  24. int pos3=90;
  25. int a=2;
  26. float posa;
  27. float A;
  28. float B;
  29. float C;
  30. float D;
  31. float pi=acos(-1.0);
  32. //界面参数
  33. PFont font;
  34. void setup()
  35. {
  36.   size(500,500);
  37.   frameRate(53);
  38.   port=new Serial(this,"COM3",9600);
  39.   font=loadFont("LilyUPCBold-48.vlw");
  40.   smooth();
  41. }
  42. void draw()
  43. {
  44.   math_funtion();
  45.   inter_face();
  46.   picture();
  47.   control();
  48. }
  49. void math_funtion()
  50. {
  51.   A=sqrt(x*x+y*y);//数学方程
  52.   B=atan(y/x);
  53.   C=acos(A/300);
  54.   D=acos(1.0-A*A/45000);
  55.   D=D/pi*180;
  56.   posa=B+C;
  57.   pos1=posa/pi*180;
  58.   pos2=180-pos1-D;
  59. }
  60. void inter_face()
  61. {
  62.    background(255);
  63.   textSize(40);
  64.   fill(245,12,32);
  65.   text("C",5,40);
  66.   fill(38,245,12);
  67.   text("L",35,40);
  68.   fill(12,91,245);
  69.   text("Y",55,40);
  70.   fill(66,204,227);
  71.   textSize(40);
  72.   text("made by yao",120,40);
  73.   textSize(30);
  74.   fill(211,191,0);
  75.   text("this interface shows data only",5,80);
  76.   fill(0);
  77.   textSize(20);
  78.   text("X=",5,110);
  79.   text(int(x),35,110);
  80.   text("Y=",85,110);
  81.   text(int(y),115,110);
  82.   text("POS=",5,140);
  83.   text(int(pos),60,140);
  84.   text("POS1=",105,140);
  85.   text(int(pos1),175,140);
  86.   text("POS2=",225,140);
  87.   text(int(pos2),295,140);
  88.   text("POS3=",345,140);
  89.   text(int(pos3),415,140);
  90.     if((keyPressed==true)&&(key=='i'))
  91.    {
  92.      fill(0,211,58);
  93.     text("opening",250,110);
  94.    }
  95.     else if((keyPressed==true)&&(key=='k'))
  96.     {
  97.       fill(0,211,58);
  98.      text("closing",250,110);
  99.     }
  100. }
  101. void picture()
  102. {
  103.   strokeWeight(4);
  104.   fill(255);
  105.   rect(5,160,490,320);
  106.   ellipse(170,320,300,300);
  107.   stroke(0);
  108.   line(415,170,415,470);
  109.   float n=map(pos3,0,180,300,40);
  110.   float m=map(x,0,300,450,190);
  111.   fill(0,0,150);
  112.   ellipse(n,m,30,30);
  113.   float l=map(y,-150,150,170,470);
  114.   l=constrain(l,170,470);
  115.   ellipse(415,l,30,30);
  116. }
  117. void control()
  118. {
  119.   if((keyPressed==true)&&(key=='a'))
  120.   {
  121.     x-=a;
  122.     serial=true;
  123.   }
  124.   else if((keyPressed==true)&&(key=='d'))
  125.   {
  126.     x+=a;
  127.     serial=true;
  128.   }
  129.   else if((keyPressed==true)&&(key=='w'))
  130.   {
  131.     y-=a;
  132.     serial=true;
  133.   }
  134.   else if((keyPressed==true)&&(key=='s'))
  135.   {
  136.     y+=a;
  137.     serial=true;
  138.   }
  139.    else if((keyPressed==true)&&(key=='i'))
  140.    {
  141.      pos+=a;
  142.      pos=constrain(pos,72,160);
  143.      serial=true;
  144.    }
  145.     else if((keyPressed==true)&&(key=='k'))
  146.     {
  147.       pos-=a;
  148.       pos=constrain(pos,72,160);
  149.       serial=true;
  150.     }
  151.      else if((keyPressed==true)&&(key=='j'))
  152.      {
  153.        pos3+=1;
  154.        serial=true;
  155.      }
  156.       else if((keyPressed==true)&&(key=='l'))
  157.       {
  158.         pos3-=1;
  159.         serial=true;
  160.       }
  161.       if(serial){
  162.           send_data();
  163.       }
  164. }
  165. void send_data()
  166. {
  167.   print("pos=");
  168.   print(int(pos));
  169.   print(',');
  170.   print("pos1=");
  171.   print(int(pos1));
  172.   print(',');
  173.   print("pos2=");
  174.   print(int(pos2));
  175.   print(',');
  176.   print("pos3=");
  177.   println(pos3);
  178.   port.write('%');
  179.   port.write(int(pos));
  180. // port.write(';');
  181.   port.write(int(pos1));
  182. // port.write(';');
  183.   port.write(int(pos2));
  184. //  port.write(';');
  185.   port.write(int(pos3));
  186.   serial=false;
  187. }
复制代码

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]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2015-10-5 20:09:37 | 显示全部楼层
为何代码发不了???再试一次
  1. /**
  2. *processing control a 4-DOF mechanical arm
  3. *by jorneryChen
  4. *
  5. *my emial address is [email protected]
  6. */

  7. import processing.serial.*;
  8. Serial port;
  9. boolean serial;
  10. //输出参数
  11. float x=150;
  12. float y=0;
  13. float pos1,pos2;
  14. int pos=72;
  15. int pos3=90;
  16. int a=2;
  17. float posa;
  18. float A;
  19. float B;
  20. float C;
  21. float D;
  22. float pi=acos(-1.0);
  23. //界面参数
  24. PFont font;
  25. void setup()
  26. {
  27.   size(500,500);
  28.   frameRate(53);
  29.   port=new Serial(this,"COM3",9600);
  30.   font=loadFont("LilyUPCBold-48.vlw");
  31.   smooth();
  32. }
  33. void draw()
  34. {
  35.   math_funtion();
  36.   inter_face();
  37.   picture();
  38.   control();
  39. }
  40. void math_funtion()
  41. {
  42.   A=sqrt(x*x+y*y);//数学方程
  43.   B=atan(y/x);
  44.   C=acos(A/300);
  45.   D=acos(1.0-A*A/45000);
  46.   D=D/pi*180;
  47.   posa=B+C;
  48.   pos1=posa/pi*180;
  49.   pos2=180-pos1-D;
  50. }
  51. void inter_face()
  52. {
  53.    background(255);
  54.   textSize(40);
  55.   fill(245,12,32);
  56.   text("C",5,40);
  57.   fill(38,245,12);
  58.   text("L",35,40);
  59.   fill(12,91,245);
  60.   text("Y",55,40);
  61.   fill(66,204,227);
  62.   textSize(40);
  63.   text("made by yao",120,40);
  64.   textSize(30);
  65.   fill(211,191,0);
  66.   text("this interface shows data only",5,80);
  67.   fill(0);
  68.   textSize(20);
  69.   text("X=",5,110);
  70.   text(int(x),35,110);
  71.   text("Y=",85,110);
  72.   text(int(y),115,110);
  73.   text("POS=",5,140);
  74.   text(int(pos),60,140);
  75.   text("POS1=",105,140);
  76.   text(int(pos1),175,140);
  77.   text("POS2=",225,140);
  78.   text(int(pos2),295,140);
  79.   text("POS3=",345,140);
  80.   text(int(pos3),415,140);
  81.     if((keyPressed==true)&&(key=='i'))
  82.    {
  83.      fill(0,211,58);
  84.     text("opening",250,110);
  85.    }
  86.     else if((keyPressed==true)&&(key=='k'))
  87.     {
  88.       fill(0,211,58);
  89.      text("closing",250,110);
  90.     }
  91. }
  92. void picture()
  93. {
  94.   strokeWeight(4);
  95.   fill(255);
  96.   rect(5,160,490,320);
  97.   ellipse(170,320,300,300);
  98.   stroke(0);
  99.   line(415,170,415,470);
  100.   float n=map(pos3,0,180,300,40);
  101.   float m=map(x,0,300,450,190);
  102.   fill(0,0,150);
  103.   ellipse(n,m,30,30);
  104.   float l=map(y,-150,150,170,470);
  105.   l=constrain(l,170,470);
  106.   ellipse(415,l,30,30);
  107. }
  108. void control()
  109. {
  110.   if((keyPressed==true)&&(key=='a'))
  111.   {
  112.     x-=a;
  113.     serial=true;
  114.   }
  115.   else if((keyPressed==true)&&(key=='d'))
  116.   {
  117.     x+=a;
  118.     serial=true;
  119.   }
  120.   else if((keyPressed==true)&&(key=='w'))
  121.   {
  122.     y-=a;
  123.     serial=true;
  124.   }
  125.   else if((keyPressed==true)&&(key=='s'))
  126.   {
  127.     y+=a;
  128.     serial=true;
  129.   }
  130.    else if((keyPressed==true)&&(key=='i'))
  131.    {
  132.      pos+=a;
  133.      pos=constrain(pos,72,160);
  134.      serial=true;
  135.    }
  136.     else if((keyPressed==true)&&(key=='k'))
  137.     {
  138.       pos-=a;
  139.       pos=constrain(pos,72,160);
  140.       serial=true;
  141.     }
  142.      else if((keyPressed==true)&&(key=='j'))
  143.      {
  144.        pos3+=1;
  145.        serial=true;
  146.      }
  147.       else if((keyPressed==true)&&(key=='l'))
  148.       {
  149.         pos3-=1;
  150.         serial=true;
  151.       }
  152.       if(serial){
  153.           send_data();
  154.       }
  155. }
  156. void send_data()
  157. {
  158.   print("pos=");
  159.   print(int(pos));
  160.   print(',');
  161.   print("pos1=");
  162.   print(int(pos1));
  163.   print(',');
  164.   print("pos2=");
  165.   print(int(pos2));
  166.   print(',');
  167.   print("pos3=");
  168.   println(pos3);
  169.   port.write('%');
  170.   port.write(int(pos));
  171. // port.write(';');
  172.   port.write(int(pos1));
  173. // port.write(';');
  174.   port.write(int(pos2));
  175. //  port.write(';');
  176.   port.write(int(pos3));
  177.   serial=false;
  178. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2015-10-5 21:30:07 | 显示全部楼层
楼主的机械臂也分享一下可行?
回复 支持 反对

使用道具 举报

发表于 2015-10-5 21:51:35 | 显示全部楼层
不错啊 这代码深化下 可以考虑研发机械手了。。。
一些代码。。。。很好的东西~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-5 22:20:25 | 显示全部楼层
darkorigin 发表于 2015-10-5 21:51
不错啊 这代码深化下 可以考虑研发机械手了。。。
一些代码。。。。很好的东西~~

大神你说的一些代码是指那些????
回复 支持 反对

使用道具 举报

发表于 2015-10-5 22:47:33 | 显示全部楼层
虽然我不太明白为什么LZ这么反感别人用你已经公开源代码盈利,
不过这种操作界面的确很直观。
如果移植到触摸设备上应该会有更好的体验。
超赞的说{:soso_e179:}{:soso_e179:}{:soso_e179:}
回复 支持 反对

使用道具 举报

发表于 2015-10-5 23:30:59 | 显示全部楼层
LZ,下面这段代码,稍微解释下呗,不懂
  1. 41.void math_funtion()

  2. 42.{

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

  4. 44.  B=atan(y/x);

  5. 45.  C=acos(A/300);

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

  7. 47.  D=D/pi*180;

  8. 48.  posa=B+C;

  9. 49.  pos1=posa/pi*180;

  10. 50.  pos2=180-pos1-D;

  11. 51.}
复制代码
回复 支持 反对

使用道具 举报

发表于 2015-10-5 23:45:07 | 显示全部楼层
这个控制会直观点

回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-6 00:01:44 | 显示全部楼层
henrypcw 发表于 2015-10-5 23:45
这个控制会直观点

嗯嗯,比我那个好多了,我刚接触processing,以后会更新的。谢谢大神。
回复 支持 反对

使用道具 举报

发表于 2015-10-6 01:35:57 | 显示全部楼层
夏夜繁星 发表于 2015-10-5 22:20
大神你说的一些代码是指那些????

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

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

还有做成不一定非要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
偶不是大神。。。只是论坛混的太久了。。。潜水员。。。

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

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

使用道具 举报

发表于 2015-10-6 09:04:57 | 显示全部楼层
夏夜繁星 发表于 2015-10-6 09:00
嗯嗯,抖动是个大问题,http://www.tudou.com/programs/view/zpUtZ78B_w8/用ps2控制都没这么都,我觉得应 ...

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

回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-6 10:09:02 | 显示全部楼层
henrypcw 发表于 2015-10-5 23:45
这个控制会直观点

大神,大神,你的processing程序和arduino程序能不能发给我,感激不尽呀呀呀呀
[email protected]
回复 支持 反对

使用道具 举报

发表于 2015-10-6 10:22:22 来自手机 | 显示全部楼层
夏夜繁星 发表于 2015-10-6 10:09
大神,大神,你的processing程序和arduino程序能不能发给我,感激不尽呀呀呀呀

这不是我的,是在网上看到的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 10:38 , Processed in 0.077448 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表