teemuatlut / TMCStepper

MIT License
515 stars 201 forks source link

TMC5160 with ESP32 SPI mode #274

Open cholet852 opened 1 year ago

cholet852 commented 1 year ago

Hello,

I managed to get my stepper motor to work with my TMC5160 and Arduino Uno via FastAccelStepper.

I try the same thing but this time with an esp32 devkitc v4. But no way to get there.

So I use: -Arduino IDE -ESP32 devkitc v4 -TMC5160-PRO

All the connections seem good, I respect the connections according to my ESP32.

#include <TMCStepper.h>
#include "FastAccelStepper.h"
#include "Wire.h"
#include <SPI.h>

#define EN_PIN           13 // Enable
#define DIR_PIN          33 // Direction
#define STEP_PIN         25 // Step
#define CS_PIN           5 // Chip select

#define SW_MOSI          23 // Software Master Out Slave In (MOSI)
#define SW_MISO          19 // Software Master In Slave Out (MISO)
#define SW_SCK           18 // Software Slave Clock (SCK)

#define R_SENSE 0.075f // TMC5160 uses 0.075

//TMC5160Stepper tmc(CS_PIN, R_SENSE);
TMC5160Stepper tmc = TMC5160Stepper(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK);

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;

float bevelGear = (24 / 12.00);
float gearReduction = 24 ;

float stepsPerRev = 200.00;
int microstepping = 32;
int stepperState = 0;

float formula = ((stepsPerRev * microstepping) / 360.00);

void setup() {

    Serial.begin(115200);
    while(!Serial);
    Serial.println("Start...");

  SPI.begin();

  Serial.print("DRV_STATUS=0b");
    Serial.println(tmc.DRV_STATUS(), BIN);

  tmc.begin();
  tmc.toff(5); //off time
  tmc.blank_time(24); //blank time
  tmc.en_pwm_mode(1); //enable extremely quiet stepping
  tmc.microsteps(32); //256 microsteps
  tmc.rms_current(2000); //400mA RMS  //Die maximale Stromstärke

  engine.init();
  stepper = engine.stepperConnectToPin(STEP_PIN);
  if (stepper) {
    stepper->setDirectionPin(DIR_PIN);
    stepper->setEnablePin(EN_PIN);
    stepper->setAutoEnable(true);

    stepper->setSpeedInUs(20);       // the parameter is us/step !!!
    stepper->setAcceleration(100000);
  }
}

void receiveEvent(int bytes) {
  stepperState = Wire.read();    // read one character from the I2C
}

void loop() {

   // Faites avancer le moteur pas à pas
  stepper->move(8000);  // Déplacer le moteur de 1000 pas

  delay(1000);  // Attendre 1 seconde

  // stepperState = Serial.read();

  //   switch (stepperState) {
  //   case 50:
  //     stepper->move((50 * gearReduction) * formula);
  //     stepperState = 0;
  //     Serial.println("move 5");
  //     break;
  //   case 51:
  //     stepper->move((-50 * gearReduction) * formula);  
  //     stepperState = 0;
  //     Serial.println("move -5");
  //     break;
  //   case 52:
  //     stepper->move((100 * gearReduction) * formula);
  //     stepperState = 0;
  //     break;
  //   case 53:
  //     stepper->move((-100 * gearReduction) * formula); 
  //     stepperState = 0;
  //     break;
  //   case 54:
  //     stepper->move(0.2 * formula);
  //     stepperState = 0;    
  //     break;                
  //   case 55:
  //     stepper->move(-0.2 * formula);
  //     stepperState = 0; 
  //     break;
  //   break;
  // }
pitchbent commented 1 year ago

Hi, did you use the Ver1 branch of the library? It seems to be the only one to support the ESP32.