Browse Source

dcStep demo

master
JonathanS 3 years ago
parent
commit
5903a79bde
  1. 64
      src/getting_started.ino

64
src/getting_started.ino

@ -26,16 +26,44 @@ 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);
#define PRINT_REG(NAME, regval) \
Serial.print(#NAME ": "); \
myStepper.read_REG(NAME, &regval); \
Serial.println(regval, BIN); \
void print_regdump()
{
uint32_t regval;
PRINT_REG(TMC_REG_GCONF, regval)
PRINT_REG(TMC_REG_GSTAT, regval)
PRINT_REG(TMC_REG_IOIN, regval)
PRINT_REG(TMC_REG_TSTEP, regval)
PRINT_REG(TMC_REG_XDIRECT, regval)
PRINT_REG(TMC_REG_MSCNT, regval)
PRINT_REG(TMC_REG_MSCURACT, regval)
PRINT_REG(TMC_REG_CHOPCONF, regval)
PRINT_REG(TMC_REG_DRV_STATUS, regval)
PRINT_REG(TMC_REG_PWM_SCALE, regval)
PRINT_REG(TMC_REG_LOST_STEPS, regval)
}
void setup(){
Serial.begin(115200);
Serial.println("Waiting for serial...");
while (!Serial);
Serial.println("Hello");
// pins
pinMode(EN_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
@ -43,18 +71,13 @@ void setup(){
digitalWrite(EN_PIN, HIGH); // disable driver
digitalWrite(DIR_PIN, LOW); // chose direction
digitalWrite(STEP_PIN, LOW); // no step yet
digitalWrite(EN_PIN, LOW); // enable driver
// stepper
myStepper.init();
Serial.print("REG_IOIN: ");
uint32_t ioin = 0;
myStepper.read_REG(TMC_REG_IOIN, &ioin);
Serial.println(ioin, BIN);
uint32_t ioin;
myStepper.read_REG(TMC_REG_IOIN, &ioin);
if (ioin >> 24 != 0x11) {
Serial.println("SPI Communication failed or incompatible firmware");
while (true);
@ -120,8 +143,8 @@ void setup(){
myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
Serial.println("Finished Configuration: ");
print_regdump();
// get ready
digitalWrite(EN_PIN, LOW); // enable driver
@ -137,21 +160,31 @@ void loop(){
static int sometimes = 0;
if (sometimes++ > 100) {
sometimes = 0;
while (!Serial);
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;
Serial.print("Stallguard value: ");
Serial.println(stallguard);
Serial.print("Current: ");
Serial.println(current);
int32_t lost_steps;
myStepper.read_REG(TMC_REG_LOST_STEPS, &lost_steps);
static int32_t last_lost_steps = 0;
Serial.print("Lost steps: ");
Serial.println(lost_steps);
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) ) {
@ -172,5 +205,6 @@ void loop(){
dirchange_timeout = DIRCHANGE_HOLDOFF;
}
}
*/
}
}

Loading…
Cancel
Save