I have a working Arduino sketch that accepts inputs from the arduino serial monitor and will move two stepper motors from position to position. It works well when controlled from the Arduino
Serial Monitor
// MultiStepper.pde
#include <AccelStepper.h>
#include <MultiStepper.h>
//define the steppers and pins used
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);
// MultiStepper
MultiStepper steppers;
int pos1=0; //absolute positions of each motor
int pos2=0;
void setup() {
Serial.begin(9600);
// Configure each stepper
stepper1.setMaxSpeed(400);
stepper2.setMaxSpeed(400);
// Then give them to MultiStepper to manage
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
Serial.println("Enter relative positions for each axis followed by 'Enter'");
}
void loop() {
long positions[2]; // Array of desired stepper positions
// Positions are absolute. Origin is set at start-up.
//So a move to say 200 will move 200 steps. If the next position read is
//also 200 the stepper will not move.
while (Serial.available() == 0);
positions[0] = Serial.parseInt(); //read int
while (Serial.available() == 0);
positions[1] = Serial.parseInt(); //read int
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
pos1 = pos1 + positions[0];
pos2 = pos2 + positions[1];
Serial.println("Absolute positions are:");
Serial.println(pos1);
Serial.println(pos2);
}
I have tried to write a python program that will eventually read positions from a file and send them to the Arduino.
#!/usr/bin/python
#serialToArduino.py
# my attempt to send stepper motor positions to an Arduino
# it works for the first pair of positions but not the second???
# when the comms are sorted I intend to read the positions from a text file
import serial
# open serial port
ser = serial.Serial('/dev/ttyUSB0')
# the arduino sketch invites 2 integers to be input via serial comms
msg = ser.readline().decode().strip()
print(msg) # This is the "Enter..." string
ser.write(b'500') # send 1st Xpos
ser.write(b'-500') # send 1st Ypos
msg = ser.readline().decode().strip()
print(msg) # This is the start of the report "Absolut...
msg = ser.readline().decode().strip()
print(msg) # reporting the 1st Xpos
msg = ser.readline().decode().strip()
print(msg) # reporting the 1st Ypos
# The steppers run to position here OK
#ser.flushInput();ser.flushOutput() this has no effect so commented out
ser.write(b'000') # send a new Xpos
ser.write(b'200') # and new Ypos
print("here") # debug: it prints the string then dies????
# this is as far as it gets.
msg = ser.readline().decode().strip()
print(msg) # This is the 2nd report "Absolute..." string
msg = ser.readline().decode().strip()
print(msg) # 2nd Xpos (not attained)
msg = ser.readline().decode().strip()
print(msg) # 2nd ypos (not attained)
ser.close() # close port
The system hangs at the marked position with the python shell showing:
======== RESTART: /home/trevorj/MyPrograms/Python/serialToArduino.py ========
Enter relative positions for each axis followed by 'Enter'
Absolute positions are:
500
-500
Can someone please tell me what I am doing wrong.
Thanks
here