New Stepper Motor Library

by reading this thread I did not really understand what Devs have done.

Have they just added the 15min internal cooldown for using RDF which is blizzlike or they have made some custom change ?
 
Hello dear Luni!
I use StepControl to move to the desired position. But I need the ability to change speed on the fly. How can i do this?
 
I did not want to wait for the Teensy 4 port to be done properly, and did not want to read any datasheets, so I made a hacky port (https://github.com/bondus/TeensyStep) to Teensy 4 using ChibiOS timers (https://github.com/greiman/ChRt)

It might hang, crash and burn, and be very slow. I have only done some very basic testing.

The pin manipulation of DIR/STEP pins did not work on Teensy 4, and the code did now allow for <Arduino.h> to be included, so you have to implement a little function somewhere:
Code:
void doPin(int pin, int state) {
    digitalWriteFast(pin, state);
}
A very hacky implementation indeed, but it works.

I'll hook it up to my little 3-stepper robot and give it a better spin...
 
I've used this library in the past with a Teensy 3.5 and had great success with the project. Any update on compatibility with Teensy 4.1? It would be great to have the additional processor speed combined with the on-board Ethernet functionality.
 
No, there are only two timer channels on FTM1/2 :-(

However, if you don't need the controllers at the same time it should work. The controllers are supposed to release the timers after the movement. So, if you use one after the other, the two FTMs should be sufficient.
 
Works great. Love the library!
Is it possible to set the motor deceleration different than the acceleration? I can't figure out how to do this, as a single number controls both.
In my application, the motors are capable of decelerating much more quickly than they can accelerate, (as is the case in many applications.)
 
For steppers the acceleration and deceleration miss-step thresholds are similar as they depend on the inertial torque
compared to the dynamic torque available. If you add a braking mechanism to a stepper system it may lose
position whenever you brake. Normally this means brakes are only for emergency-stop, otherwise the steppers
should have full control authority for reliable operation.
 
Is it possible to set the motor deceleration different than the acceleration?

I second MarkT's remarks above and the currently implemented accelerators do not provide this.

However, if for some reasons you want to change the acceleration behavior, the library is prepared to use custom accelerators. With a custom accelerator you can implement whatever acceleration scheme you need. See here https://github.com/luni64/TeensyStep/tree/master/src/accelerators for the standard accelerators. You can then simply typedef a custom controller which uses your custom accelerator. See here for the standard controllers https://github.com/luni64/TeensySte...c47d2d8982c73fc414f8702f/src/TeensyStep.h#L38

Please note that all of this can be done in your sketch without touching the library code.
 
I second MarkT's remarks above and the currently implemented accelerators do not provide this.

However, if for some reasons you want to change the acceleration behavior, the library is prepared to use custom accelerators.

Please note that all of this can be done in your sketch without touching the library code.

I am afraid that it is far beyond my "pay grade" to modify the library code. :) I have no issue modifying my sketch, however.

I am controlling the speed of a rather large spool that is winding up 3D printing filament, and gaining weight as it is doing so. The spool speed is controlled by a filament tension arm with a corresponding PID loop. The tension decreases, the arm moves up, the spool speed is increased to restore the arm position. The tension increases, the arm moves down.......

I suppose I could add an acceleration override to alter the acceleration/deceleration depending on the sign of the derivative of the control loop output. An increasing (positive) control output change would select an appropriate acceleration, while a decreasing (negative) control output change would select a different deceleration.

Is this what you suggest?
 
The pin manipulation of DIR/STEP pins did not work on Teensy 4, and the code did now allow for <Arduino.h> to be included, so you have to implement a little function somewhere:
Code:
void doPin(int pin, int state) {
    digitalWriteFast(pin, state);
}

Thank you for your (wacky?) implementation.
However, I don't understand what you mean by implementing the little function.
Can you explain further or post an example ?
Thanks a lot

@luni, any plan for an official support of Teensy 4.x ?
 
Is it possible to set the motor deceleration different than the acceleration?
I second this request with even a more complex solution for me.

My axis are vertical with some weight on.
So going down the acceleration could be faster.
But the deceleration must be slower.
While going up the acceleration must be slower.
But the deceleration could be faster.

Any thought on how to implement that?
Thank you everyone
 
@luni, any plan for an official support of Teensy 4.x ?
Actually, I'm currently working on it.

My axis are vertical with some weight on.
So going down the acceleration could be faster.
But the deceleration must be slower.
While going up the acceleration must be slower.
But the deceleration could be faster.

I don't know your mechanical setup but one often underestimates moment of inertia of the lead screw and motor anchor as limiting factors to the acceleration/deceleration. Some yeas ago I did a spreadsheet to calculate those effects (attached). It is in German, but mathematics should be easy to translate :)

Anyway, I'll try to make the acceleration settings more flexible.
 

Attachments

  • 96444.zip
    17 KB · Views: 130
Actually, I'm currently working on it.
Fantastic news!!!
Do you need beta testers?

I don't know your mechanical setup but one often underestimates moment of inertia of the lead screw and motor anchor as limiting factors to the acceleration/deceleration. Some yeas ago I did a spreadsheet to calculate those effects (attached). It is in German, but mathematics should be easy to translate :)
Our project is an art installation.
The goal is to lift a ring of light on a pillar. Each motor should lift about 2kg including the 1kg of the NEMA23 motor itself.
Our first prototype will be using 3GT belts on an Everman belt drive system. No idea if it will work... :)
As for the file, I will read it after work. That you.

