Combining stepper motors and sensors

Status
Not open for further replies.

jeex

New member
Hi all.

With Arduino it is impossible to have stepper motors do their work, while at the same time sensors measure things (tested). For example, letting a stepper motor run to point X while an ultrasonic measures and changes point X simultaneously. The way to avoid this off course is doing things linear (and stupid) in stead of parallel and smart!

Arduino simply cannot multi thread. You can simulate it (proto threading) but that does not work when controlling fast stepper motors (tested)

With raspberry comes inaccuracy, so that also does not provide highly accurate stepper motor control (tested). Besides, that's not what the berry is made for.

My question: the Teensy has a 32 bit ARM processor, and if i understand correctly, it also has the accuracy of a controller. Is it possible to really (not quasi) multi thread, so that i can accurately control several high speed stepper motors AND read out several sensors?

Thanks and best regards,

Jeex
 
My question: the Teensy has a 32 bit ARM processor, and if i understand correctly, it also has the accuracy of a controller. Is it possible to really (not quasi) multi thread, so that i can accurately control several high speed stepper motors AND read out several sensors?
It only has 1 CPU or core if you wish, does this answer your question?
Single CPU (time-slicing) multithreading has been implemented and presented in this forum.
 
Multi-core or multi-threading might even not be needed. First, the Teensys have lots of integrated half-autonomous hardware engines (PITs, SPI, I2C, FTMs, I2S, DMA, and so on, you name it) which can, once thoughtfully setup, do their work independently from the CPU without eating up resources, while the asthmatic Arduino core has to fight with bit banging. Second, the typical CPU clocks (96MHz for T3.2, 144MHz for T3.5, 180MHz for T3.6) lets ways more 32bit CPU operations happen in the same time than a 16MHz 8bit Arduino. So, I’d give it a try and be surprised...
 
Is it possible to really (not quasi) multi thread, so that i can accurately control several high speed stepper motors AND read out several sensors?

Yes, multiple steppers can be controlled while having CPU time for sensors. The best technique is more complex than merely multithreading, involving crafty use of hardware timers.

This is the best available library to do it.

https://github.com/luni64/TeensyStep
 
Thanks all,

Tomorrow, to the candy store and by me a Teensy. Do i need a big fat 3.6, or is the smaller 3.2 sufficient?
 
Last edited:
Do i need a big fat 3.6, or is the smaller 3.2 sufficient?
Obviously that depends on the details, like number of motors to run, how many sensors, how to read out the sensors etc. Regarding the motors you find information on the processor load for different teensies, step rates and number of motors here: https://github.com/luni64/TeensyStep#performance

Here a quick example showing the "parallel" execution of the following
  • synchronized, accelerated movement of 2 steppers,
  • reading out a sensor (simulated by a simple potentiometer) to change the blink frequency
  • asynchronous output of motor positions and sensor value.

All this is not really demanding for a Teensy 3.2.


Code:
#include "arduino.h"
#include "StepControl.h"

constexpr int PIN_STP1 = 0;
constexpr int PIN_DIR1 = 1;
constexpr int PIN_STP2 = 2;
constexpr int PIN_DIR2 = 3;

constexpr int PIN_POT = A0; // potentiometer to simulate sensor

Stepper m1(PIN_STP1, PIN_DIR1);
Stepper m2(PIN_STP2, PIN_DIR2);
StepControl<> controller;

void setup()
{
  Serial.begin(0);

  pinMode(LED_BUILTIN, OUTPUT);
  analogReadResolution(10);

  // motor properties
  m1.setMaxSpeed(10000).setAcceleration(5000);
  m2.setMaxSpeed(10000).setAcceleration(5000);
}

elapsedMillis blinkTimer = 0;
elapsedMillis outputTimer = 0;

void loop()
{
  // If the current movement is finished set new random targets and start again
  if (!controller.isRunning())
  {
    m1.setTargetRel(random(-5000, 5000));
    m2.setTargetRel(random(-5000, 5000));
    controller.moveAsync(m1, m2);
  }

  // simulate a sensor input (blink frequency proportional to voltage on pin PIN_POT)
  if (blinkTimer > (unsigned)analogRead(PIN_POT))
  {
    blinkTimer = 0;
    digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  }

  // asynchronous writing to serial
  if (outputTimer > 750)
  {
    outputTimer = 0;
    Serial.printf("Pot value:   %d \nPosition m1: %d\nPosition m2: %d\n\n", analogRead(PIN_POT), m1.getPosition(), m2.getPosition());
  }
}

Analyzer output. First trace: motor 1 step signal, Second trace: motor 2 step signal, third trace: LED
motorsensonr.PNG


Serial output (TyCommander)
motorsensonr2.PNG
 
Status
Not open for further replies.
Back
Top