Skip to content

Commit

Permalink
address micros() wrap-around #36
Browse files Browse the repository at this point in the history
  • Loading branch information
laurb9 committed Nov 5, 2017
1 parent 5988b56 commit 23ac5a9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
9 changes: 5 additions & 4 deletions src/BasicStepperDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void BasicStepperDriver::startMove(long steps){
} else {
// set up new move
dir_state = (steps >= 0) ? HIGH : LOW;
last_action_end = 0;
steps_remaining = abs(steps);
step_count = 0;
rest = 0;
Expand Down Expand Up @@ -253,9 +254,8 @@ void BasicStepperDriver::calcStepPulse(void){
* Toggle step and return time until next change is needed (micros)
*/
long BasicStepperDriver::nextAction(void){
long next_action_interval;
if (steps_remaining > 0){
microWaitUntil(next_action_time);
delayMicros(next_action_interval, last_action_end);
/*
* DIR pin is sampled on rising STEP edge, so it is set first
*/
Expand All @@ -267,17 +267,18 @@ long BasicStepperDriver::nextAction(void){
m = micros() - m;
// We should pull HIGH for 1-2us (step_high_min)
if (m < step_high_min){ // fast MCPU or CONSTANT_SPEED
DELAY_MICROS(step_high_min-m);
delayMicros(step_high_min-m);
m = step_high_min;
};
digitalWrite(step_pin, LOW);
// account for calcStepPulse() execution time; sets ceiling for max rpm on slower MCUs
last_action_end = micros();
next_action_interval = (pulse > m) ? pulse - m : 1;
} else {
// end of move
last_action_end = 0;
next_action_interval = 0;
}
next_action_time = micros() + next_action_interval;
return next_action_interval;
}

Expand Down
25 changes: 16 additions & 9 deletions src/BasicStepperDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
#define STEP_PULSE(steps, microsteps, rpm) (60*1000000L/steps/microsteps/rpm)

// don't call yield if we have a wait shorter than this
#define MIN_YIELD_MICROS 25
inline void microWaitUntil(unsigned long target_micros){
if (target_micros - micros() > MIN_YIELD_MICROS){
yield();
}
while (micros() < target_micros);
}
#define DELAY_MICROS(us) microWaitUntil(micros() + us)
#define MIN_YIELD_MICROS 50

/*
* Basic Stepper Driver class.
Expand All @@ -40,10 +33,24 @@ class BasicStepperDriver {
enum Mode {CONSTANT_SPEED, LINEAR_SPEED};
enum State {STOPPED, ACCELERATING, CRUISING, DECELERATING};

static inline void delayMicros(unsigned long delay_us, unsigned long start_us = 0){
if (delay_us){
if (!start_us){
start_us = micros();
}
if (delay_us > MIN_YIELD_MICROS){
yield();
}
// See https://www.gammon.com.au/millis
while (micros() - start_us < delay_us);
}
}

private:
// calculation remainder to be fed into successive steps to increase accuracy (Atmel DOC8017)
long rest;
unsigned long next_action_time = 0;
unsigned long last_action_end = 0;
unsigned long next_action_interval = 0;

protected:
/*
Expand Down
9 changes: 4 additions & 5 deletions src/MultiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ void MultiDriver::startMove(long steps1, long steps2, long steps3){
}
);
ready = false;
last_action_end = 0;
}
/*
* Trigger next step action
*/
long MultiDriver::nextAction(void){
static unsigned long next_action_time = 0;
long next_action_interval = 0;

microWaitUntil(next_action_time);
Motor::delayMicros(next_action_interval, last_action_end);
next_action_interval = 0;

// Trigger all the motors that need it (event timer = 0)
FOREACH_MOTOR(
Expand All @@ -60,7 +59,7 @@ long MultiDriver::nextAction(void){
event_timers[i] -= next_action_interval;
}
);
next_action_time = micros() + next_action_interval;
last_action_end = 0;
return next_action_interval;
}
/*
Expand Down
2 changes: 2 additions & 0 deletions src/MultiDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MultiDriver {
bool ready = true;
// when next state change is due for each motor
long event_timers[MAX_MOTORS];
unsigned long next_action_interval = 0;
unsigned long last_action_end = 0;

public:
/*
Expand Down

0 comments on commit 23ac5a9

Please sign in to comment.