Hi there,
I'm currently try to set up the Sardauscan 3D scanner (Sardauscan) and I'm having some problems with the AccelStepper library, I think.
To be sure the motor was working I uploaded a simple code using the standard Stepper library, and it moves smoothly. But when I upload the Sardauscan code it moves really "glitchy".. I tried manipulating maxSpeed, acceleration and speed values with no luck. I've also tried some AccelStepper examples, but none worked..
Below, the code:
1)Main Sardauscan program
#include "SerialCommand.h"
#include <AccelStepper.h>
#include "configuration.h"
/*
Commands :
identity
sardauscan =>ask if this is a sardaucan => response "yes" //
laser :
L => ask laser configuration => response number of lasers
L x => ask status of laser index X
=> 'x' laser number
L x y => turn laser on or off
'x' laser number
'y' state: 0=off 1=on
Table :
T => table position
T S => return the number of steps for a revolution
T C => Set Current position as 0 (for absolute positionning
T R xxx => rotate relatively xxx steps
T A xxx => turn position absolute xxx steps
unknow command
=> response "Hun?"
*/
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
SerialCommand SCmd;
int RotationToSteps(int rotation) //
{
return rotation*STEP_BY_MINMOVE;
}
int StepsToRotation(int rot)
{
return rot/STEP_BY_MINMOVE;
}
void Identification()
{
Serial.println("yes");
}
void Hun()
{
Serial.println("Hun?");
}
void TableCommand()
{
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
if(arg[0]=='R'||arg[0]=='r') // T R
{
char *arg2 = SCmd.next();
int pos=atoi(arg2);
stepper1.move(RotationToSteps(pos));
stepper1.runToPosition();
Serial.print("RELATIVE ROTATION :");
Serial.print(pos);
Serial.print(" => ");
Serial.println(StepsToRotation(stepper1.currentPosition()));
}
else if(arg[0]=='A'||arg[0]=='a') // T A
{
char *arg2 = SCmd.next();
int pos=atoi(arg2);
stepper1.moveTo(RotationToSteps(pos));
stepper1.runToPosition();
Serial.print("ABSOLUTE ROTATION ");
Serial.print(pos);
Serial.print(" => ");
Serial.println(StepsToRotation(stepper1.currentPosition()));
}
else if(arg[0]=='S'||arg[0]=='s') //T S
{
Serial.print("REVOLUTION STEPS ");
Serial.print(" => ");
Serial.println(StepsToRotation(REVOLUTION_STEP));
}
else if(arg[0]=='C' ||arg[0]=='c') //T C
{
stepper1.setCurrentPosition(0);
Serial.print("RESET CUTTENT POSITION ");
Serial.print(" => ");
Serial.println(StepsToRotation(stepper1.currentPosition()));
}
else
{
Serial.print("Unknown Table command :");
Serial.println(arg);
}
}
else {
Serial.print("Position ");
Serial.println(StepsToRotation(stepper1.currentPosition()));
}
}
int getLaserPin(int laserIndex)
{
if(laserIndex==0)
return LASER_PIN_1;
else if(laserIndex==1)
return LASER_PIN_2;
else if(laserIndex==2)
return LASER_PIN_3;
else if(laserIndex==3)
return LASER_PIN_4;
else
return (-1);
}
void LaserCommand()
{
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
int laserIndex=atoi(arg);
char *arg2 = SCmd.next();
if (arg2 == NULL)
{
Serial.print("LASER_STATE: ");
Serial.print(laserIndex);
int pin =getLaserPin(laserIndex);
Serial.print("(");
Serial.print(pin);
Serial.print(") = ");
if(pin>=0)
Serial.println(digitalRead(pin));
else
Serial.println(-1);
}
else
{
int laserState = atoi(arg2);
int pin =getLaserPin(laserIndex);
if(pin>=0)
digitalWrite( pin, laserState==1?HIGH:LOW);
Serial.print("SET_LASER: ");
Serial.print(laserIndex);
Serial.print("(");
Serial.print(pin);
Serial.print(") = ");
Serial.println(digitalRead(pin));
}
}
else
{
Serial.print("LASER_COUNT: ");
Serial.println(LASER_COUNT);
}
}
void setup() {
stepper1.setMaxSpeed(500.0);
stepper1.setAcceleration(200.0);
stepper1.setSpeed(400);
stepper1.moveTo(0);
stepper1.runToPosition();
Serial.begin(SERIAL_BAUD);
pinMode(LASER_PIN_1, OUTPUT);
pinMode(LASER_PIN_2, OUTPUT);
pinMode(LASER_PIN_3, OUTPUT);
pinMode(LASER_PIN_4, OUTPUT);
digitalWrite(LASER_PIN_1, LOW);
digitalWrite(LASER_PIN_2, LOW);
digitalWrite(LASER_PIN_3, LOW);
digitalWrite(LASER_PIN_4, LOW);
SCmd.addCommand("sardauscan",Identification);
SCmd.addCommand("T",TableCommand);
SCmd.addCommand("t",TableCommand);
SCmd.addCommand("L",LaserCommand);
SCmd.addCommand("l",LaserCommand);
SCmd.addDefaultHandler(Hun);
Serial.println(FIRMWARE_VERSION);
Serial.flush();
}
void loop() {
SCmd.readSerial(); // We don't do much, just process serial commands
Serial.flush();
}
2)Sardauscan second file
#ifndef CONFIGURATION_h
#define CONFIGURATION_h
#define FIRMWARE_VERSION "Sardauscan V0.1a"
#define SERIAL_BAUD 115200
//57600
// Motor definitions
#define motorPin1 2 // IN1 on the ULN2003 driver 1
#define motorPin2 3 // IN2 on the ULN2003 driver 1
#define motorPin3 4 // IN3 on the ULN2003 driver 1
#define motorPin4 5 // IN4 on the ULN2003 driver 1
//tips (from Mark Benson)
//If anyone else is having problems with a BYJ48 stepper not doing anything,
//change the HALFSTEP value to 4 & REVOLUTION_STEP to 2048
#define HALFSTEP 4 //8
#define REVOLUTION_STEP 2048 //4096
#define STEP_BY_MINMOVE 4 // move by STEP_BY_MINMOVE (to avoid power loss when position is between step)
#define LASER_COUNT 4
#define LASER_PIN_1 13 //yellow
#define LASER_PIN_2 A1 //orange
#define LASER_PIN_3 A2 //green
#define LASER_PIN_4 A3 //blue
#endif
3)Example program used (using standard Stepper library)
#include <Stepper.h>
Stepper myStepper(2048,2,4,3,5);
void setup() {
myStepper.setSpeed(10);
}
void loop() {
myStepper.step(2048);
delay(1000);
myStepper.step(-2048);
delay(1000);
}
I'm going to upload the official wiring and a photo of mine in the attachments, it could be useful..
I really don't know what to do to solve the problem, any suggestion is appreciated!
EDIT: it seems I can't upload the photo of my wiring, because "it failed the security check".. if needed, I'll try again!
EDIT2: I uploaded it as a zip file!
my wiring.zip (1.14 MB)