Aller au contenu


Photo
- - - - -

Communication Teensy & Processing. Différence avec l'Arduino Due

Serial.available() Teensy 3.6

  • Veuillez vous connecter pour répondre
16 réponses à ce sujet

#1 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 26 octobre 2020 - 09:36

Bonsoir, 

J'essaie d'adapter mon programme fait pour l'Arduino Due pour le faire fonctionner sur la teensy 3.6.

Mais quand j'utilise la Teensy, Processing saccade alors qu'il fonctionnait parfaitement avec l'Arduino Due.

Avec la Due, le port dans Processing apparait comme ça

[1] "/dev/cu.usbmodem14201"

Avec la Teensy, le port dans Processing apparait comme ça

[1] "/dev/cu.usbmodem72864401"

 

Dans le setup du logiciel Teensyduino, j'ai réglé la vitesse du port série sur 250000 car j'envoie à 250000 bauds aussi depuis Processing.

 

Mon animation saccade mais toutefois, la LED "buit-in" de la Teensy fonctionne comme prévue quand je démarre Processing.

 

J'ai donc mal programmé un truc quelque part. Je mets mon programme en bas. 

Aussi, je dois branché un module sur une liaison RX/TX de la Teensy et écrire sur cet autre port d'autres informations , comment  pourrais-je trouver un exemple simple pour m'en servir? Merci

 

Merci pleinement.

A bien+

 

#include "TeensyStep.h"

 
 
Stepper motor_1(1, 2);   //DIR pin = 1 , STEP pin =  2, 
Stepper motor_2(3, 4);    
Stepper motor_3(5, 6);   
Stepper motor_4(7, 8);    
Stepper motor_5(9, 10);   
Stepper motor_6(11, 12);   
 
StepControl controller;

int positionX, positionX1, positionX2, positionX3,positionX4,positionX5, positionX6, positionX7, positionX8, positionX9; 
 
//   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

void setup()
{
    // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin (250000); // receive datas from Processing with the "Programming Port"
    
  //  SerialUSB.begin (250000); // print datas to send it into the other serial port of the Arduino Due, the "Native Usb Port" 

  //====Test if datas are printed into 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
  
  // setup the motors 
  
   motor_1
    .setMaxSpeed(3200)       // steps/s
    .setAcceleration(1600); // steps/s^2 
  
  motor_2
    .setMaxSpeed (3200)       // steps/s
    .setAcceleration(2000); // steps/s^2 
  motor_3
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(2400)   // steps/s^2     
    .setStepPinPolarity(LOW); // driver expects active low pulses
   motor_4
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(2800);   // steps/s^2   
   motor_5
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(3200);   // 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
          digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100 );               // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second            
        parseData(); //  split the 33 data into its parts  
        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;
    }

  /*  
  constexpr int spr = 16*200;  // 3200 steps per revolution
  
  // lets shake    
  for(int i = 0; i < 5; i++)
  {
     motor_1.setTargetRel(spr/4); // 1/4 revolution
      motor_4.setTargetRel(spr/4); // 1/4 revolution
    controller.move(motor_1, motor_4 );  

    motor_1.setTargetRel(-spr/4);
     motor_5.setTargetRel(-spr/4);
    controller.move(motor_1, motor_5);  
  }
  delay(500);
  
  // move motor_1 to absolute position (10 revolutions from zero)
  // move motor_2 half a revolution forward  
  // both motors will arrive their target positions at the same time
   motor_2.setTargetAbs(10*spr);
   motor_2.setTargetRel(spr/2);
  controller.move(motor_1, motor_2);

  // now move motor_2 together with motor_3
   motor_1.setTargetRel(300);
   motor_2.setTargetRel(-800);
  controller.move(motor_2, motor_3);

  // move all motors back to their start positions
   motor_1.setTargetAbs(0);
 motor_2.setTargetAbs(0);
   motor_3.setTargetAbs(0);
  controller.move(motor_1, motor_2, motor_3);
 
  delay(1000);
  */
}

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
 
}

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

A



#2 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 278 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 26 octobre 2020 - 11:38

déjà tu peux faire : 

 

#define SerialUSB Serial1

Et garder tout tes SerialUSB comme avant.

Ensuite pour te simplifier la vie et pouvoir mettre ton code dans des for tu peux faire un truc du genre : 

 

#define NBSTEPPERS 5
const uint8_t DIRS[NBSTEPPERS] = {4, 5, 6, 7 , 8};                  //Tableau des pins direction des drivers de moteurs pas à pas
const uint8_t STEPS[NBSTEPPERS] = {9, 10, 11, 12, 13};              //Tableau des pins step des drivers de moteurs pas à pas

Stepper stepperMotors[NBSTEPPERS] = {Stepper (STEPS[0], DIRS[0]),  
                                     Stepper (STEPS[1], DIRS[1]),
                                     Stepper (STEPS[2], DIRS[2]),
                                     Stepper (STEPS[3], DIRS[3]),
                                     Stepper (STEPS[4], DIRS[4])};

En faisant ça tu pourras appeler tes moteurs comme ça : 

stepperMotors[i] avec i allant de 0 à NBSTEPPERS - 1

Ensuite, évite de mettre des delay ...  
Si tu veux une led qui clignote tout les x temps, utilise millis pour savoir quand tu dois changer l'état de ta led sans faire de code bloquant. ( Ou bien regarde les librairie du genre " metro " ou " eslapsedMillis " si tu vois pas comment le faire simplement avec millis() ) 

Enfin si " processing sacade " je suppose que c'est par ce que la teensy est trop rapide pour le pc et que tu envois trop d'info trop rapidement à processing.


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 

 

Les réalisations de Mike118  

 

 

 


#3 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 27 octobre 2020 - 12:45

Merci pour ta réponse. J'ai fait comme t'as dit en début de programme  #define SerialUSB Serial1 et j'ai ça quand le programme se téléverse

Users/dubruitvientlordre/Documents/Arduino/MultipleSteppersFIVE/MultipleSteppersFIVE.ino:20:0: warning: "SerialUSB" redefined
 #define SerialUSB Serial1
 ^
In file included from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/0791183E-FA15-4DE3-B5CB-FA393D292117/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/core_pins.h:35:0,
                 from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/0791183E-FA15-4DE3-B5CB-FA393D292117/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/wiring.h:39,
                 from /private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/0791183E-FA15-4DE3-B5CB-FA393D292117/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/WProgram.h:45,
                 from /var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/arduino_build_236190/pch/Arduino.h:6:
/private/var/folders/h7/_1s05_410pb0qd2lqfgjfqkr0000gn/T/AppTranslocation/0791183E-FA15-4DE3-B5CB-FA393D292117/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/pins_arduino.h:284:0: note: this is the location of the previous definition
 #define SerialUSB   Serial
 ^

Je sais pas  comment interpréter ça.

 

C'est Processing qui envoie les 33 données 30 fois / seconde. Il saccade  quand je mets ça dans la loop

 

  // move 3 motors
 
  motor_1.setTargetRel( PC9);
  motor_2.setTargetRel( PC8);
  motor_3.setTargetRel( PC7);
  controller.move(motor_1, motor_2, motor_3);
 
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  
        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;
    }


  // move 3 motors
 
  motor_1.setTargetRel( PC9);
  motor_2.setTargetRel( PC8);
  motor_3.setTargetRel( PC7);
  controller.move(motor_1, motor_2, motor_3);
 
 
}

Et il saccade aussi quand je mets le contrôle des moteurs comme ça    :Alvarin_07:

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 
          motor_1.setTargetRel( PC9);
          motor_2.setTargetRel( PC8);
          motor_3.setTargetRel( PC7); 
        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;
    }


  // move 3 motors
 
 
  controller.move(motor_1, motor_2, motor_3);
 
 
}

Et quand je retire la dernière ligne

controller.move(motor_1, motor_2, motor_3);

 

Processing ne deconne pas, mais évidemment les moteurs ne bougent pas.

 



#4 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 278 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 27 octobre 2020 - 08:02

Bon pour le SerialUSB j'ai pas fait gaffe que chez teensy ils ont déjà redefine SerialUSB en Serial ... Du coup tu as un warnig cart il il a plusieurs #define identique ... 
Il va falloir que tu fasse du chercher remplacer et remplacer tous les SerialUSB en Serial1, ou un peu mieux tu les remplace tous en mySerialUSB 

 

et tu fais un #define mySerialUSB Serial1 ( si jamais plus tard tu préfère passer sur le serial 2 ou un autre ça sera plus facile pour changer.
 

 

Pour ton histoire de saccades, ok je crois que je viens de comprendre. 

Utilise moveAsync au lieu de move.

move est bloquant alors que moveAsync ne l'est pas... Async = " Asynchrone " .
 


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 

 

Les réalisations de Mike118  

 

 

 


#5 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 27 octobre 2020 - 05:18

Hello.

J' avance mais c'est pas encore top. 

Le port serie1 fonctionne très bien comme remplaçant de SerialUSB!!!

Les moteurs suivent les positions données par Processing, mais même en les envoyant toutes les 0.5 sec Processing bug au bout d'un moment.
En plus les moteurs sont actualisés toutes les .5 sec donc c'est pas super.

Les 2 notations de commandes  controller.moveAsync ne fonctionne pas.

  //       controller.moveAsync   (motor_4, motor_3, motor_2 ) ;// fonctionne pas
 /* 
  controller. moveAsync  (motor_4) ;
  controller.moveAsync (motor_2) ;
  controller.moveAsync  (motor_3) ;
  */ //fonctionne pas

Seul celle ci fonctionne:

controller.move (motor_5, motor_4, motor_3, motor_2 );

 

Je mets le programme  Teensyduino en entier. Merci encore

/*==========================================================================
 * The sketch shows how to move more than one motor. 
 * 
 * If more than one motor is moved by one controller all motors will arrive at 
 * their targets at the same time. E.g., if the motors are part of a 
 * x/y transport system, the transport move on a straight diagonal line to the
 * target coordinates.
 * 
 * The sketch also shows examples how the motor properties are set up
 *
 * A 1/16 microstep driver is assumed. You probably want to adjust speed, 
 * acceleration and distances if you are using a driver with another microstep 
 * resolution.
 ===========================================================================*/

#include "TeensyStep.h"

#define NBMOTEURS 10

 #define SerialUSB Serial1
 
 
Stepper motor_1(13, 14);   //DIR pin = 1 , STEP pin =  2, 
Stepper motor_2(3, 4);    
Stepper motor_3(5, 6);   
Stepper motor_4(7, 8);    
Stepper motor_5(9, 10);   
Stepper motor_6(11, 12);   
 
StepControl controller;

int positionX, positionX1, positionX2, positionX3,positionX4,positionX5, positionX6, positionX7, positionX8, positionX9; 
 
//   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

void setup()
{
    // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin (115200); // receive datas from Processing with the "Programming Port"
  Serial1.begin (115200); // receive datas from Processing with the "Programming Port"    
 //   SerialUSB.begin (250000); // print datas to send it into the other serial port of the Arduino Due, the "Native Usb Port" 

  //====Test if datas are printed into 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);

      
      Serial1.print ("A "); Serial.println (-4);     
      Serial1.print ("B "); Serial.println (-3);        
      Serial1.print ("C "); Serial.println (-2);  
      Serial1.print ("D "); Serial.println (-1);
      Serial1.print ("E "); Serial.println (-10);
  
  //====Initialise Pin Motor
  
  // setup the motors 
  
   motor_1
    .setMaxSpeed(3200)       // steps/s
    .setAcceleration(1600); // steps/s^2 
  
  motor_2
    .setMaxSpeed (3200)       // steps/s
    .setAcceleration(1600); // steps/s^2 
  motor_3
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(1600);   // steps/s^2     
 //   .setStepPinPolarity(LOW); // driver expects active low pulses
   motor_4
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(1600);   // steps/s^2   
   motor_5
    //.setPullInSpeed(300)      // steps/s     currently deactivated...
    .setMaxSpeed( 3200)       // steps/s
    .setAcceleration(1600);   // 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 
       
        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;
    }


  // move 4 motors
          motor_5.setTargetAbs( PC9);
          motor_4.setTargetAbs( PC8);
          motor_3.setTargetAbs ( PC7); 
           motor_2.setTargetAbs ( PC7); 


         controller.move   (motor_5, motor_4, motor_3, motor_2 ) ;
     //       controller.moveAsync   (motor_4, motor_3, motor_2 ) ;// fonctionne pas
 /* 
  controller. moveAsync  (motor_4) ;
  controller.moveAsync (motor_2) ;
  controller.moveAsync  (motor_3) ;
  */ //fonctionne pas
 
 
}

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); 
  
}
 
 


#6 Ludovic Dille

Ludovic Dille

    Habitué

  • Membres
  • PipPip
  • 173 messages
  • Gender:Male
  • Location:Belgique
  • Interests:Robotique, électronique, embarqué, informatique, ...

Posté 27 octobre 2020 - 06:30

Hello,
 

quand tu dis que moveAsync ne fonctionne pas c'est juste en remplaçant move par moveAsync ?
De toute façon il y a une grosse amélioration à faire dans le code qui est de donner des instructions que quand il y en a besoin. Si j'ai bien compris ce que tu veux faire
-> Attendre les données envoyées par processing
-> Quand tu as reçu toutes tes données -> les parser pour connaitre les positions
-> Appliquer la commande au moteur.

donc un truc du style
 

loop(){
  if(ShouldFetchNewData()){
    FetchData();
    ParseData();
    SetTargets();
    controller.moveAsync(...);
  } else {
    // Do nothing 
  }
}

 


Donc il faudrait bouger les .setTarget et le .moveAsync qui ne devraient s'exécuter qu'une seule fois quand on a reçu les nouvelles données.

Si ton programme est encore trop lent, je pense que passer d'un traitement de string directement à un traitement avec des entier/hexa pourrait aider.

Ludo



#7 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 30 octobre 2020 - 12:52

Hello.

Merci encore pour vos eclaircissements.

 

Je reviendrai sur la library TeensyStepper plus tard, car j'ai trouver comment contrôler les moteurs independemment les uns des autres.

 

J'utilise mon programme avec accelestepper et maintenant que j'ai la Teensy overlocké,  je peux contrôler mes moteurs beaucoup rapidement.

 

Quand je fais la phase test pour 20, j'ai des différence considérable entre des réglages differents

 stepper[i].setMaxSpeed(6400); // sur 1600 step/round 4 tour/s 3200 11 sec . 6400 .6 sec .  

 stepper[i].setAcceleration(3200);  // MAX 3000?  // MAX 250?   

 

Dans le programme Teensyduino, quand dans le Setup, j'ai:

SerialUSB.begin (9600); // it doesn't make any sens but i prefer

ou

 Serial1begin (9600); // it doesn't make any sens but i prefer

 

Et que j'allume Processing, sans activer le moteur, le programme refait une phase test. Et là, j'ai des décrochages.

Et quand je fais des tourner les moteurs dans Processing, je peux les faire tourner très rapidement, mais j'ai des décrochages aussi.

Alors que j'en ai pas en phase test.

Et je n'ai pas ce souci de décrochage quand j'ai pas le Serial1 activé.

 

Processing ne bloque plus, mais y a un souci dans la transmission de données. Pourquoi?

 //*************Position from Processing

int processingPosition, processingPosition1,processingPosition2, processingPosition3, processingPosition4; 
int processingPosition5, processingPosition6, processingPosition7, processingPosition8, processingPosition9;

#include <AccelStepper.h>
#define SerialUSB Serial1
// Define a stepper and the pins it will use
#define NBMOTEURS 10

#define NBPASPARTOUR 1600 // useless
 
#define STEP 1//// 1600-->Mode pas complet MS3 sur ON
//  800-->Mode pas complet MS1+ MS2 sur ON
 

//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};


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]),  
};
 
int positionX, positionX1, positionX2, positionX3,positionX4,positionX5, positionX6, positionX7, positionX8, positionX9; 
 
//   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()
{  
 //   Serial1.begin ( );
    Serial.begin (9600);
    
//    SerialUSB.begin (9600); // it doesn't make any sens but i prefer

  //====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
 //  stepper[i].setMaxSpeed(3200); // sur 1600 step/round
 //  stepper[i].setAcceleration(3200);  // MAX 3000?  // MAX 250?
 // stepper[i].setSpeed(3200);

 
/// with 1/4 step == 800 step / round
  stepper[i].setMaxSpeed(6400); // sur 1600 step/round 4 tour/s 3200 11 sec . 6400 . 8 sec . 4000 9s
 stepper[i].setAcceleration(3200);  // MAX 3000?  // MAX 250?  6 sec
 // stepper[i].setSpeed(3200);
   }
PC0=32000;    //20 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();
    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].moveTo(PC0);
       
       stepper[9].run();
       
 //      stepper[8].moveTo(PC1);
       
       stepper[8].run(); 
             
 //      stepper[7].moveTo(PC2);
       stepper[7].run();
  //     stepper[6].moveTo(PC3);
       stepper[6].run();
  //     stepper[5].moveTo(PC4);  
       stepper[5].run();
 
 //      stepper[4].moveTo(PC5);
       stepper[4].run();
 //      stepper[3].moveTo(PC6);
       stepper[3].run();       
 //      stepper[2].moveTo(PC7);
       stepper[2].run();
  //     stepper[1].moveTo(PC8);
       stepper[1].run();
 //      stepper[0].moveTo(PC9);  
       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() { 
 
  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);   
}
 
 


#8 Ludovic Dille

Ludovic Dille

    Habitué

  • Membres
  • PipPip
  • 173 messages
  • Gender:Male
  • Location:Belgique
  • Interests:Robotique, électronique, embarqué, informatique, ...

Posté 30 octobre 2020 - 01:13

Qu'est-ce que tu appels comme décrochage ?
Que tes moteurs ne suivent pas les instructions de processing ? Si oui, mon intuition me dirait que ça vient de ta fonction recvWithStartEndMarkers() (et aussi de ton parse), vu que tu récup tes données en mode bloquant tu dois attendred e récupérer < 100 chars (33 datas de plusieurs décimal et avec des "," + le signe pour les négatifs). Ce qui veut dire que pendant tout ce temps tu ne peux pas utiliser ta fonction stepper.run() et donc les moteurs ne peuvent pas faire leur steps correctement. Je dirais que Parse aussi va prendre du temps. Le meilleur moyen pour vérifier c'est de regarder le temps que prend la fonction recvWithStartEndMarkers() et parseData() et voir l'impact que ça a sur ton système.

Ludo

 



#9 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 30 octobre 2020 - 03:10

 
Que tes moteurs ne suivent pas les instructions de processing ? 
 

 

Non, les moteurs suivent bien les instructions de Processing maintenant.

Pardon ma question était bêtement alambiquée.

C'est seulement quand j'active le Serial1 dans le Teensyduino  que Processing bloque totalement.

 

 

Si j'emploie une fonction comme ça pour recevoir les info de Processing ce serait mieux?

Je suis pas sûr car parseInt est bloquant je crois

void receiveData() {

  if (Serial.available() > 0) {  

    w0 = Serial.parseInt(); 
    w1 = Serial.parseInt(); 
    w2 = Serial.parseInt();
    w3 = Serial.parseInt();
    w4 = Serial.parseInt(); // 

    dataReady = true;
    }

  if (dataReady)

    { // actualise datas where to go 
      stepper[4].moveTo(w4);
      stepper[3].moveTo(w3);
      stepper[2].moveTo(w2);
      stepper[1].moveTo(w1);
      stepper[0].moveTo(w0);  
 
      // print data on the other serial port
      SerialUSB.print ("A "); SerialUSB.println (w4);     
      SerialUSB.print ("B "); SerialUSB.println (w3);        
      SerialUSB.print ("C "); SerialUSB.println (w2);  
      SerialUSB.print ("D "); SerialUSB.println (w1);
      SerialUSB.print ("E "); SerialUSB.println (w0); 
    dataReady == false;
    }
     
  }

    
  } 


#10 Ludovic Dille

Ludovic Dille

    Habitué

  • Membres
  • PipPip
  • 173 messages
  • Gender:Male
  • Location:Belgique
  • Interests:Robotique, électronique, embarqué, informatique, ...

Posté 30 octobre 2020 - 06:21

En soi le fait qu'une fonction soit bloquant n'est pas forcément dérangeant c'est surtout au niveau timing que tu dois regarder si ça passe. Mais question peut-être bête tes SerialUSB.print(....) servent pour le débug ?



#11 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 31 octobre 2020 - 08:07

Bonjour,

 

J'arrive enfin à avoir les données de Processing, contrôler les moteurs et lire les données de Processing sur un autre port de la Teensy.

Il fallait augmenter les bauds des deux ports séries. :yahoo:

Mais je trouve que le son des moteurs pas à  pas est meilleur dans la phase test que dans la phase où je reçois les données de Processing.

Du coup, on a l'impression qu'ils sont plus enclin à décrocher (perdre leur position qui leur est assigné) que s'ils recevaient leur position directement depuis laTeensy elle-même. 

Je mets la deuxième manière pour recevoir les donnees, la fonction recvDataAutre(); mais avec cette dernière je n'ai même pas la phase test qui s'enclenche?!

 

J'ai besoin de recevoir les données pour les traiter dans un logiciel musical (note, effet, ....).

Il faut que je trouve un moyen d'encore mieux exécuter la commande stepper[x].moveTo(PCx) pour éviter les micros tremblements et éviter les éventuels décrochages.

Pourquoi dans la phase test, dans le setup, le mouvement des moteurs est fluide ? Et pourquoi il ne l'est pas dans la loop? :Alvarin_07:


//***********  Variable de Processing
long w0, w1, w2, w3, w4; // vitesse à recevoir en nombre de pas /seconde
boolean dataReady = false;
  
#include <AccelStepper.h>
// #define Serial1 Serial1
// Define a stepper and the pins it will use
#define NBMOTEURS 10

#define NBPASPARTOUR 1600 // useless
 
#define STEP 1//// 1600-->Mode pas complet MS3 sur ON
//  800-->Mode pas complet MS1+ MS2 sur ON
 

//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};


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()
{   
   Serial1.begin (500000);  Serial.begin(500000); //ne decroche pas, affiche bien
   //   Serial1.begin (500000);  Serial.begin(250000); //ne decroche pas, mais affiche mal
  //   Serial1.begin (250000);  Serial.begin(250000); //DECROCHE
  
  //====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/4 step == 800 step / round
  stepper[i].setMaxSpeed(3200); // sur 1600 ==>step/round 2 tour/s
  stepper[i].setAcceleration(1600);  // 1 tour/s-2 
   }
   
PC0=8000;    //5 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();
   
   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 recvDataAutre() { 
  
   if (Serial.available() > 0   ) {  

    PC0 = Serial.parseInt(); //  
    PC1 = Serial.parseInt(); 
    PC2 = Serial.parseInt();
    PC3 = Serial.parseInt();
    PC4 = Serial.parseInt();  

    dataReady = true;
    }

  if (dataReady)
    {
      Serial1.print ("A "); Serial1.println (PC4);     
      Serial1.print ("B "); Serial1.println (PC3);        
      Serial1.print ("C "); Serial1.println (PC2);  
      Serial1.print ("D "); Serial1.println (PC1);
      Serial1.print ("E "); Serial1.println (PC0); 
    dataReady ==  false;
    }
     
  stepper[4].moveTo(PC4);
  stepper[3].moveTo(PC3);
  stepper[2].moveTo(PC2);
  stepper[1].moveTo(PC1);
  stepper[0].moveTo(PC0); //  
  
  
}

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() { 
  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);   
}
 
 


#12 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 31 octobre 2020 - 06:08

Pour résumer,

J'arrive à contrôler les moteurs via Processing avec la Teensy, mais ils décrochent à la vitesse 40, avec le deuxième port série réglé à 500000.

Par contre si je commente, dans le setup, le deuxième port série de la Teensy, ils ne décrochent plus du tout.

Ou alors il faut le régler à 1000000, ils ne décrochent pas mais on entend un mini bruit de tremblement. 

Mais à cette vitesse, je ne peux lire les données dans le moniteur et donc ne peux pas les utiliser dans un autre logiciel.

 

Alors qu'ils ne décrochent pas à la vitesse 100 avec l'Arduino Due et 115200 bauds suffit pour le Programmant port (Serial.begin) et l'autre port (SerialUSB)  

Il y a un problème avec la communication du deuxième port serie de la Teensy.

 

Dans les deux cas je suis avec la librairie Accelstepp. 

Je me dis que je devrais utiliser deux Arduino Due ensemble relier par un port série ou autre.

 

Je mets  le programme avec les petites différences Teensy et Arduino Due

 boolean dataReady = false; // pour autre maniere de recevoir l'info de Processing
  
#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 (500000);  Serial.begin(250000); //decroche   à 40, affiche bien AVEC TEENSY,
  //**************** PRENDRE CI DESSOUS************************.
 //  Serial4.begin (1000000);  Serial.begin(500000); //decroche pas,  , mais peux pas lire les info.
   Serial4.begin (1000000);  Serial.begin(1000000); //decroche pas,  , mais peux pas lire les info.
  //  Serial4.begin (500000);  Serial.begin(115200); //decroche à 40 , affiche bien AVEC TEENSY
  //======================
  //   Serial4.begin (500000);  Serial.begin(500000); //decroche à 40, affiche bien AVEC TEENSY
    
   //  SerialUSB.begin (115200);  Serial.begin(115200); //ne decroche pas, affiche bien AVEC ARDUINO DUE
 //====================== Test ci dessous avec Teensy uniquement 
 //    Serial4.begin (250000);  Serial.begin(250000); //DECROCHE à 30
  
  //====Test if datas come from the both serial of the Arduino Due
    
      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/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(2000);  // 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();
    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 recvDataAutre() {  if (Serial.available() > 0   ) {  

    PC0 = Serial.parseInt(); //  
    PC1 = Serial.parseInt(); 
    PC2 = Serial.parseInt();
    PC3 = Serial.parseInt();
    PC4 = Serial.parseInt();  

    dataReady = true;
    }

  if (dataReady)
    {
     SerialUSB.print ("Ab ");SerialUSB.println (PC4);     
     SerialUSB.print ("Bb ");SerialUSB.println (PC3);        
     SerialUSB.print ("Cb ");SerialUSB.println (PC2);  
     SerialUSB.print ("Db ");SerialUSB.println (PC1);
     SerialUSB.print ("Eb ");SerialUSB.println (PC0); 
     
   stepper[9].moveTo(PC0);  stepper[8].moveTo(PC1); stepper[7].moveTo(PC2); stepper[6].moveTo(PC3); stepper[5].moveTo(PC4);
     
    dataReady = false;
    } 
      stepper[9].run();  stepper[8].run(); stepper[7].run();  stepper[6].run();  stepper[5].run(); // Les moteurs avancent aux ralentit.
}

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);   
}
 
 


#13 Ludovic Dille

Ludovic Dille

    Habitué

  • Membres
  • PipPip
  • 173 messages
  • Gender:Male
  • Location:Belgique
  • Interests:Robotique, électronique, embarqué, informatique, ...

Posté 02 novembre 2020 - 05:16

Hello,

Il y a un truc que je ne comprend pas encore bien dans ton programme mais à quoi sert ton Serial4 ? Car tu retransmet sur ce port série toutes les infos que tu reçois dans ton SerialUSB.



#14 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 278 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 03 novembre 2020 - 01:35

en fait il a fait un #define SerialUSB Serial4 ... donc en théorie il devrait utiliser partout SerialUSB et pas Serial4 ...  

Ensuite SerialUSB je crois que c'est déjà une notation " utilisée " par l'ide arduino, pour éviter les ambiguitées  il vaudrait mieux utiliser un nom univoque genre SerialProcessing pour le #define ... Mais je ne suis pas sûr que ça soit un vrai problème...

