Drawing a figure with Arduino (Basic Robotics Project)

Hello everyone. I am new to Arduino as well as this forum. I will go straight to the point with what I am attempting to do as a project.

What I am trying to create is a machine that can draw a figure. No need for any type of figure, because all I’m trying to do is to do it built in. I am attempting to write a code using two servo motors (TowerPro SG-5010 Double Ball Bearing to be exact) and I want to to draw a square. I went ahead and wrote the code, and after a few troubleshooting moments, the code was successful and the command was being sent successfully to the servos. However, there is a huge problem with accuracy. It’s like the servos are trying to draw the square, but it’s all going wrong. I mean, it is barely drawing a straight line. At first, I tried to give it input signals from 5 KOhm pot resistors. That part was successful. The problem began when I removed them, and I manually put values in the “map” function after doing some calculations on how to draw the square. I put a 1000 uF capacitor to keep the servos as stable as possible (they were going berserk when I tried before this step). It is just not drawing what it should. The output is a highly inaccurate drawing :(. I have written the code, and here it is.

#include <Servo.h>

Servo mys1;
Servo mys2;
int poten1 = 0;
int poten2 = 1;
int v;
void setup()
{
mys1.attach(5);//Servo motor 2
mys2.attach(6);//Servo motor 1
}
void loop()
{
square();

}
void square()
{
//Step 1
double ma = 180.0;
int i = 21600;
int j = 11700;
v = map(i, 0.0, 32400.0, 0.0, ma);
mys2.write(v);
v = map(j, 0.0 ,32400.0, 0.0, ma);
mys1.write(v);
delay(2000);
//step1
while(i >= 16200)
{
i = i - 180;
v = map(i, 0.0, 32400.0, 0.0, ma);
mys2.write(v);
j = j + 270;
v = map(j, 0.0 ,32400.0, 0.0, ma);
mys1.write(v);
delay(100);
}
delay(1000);
//step2
while(j <= 28080)
{
i = i - 80;
v = map(i, 0.0, 32400.0, 0.0, ma);
mys2.write(v);
j = j + 216;
v = map(j, 0.0 ,32400.0, 0.0, ma);
mys1.write(v);
delay(100);
}
delay(1000);
//step3
while(i <= 16200)
{
i = i + 180;
v = map(i, 0.0, 32400.0, 0.0, ma);
mys2.write(v);
j = j - 150;
v = map(j, 0.0 ,32400.0, 0.0, ma);
mys1.write(v);
delay(100);
}
delay(1000);
//step4
while(i <= 21600)
{
i = i + 90;
v = map(i, 0.0, 32400.0, 0.0, ma);
mys2.write(v);
j = j - 216;
v = map(j, 0.0 ,32400.0, 0.0, ma);
mys1.write(v);
delay(100);
}
delay(1000);
}

Please note that the calculations are not exact due to what I believe are from the servos. It’s like they are rotating a bit more than just 180 degrees, but the Arduino still considers them as servos with only 180 degree rotation. I tried changing the max value (I named that variable “ma” in the code) and it actually made things worse. Is there any code that would allow me to draw a perfect straight line horizontally and vertically? Or perhaps there is a problem with my reasoning with the mathematical computations?

To make things clearer, here is how I thought of drawing the square (assuming both servos were 100% accurate and would turn to a maximum of 180 degrees):

But first, here is how I have the servos connected to each other:

SERVO1--------------->SERVO2------------------>End-effector(pen)
Distances between servos are even. Consider them L1 and L2.

I drew the figure on a paper, and took the lengths of each joint I plugged on the servos. Then I thought of creating a translation by manipulating two rotations (which are the servos). So, I took an initial angle. Servo 1 = 120 degrees (counter clockwise rotation) and servo 2 = 90 degrees.
The way I thought of drawing the line is that for each degree servo 1 goes clockwise or counterclockwise, provided it is greater than 90 degrees, servo 2 should rotate in the opposite direction. Which is why in my code you’ll see the numbers from 0 to 32400. 32400 is actually 180 multiplied by 180. This way, I can have accuracy on every single angle, instead of possible imbalances I get from other examples I saw (v, 0, 1023, …). And for every angle made, there is a short pause of 100 milliseconds so that the Arduino catches up with the values, and the servos make it in time to their destination before acquiring new data from the while loop I put them in. (It was for loop before, but that just gave me trouble for some reason). But, unfortunately, it was not a straight line. It was more of a small curve… Like it’s trying to fix it, but something wrong is happening and it’s just continuing the trajectory its getting. So, if there is a code, or at least any explanation on how to fix this problem with my code, I’d really appreciate it. Sorry for the long read, and thank you all in advance :slight_smile:

 v = map(i, 0.0, 32400.0, 0.0, ma);

You need to read the documentation regarding the map() function. It does NOT take floats.

mys2.write(v);
   mys1.write(v);

Stupid names for the servos. Smarter names would tell us what the servo is used for.

Without knowing anything about what the servos actually do, we can’t tell you why the servos can’t move something along a straight line.

Hi Pyrus, I have build a few bots and just done my first drawing robot! But I use stepper motors as their easy to control and predictable.. Like if you know your motor takes 200 steps to make one revolution and you know the wheel size, then you know that in 200 steps the bot will travel Wheel dia x Pi, and if one motor is still then you can do a 90 deg turn around it! in X steps..

I have done an excel sheet that does all the calaclations for you! I think I have it all right, but some one please check it for me!!

Here's a couple of pictures.. no there's not, can't I just add images?? must it be a link??

Hope it helps, regards.

Mel.

Here's a couple of pictures.. no there's not, can't I just add images?? must it be a link??

There is a thread in Website and Forum that shows how to attach pictures.

Hi Pyrus,
I have build a few bots and just done my first drawing robot! But I use stepper motors as their easy to control and predictable… Like if you know your motor takes 200 steps to make one revolution and you know the wheel size, then you know that in 200 steps the bot will travel Wheel dia x Pi, and if one motor is still then you can do a 90 deg turn around it! in X steps…

I have done an excel sheet that does all the calaclations for you! I think I have it all right, but some one please check it for me!!

Here’s a couple of pictures…

Hope it helps, regards.

Mel.

PaulS: There is a thread in Website and Forum that shows how to attach pictures.

Thanks Paul, I never found what you said, but it sort of happend, know I'd done it before, I need more forum practice!!

I thought you could add them in-line too.

Regards

Mel.

Cactusface: Thanks Paul, I never found what you said, but it sort of happend, know I'd done it before, I need more forum practice!!

I thought you could add them in-line too.

Regards

Mel.

http://forum.arduino.cc/index.php?topic=364156.0

Perhaps I should have made myself clearer. What exactly is not understood in my question? What information may I provide to give you what exactly is going on? I’ll just bulletin point some main aspects of my project:

  • I am using Arduino Uno.
  • Both servo motors are model “TowerPro SG-5010 Double Ball Bearing”, here is a link:
  • The project is meant to be a stationary drawing machine. It does not move. No wheels are included.
  • Here is how my project mostly looks like. It’s the same design, just different pieces.
  • I am trying to draw just one figure. A square. I’m using 2 servo motors to act as two degrees of freedom, but the accuracy needs much to be desired.

  • The only advantage I have is that the figure is small. So, when my servos draw a line, it is almost a straight line at first (Using only 15 degree rotation to draw that line, which makes it almost a straight line instead of a curve, or an arc).

  • Here are basically the steps I’m taking to make my Arduino draw a square: I start from the upper right corner, and draw a line from left to right. Afterwards, the Arduino has to draw a line going downward. Once this is done, it will draw a line going from right to left, and finally, it will go from upward to its initial position. But as I said, the lines keep being uneven, the servos like to act up. It’s all highly inaccurate, although the main direction depending on the code I wrote does seem to be correct when I see my Arduino in operation.

-I am using Arduino’s power supply of 5V for my servos.

I hope this makes things clearer, and thank you in advance one again. :slight_smile:

Hi Pyrus,
Are you saying your driving the servos from the Arduino 5v line, if so this is your problem, never drive motors or servos from the Arduino’s 5v it can’t supply all the current your servos need, that’s why their doing funny things… How many times do we have to tell people this?? Servos and motors need a separate power supply.

I too had in mind to make a drawing machine like that, but I would still use stepper motors for the reasons already given.

Instructable projects are well known to contain bad instructions and code…

Regards

Mel.

Hi Pyrus, The code you are using uses pin 0 & 1 these are used for serial I/O and code downloading, which is why they are labled TX & RX and should not be used. Put your POTs on pins 5 & 6, etc, and try that.

Now what was I saying about instrucables??

Really hope it helps, regards.

Mel.

Hello again. I have excluded the pots. I had initialized them only during the test, and they use the analog pins, by the way, not the digital ones. In both cases, neither of them are used in this code. Ah, I see what you mean. The problem is in the supply then. And even still, the recommended would be using stepper motors. Noted. And again, I am new to Arduino, and now I know the problem. Thank you :) . I'll get back to you guys once I try again.

Hi Pyrus, If your pots are on the analogue inputs, and your right they should be, then the pins are A0 & A1, so the Arduino knows their on the analogue inputs and not digital!!

Mel.

Hi,
.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a picture of your project so we can see how you mount and setup your servos?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

At the moment we are all flying blind, you know what your project looks like and how its wired and powered we don’t.

Thanks… Tom… :slight_smile:

How are the servos connected to the pen? Does one servo move a carriage, and the other move another carriage at 90 degree angle, on top of the first carriage, with the pen attached to it? If so, then drawing a rectangle should be relatively easy, as just one servo has to operate at a time.

On the other hand, if the two servos are linked to the pen roughly in the shape of a triangle, then you need to divide your lines into short segments, and draw the segments one at a time, for each calculating the necessary (small) movements for each servo, approximating straight lines and other figures.

This goes for the "XY" geometry above as well, when drawing lines not parallel to one of the axes, or drawing curves etc.

The problem with servos, as other have mentioned, is that you as a programmer do not know how fast they move, when instructed to go to a certain position. Servos move to the target position as fast as they can.

Hence steppers are much better for plotters, CNC routers and 3D printing.