Anyway, I'll try to make the acceleration settings more flexible.
Thank you.
My personal priority is still on the Teensy 4.1 :rolleyes:
 
Hi,

I'm using the teensystep to control a servo which works great!
but the issue is, once i've added the library i have a strange behavior.

I have also a DC motor that i contorl with a DC motor shield, which is controlling the speed of the motor by send PWM signal. i'm doing it via pin 5 of the teensy.

I can control the DC motor IF i'm not using the teensystep commands.
Once I turn on the stepper motor, the DC motor is not responding any more...
What could be the issue?

Here's the code:

Code:
#include <TeensyStep.h>

#define PUL 30
#define DIR 31
#define EN 32

#define steps_per_rev 3200
#define rpm 120

Stepper stepper_pump(PUL,DIR);
RotateControl controller;

void setup_stepper()
{
  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);

  
  stepper_pump.setMaxSpeed(steps_per_rev*rpm /60);  
  stepper_pump.setAcceleration(10*steps_per_rev*rpm /60);

  //controller.rotateAsync(stepper_pump);
//  controller.overrideSpeed(0);
}


void spin_stepper(bool spin)
{
  if(spin)
  {
    digitalWrite(EN, HIGH);
    controller.rotateAsync(stepper_pump);
  }
  else
  {
    controller.stopAsync();
    digitalWrite(EN, LOW);
  }
}

void steering_wheel_jog(int percent)
{
  float ratio = (float)percent / 100;
  
//  Serial.println("SET SPEED: " + String(ratio));
  controller.overrideSpeed(ratio);

}


