Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Combining stepper motors and sensors

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    3

    Combining stepper motors and sensors

    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

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,878
    Quote Originally Posted by jeex View Post
    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.

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,908
    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...

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,367
    Quote Originally Posted by jeex View Post
    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

  5. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    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 by jeex; 10-05-2018 at 09:03 PM.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    259
    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
    Click image for larger version. 

Name:	motorsensonr.PNG 
Views:	5 
Size:	17.9 KB 
ID:	14900


    Serial output (TyCommander)
    Click image for larger version. 

Name:	motorsensonr2.PNG 
Views:	7 
Size:	20.2 KB 
ID:	14901

  7. #7
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Cool. Thanks. On to the candy store.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •