Skip to content

Commit

Permalink
finalizing pulse counter for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Jan 16, 2021
1 parent c651c61 commit 85b8564
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 41 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ cd pio_dirs/StepperDemo
pio run -e avr --target upload --upload-port /dev/ttyUSB0
```

## TEST STRATEGY

The library is tested with different kind of tests:
* PC only, which reside in tests/.
These tests focussing primarily the ramp generator and part of the API
* simavr based for avr
The simavr is an excellent simulator for avr microcontrollers. This allows to check the avr implementation thoroughly and even count the number of steps generated. Tested code is mainly the StepperDemo, which gets fed in a line of commands to execute.
These tests focus on avr and help to check the whole library code and helps for esp32
* esp32 tests with another pulse counter attached
The FastAccelStepper-API supports to attach another free pulse counter to a stepper's step and dir pins. This counter counts in the range of -16383 to 16383 with wrap around to 0. The test condition is, that the library's view of the position should match the independently counted one. These tests are still evolving
* Test for pulse generation using examples/Pulses
This has been intensively used to debug the esp32 ISR code
* manual tests using StepperDemo
These are unstructured tests with listening to the motor and observing the behavior

## CHANGELOG

See [changelog](https://github.com/gin66/FastAccelStepper/blob/master/CHANGELOG)
Expand Down
41 changes: 30 additions & 11 deletions examples/StepperDemo/StepperDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <avr/sleep.h>
#endif

#define VERSION "post-d6679bd"
#define VERSION "post-c651c61"

struct stepper_config_s {
uint8_t step;
Expand Down Expand Up @@ -264,7 +264,12 @@ const static char messages[] PROGMEM =
#define MSG_CANNOT_SET_DIRECTION_PIN 37
"Cannot set direction pin to |"
#define MSG_STEPPER_VERSION 38
"StepperDemo Version " VERSION "\n|";
"StepperDemo Version " VERSION
"\n|"
#define MSG_ATTACH_PULSE_COUNTER 39
"Attach pulse counter |"
#define MSG_ERROR_ATTACH_PULSE_COUNTER 40
"ERROR attaching pulse counter\n|";

void output_msg(int8_t i) {
char ch;
Expand Down Expand Up @@ -434,9 +439,16 @@ uint32_t pause_ms = 0;
uint32_t pause_start = 0;

void info(FastAccelStepper *s) {
Serial.print('@');
Serial.print(s->getCurrentPosition());
#if defined(ARDUINO_ARCH_ESP32)
if (s->pulseCounterAttached()) {
Serial.print(" [");
Serial.print(s->readPulseCounter());
Serial.print(']');
}
#endif
if (s->isRunning()) {
Serial.print("@");
Serial.print(s->getCurrentPosition());
if (s->isRunningContinuously()) {
Serial.print(" nonstop");
} else {
Expand All @@ -445,7 +457,7 @@ void info(FastAccelStepper *s) {
}
Serial.print(" QueueEnd=");
Serial.print(s->getPositionAfterCommandsCompleted());
Serial.print("/");
Serial.print('/');
Serial.print(s->getPeriodAfterCommandsCompleted());
Serial.print("us");
if (s->isRampGeneratorActive()) {
Expand Down Expand Up @@ -474,11 +486,8 @@ void info(FastAccelStepper *s) {
} else {
Serial.print(" MANU");
}
} else {
Serial.print("@");
Serial.print(s->getPositionAfterCommandsCompleted());
}
Serial.print(" ");
Serial.print(' ');
}

const static char usage_str[] PROGMEM =
Expand Down Expand Up @@ -516,6 +525,7 @@ const static char usage_str[] PROGMEM =
" T ... Test selected motor with direct port access\n"
#if defined(ARDUINO_ARCH_ESP32)
" r ... Call ESP.restart()\n"
" p<n> ... Attach pulse counter n<=7\n"
#endif
" t ... Enter test mode\n"
" Q ... Toggle print usage on motor stop\n"
Expand Down Expand Up @@ -547,7 +557,7 @@ void stepper_info() {
} else {
Serial.print(" ");
}
Serial.print("M");
Serial.print('M');
Serial.print(i + 1);
Serial.print(": ");
info(stepper[i]);
Expand Down Expand Up @@ -585,7 +595,7 @@ void usage() {
void output_info() {
for (uint8_t i = 0; i < MAX_STEPPER; i++) {
if (stepper[i]) {
Serial.print("M");
Serial.print('M');
Serial.print(i + 1);
Serial.print(": ");
info(stepper[i]);
Expand Down Expand Up @@ -793,6 +803,15 @@ void loop() {
output_msg(MSG_STEPPED_BACKWARD);
}
}
#if defined(ARDUINO_ARCH_ESP32)
else if (sscanf(out_buffer, "p%lu", &val) == 1) {
output_msg(MSG_ATTACH_PULSE_COUNTER);
Serial.println(val);
if (!stepper_selected->attachToPulseCounter(val)) {
output_msg(MSG_ERROR_ATTACH_PULSE_COUNTER);
}
}
#endif
} else {
if (strcmp(out_buffer, "x") == 0) {
output_msg(MSG_EXIT_TO_MAIN_MENU);
Expand Down
8 changes: 4 additions & 4 deletions examples/StepperDemo/test_seq_08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ bool test_seq_08(FastAccelStepper *stepper, struct test_seq_s *seq,
case 0: // INIT
srand(135);
if (!stepper->attachToPulseCounter(7)) {
Serial.println("Error attaching to pulse counter");
seq->state = TEST_STATE_ERROR;
return true;
}
Serial.println("Error attaching to pulse counter");
seq->state = TEST_STATE_ERROR;
return true;
}
seq->u32_1 = time_ms;
seq->state++;
break;
Expand Down
8 changes: 4 additions & 4 deletions src/FastAccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,13 @@ void FastAccelStepper::reAttachToPin() { fas_queue[_queue_num].connect(); }
bool FastAccelStepper::attachToPulseCounter(uint8_t pcnt_unit) {
if (pcnt_unit < 8) {
if (_esp32_attachToPulseCounter(pcnt_unit, this)) {
_attached_pulse_cnt_unit = pcnt_unit;
return true;
}
_attached_pulse_cnt_unit = pcnt_unit;
return true;
}
}
return false;
}
uint16_t FastAccelStepper::readPulseCounter() {
int16_t FastAccelStepper::readPulseCounter() {
if (_attached_pulse_cnt_unit >= 0) {
return _esp32_readPulseCounter(_attached_pulse_cnt_unit);
}
Expand Down
17 changes: 16 additions & 1 deletion src/FastAccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,23 @@ class FastAccelStepper {
void reAttachToPin();

#if defined(ARDUINO_ARCH_ESP32)
// These two functions are only available on esp32.
// The first can attach any of the eight pulse counters to this stepper.
// The second then will read the current pulse counter value
//
// The user is responsible to not use a pulse counter, which is occupied by a
// stepper and by this render the stepper or even blow up the uC.
//
// Pulse counter 6 and 7 are not used by the stepper library and are judged as
// available. If only five steppers are defined, then 5 gets available. If
// four steppers are defined, then 4 is usable,too.
//
// The function are intended for testing, because the library should always
// output the correct amount of pulses. Possible application usage would be an
// immediate and interrupt friendly version for getCurrentPosition()
bool attachToPulseCounter(uint8_t pcnt_unit);
uint16_t readPulseCounter();
int16_t readPulseCounter();
bool pulseCounterAttached() { return _attached_pulse_cnt_unit >= 0; }
#endif

private:
Expand Down
29 changes: 8 additions & 21 deletions src/StepperISR_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,27 +387,14 @@ void StepperQueue::forceStop() {
bool StepperQueue::isValidStepPin(uint8_t step_pin) { return true; }
int8_t StepperQueue::queueNumForStepPin(uint8_t step_pin) { return -1; }


uint32_t sig_idx[8] = {
PCNT_SIG_CH0_IN0_IDX,
PCNT_SIG_CH0_IN1_IDX,
PCNT_SIG_CH0_IN2_IDX,
PCNT_SIG_CH0_IN3_IDX,
PCNT_SIG_CH0_IN4_IDX,
PCNT_SIG_CH0_IN5_IDX,
PCNT_SIG_CH0_IN6_IDX,
PCNT_SIG_CH0_IN7_IDX
};
uint32_t ctrl_idx[8] = {
PCNT_CTRL_CH0_IN0_IDX,
PCNT_CTRL_CH0_IN1_IDX,
PCNT_CTRL_CH0_IN2_IDX,
PCNT_CTRL_CH0_IN3_IDX,
PCNT_CTRL_CH0_IN4_IDX,
PCNT_CTRL_CH0_IN5_IDX,
PCNT_CTRL_CH0_IN6_IDX,
PCNT_CTRL_CH0_IN7_IDX
};
uint32_t sig_idx[8] = {PCNT_SIG_CH0_IN0_IDX, PCNT_SIG_CH0_IN1_IDX,
PCNT_SIG_CH0_IN2_IDX, PCNT_SIG_CH0_IN3_IDX,
PCNT_SIG_CH0_IN4_IDX, PCNT_SIG_CH0_IN5_IDX,
PCNT_SIG_CH0_IN6_IDX, PCNT_SIG_CH0_IN7_IDX};
uint32_t ctrl_idx[8] = {PCNT_CTRL_CH0_IN0_IDX, PCNT_CTRL_CH0_IN1_IDX,
PCNT_CTRL_CH0_IN2_IDX, PCNT_CTRL_CH0_IN3_IDX,
PCNT_CTRL_CH0_IN4_IDX, PCNT_CTRL_CH0_IN5_IDX,
PCNT_CTRL_CH0_IN6_IDX, PCNT_CTRL_CH0_IN7_IDX};

bool _esp32_attachToPulseCounter(uint8_t pcnt_unit, FastAccelStepper *stepper) {
// TODO: Check if free pulse counter
Expand Down

0 comments on commit 85b8564

Please sign in to comment.