# T-Bot: drawing diagonal lines

Posted by rraghvani
 T-Bot: drawing diagonal lines December 18, 2018 05:11PM Registered: 5 years ago Posts: 2
I’ve made a T-Bot similar to this design. Using stepper motor driver L6470, I can control the movements e.g. up/down, left/right and diagonally using the equations shown here corexy, via the L6470 command MOVE(direction, number_of_steps) using MAX_SPEED(100 step/s).

If I move from (0, 0) to (400, 400), both stepper motors move at a constant speed, and reaches (400, 400). Great.

If I move from (0, 0) to (400, 100), then I need to reduce the speed of one of the stepper motors by 4 times (400 / 100 = 25 step/s). Fine.

However, if I move from (0, 0) to (400, 300), I reduce the speed of one of the stepper motors by 1.3 times (400 / 300 = 1.333 step/s). Slight issue here, MAX_SPEED only accepts integers and therefore the linear path it follows is not correct – one stepper motor will stop before the other, and a diagonal line is not drawn correctly.

Is there a way of resolving this issue?

Sample Python code looks like this,

```# Maximum speed
STEP_PER_SECOND = 100

def doLine(dX, dY):
# Coords
dX = 400
dY = 300

# Coordinates to motor absolute position
dY = -dY
a_pos = dX + dY
b_pos = dX - dY
print("A_POS:", a_pos, "B_POS:", b_pos)

a_pos_temp = abs(a_pos)
b_pos_temp = abs(b_pos)

a_spd = STEP_PER_SECOND
b_spd = STEP_PER_SECOND

if (a_pos_temp > b_pos_temp):
b_spd = round(abs(STEP_PER_SECOND * (b_pos_temp / a_pos_temp)))
elif (a_pos_temp < b_pos_temp):
a_spd = round(abs(STEP_PER_SECOND * (a_pos_temp / b_pos_temp)))
print("A_SPD:", a_spd, "B_SPD:", b_spd)

MOTOR[0].setMaxSpeed(SE.steps_to_maxspeed(a_spd))
MOTOR[1].setMaxSpeed(SE.steps_to_maxspeed(b_spd))

# Move stepper MOTOR
if (a_pos < 0):
MOTOR[0].Move(SE.REV, -a_pos)
else:
MOTOR[0].Move(SE.FWD, a_pos)

if (b_pos < 0):
MOTOR[1].Move(SE.REV, -b_pos)
else:
MOTOR[1].Move(SE.FWD, b_pos)

while (GPIO.input(SE.MTR0_BUSY) != 1) or (GPIO.input(SE.MTR3_BUSY) != 1):
pass```

Edited 2 time(s). Last edit at 12/18/2018 05:44PM by rraghvani.
 Re: T-Bot: drawing diagonal lines December 18, 2018 11:20PM Registered: 8 years ago Posts: 5,232
Integer math is funny, isn't it? Don't know, if I can explain...

Usually you multiply the floating value by right shifting ( eg. >>8 ),
then forget about the fractions and do the operation in integer.
Later you left shift (<<8 ) the result to get your final value.

Maybe you can read more qualified explanation about Integer math in the net.
 Re: T-Bot: drawing diagonal lines December 29, 2018 06:05AM Registered: 5 years ago Posts: 2
Update,

As I was unable to use the methods provided by the L6470 chip, to do what I wanted. I have now used Step Clock mode instead. I have written methods which allows me to rotate the stepper motors a step (i.e. 1.8 degrees); using Bresenham’s line algorithm to draw diagonal lines without having to decrease or increase speeds.

Here’s a little video, which follows some basic coordinates.
 Re: T-Bot: drawing diagonal lines December 30, 2018 02:45AM Registered: 8 years ago Posts: 5,232
I just read my own comment and noticed it's the opposite way around: left shift doubles the value, right shift halfes it.
Sorry, only registered users may post in this forum.