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

Thread: Problem with Serial1 port on Teensy 3.6. I haven't had it with Arduino Due.

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    15

    Problem with Serial1 port on Teensy 3.6. I haven't had it with Arduino Due.

    Hi,
    I bought the teensy 3.6 to have better processor speed and now I can control my stepper motor 10 faster.
    They are controlled with Processing thanks to Serial.port.
    I would like to read on another serial port the Processing data (33 data 30 times per second) but when I do this there is a rumble in my motors and they no longer hold their position.

    Stepper motor tremors are less when I set the Serial1.port to 1,000,000 baud.

    But with this setting I cannot read the data in the serial monitor. You can read data at 500,000 bauds maximum, but with this setting the motors shake.

    With the Arduino Due, I was receiving the data at 115000 baud on the Programming Port and writing the data on the SerialUSB port at 115000 baud and I had no motor shake.

    So now, I set the two ports like this:
    Serial4.begin (1000000); Serial.begin(1000000); //
    Steppers motors don't shake too much but I can't read datas

    I put you the program below
    Thanks for helping.


    Code:
    #include <AccelStepper.h>
      #define SerialUSB Serial4
    // Define a stepper and the pins it will use
    #define NBMOTEURS 10
    
    #define NBPASPARTOUR 3200 // Set on the driver
     
    #define STEP 1// 
    //  800-->  Mode 1/4 pas MS2 sur ON
    //  1600--> Mode 1/8 MS1+ MS2 sur ON
    //  3200--> Mode 1/16 MS3 sur ON
     
    // 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;
    
    //============
     
    void setup()
    {   
       SerialUSB.begin (1000000);  Serial.begin(1000000); //decroche pas,  , mais peux pas lire les info.
      
       //  SerialUSB.begin (115200);  Serial.begin(115200); //ne decroche pas, affiche bien AVEC ARDUINO DUE
      
      //====Test if datas come from the both serial of the Arduino Due or Teensy
        
          Serial4.print ("A "); Serial4.println (-4);     
          Serial4.print ("B "); Serial4.println (-3);        
          Serial4.print ("C "); Serial4.println (-2);  
          Serial4.print ("D "); Serial4.println (-1);
          Serial4.print ("E "); Serial4.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(6400); // sur 1600 ==>step/round 2 tour/s
         stepper[i].setAcceleration(2000);  // 1 tour/s-2  //  
       }
       
    PC0=6400;    //2 round
    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() { 
     // recvDataAutre(); // deconne
        recvWithStartEndMarkers();
        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(); // 31 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;
        }       
           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;
    
        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() { 
      Serial4 .print ("K ");Serial4 .println(PC0); 
      Serial4.print ("K ");Serial4.println(PC0);
      Serial4.print ("L ");Serial4.println(PC1);
      Serial4.print ("M ");Serial4.println(PC2);
      Serial4.print ("N ");Serial4.println(PC3);
      Serial4.print ("O ");Serial4.println(PC4); 
      Serial4.print ("P ");Serial4.println(PC5);
      Serial4.print ("Q ");Serial4.println(PC6);
      Serial4.print ("R ");Serial4.println(PC7);
      Serial4.print ("S ");Serial4.println(PC8);
      Serial4.print ("T ");Serial4.println(PC9);   
    }
    
    void showPhazisScaled()  {  
      
        Serial4.print ("A "); Serial4.println (integerFromPC0);    
        Serial4.print ("B "); Serial4.println (integerFromPC1);        
        Serial4.print ("C "); Serial4.println (integerFromPC2);    
        Serial4.print ("D "); Serial4.println (integerFromPC3);         
        Serial4.print ("E "); Serial4.println (integerFromPC4); 
        Serial4.print ("F "); Serial4.println (integerFromPC5);    
        Serial4.print ("G "); Serial4.println (integerFromPC6);        
        Serial4.print ("H "); Serial4.println (integerFromPC7);    
        Serial4.print ("I "); Serial4.println (integerFromPC8);         
        Serial4.print ("J "); Serial4.println (integerFromPC9); 
      
    }
    
    void showTrigBangWithRevolution() {
     
      Serial4.print ("U ");Serial4.println(PCTer0);
      Serial4.print ("V ");Serial4.println(PCTer1);
      Serial4.print ("W ");Serial4.println(PCTer2);
      Serial4.print ("X ");Serial4.println(PCTer3);
      Serial4.print ("Y ");Serial4.println(PCTer4);   
      Serial4.print ("Z ");Serial4.println(PCTer5);
      Serial4.print ("a ");Serial4.println(PCTer6);
      Serial4.print ("b ");Serial4.println(PCTer7);
      Serial4.print ("c ");Serial4.println(PCTer8);
      Serial4.print ("d ");Serial4.println(PCTer9);  
      Serial4.print ("e ");Serial4.println(orderTrigger); 
      Serial4.print ("f ");Serial4.println(orderCohesion);  
      Serial4.print ("g ");Serial4.println(orderCohesionB);   
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,515
    If Serial1 or Serial2 were used it might help as they have an 8 byte FIFO, it seems IIRC Serial4 has just 1 byte causing more frequent interrupts.

    Serial1 is mentioned above but not apparent in the code?

    It uses : #define SerialUSB Serial4 and Serial4 interchangeably which is a UART port.

    The USB on Teensy will run at the max speed of the host connection typically T_3.6's max of 12 Mbps and the value passed in this statement does not change that: Serial.begin(1000000);

    Just wondering if adding this if before the while makes any difference to allow the Serial UART buffer to fill more before pulling the bytes off one at a time?
    Code:
      if ( Serial.available() > 20 ||  recvInProgress == true ) // 20 or larger up to message size.
        while (Serial.available() > 0 && newData == false) {
    ...
    It will make it better if the delay is from taking the data a byte at a time - and perhaps worse if the byte processing time takes too long and that affects that calls to the .run()'s

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,379
    You might also think of calling the run() functions from an IntervalTimer. Thus, they are not blocked from long running foreground tasks.

    Code:
    //....
    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();
    }
    
    IntervalTimer t1;
    
    void setup(){
       //....
        t1.begin(tickSteppers, 100);  // call every 100µs, change if requred
      //...
    }
    (You can also think of using TeensyStep https://luni64.github.io/TeensyStep/ which is made for the T3.x boards.)

  4. #4
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    [
    Code:
      if ( Serial.available() > 20 ||  recvInProgress == true ) // 20 or larger up to message size.
        while (Serial.available() > 0 && newData == false) {
    ...
    Hello,

    I have changed 20 with 2, 20 and 67 but there is no change between those 3 value. My motors shake at speed 60.
    As soon as I take off SerialUSB in the setup, I have no problem.
    I send 33 datas separated with coma and with end marker 30 times in a sec, like that

    Code:
    <1,1,1,1,1,1,1,1,1,1,93230,93230,93230,93230,93230,93230,93230,93230,93230,93230,34,34,34,34,34,34,34,34,34,34,0,976,-100>
    So I have (33*2)-1 datas +2 datas= 67 datas *33 = 2211 datas each secondes, if it can help

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    IntervalTimer t1;

    void setup(){
    //....
    t1.begin(tickSteppers, 100); // call every 100µs, change if requred
    //...
    }

    I have try different t1.
    It's better my motor shake at speed 70, and it was 60 with the solution of defragster.
    Could I improve my program again?
    Code:
    //#include <Wire.h>
    
     boolean dataReady = false; // pour autre maniere de recevoir l'info de Processing
      
    #include <AccelStepper.h>
    
      #define SerialUSB Serial1
    // Define a stepper and the pins it will use
    #define NBMOTEURS 10
    
    #define NBPASPARTOUR 3200 // Set on the driver
     
    #define STEP 1//// 1600-->Mode 1/4 pas MS2 sur ON
    //  1600-->Mode 1/8 MS1+ MS2 sur ON
    // 3200--> Mode 1/16 MS3 sur ON
     
    // 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;
     
    void setup()
    {    
         t1.begin(tickSteppers, 2000);  // call every 100µs, change if requred
         // 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
        SerialUSB.begin (500000);// motor begin to bug at speed 60 
        Serial.begin (115200);
       
      
      //====Test if datas come from the both serial of the Arduino Due
        
          SerialUSB.print ("A "); SerialUSB.println (-4);     
          SerialUSB.print ("B "); SerialUSB.println (-3);        
          SerialUSB.print ("C "); SerialUSB.println (-2);  
          SerialUSB.print ("D "); SerialUSB.println (-1);
          SerialUSB.print ("E "); SerialUSB.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/8 step == 1600 step / round // deconne à 60
     //   stepper[i].setMaxSpeed(3200); // sur 1600 ==>step/round 2 tour/s
     //   stepper[i].setAcceleration(1000);  // 1 tour/s-2 
    
      /// with 1/16 step == 16*200= 3200   step / round
         stepper[i].setMaxSpeed(6400); // sur 1600 ==>step/round 2 tour/s
         stepper[i].setAcceleration(3200);  // 1 tour/s-2  // deconne à 60
       }
       
    PC0=6400;    //2 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() { 
     // recvDataAutre(); // deconne
        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() { 
      SerialUSB .print ("K ");SerialUSB .println(PC0); 
      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(orderTrigger); 
      SerialUSB.print ("f ");SerialUSB.println(orderCohesion);  
      SerialUSB.print ("g ");SerialUSB.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 don't manage to well use the teensyStep library for the moment I will take an other request.

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    Sorry I have not studied your code in detail and have not played around with stepper motors.
    But for example if it is because Serial1 is not being procecessed fast enough than maybe raise the priority of the IRQ that services it. I think all of the UARTS are set to 64 (lower number is higher priority).
    Code:
    	NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32);
    Or maybe it is that the interval timer needs to have a higher priority here.
    I think you can set it's priority like: t1.priority(32);
    I believe it defaults to default priority of most things 128...

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,546
    The default transmit buffer size for Serial4 is 40 characters. When your code receives a message, it blasts out over 200 characters to Serial4. The code will hang until most of those characters have been transmitted.
    Try adding an output buffer for Tx.
    Before the setup function add this:
    Code:
    #define TX_SIZE 512
    uint8_t tx_buffer[TX_SIZE];
    and after SerialUSB.begin add this:
    Code:
    SerialUSB.addMemoryForWrite(tx_buffer,TX_SIZE);
    I too, like @KurtE, have not played around with stepper motors so this is just a guess as to what is wrong.
    However, I think it must be something "under the hood" like this because the T3.6 processor is more than twice as fast as a Due and shouldn't have any problem handling this code.

    Pete

  8. #8
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    Quote Originally Posted by el_supremo View Post
    The default transmit buffer size for Serial4 is 40 characters. When your code receives a message, it blasts out over 200 characters to Serial4. The code will hang until most of those characters have been transmitted.
    Try adding an output buffer for Tx.
    Before the setup function add this:
    Code:
    #define TX_SIZE 512
    uint8_t tx_buffer[TX_SIZE];
    and after SerialUSB.begin add this:
    Code:
    SerialUSB.addMemoryForWrite(tx_buffer,TX_SIZE);
    I too, like @KurtE, have not played around with stepper motors so this is just a guess as to what is wrong.
    However, I think it must be something "under the hood" like this because the T3.6 processor is more than twice as fast as a Due and shouldn't have any problem handling this code.

    Pete
    Side Note: I don't think the addMemoryForWrite has been implemented on T3.x. I added it in T4 code base, and at one point had similar code for the T3.x where all of the Serial objects were implemented in one C++ class, but at the time it was considered too risky to move T3.x code base to this one class....

    I think the only way to change it on T3.x is to edit the Core code base, in this case I think serial1.c and edit the define for: SERIAL1_TX_BUFFER_SIZE.
    Or with some build systems one can somehow pass in a different value for this define and the code checks first to see if it is not previously defined and then define it.

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,546
    Oooops. Thanks Kurt. I had been playing with a T4 and forgot to change the target board.

    Pete

  10. #10
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    [QUOTE=KurtE;257946]
    But for example if it is because Serial1 is not being procecessed fast enough than maybe raise the priority of the IRQ that services it. I think all of the UARTS are set to 64 (lower number is higher priority).

    Code:
    	NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32);
    I would like to rise up the priority of the IRQ servicing the UARTS but when I write your code above before the setup, Teensyduino write:

    Code:
    In file included from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/core_pins.h:34:0,
                     from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/wiring.h:39,
                     from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/WProgram.h:45,
                     from /var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/arduino_build_28602/pch/Arduino.h:6:
    /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/kinetis.h:5666:50: error: expected unqualified-id before 'volatile'
     #define NVIC_SET_PRIORITY(irqnum, priority)  (*((volatile uint8_t *)0xE000E400 + (irqnum)) = (uint8_t)(priority))
                                                      ^
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino:92:3: note: in expansion of macro 'NVIC_SET_PRIORITY'
       NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32); 
       ^
    /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/kinetis.h:5666:50: error: expected ')' before 'volatile'
     #define NVIC_SET_PRIORITY(irqnum, priority)  (*((volatile uint8_t *)0xE000E400 + (irqnum)) = (uint8_t)(priority))
                                                      ^
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino:92:3: note: in expansion of macro 'NVIC_SET_PRIORITY'
       NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32); 
       ^
    /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/kinetis.h:5666:50: error: expected ')' before 'volatile'
     #define NVIC_SET_PRIORITY(irqnum, priority)  (*((volatile uint8_t *)0xE000E400 + (irqnum)) = (uint8_t)(priority))
                                                      ^
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino:92:3: note: in expansion of macro 'NVIC_SET_PRIORITY'
       NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32); 
       ^
    /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/kinetis.h:5666:50: error: expected ')' before 'volatile'
     #define NVIC_SET_PRIORITY(irqnum, priority)  (*((volatile uint8_t *)0xE000E400 + (irqnum)) = (uint8_t)(priority))
                                                      ^
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino:92:3: note: in expansion of macro 'NVIC_SET_PRIORITY'
       NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32); 
       ^
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino: In function 'void setup()':
    /Users/dubruitvientlordre/Documents/Arduino/Positions1600.TWOserialTerBis/Positions1600.TWOserialTerBis.ino:145:41: warning: operation on 'PC2' may be undefined [-Wsequence-point]
     PC1=PC2=PC2=PC4=PC5=PC6=PC7=PC8=PC9= PC0;
                                             ^
    Plusieurs bibliothèque trouvées pour "AccelStepper.h"
    Utilisé : /Users/dubruitvientlordre/Documents/Arduino/libraries/AccelStepper
    Non utilisé : /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/7FFE66FA-2CD5-40E9-A420-5B14DD0451F9/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/libraries/AccelStepper
    Erreur de compilation pour la carte Teensy 3.6

  11. #11
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    This compiles just fine for me:
    Code:
    void setup() {
      Serial1.begin(115200);
      NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32);
    
    }
    
    void loop() {
      
    }
    I am running the Teensyduino beta released yesterday on 1.8.14... Configured for T3.6

    Edit: Note the value: IRQ_UART0_STATUS is specific to Serial1... There are different ones for the different Serial ports.
    Also different for T4.x

  12. #12
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Hi Kurte,
    I have added few thing in the setup and the motor (because I have only one for now) seems to follow the animation from Processing.
    But why when I set Serial1.begin (115200) the motor shake more than with Serial1.begin (500000)???

    I put my hole program
    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//// 1600-->Mode 1/4 pas MS2 sur ON
    //  1600-->Mode 1/8 MS1+ MS2 sur ON
    // 3200--> Mode 1/16 MS3 sur ON
    
     //******* 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;
      
    void setup()
    { 
       //  Serial1.begin(115200); // lot of noise and rumble
       Serial1.begin(500000); // work better
       NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 32);   
       t1.priority(32);  
       
           t1.begin(tickSteppers, 1000);  // call every 1000µs, change if requred // make some noise
            
           //====Theses results without  
           //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
       //    Serial1.begin (115200);// motor begin to bug at speed 60 
       //    Serial1.begin (500000);// motor begin to bug at speed 60 
         
            Serial.begin (500000);
       
      //====Test if datas come from the both serial of the Arduino Due
        
          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); // 4 round/s
      // stepper[i].setMaxSpeed(9000 ); //  
         stepper[i].setAcceleration(6400);  // 2 tour/s-2  // deconne à 60
     // stepper[i].setAcceleration(9000);  
       }
    
    // TEST ONE ROUND   
    PC0=3200;    //1 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() {
     
      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();
    }

  13. #13
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    Probably because it takes longer for that string to output... So back to the fastest speed your Serial can handle.

    Again as Pete mentioned, you might try expanding the size of the Serial1 buffer.

    Go into .../cores/teensy3/serial1.c and change the line:
    Code:
    #define SERIAL1_TX_BUFFER_SIZE     64 // number of outgoing bytes to buffer
    to something like:
    Code:
    #define SERIAL1_TX_BUFFER_SIZE     255// number of outgoing bytes to buffer
    And see if that helps. Could go larger as well, like 1000 or the like, assuming you have enough memory left after build.

    Also depending on just how much stuff you are outputting, you can also do things like:
    if you don't need every single ones of these outputs... You could do things like:
    Code:
    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);    
    }
    But again if it turns out you are trying to output more data to Serial1, than can be output, than you need to figure out how to reduce just how much you are outputting...

  14. #14
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Quote Originally Posted by KurtE View Post

    Go into .../cores/teensy3/serial1.c and change the line:
    Code:
    #define SERIAL1_TX_BUFFER_SIZE     64 // number of outgoing bytes to buffer
    to something like:
    Code:
    #define SERIAL1_TX_BUFFER_SIZE     255// number of outgoing bytes to buffer

    Hi Kurte,
    I' m working on Mac OS. I found a file name Serial.c but it's in a library called Grbl. And this library is not managed in the partition of my Mac, that I'm actually using with Teensy.
    How can download the good library to manage Serial.c? In this manner, I will be able to manage serial.c in the good folder from the partition I'm working with.

    In the photo you see where is my file serial.c


    Click image for larger version. 

