Skip to content

Commit

Permalink
ULN2003 mostly done
Browse files Browse the repository at this point in the history
  • Loading branch information
peteGSX committed Feb 24, 2024
1 parent 2a0c1b2 commit d8e2e37
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
80 changes: 69 additions & 11 deletions IOFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ void processSerialInput() {
}
}
if (newSerialData == true) {
Serial.print(F("Received serial input: "));
Serial.println(serialInputChars);
newSerialData = false;
char * strtokIndex;
strtokIndex = strtok(serialInputChars," ");
char command = strtokIndex[0]; // first parameter is activity
strtokIndex = strtok(NULL," "); // space separator
long steps;
if (command == 'M') {
steps = atoi(strtokIndex);
steps = atol(strtokIndex);
strtokIndex = strtok(NULL," ");
testActivity = atoi(strtokIndex);
}
switch (command) {
case 'C':
serialCommandC();
break;

case 'D':
serialCommandD();
break;
Expand All @@ -96,6 +98,10 @@ void processSerialInput() {
serialCommandE();
break;

case 'H':
serialCommandH();
break;

case 'M':
serialCommandM(steps);
break;
Expand All @@ -118,6 +124,17 @@ void processSerialInput() {
}
}

// C command to initiate calibration
void serialCommandC() {
if (stepper.isRunning()) {
Serial.println(F("Stepper is running, ignoring <C>"));
return;
}
if (!calibrating || homed == 2) {
initiateCalibration();
}
}

// D command to enable debug output
void serialCommandD() {
if (debug) {
Expand All @@ -131,6 +148,10 @@ void serialCommandD() {

// E command to erase EEPROM
void serialCommandE() {
if (stepper.isRunning()) {
Serial.println(F("Stepper is running, ignoring <E>"));
return;
}
Serial.println(F("Erasing full step count from EEPROM"));
clearEEPROM();
#ifndef FULL_STEP_COUNT
Expand All @@ -139,10 +160,27 @@ void serialCommandE() {
#endif
}

// H command to initiate homing
void serialCommandH() {
if (stepper.isRunning()) {
Serial.println(F("Stepper is running, ignoring <H>"));
return;
}
if (!calibrating || homed == 2) {
initiateHoming();
}
}

// M command to move
void serialCommandM(long steps) {
if (stepper.isRunning()) {
Serial.println(F("Stepper is running, ignoring <M>"));
return;
}
if (steps < 0) {
Serial.println(F("Cannot provide a negative step count"));
} else if (steps > 32767) {
Serial.println(F("Step count too large, refer to the documentation for large step counts > 32767"));
} else {
Serial.print(F("Test move "));
Serial.print(steps);
Expand All @@ -162,6 +200,10 @@ void serialCommandR() {

// T command to perform sensor testing
void serialCommandT() {
if (stepper.isRunning()) {
Serial.println(F("Stepper is running, ignoring <T>"));
return;
}
if (sensorTesting) {
Serial.println(F("Disabling sensor testing mode, reboot required"));
sensorTesting = false;
Expand Down Expand Up @@ -226,6 +268,16 @@ void displayTTEXConfig() {
Serial.println(F("Rotating SHORTEST DIRECTION"));
#endif

#if defined(INVERT_DIRECTION)
Serial.println(F("INVERT_DIRECTION enabled"));
#endif
#if defined(INVERT_STEP)
Serial.println(F("INVERT_STEP enabled"));
#endif
#if defined(INVERT_ENABLE)
Serial.println(F("INVERT_ENABLE enabled"));
#endif

// If in sensor testing mode, display this, don't enable stepper or I2C
if (sensorTesting) {
Serial.println(F("SENSOR TESTING ENABLED, EX-Turntable operations disabled"));
Expand Down Expand Up @@ -263,19 +315,25 @@ void receiveEvent(int received) {
receivedStepsLSB = Wire.read();
activity = Wire.read();
}
if (debug) {
Serial.print(F("DEBUG: receivedStepsMSB:"));
Serial.print(receivedStepsMSB);
Serial.print(F(", receivedStepsLSB:"));
Serial.print(receivedStepsLSB);
Serial.print(F(", activity:"));
Serial.println(activity);
}
receivedSteps = (receivedStepsMSB << 8) + receivedStepsLSB;
if (gearingFactor > 10) {
gearingFactor = 10;
}
steps = receivedSteps * gearingFactor;
if (debug) {
Serial.print(F("DEBUG: receivedStepsMSB|receivedStepsLSB|activity: "));
Serial.print(receivedStepsMSB);
Serial.print(F("|"));
Serial.print(receivedStepsLSB);
Serial.print(F("|"));
Serial.println(activity);
Serial.print(F("DEBUG: gearingFactor|receivedSteps|steps: "));
Serial.print(gearingFactor);
Serial.print(F("|"));
Serial.print(receivedSteps);
Serial.print(F("|"));
Serial.println(steps);
}
if (steps <= fullTurnSteps && activity < 2 && !stepper.isRunning() && !calibrating) {
// Activities 0/1 require turning and setting phase, process only if stepper is not running.
if (debug) {
Expand Down
2 changes: 2 additions & 0 deletions IOFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ extern bool sensorTesting;

void setupWire();
void processSerialInput();
void serialCommandC();
void serialCommandD();
void serialCommandE();
void serialCommandH();
void serialCommandM(long steps);
void serialCommandR();
void serialCommandT();
Expand Down
12 changes: 5 additions & 7 deletions TurntableFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ long fullTurnSteps; // Assign our defined full t
long halfTurnSteps; // Defines a half turn to enable moving the least distance.
long phaseSwitchStartSteps; // Defines the step count at which phase should automatically invert.
long phaseSwitchStopSteps; // Defines the step count at which phase should automatically revert.
long lastTarget = sanitySteps; // Holds the last step target (prevents continuous rotatins if homing fails).
long lastTarget = sanitySteps; // Holds the last step target (prevents continuous rotation if homing fails).
uint8_t ledState = 7; // Flag for the LED state: 4 on, 5 slow, 6 fast, 7 off.
bool ledOutput = LOW; // Boolean for the actual state of the output LED pin.
unsigned long ledMillis = 0; // Required for non blocking LED blink rate timing.
Expand All @@ -60,10 +60,10 @@ bool invertDirection = true;
#else
bool invertDirection = false;
#endif
#ifdef INVERT_STEPS
bool invertSteps = true;
#ifdef INVERT_STEP
bool invertStep = true;
#else
bool invertSteps = false;
bool invertStep = false;
#endif
#ifdef INVERT_ENABLE
bool invertEnable = true;
Expand All @@ -77,9 +77,7 @@ AccelStepper stepper = STEPPER_DEFINITION;
void startupConfiguration() {
#if SELECTED_DRIVER == A4988_DRIVER
stepper.setEnablePin(A2);
stepper.setPinsInverted(invertDirection, invertSteps, invertEnable);
#else
stepper.setPinsInverted(invertDirection, invertDirection, invertDirection, invertDirection, invertEnable);
stepper.setPinsInverted(invertDirection, invertStep, invertEnable);
#endif
#if HOME_SENSOR_ACTIVE_STATE == LOW
pinMode(homeSensorPin, INPUT_PULLUP);
Expand Down
13 changes: 7 additions & 6 deletions config.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,22 @@
// in those instances, no custom configuration would be required.
//

#define STEPPER_DRIVER ULN2003_HALF
// #define STEPPER_DRIVER ULN2003_HALF
// #define STEPPER_DRIVER ULN2003_FULL
// #define STEPPER_DRIVER A4988
#define STEPPER_DRIVER A4988
//
// If you need to invert the direction of the stepper, uncomment this line. This is likely
// required when using a TMC2208. It may also be required to change the rotation from
// counter clockwise to clockwise when using the ULN2003.
// required when using a TMC2208. On two wire drivers such as A4988, DRV8825, and TMC2208,
// this inverts the DIR pin. On ULN2003, this alters the pin order to reverse direction.
// #define INVERT_DIRECTION
//
// When using a two wire driver (eg. A4988, DRV8825, TMC2208), it may be necessary to invert
// the step pin. If so, uncomment this line.
// #define INVERT_STEPS
// the step pin. If so, uncomment this line. This has no effect on ULN2003.
// #define INVERT_STEP
//
// When using a two wire driver (eg. A4988, DRV8825, TMC2208), it may be necessary to invert
// the enable pin behaviour if you wish to have the stepper driver disabled when not moving.
// This has no effect on ULN2003.
// #define INVERT_ENABLE

/////////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion standard_steppers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

#define FULLSTEPS 4096

#if STEPPER_DRIVER == ULN2003_HALF
#if STEPPER_DRIVER == ULN2003_HALF && defined(INVERT_DIRECTION)
#define STEPPER_DEFINITION AccelStepper(AccelStepper::HALF4WIRE, A3, A1, A2, A0)
#elif STEPPER_DRIVER == ULN2003_HALF
#define STEPPER_DEFINITION AccelStepper(AccelStepper::HALF4WIRE, A0, A2, A1, A3)
#elif STEPPER_DRIVER == ULN2003_FULL && defined(INVERT_DIRECTION)
#define STEPPER_DEFINITION AccelStepper(AccelStepper::FULL4WIRE, A3, A1, A2, A0)
#elif STEPPER_DRIVER == ULN2003_FULL
#define STEPPER_DEFINITION AccelStepper(AccelStepper::FULL4WIRE, A0, A2, A1, A3)
#elif STEPPER_DRIVER == A4988
Expand Down

0 comments on commit d8e2e37

Please sign in to comment.