TeensyStep. Why I have to use moveAsync with a while and delay?

Status
Not open for further replies.

Bking

Member
Hello,
I want to assign different position to stepper motors from Processing.
I can do this very well with the library accelstepper.
But with the teensy stepper library, I have to add this at the end of the void loop
Code:
 while(controller_1.isRunning() || controller_2.isRunning() || controller_3.isRunning()){  // wait until both controllers finished their movements
    delay( 0);

Morever it block my animation in Processing.

For now I just to play with 3 motor at different random position without the while and the delay because It blocks my animation in Processing

I put my entire program below
Code:
#include "TeensyStep.h"
//# define SerialUSB Serial1

 StepControl  controller_1;
 StepControl  controller_2;
 StepControl  controller_3;

    Stepper s1(3,2), s2(5,4), s3(7,6);
 
int i; //Test la loop

 //   Receive with start- and end-markers combined with parsing

const byte numChars = 200;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0}; //or {5} doesn't change anything

int integerFromPC0 = 0;
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
int integerFromPC4 = 0;
int integerFromPC5 = 0;
int integerFromPC6 = 0;
int integerFromPC7 = 0;
int integerFromPC8 = 0;
int integerFromPC9 = 0;

int PC0= 0;
int PC1= 0;
int PC2= 0;
int PC3= 0;
int PC4= 0;
int PC5= 0;
int PC6= 0;
int PC7= 0;
int PC8= 0;
int PC9= 0;

int PCTer0= 0;
int PCTer1= 0;
int PCTer2= 0;
int PCTer3= 0;
int PCTer4= 0;
int PCTer5= 0;
int PCTer6= 0;
int PCTer7= 0;
int PCTer8= 0;
int PCTer9= 0;

int LoworderTrigger = 0;
int orderTrigger = 0;
int orderCohesion  = 0;


float floatFromPC = 0.0; // no flat are used for the moment

boolean newData = false;

//============
const int ledPin =  LED_BUILTIN;// the number of the LED pin

//StepControl controller;
 
void setup()
{
  Serial.begin ( 500000); 
  Serial1.begin (500000); // receive datas from Processing with the "Programming Port"    

   while (!Serial);
    Serial.println("TEST" ); 
  
  s1
    .setMaxSpeed(3200)       // steps/s
    .setAcceleration(1000); // steps/s^2 
  
  s2
   .setMaxSpeed(3200)       // steps/s
    .setAcceleration(1000); // steps/s^2 

   s3
   .setMaxSpeed(3200)       // steps/s
    .setAcceleration(1000); // steps/s^2   
 
}

void loop() 
{  
    recvWithStartEndMarkers();  // receive 33 datas from Processing 30 times/sec. 
   
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
 
           
        parseData(); //  split the 33 data into its parts 
        
        i=  random (0, 6400);
            
    //    showPosition(); // These datas are used to control positions of 10 motors. Print them on the native port   
    //    showPhazisScaled(); // Print datas on the native port
    //    showTrigBangWithRevolution(); // Print datas on the native port
    
        newData = false;
    } 
    
 
 // Serial.print (i);
 // Serial1.print (i);
  
   //  s1.setTargetAbs(PC0);
   //  s2.setTargetAbs(PC0 );
   //  s3.setTargetAbs(PC0);

      s1.setTargetAbs(i*2);
      s2.setTargetAbs(i );
      s3.setTargetAbs(-i*2);
 

    controller_1.moveAsync  (s1   );   controller_2.moveAsync  ( s2  );  controller_3.moveAsync (s3 );
   
   while(controller_1.isRunning() || controller_2.isRunning() || controller_3.isRunning()){  // wait until both controllers finished their movements
    delay( 0);
  }
 
}

void recvWithStartEndMarkers() { // receive 33 datas from Processing 30 times/sec. They are marked like that in Processing <12, -98, 34,... ,-340>  
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the 33 data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
 
    integerFromPC0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC9 = atoi(strtokIndx);     // convert this part to an integer

    
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC9 = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer9 = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    LoworderTrigger = atoi(strtokIndx); // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    orderTrigger = atoi(strtokIndx); // convert this part to an integer
     
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    orderCohesion  = atoi(strtokIndx); // convert this part to an integer
 
}

//============
void  showPosition() { 
 
  SerialUSB.print ("K ");SerialUSB.println(PC0); 
  SerialUSB.print ("L ");SerialUSB.println(PC1);
  SerialUSB.print ("M ");SerialUSB.println(PC2);
  SerialUSB.print ("N ");SerialUSB.println(PC3);
  SerialUSB.print ("O ");SerialUSB.println(PC4); 
  SerialUSB.print ("P ");SerialUSB.println(PC5);
  SerialUSB.print ("Q ");SerialUSB.println(PC6);
  SerialUSB.print ("R ");SerialUSB.println(PC7);
  SerialUSB.print ("S ");SerialUSB.println(PC8);
  SerialUSB.print ("T ");SerialUSB.println(PC9);  
   
}

void showPhazisScaled()  {  
 
    SerialUSB.print ("A "); SerialUSB.println (integerFromPC0);    
    SerialUSB.print ("B "); SerialUSB.println (integerFromPC1);        
    SerialUSB.print ("C "); SerialUSB.println (integerFromPC2);    
    SerialUSB.print ("D "); SerialUSB.println (integerFromPC3);         
    SerialUSB.print ("E "); SerialUSB.println (integerFromPC4); 
    SerialUSB.print ("F "); SerialUSB.println (integerFromPC5);    
    SerialUSB.print ("G "); SerialUSB.println (integerFromPC6);        
    SerialUSB.print ("H "); SerialUSB.println (integerFromPC7);    
    SerialUSB.print ("I "); SerialUSB.println (integerFromPC8);         
    SerialUSB.print ("J "); SerialUSB.println (integerFromPC9); 
  
}

void showTrigBangWithRevolution() {
 
  SerialUSB.print ("U ");SerialUSB.println(PCTer0);
  SerialUSB.print ("V ");SerialUSB.println(PCTer1);
  SerialUSB.print ("W ");SerialUSB.println(PCTer2);
  SerialUSB.print ("X ");SerialUSB.println(PCTer3);
  SerialUSB.print ("Y ");SerialUSB.println(PCTer4);   
  SerialUSB.print ("Z ");SerialUSB.println(PCTer5);
  SerialUSB.print ("a ");SerialUSB.println(PCTer6);
  SerialUSB.print ("b ");SerialUSB.println(PCTer7);
  SerialUSB.print ("c ");SerialUSB.println(PCTer8);
  SerialUSB.print ("d ");SerialUSB.println(PCTer9);  
  SerialUSB.print ("e ");SerialUSB.println(LoworderTrigger);  
  SerialUSB.print ("f ");SerialUSB.println(orderTrigger); 
  SerialUSB.print ("g ");SerialUSB.println(orderCohesion); 
  
}
 
But with the teensy stepper library, I have to add this at the end of the void loop

moveAsync moves the motor in the background so that you can do other stuff while they move. Your code starts the async move and then waits until the move is finished. If you want to do other stuff while the motor moves you should not wait until it is ready.

In case your original intention is to update the target position while the motor still moves: This is not possible with TeensyStep. In that case I suggest to stick with AccelStepper.
 
Problem with Teensy Stepper. Difficulty to modify the library ?

In case your original intention is to update the target position while the motor still moves: This is not possible with TeensyStep. In that case I suggest to stick with AccelStepper.

Hello.
Accelstepper with an Arduino Uno running at 16MHz run at 4000 step/s
While the Teensy 3.6 runs at 256MHz, so we can expect to do about 4000 * 256MHz / 16MHz = 640000 steps at best on the Teensy 3.6 overlocked at 256MHZ (if nothing else is done).

I want to manage 10 motors and update the target position (with datas from the serial port 30 times/s.)
So, i will be able to run my 10 motors at 640000/10=64000 step.s-1
I need 200*32= 6400 to make one revolution.
So, it means I will be able to control my ten motors at the speed 64000/6400= 10 round/s
Am I right? What is the acceleration I could expect?
Thanks for your lights ;)


I put my program below with accelstepper
Code:
 #include <AccelStepper.h> // Define a stepper and the pins it will use
#define NBMOTEURS 10

#define NBPASPARTOUR 3200 // Set on the driver
 
#define STEP 1 --> My motor at full step needs 200 steps to make one revolution
Mode 1/4 pas MS2 sur ON --> 800 step/round 
Mode 1/8 MS1+ MS2 sur ON --> 1600 step/round
Mode 1/16 MS3 sur ON -->  3200
Mode 1/32 MS1+ MS3 sur ON --> 6400

 //******* From the behind to the front
// 1600 Arduino Due 
// const uint8_t PINDIRECTION[NBMOTEURS] = { 11, 7, 3, 23, 33, 37, 41, 45, 49, 53 };//{ 10, 6, 2, 22, 26};
// const uint8_t PINSPEED[NBMOTEURS]=      { 10, 6, 2, 22, 32, 36, 40, 44, 48, 52 }; //{ 11, 7, 3, 23, 27};

// 3200 Teensy
 const uint8_t PINDIRECTION[NBMOTEURS] = {  2, 4, 6, 8, 10, 45, 49, 53, 12, 99 };//{ 10, 6, 2, 22, 26};
 const uint8_t PINSPEED[NBMOTEURS]=      { 3, 5, 7, 9, 11, 44, 48, 52, 13, 98 }; //{ 11, 7, 3, 23, 27};

// Define a stepper and the pins it will use 
AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED[0], PINDIRECTION[0]), 
AccelStepper (STEP, PINSPEED[1], PINDIRECTION[1]), 
AccelStepper (STEP, PINSPEED[2], PINDIRECTION[2]), 
AccelStepper (STEP, PINSPEED[3], PINDIRECTION[3]), 
AccelStepper (STEP, PINSPEED[4], PINDIRECTION[4]),  
AccelStepper (STEP, PINSPEED[5], PINDIRECTION[5]), 
AccelStepper (STEP, PINSPEED[6], PINDIRECTION[6]), 
AccelStepper (STEP, PINSPEED[7], PINDIRECTION[7]), 
AccelStepper (STEP, PINSPEED[8], PINDIRECTION[8]), 
AccelStepper (STEP, PINSPEED[9], PINDIRECTION[9]),  
};
   
//   Receive with start- and end-markers combined with parsing

const byte numChars = 200;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0}; //or 5 doesn't change anything

int integerFromPC0 = 0;
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
int integerFromPC4 = 0;
int integerFromPC5 = 0;
int integerFromPC6 = 0;
int integerFromPC7 = 0;
int integerFromPC8 = 0;
int integerFromPC9 = 0;

int PC0= 0;
int PC1= 0;
int PC2= 0;
int PC3= 0;
int PC4= 0;
int PC5= 0;
int PC6= 0;
int PC7= 0;
int PC8= 0;
int PC9= 0;

int PCTer0= 0;
int PCTer1= 0;
int PCTer2= 0;
int PCTer3= 0;
int PCTer4= 0;
int PCTer5= 0;
int PCTer6= 0;
int PCTer7= 0;
int PCTer8= 0;
int PCTer9= 0;

int orderTrigger = 0;
int orderCohesion  = 0;
int orderCohesionB = 0;
 
float floatFromPC = 0.0; // not used for the moment

boolean newData = false;

//============
IntervalTimer t1;

#define TX_SIZE 512
uint8_t tx_buffer[TX_SIZE];
  
void setup()
{ 
 //  Serial1.begin(115200); // lot of noise and rumble without changes buffer size in serial1.c 64 before, now 255 
   Serial1.begin(115200); // work perfectly now.
   
   Serial .begin(115200); // work better
 //  Serial1.addMemoryForWrite(tx_buffer,TX_SIZE); Don't work
   NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 16); //32  
   t1.priority(16);  //32
   
       t1.begin(tickSteppers, 1000 );  // call every 1000µs, change if requred // make some noise
        
       //====TEST of t1.begin(tickSteppers, 1000 ); Results below are without  the 2 lines below 
       
       //NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32);   
       //t1.priority(32);      
           
     // 100: no difference,, motors shake at speed 40
     // 1: block Processing and I don't the test in the setup
     // 10: the test in the setup shake the motor 
     // 1000: motors shake at speed 70
     // 2000:   motors shake at speed 70
    
  //====Test if datas come from the both serial of the Arduino Due or Teensy 3.6
    
      Serial1.print ("A "); Serial1.println (-4);     
      Serial1.print ("B "); Serial1.println (-3);        
      Serial1.print ("C "); Serial1.println (-2);  
      Serial1.print ("D "); Serial1.println (-1);
      Serial1.print ("E "); Serial1.println (-10);

      Serial.print ("A "); Serial.println (4);     
      Serial.print ("B "); Serial.println (3);        
      Serial.print ("C "); Serial.println (2);  
      Serial.print ("D "); Serial.println (1);
      Serial.print ("E "); Serial.println (10);

 //====Initialise Pin Motor
    
 for(uint8_t i = 0; i < NBMOTEURS; i++) { 
   
    pinMode(PINDIRECTION[i], OUTPUT);
    digitalWrite(PINDIRECTION[i], OUTPUT);
    pinMode(PINSPEED[i], OUTPUT);
    digitalWrite(PINSPEED[i], OUTPUT);
  
  /// with 1/16 step == 16*200= 3200   step / round
     stepper[i].setMaxSpeed(12800); // WORK at 4 round/s
  // stepper[i].setMaxSpeed(9000 ); //  
     stepper[i].setAcceleration(25600);  // 4 tour/s-2  // WORK
 // stepper[i].setAcceleration(9000);  
   }
   
PC0=25600;    //8 tours
PC1=PC2=PC2=PC4=PC5=PC6=PC7=PC8=PC9= PC0;
 
stepper[9].moveTo(PC0);
stepper[8].moveTo(PC1);      
stepper[7].moveTo(PC2);
stepper[6].moveTo(PC3);
stepper[5].moveTo(PC4);  
  
stepper[4].moveTo(PC5);
stepper[3].moveTo(PC6);     
stepper[2].moveTo(PC7);
stepper[1].moveTo(PC8);
stepper[0].moveTo(PC9);
   
}

void loop() { 
 
    recvWithStartEndMarkers(); //receive 70 datas*30 in on secondes
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData(); // 33 datas marked and separated with a coma.  
           stepper[9].moveTo(PC0);  stepper[8].moveTo(PC1); stepper[7].moveTo(PC2); stepper[6].moveTo(PC3); stepper[5].moveTo(PC4);
           stepper[4].moveTo(PC5);  stepper[3].moveTo(PC6); stepper[2].moveTo(PC7); stepper[1].moveTo(PC8); stepper[0].moveTo(PC9); 
        showPosition();
        showPhazisScaled(); 
        showTrigBangWithRevolution();
       
        newData = false;
    }   

        tickSteppers();
    //   stepper[9].run();  stepper[8].run(); stepper[7].run();  stepper[6].run();  stepper[5].run(); 
    //   stepper[4].run();  stepper[3].run(); stepper[2].run();  stepper[1].run();  stepper[0].run();  
 
}
 

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

//     if ( Serial.available() > 67 ||  recvInProgress == true ) // 20 or 2 or 60 no difference
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the 31 data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
 
    integerFromPC0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC9 = atoi(strtokIndx);     // convert this part to an integer

    
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PC9 = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer0 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer1 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer2 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer3 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer4 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer5 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer6 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer7 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer8 = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    PCTer9 = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    orderTrigger = atoi(strtokIndx); // convert this part to an integer 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    orderCohesion  = atoi(strtokIndx); // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    orderCohesionB  = atoi(strtokIndx); // convert this part to an integer
 
}

//============
void  showPosition() { 
  Serial1 .print ("K ");Serial1 .println(PC0); 
  Serial1.print ("K ");Serial1.println(PC0);
  Serial1.print ("L ");Serial1.println(PC1);
  Serial1.print ("M ");Serial1.println(PC2);
  Serial1.print ("N ");Serial1.println(PC3);
  Serial1.print ("O ");Serial1.println(PC4); 
  Serial1.print ("P ");Serial1.println(PC5);
  Serial1.print ("Q ");Serial1.println(PC6);
  Serial1.print ("R ");Serial1.println(PC7);
  Serial1.print ("S ");Serial1.println(PC8);
  Serial1.print ("T ");Serial1.println(PC9);   
}

void showPhazisScaled()  {  
  
    Serial1.print ("A "); Serial1.println (integerFromPC0);    
    Serial1.print ("B "); Serial1.println (integerFromPC1);        
    Serial1.print ("C "); Serial1.println (integerFromPC2);    
    Serial1.print ("D "); Serial1.println (integerFromPC3);         
    Serial1.print ("E "); Serial1.println (integerFromPC4); 
    Serial1.print ("F "); Serial1.println (integerFromPC5);    
    Serial1.print ("G "); Serial1.println (integerFromPC6);        
    Serial1.print ("H "); Serial1.println (integerFromPC7);    
    Serial1.print ("I "); Serial1.println (integerFromPC8);         
    Serial1.print ("J "); Serial1.println (integerFromPC9); 
  
}

void showTrigBangWithRevolution() {
  // check to see if we have room enough in output queue to hold this...
//  If (Serial1.availableForWrite() < MIN_SIZE_TO_ENABLE_WRITE) return;
  Serial1.print ("U ");Serial1.println(PCTer0);
  Serial1.print ("V ");Serial1.println(PCTer1);
  Serial1.print ("W ");Serial1.println(PCTer2);
  Serial1.print ("X ");Serial1.println(PCTer3);
  Serial1.print ("Y ");Serial1.println(PCTer4);   
  Serial1.print ("Z ");Serial1.println(PCTer5);
  Serial1.print ("a ");Serial1.println(PCTer6);
  Serial1.print ("b ");Serial1.println(PCTer7);
  Serial1.print ("c ");Serial1.println(PCTer8);
  Serial1.print ("d ");Serial1.println(PCTer9);  
  Serial1.print ("e ");Serial1.println(orderTrigger); 
  Serial1.print ("f ");Serial1.println(orderCohesion);  
  Serial1.print ("g ");Serial1.println(orderCohesionB);    
}

void tickSteppers()
{
    stepper[0].run();
    stepper[1].run();
    stepper[2].run();
    stepper[3].run();
    stepper[4].run();
    
    stepper[5].run();
    stepper[6].run();
    stepper[7].run();
    stepper[8].run();
    stepper[9].run();
    
}
 
I'm not very familiar with AccelStepper. As far as I remember it actively limits the max speed, but this might have changed.

If you want to run your steppers with 64000 stp/s you need to make sure to call all the tick functions at least once per 1'000'000µs/64000 = 15.6µs. This of course only works if your tickSteppers() doesn't take longer than this. To test, I'd set a pin to HIGH before stepper[0].run and back to low after stepper[9].run. You can then measure the execution time with a scope or a logic analyzer. The execution time will ultimately limit how fast you can move your 10 motors. And don't forget that you will need time for the foreground work as well.

BTW: you still call tickSteppers() in loop. This doesn't make sense since it is already called from the interval timer. Your interval timer period of 1000µs will limit your maximal step rate to 1/1000µs = 1kHz. So, you need to decrease that if you want to go faster.
 
