极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15021|回复: 7

写字机器人代码

[复制链接]
发表于 2014-3-26 13:27:58 | 显示全部楼层 |阅读模式
本帖最后由 Damn_intuition 于 2014-3-26 13:29 编辑
  1. #define CALIBRATION

  2. // When in calibration mode, adjust the following factor until the servos move exactly 90 degrees
  3. #define SERVOFAKTOR 620

  4. // Zero-position of left and right servo
  5. // When in calibration mode, adjust the NULL-values so that the servo arms are at all times parallel
  6. // either to the X or Y axis
  7. #define SERVOLEFTNULL 1900
  8. #define SERVORIGHTNULL 984

  9. #define SERVOPINLIFT  2
  10. #define SERVOPINLEFT  3
  11. #define SERVOPINRIGHT 4

  12. // lift positions of lifting servo
  13. #define LIFT0 1080 // on drawing surface
  14. #define LIFT1 925  // between numbers
  15. #define LIFT2 725  // going towards sweeper

  16. // speed of liftimg arm, higher is slower
  17. #define LIFTSPEED 1500

  18. // length of arms
  19. #define L1 35
  20. #define L2 55.1
  21. #define L3 13.2


  22. // origin points of left and right servo
  23. #define O1X 22
  24. #define O1Y -25
  25. #define O2X 47
  26. #define O2Y -25



  27. #include <Time.h> // see [url=http://playground.arduino.cc/Code/time]http://playground.arduino.cc/Code/time[/url]
  28. #include <Servo.h>

  29. int servoLift = 1500;

  30. Servo servo1;  //
  31. Servo servo2;  //
  32. Servo servo3;  //

  33. volatile double lastX = 75;
  34. volatile double lastY = 47.5;

  35. int last_min = 0;

  36. void setup()
  37. {
  38.   // Set current time only the first to values, hh,mm are needed
  39.   setTime(19,38,0,0,0,0);

  40.   drawTo(75.2, 47);
  41.   lift(0);
  42.   servo1.attach(SERVOPINLIFT);  //  lifting servo
  43.   servo2.attach(SERVOPINLEFT);  //  left servo
  44.   servo3.attach(SERVOPINRIGHT);  //  right servo
  45.   delay(1000);

  46. }

  47. void loop()
  48. {

  49. #ifdef CALIBRATION

  50.   // Servohorns will have 90° between movements, parallel to x and y axis
  51.   drawTo(-3, 29.2);
  52.   delay(500);
  53.   drawTo(74.1, 28);
  54.   delay(500);

  55. #else


  56.   int i = 0;
  57.   if (last_min != minute()) {

  58.     if (!servo1.attached()) servo1.attach(SERVOPINLIFT);
  59.     if (!servo2.attached()) servo2.attach(SERVOPINLEFT);
  60.     if (!servo3.attached()) servo3.attach(SERVOPINRIGHT);

  61.     lift(0);

  62.     hour();
  63.     while ((i+1)*10 <= hour())
  64.     {
  65.       i++;
  66.     }

  67.     number(3, 3, 111, 1);
  68.     number(5, 25, i, 0.9);
  69.     number(19, 25, (hour()-i*10), 0.9);
  70.     number(28, 25, 11, 0.9);

  71.     i=0;
  72.     while ((i+1)*10 <= minute())
  73.     {
  74.       i++;
  75.     }
  76.     number(34, 25, i, 0.9);
  77.     number(48, 25, (minute()-i*10), 0.9);
  78.     lift(2);
  79.     drawTo(74.2, 47.5);
  80.     lift(1);
  81.     last_min = minute();

  82.     servo1.detach();
  83.     servo2.detach();
  84.     servo3.detach();
  85.   }

  86. #endif

  87. }

  88. // Writing numeral with bx by being the bottom left originpoint. Scale 1 equals a 20 mm high font.
  89. // The structure follows this principle: move to first startpoint of the numeral, lift down, draw numeral, lift up
  90. void number(float bx, float by, int num, float scale) {

  91.   switch (num) {

  92.   case 0:
  93.     drawTo(bx + 12 * scale, by + 6 * scale);
  94.     lift(0);
  95.     bogenGZS(bx + 7 * scale, by + 10 * scale, 10 * scale, -0.8, 6.7, 0.5);
  96.     lift(1);
  97.     break;
  98.   case 1:

  99.     drawTo(bx + 3 * scale, by + 15 * scale);
  100.     lift(0);
  101.     drawTo(bx + 10 * scale, by + 20 * scale);
  102.     drawTo(bx + 10 * scale, by + 0 * scale);
  103.     lift(1);
  104.     break;
  105.   case 2:
  106.     drawTo(bx + 2 * scale, by + 12 * scale);
  107.     lift(0);
  108.     bogenUZS(bx + 8 * scale, by + 14 * scale, 6 * scale, 3, -0.8, 1);
  109.     drawTo(bx + 1 * scale, by + 0 * scale);
  110.     drawTo(bx + 12 * scale, by + 0 * scale);
  111.     lift(1);
  112.     break;
  113.   case 3:
  114.     drawTo(bx + 2 * scale, by + 17 * scale);
  115.     lift(0);
  116.     bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 3, -2, 1);
  117.     bogenUZS(bx + 5 * scale, by + 5 * scale, 5 * scale, 1.57, -3, 1);
  118.     lift(1);
  119.     break;
  120.   case 4:
  121.     drawTo(bx + 10 * scale, by + 0 * scale);
  122.     lift(0);
  123.     drawTo(bx + 10 * scale, by + 20 * scale);
  124.     drawTo(bx + 2 * scale, by + 6 * scale);
  125.     drawTo(bx + 12 * scale, by + 6 * scale);
  126.     lift(1);
  127.     break;
  128.   case 5:
  129.     drawTo(bx + 2 * scale, by + 5 * scale);
  130.     lift(0);
  131.     bogenGZS(bx + 5 * scale, by + 6 * scale, 6 * scale, -2.5, 2, 1);
  132.     drawTo(bx + 5 * scale, by + 20 * scale);
  133.     drawTo(bx + 12 * scale, by + 20 * scale);
  134.     lift(1);
  135.     break;
  136.   case 6:
  137.     drawTo(bx + 2 * scale, by + 10 * scale);
  138.     lift(0);
  139.     bogenUZS(bx + 7 * scale, by + 6 * scale, 6 * scale, 2, -4.4, 1);
  140.     drawTo(bx + 11 * scale, by + 20 * scale);
  141.     lift(1);
  142.     break;
  143.   case 7:
  144.     drawTo(bx + 2 * scale, by + 20 * scale);
  145.     lift(0);
  146.     drawTo(bx + 12 * scale, by + 20 * scale);
  147.     drawTo(bx + 2 * scale, by + 0);
  148.     lift(1);
  149.     break;
  150.   case 8:
  151.     drawTo(bx + 5 * scale, by + 10 * scale);
  152.     lift(0);
  153.     bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 4.7, -1.6, 1);
  154.     bogenGZS(bx + 5 * scale, by + 5 * scale, 5 * scale, -4.7, 2, 1);
  155.     lift(1);
  156.     break;

  157.   case 9:
  158.     drawTo(bx + 9 * scale, by + 11 * scale);
  159.     lift(0);
  160.     bogenUZS(bx + 7 * scale, by + 15 * scale, 5 * scale, 4, -0.5, 1);
  161.     drawTo(bx + 5 * scale, by + 0);
  162.     lift(1);
  163.     break;

  164.   case 111:

  165.     lift(0);
  166.     drawTo(70, 46);
  167.     drawTo(65, 43);

  168.     drawTo(65, 49);
  169.     drawTo(5, 49);
  170.     drawTo(5, 45);
  171.     drawTo(65, 45);
  172.     drawTo(65, 40);

  173.     drawTo(5, 40);
  174.     drawTo(5, 35);
  175.     drawTo(65, 35);
  176.     drawTo(65, 30);

  177.     drawTo(5, 30);
  178.     drawTo(5, 25);
  179.     drawTo(65, 25);
  180.     drawTo(65, 20);

  181.     drawTo(5, 20);
  182.     drawTo(60, 44);

  183.     drawTo(75.2, 47);
  184.     lift(2);

  185.     break;

  186.   case 11:
  187.     drawTo(bx + 5 * scale, by + 15 * scale);
  188.     lift(0);
  189.     bogenGZS(bx + 5 * scale, by + 15 * scale, 0.1 * scale, 1, -1, 1);
  190.     lift(1);
  191.     drawTo(bx + 5 * scale, by + 5 * scale);
  192.     lift(0);
  193.     bogenGZS(bx + 5 * scale, by + 5 * scale, 0.1 * scale, 1, -1, 1);
  194.     lift(1);
  195.     break;

  196.   }
  197. }



  198. void lift(char lift) {
  199.   switch (lift) {
  200.     // room to optimize  !

  201.   case 0: //850

  202.       if (servoLift >= LIFT0) {
  203.       while (servoLift >= LIFT0)
  204.       {
  205.         servoLift--;
  206.         servo1.writeMicroseconds(servoLift);                                
  207.         delayMicroseconds(LIFTSPEED);
  208.       }
  209.     }
  210.     else {
  211.       while (servoLift <= LIFT0) {
  212.         servoLift++;
  213.         servo1.writeMicroseconds(servoLift);
  214.         delayMicroseconds(LIFTSPEED);

  215.       }

  216.     }

  217.     break;

  218.   case 1: //150

  219.     if (servoLift >= LIFT1) {
  220.       while (servoLift >= LIFT1) {
  221.         servoLift--;
  222.         servo1.writeMicroseconds(servoLift);
  223.         delayMicroseconds(LIFTSPEED);

  224.       }
  225.     }
  226.     else {
  227.       while (servoLift <= LIFT1) {
  228.         servoLift++;
  229.         servo1.writeMicroseconds(servoLift);
  230.         delayMicroseconds(LIFTSPEED);
  231.       }

  232.     }

  233.     break;

  234.   case 2:

  235.     if (servoLift >= LIFT2) {
  236.       while (servoLift >= LIFT2) {
  237.         servoLift--;
  238.         servo1.writeMicroseconds(servoLift);
  239.         delayMicroseconds(LIFTSPEED);
  240.       }
  241.     }
  242.     else {
  243.       while (servoLift <= LIFT2) {
  244.         servoLift++;
  245.         servo1.writeMicroseconds(servoLift);                                
  246.         delayMicroseconds(LIFTSPEED);
  247.       }
  248.     }
  249.     break;
  250.   }
  251. }


  252. void bogenUZS(float bx, float by, float radius, int start, int ende, float sqee) {
  253.   float inkr = -0.05;
  254.   float count = 0;

  255.   do {
  256.     drawTo(sqee * radius * cos(start + count) + bx,
  257.     radius * sin(start + count) + by);
  258.     count += inkr;
  259.   }
  260.   while ((start + count) > ende);

  261. }

  262. void bogenGZS(float bx, float by, float radius, int start, int ende, float sqee) {
  263.   float inkr = 0.05;
  264.   float count = 0;

  265.   do {
  266.     drawTo(sqee * radius * cos(start + count) + bx,
  267.     radius * sin(start + count) + by);
  268.     count += inkr;
  269.   }
  270.   while ((start + count) <= ende);
  271. }


  272. void drawTo(double pX, double pY) {
  273.   double dx, dy, c;
  274.   int i;

  275.   // dx dy of new point
  276.   dx = pX - lastX;
  277.   dy = pY - lastY;
  278.   //path lenght in mm, times 4 equals 4 steps per mm
  279.   c = floor(4 * sqrt(dx * dx + dy * dy));

  280.   if (c < 1) c = 1;

  281.   for (i = 0; i <= c; i++) {
  282.     // draw line point by point
  283.     set_XY(lastX + (i * dx / c), lastY + (i * dy / c));

  284.   }

  285.   lastX = pX;
  286.   lastY = pY;
  287. }

  288. double return_angle(double a, double b, double c) {
  289.   // cosine rule for angle between c and a
  290.   return acos((a * a + c * c - b * b) / (2 * a * c));
  291. }

  292. void set_XY(double Tx, double Ty)
  293. {
  294.   delay(1);
  295.   double dx, dy, c, a1, a2, Hx, Hy;

  296.   // calculate triangle between pen, servoLeft and arm joint
  297.   // cartesian dx/dy
  298.   dx = Tx - O1X;
  299.   dy = Ty - O1Y;

  300.   // polar lemgth (c) and angle (a1)
  301.   c = sqrt(dx * dx + dy * dy); //
  302.   a1 = atan2(dy, dx); //
  303.   a2 = return_angle(L1, L2, c);

  304.   servo2.writeMicroseconds(floor(((a2 + a1 - M_PI) * SERVOFAKTOR) + SERVOLEFTNULL));

  305.   // calculate joinr arm point for triangle of the right servo arm
  306.   a2 = return_angle(L2, L1, c);
  307.   Hx = Tx + L3 * cos((a1 - a2 + 0.621) + M_PI); //36,5°
  308.   Hy = Ty + L3 * sin((a1 - a2 + 0.621) + M_PI);

  309.   // calculate triangle between pen joint, servoRight and arm joint
  310.   dx = Hx - O2X;
  311.   dy = Hy - O2Y;

  312.   c = sqrt(dx * dx + dy * dy);
  313.   a1 = atan2(dy, dx);
  314.   a2 = return_angle(L1, (L2 - L3), c);

  315.   servo3.writeMicroseconds(floor(((a1 - a2) * SERVOFAKTOR) + SERVORIGHTNULL));

  316. }




复制代码



回复

使用道具 举报

 楼主| 发表于 2014-3-26 13:31:43 | 显示全部楼层
本帖最后由 Damn_intuition 于 2014-3-26 13:33 编辑

下面是机械部分设计,有兴趣的朋友可以用3D打印做一个。
具体原作者是谁已经不知道了,设计由Johannes Heberlein 提供。

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

发表于 2014-3-26 13:57:18 | 显示全部楼层
有视频吗?放上来看看
回复 支持 反对

使用道具 举报

发表于 2014-3-26 18:31:07 | 显示全部楼层
sn10 发表于 2014-3-26 13:57
有视频吗?放上来看看

应该是那个小贱笔  写时间的那个
回复 支持 反对

使用道具 举报

发表于 2014-3-26 21:38:58 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2014-7-25 19:20:08 | 显示全部楼层
你好,问一下程序当中bogenUZS,bogenGZS代表什么意思?
回复 支持 反对

使用道具 举报

发表于 2014-7-26 08:41:42 | 显示全部楼层
有没有中文注释的
回复 支持 反对

使用道具 举报

发表于 2017-11-8 21:55:16 | 显示全部楼层
很不错哦,顶上!!!!!!!!!!
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-29 09:13 , Processed in 0.060149 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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