Draw a circle using 2 stepper motors (Arduino)

2.1k views Asked by At

I want to draw a circle using 2 stepper motors(42H48) and 2 drivers (TB6560).

The hardware are like this:

   (y-axis motor)
       |
       |
--—--- |-------- (x-axis motor)
       |
       |

Now, i only can control these motors to draw like Diamond shape.

// 1
digitalWrite(xAxisMotorDirection, HIGH);
digitalWrite(yAxisMotorDirection , HIGH);
for(i=0; i<12000; i++)
{
    digitalWrite( xAxisMotorPulse, HIGH);
    digitalWrite( yAxisMotorPulse , HIGH);
    delay(1);
    digitalWrite( xAxisMotorPulse , LOW);
    digitalWrite( yAxisMotorPulse , LOW);
    delay(1);
}

// 2
digitalWrite(xAxisMotorDirection, HIGH);
digitalWrite(yAxisMotorDirection , LOW);
for(i=0; i<12000; i++)
{
    digitalWrite( xAxisMotorPulse, HIGH);
    digitalWrite( yAxisMotorPulse , HIGH);
    delay(1);
    digitalWrite( xAxisMotorPulse , LOW);
    digitalWrite( yAxisMotorPulse , LOW);
    delay(1);
}

// 3
digitalWrite(xAxisMotorDirection, LOW);
digitalWrite(yAxisMotorDirection , LOW);
for(i=0; i<12000; i++)
{
    digitalWrite( xAxisMotorPulse, HIGH);
    digitalWrite( yAxisMotorPulse , HIGH);
    delay(1);
    digitalWrite( xAxisMotorPulse , LOW);
    digitalWrite( yAxisMotorPulse , LOW);
    delay(1);
}

// 4
digitalWrite(xAxisMotorDirection, LOW);
digitalWrite(yAxisMotorDirection , HIGH);
for(i=0; i<12000; i++)
{
    digitalWrite( xAxisMotorPulse, HIGH);
    digitalWrite( yAxisMotorPulse , HIGH);
    delay(1);
    digitalWrite( xAxisMotorPulse , LOW);
    digitalWrite( yAxisMotorPulse , LOW);
    delay(1);
}

Any good approach to draw a circle using pulse ?

I googled "bresenham algorithm" , but I have no idea how to implement this algorithm using pulse.

1

There are 1 answers

0
Sneaky Polar Bear On

This is just a concept, I have not tested it:

Make a lookup table with x and y pairs. To create this, space 100 or so values between 0 and 2pi and calculate sin and cos of that set and place in the table.

Create an integer index variable for x and one for y. Use a timer or delay to establish a regular time interval. On each time interval, look at the next position in the lookup table and make steps on the appropriate axes to make the stepper index from middle match the lookup table at that time-step.

Example: table[1] = (1,2) My current stepper positions are (2,-1) this is arbitrary on that timestep I would step my x stepper back 1 step and my y forward 3 steps.

I think you are going to need to stay away from delay for this as you will need to be executing the steps during the time interval. At a time step, you will establish where you are and need to be, but then will have to create several timed steps to get there.

My biggest concern is that your steppers may not be fast enough for this, but idk. Up to a point higher current can enable faster stepping.