Skip to content

Commit

Permalink
Extend a CRUISING state move by calling startMove() again
Browse files Browse the repository at this point in the history
  • Loading branch information
laurb9 committed Sep 11, 2017
1 parent 81bbd55 commit 33abe65
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
41 changes: 34 additions & 7 deletions src/BasicStepperDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,19 @@ void BasicStepperDriver::rotate(double deg){
}

/*
* Set up a new move (calculate and save the parameters)
* Set up a new move or alter an active move (calculate and save the parameters)
*/
void BasicStepperDriver::startMove(long steps){
long speed;
dir_state = (steps >= 0) ? HIGH : LOW;
step_state = LOW;
steps_remaining = abs(steps);
step_count = 0;
switch (mode){
if (steps_remaining){
alterMove(steps);
} else {
// set up new move
dir_state = (steps >= 0) ? HIGH : LOW;
step_state = LOW;
steps_remaining = abs(steps);
step_count = 0;
switch (mode){
case LINEAR_SPEED:
// speed is in [steps/s]
speed = rpm * motor_steps / 60;
Expand All @@ -132,12 +136,35 @@ void BasicStepperDriver::startMove(long steps){
// Initial pulse (c0) including error correction factor 0.676 [us]
step_pulse = (1e+6)*0.676*sqrt(2.0f/(accel*microsteps));
break;

case CONSTANT_SPEED:
default:
step_pulse = STEP_PULSE(rpm, motor_steps, microsteps);
steps_to_cruise = 0;
steps_to_brake = 0;
}
}
}
/*
* Alter a running move by adding/removing steps
* FIXME: This is a naive implementation and it only works well in CRUISING state
*/
void BasicStepperDriver::alterMove(long steps){
switch (getCurrentState()){
case ACCELERATING: // this also works but will keep the original speed target
case CRUISING:
if (steps >= 0){
steps_remaining += steps;
} else {
steps_remaining = max(steps_to_brake, steps_remaining+steps);
};
break;
case DECELERATING:
// would need to start accelerating again -- NOT IMPLEMENTED
break;
case STOPPED:
startMove(steps);
break;
}
}
/*
Expand Down
11 changes: 7 additions & 4 deletions src/BasicStepperDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class BasicStepperDriver {

void calcStepPulse(void);

// this is internal because one can call the start methods while CRUISING to get here
void alterMove(long steps);

private:
// microstep range (1, 16, 32 etc)
static const short MAX_MICROSTEP = 128;
Expand Down Expand Up @@ -151,14 +154,14 @@ class BasicStepperDriver {
};
void startRotate(long deg);
void startRotate(double deg);
/*
* Optionally, call this to begin braking (and then stop) early
*/
void startBrake(void);
/*
* Toggle step and return time until next change is needed (micros)
*/
long nextAction(void);
/*
* Optionally, call this to begin braking (and then stop) early
*/
void startBrake(void);
/*
* State querying
*/
Expand Down

0 comments on commit 33abe65

Please sign in to comment.