Spiral movement using X,Y motors

Hi there?
I am new to adruino even to electronics, can some one help me write a program..

I am building a X,Y table with two servo motors

I need to generate a spiral motion on the table

My mathematical equation for the X,Y are

X=Rtsin(t2pin)
Y=R
tcos(t2pin)

t = varies between 0 to 1

"R" is outer radius of the the spiral
"n" is number of turns

Thanks.

Ok, so where are you at with this project?
Do you have a working X,Y table ?
What code have you got already ?
Can you move in a line? a square? or even a circle ?

Really need to know a bit more before jumping in to tracking a spiral.

Yours,
TonyWilk

Amuthageethan:
My mathematical equation for the X,Y are

X=Rtsin(t2pin)
Y=R
tcos(t2pin)

If you have the equation you seem to be on the downward side of the hill.

...R

Do you have any programming experience?

For drawing a spiral, forget about the Arduino and servos, just write a program that outputs a few X,Y points and plot them by hand. Once you know you are calculatin gthe points correctly then move onto the mechanical side.

Begin by doing what @TonyWilk suggests and first draw a line then a square.
Once that works add in our code to generate the spiral points.

The X,Y table is going to approximate the spiral by drawing straight lines between the points you calculate. This means that you will need to generate more points per degree of turn the further out you get from the centre, otherwise your spiral will have a stepped effect.

Figure out how many line segments you want to break the spiral into. If you want your spiral in 1° increments you would need 360*n segments. Divide t into that many segments:

  for (float t = 0.0; t <= 1.0; t += 1.0/(360.0*n)) {
    int X=R*t*sin(t*2*pi*n);
    int Y=R*t*cos(t*2*pi*n);
    drawTo(X,Y);
  }

Rather than using fixed increments I think you would be better to make smaller and smaller steps as the radius of the spiral gets bigger as that will avoid the spiral having visible steps in it.

You can play with the code below at

At the moment it uses a small number of fixed steps and as you can see the output is very straight line. It should be easy to adapt though;

#include <iostream>
#include <math.h>

using namespace std;

void plot(double x, double y, double t);

int main()
{
    const double pi = 3.14159265358979323846;
    int n=2;// number of turns
    int loop=9;// number of points
    double r=10.0;// outer radius of the spiral
    double c;//current radius
    double x,y; // cartesian coordinates
    double t; // varies 0..1
    
    t=0;
    do
    {
        x=r*t*sin(t*2*pi*n);
        y=r*t*cos(t*2*pi*n);
        c=sqrt(x*x+y*y);
        plot(x,y,t);
        t=t+1.0/(loop-1);// to avoid steps this needs to vary with current radius instead of being constant
    }while(1-t>=0);
    
   return 0;
}

void plot(double x,double y, double t)
{
    //drive the servos to position here
    printf("X=%7.4lf, Y=%7.4lf   t=%7.4lf\n",x,y,t);
    return;
}

Output is;

X= 0.0000, Y= 0.0000 t= 0.0000
X= 1.2500, Y= 0.0000 t= 0.1250
X= 0.0000, Y=-2.5000 t= 0.2500
X=-3.7500, Y=-0.0000 t= 0.3750
X=-0.0000, Y= 5.0000 t= 0.5000
X= 6.2500, Y= 0.0000 t= 0.6250
X= 0.0000, Y=-7.5000 t= 0.7500
X=-8.7500, Y=-0.0000 t= 0.8750
X=-0.0000, Y=10.0000 t= 1.0000

ardly:
Rather than using fixed increments I think you would be better to make smaller and smaller steps as the radius of the spiral gets bigger as that will avoid the spiral having visible steps in it.

Ideally you would calculate new X,Y points which, when connected by a straight line, only deviate from the actual curve by a given amount. Converting curves to G-Code for a CNC router for example, you would set, say, 0.1mm as the maximum deviation from the true curve.

Bit more difficult to calculate tho'.
( did a project recently with my mate's CNC machine to cut a particular spiral groove around a cylinder, had to turn the curve into "X,Y" type moves. Deviation from the curve? Nah, Just did it in lots of small steps :slight_smile: ).

You can play with the code below at
Online C++ Compiler | Compile C++ Code Online

Neat.

Yours,
TonyWilk

I am building a X,Y table with two servo motors

What is the degree of rotation of these motors? Most hobby servos degrees.

I think servos are the wrong choice. They are not very accurate and you can't control the speed of the servo. To draw a diagonal line other than 45degrees you must drive X and Y at different speeds.

Steppers are much better for this. You can also re-use one of a zillion 3D printer programs to run your steppers.

MorganS:
Steppers are much better for this. You can also re-use one of a zillion 3D printer programs to run your steppers.

I must say that is the way my mind was working.

Draw a spiral with a CAD program. Get something like Slic3r to convert it to GCode. Bam! The maths is someone else's problem.

...R

TonyWilk:
Ideally you would calculate new X,Y points which, when connected by a straight line, only deviate from the actual curve by a given amount. Converting curves to G-Code for a CNC router for example, you would set, say, 0.1mm as the maximum deviation from the true curve.

Bit more difficult to calculate tho'.
( did a project recently with my mate's CNC machine to cut a particular spiral groove around a cylinder, had to turn the curve into "X,Y" type moves. Deviation from the curve? Nah, Just did it in lots of small steps :slight_smile: ).
Neat.

Yours,
TonyWilk

I too was thinking stepper motors would be a better choice than servos. Let's say steppers are used and the minimum step in either X or Y is 0.1mm. In the code I posted instead of adding a large constant to 't' you could loop adding tiny amounts to 't' until it reaches a value such that either the new 'x' or the new 'y' is at least 0.1mm greater than the old 'x' or 'y' position at which point you draw a line to the new position.
No fancy maths involved, and the next 't' will probably be calculated before the plotter has finished drawing the last line. I think that would probably draw as smooth a curve as possible - or am I wrong?

Out of interest: There are different kinds of spiral. The given formula draws an Archimedes spiral where turns are a fixed distance apart. With the Logarithmic spiral, which occurs a lot in nature, the turns get further apart, and to me look much more pleasing.

johnwasser:
Figure out how many line segments you want to break the spiral into. If you want your spiral in 1° increments you would need 360*n segments. Divide t into that many segments:

  for (float t = 0.0; t <= 1.0; t += 1.0/(360.0*n)) {

int X=Rtsin(t2pin);
   int Y=R
tcos(t2pin);
   drawTo(X,Y);
 }

Haven't tested anything, but a round() may be needed in there to prevent glitches from converting something like float 49.85 into integer 49 when it clearly should be 50. And, round() handles negative rounding properly... no need to reinvent the wheel. :slight_smile:

@krupski, this is a very old Thread.

...R

Robin2:
@krupski, this is a very old Thread.

...R

So I see. I wonder how it got bumped up? I usually only look at the first page of various forums (i.e. newest posts). Oh well... :slight_smile: