Close

Fancy stepper motor controller update!

A project log for HaRoCo @ The DesignLab

Emotive Support Robots

richardrichard 07/13/2018 at 21:192 Comments

I've been working on a fairly platform-agnostic library for the fantastic Trinamic motion controller chips that we're using in the HaRoCo-12, and much progress has been made! These first functions make the basic operations we need incredibly simple to use. The register configuration (both necessary to do and unnecessarily complex in structure) is buried for now, but has been carved up and commented into a more understandable state. 

Here's a video of a NEMA-8 size stepper driving back and forth ~180 degrees:

It doesn't look like much, but it took a lot of datasheet reading to make the code this simple. The motion controller is performing a bunch of tasks for the Arduino, including those lovely velocity curves and counting the steps so there's no longer timing-critical tasks on the Arduino. Here's how simple it is:

#include <TMC5041.h>
TMC5041 motorGroup1 = TMC5041(8);

void setup(){
  motorGroup1.begin();
}

void loop(){
  motorGroup1.moveAngle(1, 180);
  while(!motorGroup1.reachedTarget(1)){
    delay(100);
  }
  delay(1000);
  motorGroup1.moveAngle(1, 0);
  while(!motorGroup1.reachedTarget(1)){
    delay(100);
  }
}

You can see that I'm using some blocking whiles as a test for the reachedTarget function, but they're not necessary. The 1 second delay in between changing direction is to create an asymmetry in the behavior so I can check some other features I'm working on now like braking and acceleration curves.

If you're desperate to get started with the TMC5041 let me know and I'll send you the current library!

Protip: If a register has bit-by-bit important configurations, don't muck up the readability by converting that into hex. It's a jerk move. Keep it in binary so it's easy to modify and understand in the future, like this:

  // PWM Config                                freewheel    autoscale   pwm_freq    pwm_grad   pwm_amplitude        
  //unsigned long pwmconfig = 0b 0000 0000  00     01    0      1          01       11111111   11111111;
  unsigned long pwmconfig = 0b00000000001001011111111111111111; 
  sendData(0x90,pwmconfig);      // PWMCONF Motor1

Discussions

agha.dark.net wrote 04/24/2024 at 04:30 point

Hi Richard. Great work is it possible for you to share your library please?

  Are you sure? yes | no

rob.car wrote 01/15/2021 at 16:38 point

Hi Richard. Nice project! Is it possible to share your library? Would be great!

  Are you sure? yes | no