'Stepper motor alternating direction with every G-code command, Pyserial and Ramps 1.4

I'm trying to control a 3D -printers extruder motor, connected to an Arduino and Ramps 1.4 shield, through python with pyserial. Currently I can send the motor individual lines of G-code, G1 F100 E10 will turn the motor and extrude 10 mm of filament, however when I try and send the motor a number of commands one after the other it alternates directions with every second command.

This looks like:

  1. G1 F100 E10 - clockwise
  2. G1 F100 E5 - counter clockwise
  3. G1 F100 E7 - clockwise

and so on.

If I send an individual command to the motor and attach a minus to it i.e. E-10 it will invert the direction of rotation, however when I do this to every second command to try and correct the change of direction it has no effect

I am wondering if anyone has every encountered this problem before? I believe it may be a issue with my code, however I have seen similar code implemented successfully. I do not believe it is the Arduino firmware as I have not edited any of it.

I have attached my code below with a sample of the data frame I will be using.

import re
import pandas as pd
import serial
import time
from datetime import datetime



raw_text = '''LINEAR X -0.495727 Y 47.849700 Z 24.010800 PH 0.000000 RH 18.139500 RZ -0.495727 
F80.00
CALL Extruder(0.4039)
LINEAR X -0.312469 Y 47.740900 Z 24.294700 PH 0.000000 RH 17.964400 RZ -0.312469 F80.00
CALL Extruder(0.1749)
LINEAR X -0.250105 Y 47.686800 Z 24.436100 PH 0.000000 RH 17.877100 RZ -0.250105 F80.00
CALL Extruder(0.1556)
LINEAR X -0.217890 Y 47.632900 Z 24.577100 PH 0.000000 RH 17.790000 RZ -0.217890 F80.00
CALL Extruder(0.1485)
LINEAR X -0.208664 Y 47.579200 Z 24.717900 PH 0.000000 RH 17.702900 RZ -0.208664 F80.00
CALL Extruder(0.1487)
LINEAR X -0.219338 Y 47.525800 Z 24.858000 PH 0.000000 RH 17.616100 RZ -0.219338 F80.00
CALL Extruder(0.1560)
LINEAR X -0.253462 Y 47.472500 Z 24.998300 PH 0.000000 RH 17.529200 RZ -0.253462 F80.00
CALL Extruder(0.1756)
LINEAR X -0.318285 Y 47.419400 Z 25.138100 PH 0.000000 RH 17.442500 RZ -0.318285 F80.00
CALL Extruder(0.4039)
LINEAR X -0.506983 Y 47.314100 Z 25.416000 PH 0.000000 RH 17.269900 RZ -0.506983 F80.00
CALL Extruder(0.1749)
LINEAR X -0.571666 Y 47.261500 Z 25.555100 PH 0.000000 RH 17.183400 RZ -0.571666 F80.00
CALL Extruder(0.1556)
LINEAR X -0.605784 Y 47.209000 Z 25.694300 PH 0.000000 RH 17.096700 RZ -0.605784 F80.00
LINEAR X -0.616829 Y 47.156400 Z 25.833800 PH 0.000000 RH 17.009900 RZ -0.616829 F80.00
CALL Extruder(0.1487)
LINEAR X -0.607234 Y 47.103800 Z 25.973400 PH 0.000000 RH 16.922800 RZ -0.607234 F80.00
CALL Extruder(0.1560)
LINEAR X -0.574094 Y 47.051400 Z 26.112800 PH 0.000000 RH 16.835700 RZ -0.574094 F80.00
CALL Extruder(0.1756)
LINEAR X -0.509826 Y 46.998900 Z 26.252900 PH 0.000000 RH 16.748200 RZ -0.509826 F80.00
CALL Extruder(0.4039)
LINEAR X -0.321389 Y 46.894100 Z 26.532700 PH 0.000000 RH 16.573200 RZ -0.321389 F80.00
CALL Extruder(0.1749)
LINEAR X -0.257257 Y 46.842300 Z 26.671600 PH 0.000000 RH 16.486100 RZ -0.257257 F80.00
CALL Extruder(0.1556)
LINEAR X -0.224132 Y 46.790500 Z 26.810400 PH 0.000000 RH 16.399100 RZ -0.224132 F80.00
CALL Extruder(0.1485)
LINEAR X -0.214652 Y 46.738900 Z 26.949000 PH 0.000000 RH 16.312100 RZ -0.214652 F80.00
CALL Extruder(0.1487)
LINEAR X -0.225645 Y 46.687500 Z 27.087300 PH 0.000000 RH 16.225200 RZ -0.225645 F80.00

'''

raw_text = list(raw_text)

X = 'X'

combine = "".join(line.strip()for line in raw_text)         # Combine each line into one line 
split = combine.split('LINEAR')                             # Split line at work LINEAR


cordinates_list = []    

p = re.compile(r'-?\d+\.\d+')                  # Compile a pattern to capture float values

# Loop through every line 
for raw_text_line in split:


    if X in raw_text_line:
 
        cordinates = [float(i) for i in p.findall(raw_text_line)]  # Extract numbers
        cordinates_list.append(cordinates)         # Add them cordinates to the list


cordinates_df = pd.DataFrame(cordinates_list)
cordinates_df = cordinates_df.fillna(0)
#print(cordinates_df)

def command(ser, command):
  #start_time = datetime.now()
  ser.write(str.encode(command)) 
  time.sleep(1)

  while True:
    line = ser.readline()
    print(line)

    if line == b'ok\n':
      break

ser = serial.Serial('COM4', 250000)
time.sleep(1)

for i in range(len(cordinates_df)): 

    extrude = cordinates_df[7][i]
    #print(extrude)
    extrude = str(extrude)
    extrude_command = "G1 F100 E\r\n"
    extrude_command = extrude_command[:9] + extrude + extrude_command[9:]
    print("Sending ", extrude_command)


    command(ser, extrude_command)

time.sleep(1)   
ser.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source