Hi luni,
I don't have a scope or a logic analyze to measure time before before stepper[0].run stepper[9].
So, I did this test in the loop
Code:
void loop() { 
 
    recvWithStartEndMarkers(); //receive 70 datas*30 in on secondes
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData(); // 33 datas marked and separated with a coma.  
           stepper[9].moveTo(PC0);  stepper[8].moveTo(PC1); stepper[7].moveTo(PC2); stepper[6].moveTo(PC3);     stepper[5].moveTo(PC4);
           stepper[4].moveTo(PC5);  stepper[3].moveTo(PC6); stepper[2].moveTo(PC7); stepper[1].moveTo(PC8); stepper[0].moveTo(PC9); 
        showPosition();
        showPhazisScaled(); 
        showTrigBangWithRevolution(); 
        newData = false;
    }    
            Serial1.print("BeforetickSteppers>"); 
           Serial1.println(micros()); 
         tickSteppers(); 
         Serial1.print("AftertickSteppers>"); 
          Serial1.println(micros());   
}
And I read this
Code:
13:54:49.644 -> g -100
13:54:49.644 -> BeforetickSteppers>1373340041
13:54:49.644 -> AftertickSteppers>1373342645
13:54:49.644 -> BeforetickSteppers>1373345249
13:54:49.644 -> AftertickSteppers>1373347853
13:54:49.678 -> BeforetickSteppers>1373350977
13:54:49.678 -> AftertickSteppers>1373353581
13:54:49.678 -> BeforetickSteppers>1373356185
13:54:49.678 -> AftertickSteppers>1373358789
13:54:49.678 -> K 0
13:54:49.678 -> K 0
The difference between each AftertickSteppers and BeforetickSteppers is 2604.
Can this result be helpful to calculate the maximum speed I could have with my ten motors???.

Morever you told me that
BTW: you still call tickSteppers() in loop. This doesn't make sense since it is already called from the interval timer. Your interval timer period of 1000µs will limit your maximal step rate to 1/1000µs = 1kHz. So, you need to decrease that if you want to go faster.

When you mean decrease, you mean rise up or or reduce the interval timer period of 1000µs?
If I can't measure the optimal time, I could modulate this value, and check the best behavior of my ten motors. That's what you mean?

Thank you
 
Your code includes the time for print which is probably much longer thant he ticking takes...
Do this instead:
Code:
uint32_t start = micros();
tickSteppers();
uint32_t end = micros();
Serial1.print(end - start);

When you mean decrease, you mean rise up or or reduce the interval timer period of 1000µs?

Reduce the period. Since the run function generates the step pulses the time between two calls to the run functions must be less than the time between two steps. Your code calls run at 1ms intervals. Thus, you can't go higher than 1kHz.

If I can't measure the optimal time, I could modulate this value, and check the best behavior of my ten motors. That's what you mean?
No, there is no optimal value, just make sure that you call it faster than you want to step.

I don't have a scope or a logic analyze to measure time before before stepper[0].run stepper[9].
Do yourself a favor and get one of those or a similar one. https://www.amazon.com/-/de/dp/B07K...1604510469&sprefix=logic+anal,aps,306&sr=8-11
 
Hi Luni,
Thanks for you time, for explain me the best way to rum my motor with the highest speed
I made the test and when I send positions to all motors going forward or backward I have this:
Code:
3
4
3
3
16
3
3
4
4
3
3
3
3
6
3
19
4
3
3
4
40
3
3
4
34
3
3
3
3
3
40
3
3
34
4
3
4
3
3
40
4
3
3
34
3
3
3
3
40
3
I have 50 datas.

When I send same positions without moving, I have this:
Code:
1
1
1
2
2
1
1
1
1
1
1
2
2
2
3
1
1
1
1
1
1
2
2
3
2
1
1
1
1
1
2
2
2
1
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
2
I have 73 datas.

I don't know How to interpret these results?

But my motors follow pretty well my animation from Processing, because I think, I have overclocked the teensy 3.6 and I rise the buffer size of the second USB port I use to send data to an other software. So If you want to stop the discussion now, we can. Thanks you
 
Results are easy to interpret: it takes 1-2µs if Accelstep doesn't need to do any steps. it takes about 40µs if it needs to do some steps. So, you won't be able to do more then 25`000 steps/sec with your setup. But if everything runs now as you like I suggest to call it a day and continue with your project...
 
Status
Not open for further replies.
Back
Top