|
本帖最后由 shihaipeng04 于 2014-6-6 00:54 编辑
之前的代码因为误删了一段很重要的内容,所以调试起来非常困难,现在前面的推翻,重头来过。
警告:在没有调试完毕之前,请不要安装笔、笔擦和笔擦支架,以免摆臂和以上零件纠缠在一起,可能会损坏支架。
调试程序,运行调试程序,舵机会移动两只悬臂分别停留在一只水平另一只垂直的位置上。如果差距非常大,可拆下悬臂调节,如果比较接近了,就修改程序第3~6行的四个参数 SERVOFAKTORLEFT 左悬臂垂直位置,SERVOFAKTORRIGHT 右悬臂垂直位置,SERVOLEFTNULL 左悬臂180度位置,SERVORIGHTNULL 右悬臂0度位置(右侧为0度,逆时针画圆),参数增加摆臂逆时针转动,一般增幅50,才能看到比较明显的变化。
调节参数,直到摆臂停在下图的标准位置上为止。
LIFT0、1、2参数原理相同调试方法略过
《调试摆臂用代码》 - //左右悬臂舵机的90度位置
- #define SERVOFAKTORLEFT 550
- #define SERVOFAKTORRIGHT 630 //此参数650是理论中间位置,需要看实际情况调节
-
- //左右悬臂舵机的 0或180度位置
- #define SERVOLEFTNULL 1750
- #define SERVORIGHTNULL 880
-
- //三只舵机的接口
- #define SERVOPINLIFT 2
- #define SERVOPINLEFT 3
- #define SERVOPINRIGHT 4
-
- //升举舵机的3个角度
- #define LIFT0 1080 // on drawing surface
- #define LIFT1 925 // between numbers
- #define LIFT2 725 // going towards sweeper
-
- // 速度
- #define LIFTSPEED 1500
-
- // 悬臂的长度,根据图纸测量,无需改变
- #define L1 35
- #define L2 57.2
- #define L3 14.2
-
-
- // 左右舵机轴心的位置
- #define O1X 23
- #define O1Y -25
- #define O2X 49
- #define O2Y -25
-
-
-
- #include <Time.h> // see [url=http://playground.arduino.cc/Code/time]http://playground.arduino.cc/Code/time[/url]
- #include <Servo.h>
-
- int servoLift = 1500;
-
- Servo servo1; //
- Servo servo2; //
- Servo servo3; //
-
- volatile double lastX = 75;
- volatile double lastY = 47.5;
-
- int last_min = 0;
-
- void setup()
- {
- // Set current time only the first to values, hh,mm are needed
- setTime(19,38,0,0,0,0);
-
- drawTo(38, 23);
- lift(0);
- servo1.attach(SERVOPINLIFT); // lifting servo
- servo2.attach(SERVOPINLEFT); // left servo
- servo3.attach(SERVOPINRIGHT); // right servo
- delay(1000);
-
- }
-
- void loop()
- {
-
-
-
- // 移动左右悬臂,让两只悬臂分别停留在水平和垂直的位置上。 通过修改 SERVOFAKTORLEFT , SERVOFAKTORRIGHT, SERVOLEFTNULL , SERVORIGHTNULL 四个参数来微调
- drawTo(-3, 29.2);
- delay(500);
- drawTo(74.1, 28);
- delay(500);
-
-
-
- }
-
- // Writing numeral with bx by being the bottom left originpoint. Scale 1 equals a 20 mm high font.
- // The structure follows this principle: move to first startpoint of the numeral, lift down, draw numeral, lift up
- void number(float bx, float by, int num, float scale) {
-
- switch (num) {
-
- case 0:
- drawTo(bx + 12 * scale, by + 6 * scale);
- lift(0);
- bogenGZS(bx + 7 * scale, by + 10 * scale, 10 * scale, -0.8, 6.7, 0.5);
- lift(1);
- break;
- case 1:
-
- drawTo(bx + 3 * scale, by + 15 * scale);
- lift(0);
- drawTo(bx + 10 * scale, by + 20 * scale);
- drawTo(bx + 10 * scale, by + 0 * scale);
- lift(1);
- break;
- case 2:
- drawTo(bx + 2 * scale, by + 12 * scale);
- lift(0);
- bogenUZS(bx + 8 * scale, by + 14 * scale, 6 * scale, 3, -0.8, 1);
- drawTo(bx + 1 * scale, by + 0 * scale);
- drawTo(bx + 12 * scale, by + 0 * scale);
- lift(1);
- break;
- case 3:
- drawTo(bx + 2 * scale, by + 17 * scale);
- lift(0);
- bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 3, -2, 1);
- bogenUZS(bx + 5 * scale, by + 5 * scale, 5 * scale, 1.57, -3, 1);
- lift(1);
- break;
- case 4:
- drawTo(bx + 10 * scale, by + 0 * scale);
- lift(0);
- drawTo(bx + 10 * scale, by + 20 * scale);
- drawTo(bx + 2 * scale, by + 6 * scale);
- drawTo(bx + 12 * scale, by + 6 * scale);
- lift(1);
- break;
- case 5:
- drawTo(bx + 2 * scale, by + 5 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 6 * scale, 6 * scale, -2.5, 2, 1);
- drawTo(bx + 5 * scale, by + 20 * scale);
- drawTo(bx + 12 * scale, by + 20 * scale);
- lift(1);
- break;
- case 6:
- drawTo(bx + 2 * scale, by + 10 * scale);
- lift(0);
- bogenUZS(bx + 7 * scale, by + 6 * scale, 6 * scale, 2, -4.4, 1);
- drawTo(bx + 11 * scale, by + 20 * scale);
- lift(1);
- break;
- case 7:
- drawTo(bx + 2 * scale, by + 20 * scale);
- lift(0);
- drawTo(bx + 12 * scale, by + 20 * scale);
- drawTo(bx + 2 * scale, by + 0);
- lift(1);
- break;
- case 8:
- drawTo(bx + 5 * scale, by + 10 * scale);
- lift(0);
- bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 4.7, -1.6, 1);
- bogenGZS(bx + 5 * scale, by + 5 * scale, 5 * scale, -4.7, 2, 1);
- lift(1);
- break;
-
- case 9:
- drawTo(bx + 9 * scale, by + 11 * scale);
- lift(0);
- bogenUZS(bx + 7 * scale, by + 15 * scale, 5 * scale, 4, -0.5, 1);
- drawTo(bx + 5 * scale, by + 0);
- lift(1);
- break;
-
- case 111:
-
- lift(0);
- drawTo(70, 46);
- drawTo(65, 43);
-
- drawTo(65, 49);
- drawTo(5, 49);
- drawTo(5, 45);
- drawTo(65, 45);
- drawTo(65, 40);
-
- drawTo(5, 40);
- drawTo(5, 35);
- drawTo(65, 35);
- drawTo(65, 30);
-
- drawTo(5, 30);
- drawTo(5, 25);
- drawTo(65, 25);
- drawTo(65, 20);
-
- drawTo(5, 20);
- drawTo(60, 44);
-
- drawTo(75.2, 47);
- lift(2);
-
- break;
-
- case 11:
- drawTo(bx + 5 * scale, by + 15 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 15 * scale, 0.1 * scale, 1, -1, 1);
- lift(1);
- drawTo(bx + 5 * scale, by + 5 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 5 * scale, 0.1 * scale, 1, -1, 1);
- lift(1);
- break;
-
- }
- }
-
-
-
- void lift(char lift) {
- switch (lift) {
- // room to optimize !
-
- case 0: //850
-
- if (servoLift >= LIFT0) {
- while (servoLift >= LIFT0)
- {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- else {
- while (servoLift <= LIFT0) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
-
- }
-
- }
-
- break;
-
- case 1: //150
-
- if (servoLift >= LIFT1) {
- while (servoLift >= LIFT1) {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
-
- }
- }
- else {
- while (servoLift <= LIFT1) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
-
- }
-
- break;
-
- case 2:
-
- if (servoLift >= LIFT2) {
- while (servoLift >= LIFT2) {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- else {
- while (servoLift <= LIFT2) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- break;
- }
- }
-
-
- void bogenUZS(float bx, float by, float radius, int start, int ende, float sqee) {
- float inkr = -0.05;
- float count = 0;
-
- do {
- drawTo(sqee * radius * cos(start + count) + bx,
- radius * sin(start + count) + by);
- count += inkr;
- }
- while ((start + count) > ende);
-
- }
-
- void bogenGZS(float bx, float by, float radius, int start, int ende, float sqee) {
- float inkr = 0.05;
- float count = 0;
-
- do {
- drawTo(sqee * radius * cos(start + count) + bx,
- radius * sin(start + count) + by);
- count += inkr;
- }
- while ((start + count) <= ende);
- }
-
-
- void drawTo(double pX, double pY) {
- double dx, dy, c;
- int i;
-
- // dx dy of new point
- dx = pX - lastX;
- dy = pY - lastY;
- //path lenght in mm, times 4 equals 4 steps per mm
- c = floor(4 * sqrt(dx * dx + dy * dy));
-
- if (c < 1) c = 1;
-
- for (i = 0; i <= c; i++) {
- // draw line point by point
- set_XY(lastX + (i * dx / c), lastY + (i * dy / c));
-
- }
-
- lastX = pX;
- lastY = pY;
- }
-
- double return_angle(double a, double b, double c) {
- // cosine rule for angle between c and a
- return acos((a * a + c * c - b * b) / (2 * a * c));
- }
-
- void set_XY(double Tx, double Ty)
- {
- delay(1);
- double dx, dy, c, a1, a2, Hx, Hy;
-
- // calculate triangle between pen, servoLeft and arm joint
- // cartesian dx/dy
- dx = Tx - O1X;
- dy = Ty - O1Y;
-
- // polar lemgth (c) and angle (a1)
- c = sqrt(dx * dx + dy * dy); //
- a1 = atan2(dy, dx); //
- a2 = return_angle(L1, L2, c);
-
- servo2.writeMicroseconds(floor(((a2 + a1 - M_PI) * SERVOFAKTORLEFT) + SERVOLEFTNULL));
-
- // calculate joinr arm point for triangle of the right servo arm
- a2 = return_angle(L2, L1, c);
- Hx = Tx + L3 * cos((a1 - a2 + 0.621) + M_PI); //36,5掳
- Hy = Ty + L3 * sin((a1 - a2 + 0.621) + M_PI);
-
- // calculate triangle between pen joint, servoRight and arm joint
- dx = Hx - O2X;
- dy = Hy - O2Y;
-
- c = sqrt(dx * dx + dy * dy);
- a1 = atan2(dy, dx);
- a2 = return_angle(L1, (L2 - L3), c);
-
- servo3.writeMicroseconds(floor(((a1 - a2) * SERVOFAKTORRIGHT) + SERVORIGHTNULL));
-
- }
复制代码
如果调试完毕,就可以安装笔擦等附件,开始耍贱了!
标准程序需要时间模块读取时间,如果没有模块,可以修改函数,返回随机数或者从00:00开始计时等办法。
《Plotclock小贱钟程序》 - //左右悬臂舵机的90度位置
- #define SERVOFAKTORLEFT 550
- #define SERVOFAKTORRIGHT 630
-
- //左右悬臂舵机的 0或180度位置
- #define SERVOLEFTNULL 1750
- #define SERVORIGHTNULL 880
-
- //三只舵机的接口
- #define SERVOPINLIFT 2
- #define SERVOPINLEFT 3
- #define SERVOPINRIGHT 4
-
- //升举舵机的3个角度
- #define LIFT0 1080 // on drawing surface
- #define LIFT1 925 // between numbers
- #define LIFT2 725 // going towards sweeper
-
- // 速度
- #define LIFTSPEED 1500
-
- // 悬臂的长度,根据图纸测量,无需改变
- #define L1 35
- #define L2 57.2
- #define L3 14.2
-
-
- // 左右舵机轴心的位置
- #define O1X 23
- #define O1Y -25
- #define O2X 49
- #define O2Y -25
-
-
-
- #include <Time.h> // see [url=http://playground.arduino.cc/Code/time]http://playground.arduino.cc/Code/time[/url]
- #include <Servo.h>
-
- int servoLift = 1500;
-
- Servo servo1; //
- Servo servo2; //
- Servo servo3; //
-
- volatile double lastX = 75;
- volatile double lastY = 47.5;
-
- int last_min = 0;
-
- void setup()
- {
- // Set current time only the first to values, hh,mm are needed
- setTime(19,38,0,0,0,0);
- lift(2);
- drawTo(75.2, 47);
-
- servo1.attach(SERVOPINLIFT); // lifting servo
- servo2.attach(SERVOPINLEFT); // left servo
- servo3.attach(SERVOPINRIGHT); // right servo
- delay(1000);
-
- }
-
- void loop()
- {
-
- int i = 0;
- if (last_min != minute()) //标准程序是每分钟书写一次,如果你着急可以改成 if (1 || last_min != minute()) //这样它就会不停的书写。
- {
-
- if (!servo1.attached()) servo1.attach(SERVOPINLIFT);
- if (!servo2.attached()) servo2.attach(SERVOPINLEFT);
- if (!servo3.attached()) servo3.attach(SERVOPINRIGHT);
-
- lift(0);
-
- hour();
- while ((i+1)*10 <= hour())
- {
- i++;
- }
-
- number(3, 3, 111, 1);
- number(5, 25, i, 0.9);
- number(19, 25, (hour()-i*10), 0.9);
- number(28, 25, 11, 0.9);
-
- i=0;
- while ((i+1)*10 <= minute())
- {
- i++;
- }
- number(34, 25, i, 0.9);
- number(48, 25, (minute()-i*10), 0.9);
- lift(2);
- drawTo(74.2, 47.5);
- lift(1);
- last_min = minute();
-
- servo1.detach();
- servo2.detach();
- servo3.detach();
- }
-
-
- }
-
- // Writing numeral with bx by being the bottom left originpoint. Scale 1 equals a 20 mm high font.
- // The structure follows this principle: move to first startpoint of the numeral, lift down, draw numeral, lift up
- void number(float bx, float by, int num, float scale) {
-
- switch (num) {
-
- case 0:
- drawTo(bx + 12 * scale, by + 6 * scale);
- lift(0);
- bogenGZS(bx + 7 * scale, by + 10 * scale, 10 * scale, -0.8, 6.7, 0.5);
- lift(1);
- break;
- case 1:
-
- drawTo(bx + 3 * scale, by + 15 * scale);
- lift(0);
- drawTo(bx + 10 * scale, by + 20 * scale);
- drawTo(bx + 10 * scale, by + 0 * scale);
- lift(1);
- break;
- case 2:
- drawTo(bx + 2 * scale, by + 12 * scale);
- lift(0);
- bogenUZS(bx + 8 * scale, by + 14 * scale, 6 * scale, 3, -0.8, 1);
- drawTo(bx + 1 * scale, by + 0 * scale);
- drawTo(bx + 12 * scale, by + 0 * scale);
- lift(1);
- break;
- case 3:
- drawTo(bx + 2 * scale, by + 17 * scale);
- lift(0);
- bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 3, -2, 1);
- bogenUZS(bx + 5 * scale, by + 5 * scale, 5 * scale, 1.57, -3, 1);
- lift(1);
- break;
- case 4:
- drawTo(bx + 10 * scale, by + 0 * scale);
- lift(0);
- drawTo(bx + 10 * scale, by + 20 * scale);
- drawTo(bx + 2 * scale, by + 6 * scale);
- drawTo(bx + 12 * scale, by + 6 * scale);
- lift(1);
- break;
- case 5:
- drawTo(bx + 2 * scale, by + 5 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 6 * scale, 6 * scale, -2.5, 2, 1);
- drawTo(bx + 5 * scale, by + 20 * scale);
- drawTo(bx + 12 * scale, by + 20 * scale);
- lift(1);
- break;
- case 6:
- drawTo(bx + 2 * scale, by + 10 * scale);
- lift(0);
- bogenUZS(bx + 7 * scale, by + 6 * scale, 6 * scale, 2, -4.4, 1);
- drawTo(bx + 11 * scale, by + 20 * scale);
- lift(1);
- break;
- case 7:
- drawTo(bx + 2 * scale, by + 20 * scale);
- lift(0);
- drawTo(bx + 12 * scale, by + 20 * scale);
- drawTo(bx + 2 * scale, by + 0);
- lift(1);
- break;
- case 8:
- drawTo(bx + 5 * scale, by + 10 * scale);
- lift(0);
- bogenUZS(bx + 5 * scale, by + 15 * scale, 5 * scale, 4.7, -1.6, 1);
- bogenGZS(bx + 5 * scale, by + 5 * scale, 5 * scale, -4.7, 2, 1);
- lift(1);
- break;
-
- case 9:
- drawTo(bx + 9 * scale, by + 11 * scale);
- lift(0);
- bogenUZS(bx + 7 * scale, by + 15 * scale, 5 * scale, 4, -0.5, 1);
- drawTo(bx + 5 * scale, by + 0);
- lift(1);
- break;
-
- case 111:
-
- lift(0);
- drawTo(70, 46);
- drawTo(65, 43);
-
- drawTo(65, 49);
- drawTo(5, 49);
- drawTo(5, 45);
- drawTo(65, 45);
- drawTo(65, 40);
-
- drawTo(5, 40);
- drawTo(5, 35);
- drawTo(65, 35);
- drawTo(65, 30);
-
- drawTo(5, 30);
- drawTo(5, 25);
- drawTo(65, 25);
- drawTo(65, 20);
-
- drawTo(5, 20);
- drawTo(60, 44);
-
- drawTo(75.2, 47);
- lift(2);
-
- break;
-
- case 11:
- drawTo(bx + 5 * scale, by + 15 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 15 * scale, 0.1 * scale, 1, -1, 1);
- lift(1);
- drawTo(bx + 5 * scale, by + 5 * scale);
- lift(0);
- bogenGZS(bx + 5 * scale, by + 5 * scale, 0.1 * scale, 1, -1, 1);
- lift(1);
- break;
-
- }
- }
-
-
-
- void lift(char lift) {
- switch (lift) {
- // room to optimize !
-
- case 0: //850
-
- if (servoLift >= LIFT0) {
- while (servoLift >= LIFT0)
- {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- else {
- while (servoLift <= LIFT0) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
-
- }
-
- }
-
- break;
-
- case 1: //150
-
- if (servoLift >= LIFT1) {
- while (servoLift >= LIFT1) {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
-
- }
- }
- else {
- while (servoLift <= LIFT1) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
-
- }
-
- break;
-
- case 2:
-
- if (servoLift >= LIFT2) {
- while (servoLift >= LIFT2) {
- servoLift--;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- else {
- while (servoLift <= LIFT2) {
- servoLift++;
- servo1.writeMicroseconds(servoLift);
- delayMicroseconds(LIFTSPEED);
- }
- }
- break;
- }
- }
-
-
- void bogenUZS(float bx, float by, float radius, int start, int ende, float sqee) {
- float inkr = -0.05;
- float count = 0;
-
- do {
- drawTo(sqee * radius * cos(start + count) + bx,
- radius * sin(start + count) + by);
- count += inkr;
- }
- while ((start + count) > ende);
-
- }
-
- void bogenGZS(float bx, float by, float radius, int start, int ende, float sqee) {
- float inkr = 0.05;
- float count = 0;
-
- do {
- drawTo(sqee * radius * cos(start + count) + bx,
- radius * sin(start + count) + by);
- count += inkr;
- }
- while ((start + count) <= ende);
- }
-
-
- void drawTo(double pX, double pY) {
- double dx, dy, c;
- int i;
-
- // dx dy of new point
- dx = pX - lastX;
- dy = pY - lastY;
- //path lenght in mm, times 4 equals 4 steps per mm
- c = floor(4 * sqrt(dx * dx + dy * dy));
-
- if (c < 1) c = 1;
-
- for (i = 0; i <= c; i++) {
- // draw line point by point
- set_XY(lastX + (i * dx / c), lastY + (i * dy / c));
-
- }
-
- lastX = pX;
- lastY = pY;
- }
-
- double return_angle(double a, double b, double c) {
- // cosine rule for angle between c and a
- return acos((a * a + c * c - b * b) / (2 * a * c));
- }
-
- void set_XY(double Tx, double Ty)
- {
- delay(1);
- double dx, dy, c, a1, a2, Hx, Hy;
-
- // calculate triangle between pen, servoLeft and arm joint
- // cartesian dx/dy
- dx = Tx - O1X;
- dy = Ty - O1Y;
-
- // polar lemgth (c) and angle (a1)
- c = sqrt(dx * dx + dy * dy); //
- a1 = atan2(dy, dx); //
- a2 = return_angle(L1, L2, c);
-
- servo2.writeMicroseconds(floor(((a2 + a1 - M_PI) * SERVOFAKTORLEFT) + SERVOLEFTNULL));
-
- // calculate joinr arm point for triangle of the right servo arm
- a2 = return_angle(L2, L1, c);
- Hx = Tx + L3 * cos((a1 - a2 + 0.621) + M_PI); //36,5掳
- Hy = Ty + L3 * sin((a1 - a2 + 0.621) + M_PI);
-
- // calculate triangle between pen joint, servoRight and arm joint
- dx = Hx - O2X;
- dy = Hy - O2Y;
-
- c = sqrt(dx * dx + dy * dy);
- a1 = atan2(dy, dx);
- a2 = return_angle(L1, (L2 - L3), c);
-
- servo3.writeMicroseconds(floor(((a1 - a2) * SERVOFAKTORRIGHT) + SERVORIGHTNULL));
-
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|