Skip to content

Commit

Permalink
avoid compile error on sam and esp32 for oveTimed example
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Feb 20, 2025
1 parent 65e5105 commit 52bf6ab
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions examples/MoveTimed/MoveTimed.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if defined(ARDUINO_ARCH_AVR)
#include "AVRStepperPins.h"
#include "FastAccelStepper.h"

Expand All @@ -15,6 +16,29 @@ FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepperX = NULL;
FastAccelStepper *stepperY = NULL;

struct control_s {
uint64_t time;
uint16_t phi;
bool is_running;
uint16_t drift; // time delta in ticks
};
struct control_s controlX = {
.time = 0, .phi = 0, .is_running = false, .drift = 0
};
struct control_s controlY = {
.time = 0, .phi = 90, .is_running = false, .drift = 0
};

// This means, the circle is executed in 360*4ms = 1440ms
#ifndef TIMESTEP_TICKS
#define TIMESTEP_TICKS (TICKS_PER_S/4000)
#endif

// Table generated by python:
// [int(math.sin(x*math.pi/180)*1600) for x in range(0,91)]
int16_t steps[91] = { 0, 27, 55, 83, 111, 139, 167, 194, 222, 250, 277, 305, 332, 359, 387, 414, 441, 467, 494, 520, 547, 573, 599, 625, 650, 676, 701, 726, 751, 775, 799, 824, 847, 871, 894, 917, 940, 962, 985, 1006, 1028, 1049, 1070, 1091, 1111, 1131, 1150, 1170, 1189, 1207, 1225, 1243, 1260, 1277, 1294, 1310, 1326, 1341, 1356, 1371, 1385, 1399, 1412, 1425, 1438, 1450, 1461, 1472, 1483, 1493, 1503, 1512, 1521, 1530, 1538, 1545, 1552, 1558, 1565, 1570, 1575, 1580, 1584, 1588, 1591, 1593, 1596, 1597, 1599, 1599, 1600
};

void setup() {
engine.init();
stepperX = engine.stepperConnectToPin(stepPinStepperX);
Expand All @@ -30,6 +54,81 @@ void setup() {
stepperY->setDirectionPin(dirPinStepperX);
stepperY->setEnablePin(enablePinStepperY);
stepperY->setAutoEnable(false);

// Without auto enable, need to enable manually both steppers
stepperX->enableOutputs();
stepperY->enableOutputs();

// Fill the queue with 1ms delay and start the queues.
stepperX->moveTimed(0,TICKS_PER_S/1000,NULL,false);
stepperY->moveTimed(0,TICKS_PER_S/1000,NULL,false);

// Start the queues. Currently best but not perfect synchronicity
stepperX->moveTimed(0,0,NULL,true);
stepperY->moveTimed(0,0,NULL,true);
}

void moveStepper(FastAccelStepper *stepper, struct control_s *control) {
if (control->phi == 360) {
control->phi = 0;
}

// The table contains only one quadrant
uint16_t index = control->phi;
bool negate = false;
if (index > 180) {
negate = true;
index = 360 - index;
}
if (index > 90) {
index = 180 - index;
}
int16_t position = steps[index];
if (negate) {
position = -position;
}
int32_t current_position = stepper->getPositionAfterCommandsCompleted();
if (!control->is_running) {
// we need to wait until current_position and position match
if (position != current_position) {
// just advance time and phi
control->time += TIMESTEP_TICKS;
control->phi += 1;
return;
}
control->is_running = true;
}
int32_t steps = position - current_position;
uint32_t actual;
int8_t rc;
uint32_t duration = TIMESTEP_TICKS + control->drift;
rc = stepper->moveTimed(steps,duration,&actual,true);
switch(rc) {
case MOVE_TIMED_EMPTY:
Serial.println("Empty");
/* fallthrough */
case MOVE_TIMED_OK:
control->drift = duration-actual;
control->time += actual;
control->phi += 1;
break;
case MOVE_TIMED_BUSY:
break;
case MOVE_TIMED_TOO_LARGE_ERROR:
Serial.println("Too large");
break;
}
}

void loop() {
if (controlX.time < controlY.time) {
moveStepper(stepperX, &controlX);
}
else {
moveStepper(stepperY, &controlY);
}
}
#else
void setup() {}
void loop() {}
#endif

0 comments on commit 52bf6ab

Please sign in to comment.