#undef HW
//#define HW 1
#ifdef HW
#include<QuadEncoder.h>
#else
#include <EncoderTool.h>
using namespace EncoderTool;
#endif
// Rotary Encoder Pins
#define A 0
#define B 1
#define IDX 4
// Stepper driver pins
#define STEP_PIN 5
#define DIR_PIN 6
#define EN_PIN 7
#define pulseWidth 100 // if your stepper won't start, increase the PW
#define fwd 0
#define rev 1
int PPR = 200; // stepper pulses per revolution
int MPR = 4; // microsteps per step
int revolutions = 10; // desired number of revolutions for test
uint8_t adir;
int16_t count = 0;
int16_t stopcnt = 2; // do the test N times
unsigned long mytime;
#ifdef HW
volatile int mCurPosValue, old_position; // encoder outputs
QuadEncoder myEnc(1, A, B, 0, IDX); // HW spindle encoder
#else
uint32_t errorcount = 0;
int val1;
volatile int mCurPosValue, old_position; // encoder outputs
Encoder myEnc;
#endif
void setup() {
pinMode(A, INPUT); pinMode(B, INPUT); pinMode(IDX, INPUT);
pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); pinMode(EN_PIN, OUTPUT);
while(!Serial && millis() < 5000);
#ifdef HW
myEnc.setInitConfig();
myEnc.init();
myEnc.write(16384); // just because
Serial.printf("Using HW encoder\n");
#else
myEnc.begin(A, B, CountMode::full);
myEnc.attachCallback(nullptr);
myEnc.setValue(16384); // sets encoder count to 16K
myEnc.attachCallback(onEncChanged); // enable spindle enc callback
Serial.printf("Using SW encoder\n");
#endif
old_position = 16384;
adir = fwd;
digitalWriteFast(DIR_PIN, adir); // set the direction before enable
delayMicroseconds(4);
digitalWriteFast(EN_PIN, LOW); // enables the stepper driver
#ifdef HW
Serial.printf("Initial position = %d\n", myEnc.read());
#else
Serial.printf("Initial position = %d\n", myEnc.getValue());
#endif
Serial.printf("Number of revolutions before reversal = %d\n", revolutions);
}
void loop() {
runMove();
adir = !adir;
digitalWriteFast(DIR_PIN, adir);
delay(100);
runMove();
adir = !adir;
digitalWriteFast(DIR_PIN, adir);
delay(100);
count +=1;
if (count >= stopcnt) {
digitalWriteFast(EN_PIN, HIGH); // disable stepper driver
#ifdef HW
Serial.printf("Final position = %d\n", myEnc.read()); // what's the encoder say?
#else
Serial.printf("Final position = %d\n", myEnc.getValue());
#endif
while(1);
}
}
void runMove()
{
int stepsPerRevolution = PPR * MPR;
int total_revs = revolutions * stepsPerRevolution;
Serial.println("Moving");
for(int x = 0; x < total_revs; x++) {
mytime = micros();
digitalWriteFast(STEP_PIN, HIGH);
delayMicroseconds(pulseWidth);
digitalWriteFast(STEP_PIN, LOW);
delayMicroseconds(pulseWidth);
getPosition();
}
}
void getPosition(){
/* Read the position values. */
#ifdef HW
mCurPosValue = myEnc.read();
#else
mCurPosValue = myEnc.getValue();
#endif
if(mCurPosValue != old_position){
Serial.printf("%d, %i\n", micros() - mytime, mCurPosValue);
}
old_position = mCurPosValue;
}
#ifndef HW
void onEncChanged(int value, int delta) {
// if (delta > 0) {
// if (delta > 1) {
// Serial.printf("fault! delta = %lli\n", delta); errorcount += 1; }
// else {
// // delta = 1
// }
// }
// if (delta < 0) {
// if (delta<-1) {
// Serial.printf("fault! delta = %lli\n", delta);
// errorcount += 1;
// }
// else {
// // delta = -1
// }
// }
val1 = value;
}
#endif