极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9867|回复: 1

制作小贱钟出问题求大神帮助

[复制链接]
发表于 2014-12-14 15:56:27 | 显示全部楼层 |阅读模式
网上买了一套小贱钟的亚克力零件,用arduino pro mini做控制板,安装过程,用商家给的调试程序调整摆臂位置均正常,但用商家给的主程序(摆臂位置已我改成调试后的值)运行出错,不写时间只是不停的在同一位置上点点,整个一磕头机。郁闷啊
代码如下
  1. //左右悬臂舵机的90度位置,,数字增加,左侧舵机顺时针调整,右侧舵机逆时针调整
  2. #define SERVOFAKTORLEFT 620
  3. #define SERVOFAKTORRIGHT 580

  4. //左右悬臂舵机的 0或180度位置,,数字增加,左侧舵机顺时针调整,右侧舵机逆时针调整
  5. #define SERVOLEFTNULL 2060
  6. #define SERVORIGHTNULL 1090

  7. //升举舵机的3个角度
  8. #define LIFT0 1140 // on drawing surface
  9. #define LIFT1 925  // between numbers
  10. #define LIFT2 725  // going towards sweeper

  11. //
  12. //以上参数,请运行调试程序 plotclockadj ,调整好位置后,将数据复制过来
  13. //

  14. //三只舵机的接口
  15. #define SERVOPINLIFT  2
  16. #define SERVOPINLEFT  3
  17. #define SERVOPINRIGHT 4

  18. // 速度
  19. #define LIFTSPEED 1500

  20. // 悬臂的长度,根据图纸测量,无需改变
  21. #define L1 35
  22. #define L2 57.2
  23. #define L3 14.2


  24. // 左右舵机轴心的位置
  25. #define O1X 22
  26. #define O1Y -25
  27. #define O2X 47
  28. #define O2Y -25



  29. #include <Time.h> // see http://playground.arduino.cc/Code/time
  30. //这里报错请把压缩包内的time.h复制到C:\Program Files\Arduino\libraries下
  31. #include <Servo.h>

  32. int  rubberx=72,rubbery=42; //笔擦的坐标位置,如不能对准笔擦可以微调单位毫米

  33. int servoLift = 1500;

  34. Servo servo1;  //
  35. Servo servo2;  //
  36. Servo servo3;  //

  37. volatile double lastX = 70;
  38. volatile double lastY = 42;

  39. int last_min = 0;

  40. void setup()
  41. {
  42.   // 在这里设置一个初始时间
  43.   setTime(06,41,0,0,0,0);

  44.   
  45.   servo1.attach(SERVOPINLIFT);  //  lifting servo
  46.   servo2.attach(SERVOPINLEFT);  //  left servo
  47.   servo3.attach(SERVOPINRIGHT);  //  right servo
  48.   
  49.   lift(1); //抬笔
  50.   drawTo(rubberx, rubbery); //停留在笔擦位置
  51.   delay(1000);

  52. }

  53. void loop()
  54. {

  55. int i = 0;
  56. if  ( last_min != minute())   //现在是不停的书写,如果每分钟写一次就改成        if (1 || last_min != minute())
  57.   {

  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(rubberx, rubbery); //回到笔擦位置
  80.     lift(1);
  81.     last_min = minute();

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


  86. }

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

  90.   switch (num) {

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

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

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

  163.   case 111://擦除面板的动作

  164.     lift(0);
  165.     drawTo(rubberx, rubbery);
  166.     drawTo(58, 42);

  167.     drawTo(58, 45);
  168.     drawTo(2, 45);
  169.     drawTo(2, 41);
  170.     drawTo(58, 41);
  171.     drawTo(60, 37);

  172.     drawTo(2, 37);
  173.     drawTo(2, 33);
  174.     drawTo(60, 33);
  175.     drawTo(60, 29);

  176.     drawTo(2, 29);
  177.     drawTo(2, 25);
  178.     drawTo(60, 25);
  179.     drawTo(60, 20);

  180.     drawTo(2, 20);
  181.     drawTo(60, rubbery);

  182.     drawTo(rubberx + 3 , rubbery); //向左侧多推一些,让笔擦能够进入笔架
  183.     lift(2);

  184.     break;

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

  195.   }
  196. }



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

  200.   case 0: //850

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

  214.       }

  215.     }

  216.     break;

  217.   case 1: //150

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

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

  231.     }

  232.     break;

  233.   case 2:

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


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

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

  260. }

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

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


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

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

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

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

  283.   }

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

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

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

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

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

  303.     servo2.writeMicroseconds(floor(((a2 + a1 - M_PI) * SERVOFAKTORLEFT) + SERVOLEFTNULL));

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

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

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

  314.    servo3.writeMicroseconds(floor(((a1 - a2) * SERVOFAKTORRIGHT) + SERVORIGHTNULL));

  315. }




复制代码

下面是视频链接
http://v.qq.com/page/i/m/z/i0142894amz.html
回复

使用道具 举报

 楼主| 发表于 2014-12-14 15:58:27 | 显示全部楼层
求各位大神看看问题出在哪了
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-5-6 03:23 , Processed in 0.048964 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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