Après bvking tu parles d'un truc qui marche pas avec le teensy et qui marche avec la due, mais tu compares bien le cas avec 10 moteurs dans les deux cas ? 
J'ai l'impression que tu en demandes beaucoup entre les moteurs + tous les prints sur le serial, donc à voir si on peut " optimiser " / "limiter " les appels qui servent à rien ...  Et voir pour optimiser la partie du code qui prend le plus de temps ... 

 

 

si tu fais une modif du genre comme ça : 

 

void loop() { 
 // recvDataAutre(); // deconne
    recvWithStartEndMarkers();
    if (newData == true) {
        Serial.print("0======================>"); 
        Serial.println(micros());
        strcpy(tempChars, receivedChars);
        Serial.print("1======================>"); 
        Serial.println(micros());
            // 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.  
        Serial.print("2======================>"); 
        Serial.println(micros());
           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); 
        Serial.print("3======================>"); 
        Serial.println(micros());
        showPosition();
        Serial.print("4======================>"); 
        Serial.println(micros());
        showPhazisScaled();
        Serial.print("5======================>"); 
        Serial.println(micros());
        showTrigBangWithRevolution();
        Serial.print("6======================>"); 
        Serial.println(micros());
       
        newData = false;
    }  
        Serial.print("avantrun======================>"); 
        Serial.println(micros());     
    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();  
        Serial.print("aprèsrun======================>"); 
        Serial.println(micros());  
}

Est ce que tu peux faire tourner ton programme et dire qu'est ce qui prend le plus de temps ? genre la différence de temps entre 1 et 2 elle est de combien par rapport à 2 et 3 ? etc ...  

Ensuite normalement la force de la librairie teensy step c'est quelle est censée être plus optimisée ... et devrait mieux gérer le temps passer à gérer les moteurs ...


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 

 

Les réalisations de Mike118  

 

 

 


#15 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 03 novembre 2020 - 10:23

Hello.

De 2 à 1, il faut 46 microsecondes.  

Alors que de 3 à 2, il faut 4.

 

La fonction showPhazisScaled() est entre 5 et 4.

Elle prend 979. Si je m'en passai, j'aurai moins de tremblement dans les moteurs?

10:10:48.253 -> 4>37373566
10:10:48.253 -> A 0
10:10:48.253 -> B 0
10:10:48.253 -> C 0
10:10:48.253 -> D 0
10:10:48.253 -> E 0
10:10:48.253 -> F 0
10:10:48.253 -> G 0
10:10:48.253 -> H 0
10:10:48.253 -> I 0
10:10:48.253 -> J 0
10:10:48.253 -> 5>37374545
 
Et le temps entre tous les stepper.run est de 470 micros
Je vais refaire un test pour cette dernière affirmation.
 
De toute façon je vais utiliser teensy step, mais j'ai un truc qui  ne va pas dans la manière d'exciter les moteur de manière asynchrone.
Je referai un post dans l'après-midi.
Merci à toi.


#16 bvking

bvking

    Nouveau membre

  • Membres
  • 74 messages

Posté 03 novembre 2020 - 07:04

Hello.

J'ai demandé de l'aide sur le site du fabriquant de la Teensy.

En fait le problème du Serial1  était son buffer size que j'ai pu modifier dans le programme Serial1.c qu'il y a dans le Teensyduino .

J'ai mis 255 au lieu de 64. Et apparemment on peut encore l'augmenter.

J'ai du changer aussi des "ticks" qui actualise les steppers motor.  

Donc plus la peine de réfléchir la dessus.

Merci et des bisous.

 

Je mets le programme si t'es curieux. Il ya des modif dans le setup, et les trois lignes avant.  

 
 
 #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;

#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
        
       //====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 
     
     
   
  //====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); // 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();
    
} 


#17 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 278 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 04 novembre 2020 - 12:14

Sur ce coup là c'était un peu trop spécifique à la teensy pour moi ;) Mais content de voir que tu as pu trouver ;)


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 

 

Les réalisations de Mike118  

 

 

 





0 utilisateur(s) li(sen)t ce sujet

0 members, 0 guests, 0 anonymous users