Browse Source

bunch of debugging stuff

develop
JonathanS 3 years ago
parent
commit
158ba22514
  1. 183
      src/getting_started.ino

183
src/getting_started.ino

@ -26,14 +26,31 @@ v0.1 - it works
#include <Trinamic_TMC2130.h>
// pin configuration (this is the X-axis on a standard RAMPS 1.4)
#define CS_PIN 21
#define EN_PIN 18 //enable (CFG6)
#define DIR_PIN 19 //direction
#define STEP_PIN 20 //step
#define CS_PIN 7
#define EN_PIN 8 //enable (CFG6)
#define DIR_PIN 9 //direction
#define STEP_PIN 10 //step
Trinamic_TMC2130 myStepper(CS_PIN);
// hand-tuned constants for our application
const int8_t STALLGUARD_THRESHOLD = 10;
int8_t sgt = STALLGUARD_THRESHOLD;
int8_t irun = 10;
int8_t ihold = 10;
int8_t iholddelay = 10;
int16_t dc_time = 25;
int16_t dc_sg = 2;
void setup(){
Serial.begin(1500000);
while (!Serial);
// pins
@ -48,7 +65,7 @@ void setup(){
digitalWrite(EN_PIN, LOW); // enable driver
// stepper
myStepper.init();
myStepper.init();
Serial.print("REG_IOIN: ");
uint32_t ioin = 0;
@ -79,47 +96,54 @@ void setup(){
while (true);
}
chopconf = chopconf ^ 0x05;
chopconf = 0x0000;
myStepper.write_REG(TMC_REG_CHOPCONF, chopconf);
//myStepper.set_mres(64); // ({1,2,4,8,16,32,64,128,256}) number of microsteps
//myStepper.set_IHOLD_IRUN(31,31,5); // ([0-31],[0-31],[0-5]) sets all currents to maximum
//myStepper.set_I_scale_analog(1); // ({0,1}) 0: I_REF internal, 1: sets I_REF to AIN
//myStepper.set_tbl(1); // ([0-3]) set comparator blank time to 16, 24, 36 or 54 clocks, 1 or 2 is recommended
myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only withTBL>2, 2-15: off time setting during slow decay phase
myStepper.set_tbl(1);
myStepper.set_hend(0x3 + -2);
myStepper.set_hstrt(4);
myStepper.set_mres(32);
myStepper.set_mres(128);
// myStepper.set_chm(2);
// myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
// configure motor current
//myStepper.set_IHOLD_IRUN(1,31,31);
myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay);
//myStepper.set_freewheel(01);
//myStepper.set_I_scale_analog(1); // use external Aref
myStepper.set_hstrt(0);
myStepper.set_hend(13);
// configure coolStep
myStepper.set_semin(4); // semin*32 = 128
myStepper.set_semax(3); // (semin + semax + 1)*32 = 256
myStepper.set_seimin(1); // 1/4 of IRUN
myStepper.set_TCOOLTHRS(900);
myStepper.set_THIGH(100);
// configure motor current
myStepper.set_IHOLD_IRUN(1,31,31);
myStepper.set_I_scale_analog(1);
myStepper.set_tbl(4);
myStepper.set_TPWMTHRS(1000);
// configure dcStep mode
myStepper.set_VDCMIN(1);
myStepper.set_DCCTRL(50, 10);
//myStepper.set_sedn(0b01);
// configure dcStep mode
myStepper.set_vhighchm(1);
myStepper.set_vhighfs(1);
myStepper.set_VDCMIN(300);
myStepper.set_DCCTRL(dc_time, dc_sg);
myStepper.set_sgt(127);
myStepper.set_TCOOLTHRS(1);
myStepper.set_TPWMTHRS(1);
myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
// configure StallGuard
myStepper.set_sfilt(1);
myStepper.set_sgt(STALLGUARD_THRESHOLD);
@ -130,36 +154,116 @@ void setup(){
void loop(){
// make a step
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(42);
delayMicroseconds(84);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(42);
delayMicroseconds(84);
static int sometimes = 0;
if (sometimes++ > 100) {
if (sometimes++ > 20) {
sometimes = 0;
int32_t gstat;
myStepper.read_REG(TMC_REG_GSTAT, &gstat);
uint32_t drv_status = 0;
myStepper.read_REG(TMC_REG_DRV_STATUS, &drv_status);
uint32_t stallguard = drv_status & 0x1ff;
uint32_t current = (drv_status >> 16) & 0x1f;
if (gstat || (drv_status >> 24 & 0x0F)) {
Serial.print("ALARM: GSTAT=");
Serial.print(gstat, HEX);
uint32_t drv_status = 0;
myStepper.read_REG(TMC_REG_DRV_STATUS, &drv_status);
Serial.print(" DRV_STATUS>>24=");
Serial.println((drv_status >> 24) & 0x0F, HEX);
}
int32_t lost_steps;
myStepper.read_REG(TMC_REG_LOST_STEPS, &lost_steps);
uint32_t tstep;
myStepper.read_REG(TMC_REG_TSTEP, &tstep);
static int32_t last_lost_steps = 0;
Serial.print("Lost Steps: ");
Serial.print(lost_steps);
Serial.print(" Current: ");
Serial.print(current);
Serial.print(" TSTEP=");
Serial.print(tstep);
Serial.print(" SGFLAG=");
Serial.print((drv_status >> 24) & 1);
Serial.print(" Stallguard =");
Serial.print(stallguard);
/*
for (int i = 0; i < 64; i++) {
Serial.print(stallguard * 64 / 512 >= i ? "|" : " ");
}
* */
Serial.print("\r");
if (Serial.available()) {
int8_t read_byte = Serial.read();
if (read_byte == '0') { /* TIMSK1 &= ~(1 << OCIE1A); */ digitalWrite( EN_PIN, HIGH ); }
else if (read_byte == '1') { /* TIMSK1 |= (1 << OCIE1A); */ digitalWrite( EN_PIN, LOW ); }
//else if (read_byte == '+') { if (OCR1A > MAX_SPEED) OCR1A -= 20; }
//else if (read_byte == '-') { if (OCR1A < MIN_SPEED) OCR1A += 20; }
else if (read_byte == 'r') { sgt++; myStepper.set_sgt(sgt); }
else if (read_byte == 'f') { sgt--; myStepper.set_sgt(sgt); }
else if (read_byte == 'q') { ihold++; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 'a') { ihold--; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 'w') { irun++; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 's') { irun--; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 'e') { iholddelay++; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 'd') { iholddelay--; myStepper.set_IHOLD_IRUN(ihold,irun,iholddelay); }
else if (read_byte == 't') { dc_time++; myStepper.set_DCCTRL(dc_time, dc_sg); }
else if (read_byte == 'g') { dc_time--; myStepper.set_DCCTRL(dc_time, dc_sg); }
else if (read_byte == 'z') { dc_sg++; myStepper.set_DCCTRL(dc_time, dc_sg); }
else if (read_byte == 'h') { dc_sg--; myStepper.set_DCCTRL(dc_time, dc_sg); }
}
const int STEP_THRESHOLD = 5;
const int DIRCHANGE_HOLDOFF = 2;
static int dirchange_timeout = DIRCHANGE_HOLDOFF;
if ((lost_steps >= last_lost_steps + STEP_THRESHOLD) || (lost_steps <= last_lost_steps - STEP_THRESHOLD) ) {
/*
if (stallguard < 1) {
//if ((lost_steps >= last_lost_steps + STEP_THRESHOLD) || (lost_steps <= last_lost_steps - STEP_THRESHOLD) ) {
if (--dirchange_timeout < 0) {
Serial.print("Lost Steps: ");
Serial.println(lost_steps);
Serial.print("Last Lost Steps: ");
Serial.println(lost_steps);
@ -167,10 +271,13 @@ void loop(){
Serial.println(dirchange_timeout);
digitalWrite(DIR_PIN, !digitalRead(DIR_PIN));
delay(2000);
last_lost_steps = lost_steps;
dirchange_timeout = DIRCHANGE_HOLDOFF;
}
}
* */
}
}

Loading…
Cancel
Save