switch (char_from_serial)                     //decide what command was sent
    {
      case 't': //top motor
        tmotor_mode = !tmotor_mode; 
        Top_Mixer_Motor(tmotor_mode);
        if (!tmotor_mode){
          Serial.println ("Top motor off");
        }
        if (tmotor_mode){
          Serial.println ("Top motor on");
        }
        break;

void Top_Mixer_Motor(int opmode1)
{
  if (opmode1)
    analogWrite (Top_pwm,255);
  else 
    analogWrite (Top_pwm,0);
  
}


The definitions are in setup section:

Code:
#define  Top_Mixer 6
#define  Top_Mixer2 7
#define  Top_pwm 5

//Define pins for 
  pinMode (Top_Mixer, OUTPUT);
  pinMode (Top_Mixer2, OUTPUT);
  pinMode (Top_pwm, OUTPUT);
 
You might want to try this https://github.com/luni64/TeensyStep4 (very experimental at the moment but should work somehow)

The API changed significantly. Main difference is, that I disposed the controllers and did stepper groups instead. I.e. you can now directly move a stepper or a group of steppers without using a controller.

Example:

Code:
#include "Arduino.h"
#include "teensystep4.h"

using namespace TS4;

Stepper s1(0, 3);
Stepper s2(1, 4);
Stepper s3(2, 5);

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);

    TS4::begin(); // calling begin is required now

    for (Stepper* s : {&s1, &s2, &s3}) // setup all motors
    {
        s->setMaxSpeed(10'000);
        s->setAcceleration(50'000);
    }

    // steppers can be moved directly
    s1.moveAbs(1000);
    s2.moveAbs(2000);
    delay(100);

    // define and move a group of steppers
    StepperGroup g1 = {s1, s2};
    s1.setTargetAbs(-1000);
    s2.setTargetAbs(-3000);
    g1.move();
    delay(100);

    // define and move a stepper group on the fly
    s3.setTargetAbs(3000);
    s1.setTargetAbs(0);
    StepperGroup{s3, s1}.move();
}

void loop()
{
    digitalToggleFast(LED_BUILTIN);
    delay(200);
}
https://user-images.githubuserconte...7095-d0670328-d67c-4736-aaa7-54cd02f901c8.png

I'm currently working on replacing the interval timers (limited to 4) by the TMR and FLEX timers. This means, the number of independently moving motors will not be limited to 4 anymore. I also try to get rid of the step interrupts (which are very inefficient). Instead I try to use the timers in PWM mode, reloading the required periods with DMA. First experiments look promising. If that works out, it should be possible to run a lot of motors simultaneously using very little processor time.
 
Hello luni,
I am using your library at the moment.
I have a big issue using a stepper motor from one side, and a DC motor from the other side...
Actually, when " controller.move(motor); " is running, it stops the activity of the DC motor, and even within the loop, the DC motor will never start again without reseting the teensy....
Could you confirm ?
Thanks !!

I am using the following code (very basic) :
Code:
#include "TeensyStep.h"

Stepper motor(0, 1);       // STEP pin: 0, DIR pin: 1
StepControl controller;    // Use default settings 

int motor1dir = 23;
int motor1vit = 22;

void setup()
{
  pinMode(motor1dir, OUTPUT);
  pinMode(motor1vit, OUTPUT);
}

void loop() 
{
  digitalWrite(motor1dir, LOW);
  analogWrite(motor1vit, 200);
  delay(5000);
  
  motor.setMaxSpeed(8000);
  motor.setAcceleration(2000);
  motor.setTargetRel(-20000);
  controller.move(motor); 
  delay(3500);

  digitalWrite(motor1dir, LOW);
  analogWrite(motor1vit, 200);

  delay(1500);

  motor.setMaxSpeed(2000);
  motor.setAcceleration(2000);
  motor.setTargetRel(2000);  // Set target position to 1000 steps from current position
  controller.move(motor);    // Do the move
  delay(1500);
}
 
The library uses the FTM0 timer module which is also needed for PWM generation. Here https://www.pjrc.com/teensy/td_pulse.html you find a table (scroll down to PWM Frequency) which shows which pin uses which FTM module. If you can change your PWM pin to one which uses FTM1, FTM2, FTM3 or TMP1 it should work.
 
Please correct me if i am wrong or have missed it reading the thread but couldn't this code be used for a solar tracker. the motors do not move all the time between sun tracking movement so the LDR's are used to update the new position say every 5 or 10 degrees. then after sundown return to zero which is east and vertical.

Just thinking out loud i guess thinking of T4 or 4.1.
Regards.
 
Back
Top