Name:	Capture d’écran 2020-11-03 à 08.59.08.jpg 
Views:	8 
Size:	66.3 KB 
ID:	22295
    Last edited by Bking; 11-03-2020 at 08:52 AM.

  15. #15
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    I am not a complete MAC person. I should pull mine out, but...

    There is a command named something like if you bring up the menu on the Teensy app, there is some command like show file contents, and then you navigate through the contents.
    I think the Hardware folder I mention is under some other directory name like Java... but under there will then be the teensy\avr\cores\teensy3...

  16. #16
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    I can't find this file in my computer.
    Could send me your folder where you file serial.c? I will put it in the folder where library for teensy is.

    Thank you.

  17. #17
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    Found it on my older Mac notebook...

    Actually downloaded current beta build.

    I then moved it into my applications folder (replacing the old one).

    I then in finder opened the applications folder. and see the Teensyduino app. I then two finger click on trackpad to bring up menu, which second menu item is: Show Package contents.

    Open Contents -> Java -> hardware -> teensy -> avr -> cores -> teensy3

    And you should find the file.

  18. #18
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Hi Kurte.

    My program seems to work perfectly. No noise, the motor keeps it position, and I can read data at 115200 bauds.
    Thanks so much for your didactism.
    Now I have to use Teensy Step to improve again my movement.

    Just a little something when I added this in the setup (what Pete suggested):
    Code:
    Serial1.addMemoryForWrite(tx_buffer,TX_SIZE);
    I have this error message:
    Positions3200PJRC: In function 'void setup()':
    Positions3200PJRC:97: error: 'class HardwareSerial' has no member named 'addMemoryForWrite'
    Serial1.addMemoryForWrite(tx_buffer,TX_SIZE);
    ^

    Is it important to add? Will I win some speed and fluidity in my movement? I make an artistic installation where the movement is poetic value, so I need the highest precision.
    Thanks again.

  19. #19
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,528
    As mentioned recently in another thread. The addMemory stuff is only implemented on T4.x code base. The T3.x/LC code was not changed to add this as we did not convert the individual code bases for each Serial port into a common class, like I did for the T4 (Had one but never integrated.) The same calls could be added to it, but then would require changes to 9 implementations of the code...

    So in the main way you can increase the size is to edit the file I mentioned.

  20. #20
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Understood!

Posting Permissions

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