Go Down

Topic: Resolved. Reading & Writing datas with the two Serial port of the Due.  (Read 2750 times) previous topic - next topic

Robin2

Referring to the Arduino code in Reply #28 ...

This code
Code: [Select]
void showParsedData() {
 
  SerialUSB.print(integerFromPC0);
  SerialUSB.print(integerFromPC1);
  SerialUSB.print(integerFromPC2);
  SerialUSB.print(integerFromPC3);
  SerialUSB.println(integerFromPC4);

 
 
}

Would be much better if you were to print some space characters between each value - like this
Code: [Select]
  SerialUSB.print(integerFromPC0);
  SerialUSB.print("  ");
  SerialUSB.print(integerFromPC1);
  SerialUSB.print("  ");
  SerialUSB.print(integerFromPC2);
  SerialUSB.print("  ");
  SerialUSB.print(integerFromPC3);
  SerialUSB.print("  ");
  SerialUSB.println(integerFromPC4);


And how can it make sense to put this stepper code into a function named showParsedData()
Code: [Select]
       stepper[4].moveTo(integerFromPC4);
       stepper[3].moveTo(integerFromPC3);
       stepper[2].moveTo(integerFromPC2);
       stepper[1].moveTo(integerFromPC1);
       stepper[0].moveTo(integerFromPC0);
       
       stepper[4].run();
       stepper[3].run();
       stepper[2].run();
       stepper[1].run();
       stepper[0].run();

Put the stepper code into its own function.


You say that the second motor should run at a different speed (because it gets a different value) but I can't see where you use the received value to affect the speed.


There is a lot of junk code in your program - parts that are not being used. Take out all the bits that are not immediately essential so they are not confusing us.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

bvking

    
i+=1;
String positions ="<"+ int (i)+","+ int ((2*i))+","+ int (-i)+","+ int (i)+","+ int (i)+","+ ">";//
   
This line from Processing sends (2*i) to the second motor in Arduino. And just (i) to the others motors.
So the second motors should go twice faster than the other.

All the speeds are set in Arduino at the same constant speed in the setup.
Code: [Select]

// Set MaxSpeed, Acceleration and speed of each motors
  for(uint8_t i = 0; i < NBMOTEURS; i++) {      
  stepper[i].setMaxSpeed(400); // number max of step in one sec
  stepper[i].setAcceleration(50);  // number of step * sec *-2
  stepper[i].setSpeed(200); // number of step in one sec
   }

 

I think the problem is maybe the manner I send datas from Processing because
I have tested to send very slowly or very quickly datas from Processing by changing the the frameRate
and it changes speed of my motors but not well at all.

I try to explain to you.
If I send the data 200 to one motor, It should go the position 200 with the speed set in the Arduino Program.
It should go at the  position 200 in one second.
But, I I set frameRate(1)  and if(frameCount%2 == 0); the motor goes step by step to its position frame by frame. My motor make One step / 2 secondes.

If I mount the frameRate at 60, the motors go faster but the movement is not fluid as when the data comes from Potar. There are movements backwards as if the engines are receiving too much information.
So I set  the frameRate at 30, but motors don't go fast as they should do.

I put the Arduino Program cleaned up in the next post

bvking

Code: [Select]


//***********  Managing Variable coming from Processing
long w0, w1, w2, w3, w4; // position in step
boolean dataReady = false;
int i; // test speed of motor

#include <AccelStepper.h>

// Define a stepper and the pins it will use
#define NBMOTEURS 5  // number of motors

#define NBPASPARTOUR 200  // number of step for one revolution.

// Define a stepper and the pins it will use
#define STEP 1//FULL STEP MODE--> motor makes 1 full step. It needs 200 steps to make one revolution

#define PINDIRECTION0  12
#define PINSPEED0  13

#define PINDIRECTION1  10
#define PINSPEED1  11

#define PINDIRECTION2  8
#define PINSPEED2  9

#define PINDIRECTION3  6
#define PINSPEED3  7

#define PINDIRECTION4  4
#define PINSPEED4  5

AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED0, PINDIRECTION0),
AccelStepper (STEP, PINSPEED1, PINDIRECTION1),
AccelStepper (STEP, PINSPEED2, PINDIRECTION2),
AccelStepper (STEP, PINSPEED3, PINDIRECTION3),
AccelStepper (STEP, PINSPEED4, PINDIRECTION4), 
};

#define ANALOG_IN A0

int analog_in;
int positionX;

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

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

      // variables to hold the parsed data
char messageFromPC[numChars] = {0}; //not used for the moment
int integerFromPC0 = 0;
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
int integerFromPC4 = 0;
float floatFromPC = 0.0; // not used for the moment

boolean newData = false; // In order to not copy any datas from the serial if there is no datas coming.

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

void setup()

    Serial.begin (115200); //Port to receive datas: It's a serial port called the Programming port, the program of Arduino is transfered with. And in which go datas from Processing
    SerialUSB.begin (9600); //Port to send datas.It's a NativeUSB port in the Arduino Due just to transfer datas from Arduino to an other software.
    // I read datas in the Arduino monitor with this Native serial port. In this one go each of 5 datas,
    // from Processing named with a letter. example 10, 12, 14,16, 18--> A 10, B 12, C 14, D 16, E 18
    // Thoses datas indexed will go in Max Msp.
   
 // Select the good port to see datas in the monitor
    Serial.print ("To see datas from Processing, don't forget to select the Native Port, you are onthe programming port");     
 
   
// Set MaxSpeed, Acceleration and speed of each motors
  for(uint8_t i = 0; i < NBMOTEURS; i++) {     
  stepper[i].setMaxSpeed(400);
  stepper[i].setAcceleration(50);
  stepper[i].setSpeed(200);
   }
}

void loop() {

  //testStepMotors();

   recvWithStartEndMarkers();  // Receive datas from Processing.
    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();
        showParsedData();
        controlPositionMotor();
        newData = false;
    }
 

// testPotarMotor(); // work perfectly
// TestSpeedSendDataToMax (); work perfectly


}

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

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

void showParsedData() {
 
  SerialUSB.print(integerFromPC0);
  SerialUSB.print(integerFromPC1);
  SerialUSB.print(integerFromPC2);
  SerialUSB.print(integerFromPC3);
  SerialUSB.println(integerFromPC4);   
}

void controlPositionMotor () {   
       stepper[4].moveTo(integerFromPC4);
       stepper[4].run();
       stepper[3].moveTo(integerFromPC3);
       stepper[3].run();
       stepper[2].moveTo(integerFromPC2);
       stepper[2].run();
       stepper[1].moveTo(integerFromPC1);
       stepper[1].run();
       stepper[0].moveTo(integerFromPC0); // First data from Processing reachs here.
       stepper[0].run();
}
 
void testStepMotors() {
    for(uint8_t i = 0; i < NBMOTEURS; i++) {     
     stepper[i].setCurrentPosition(0);
  // Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
  while (stepper[i].currentPosition() != 600)  {
  //  stepper[i].setMaxSpeed(300);
    stepper[i].setSpeed(400);
    stepper[i].setAcceleration(150);
    stepper[i].runSpeed();
}
 
 //delay(1);
  // Reset the position to 0:
  stepper[i].setCurrentPosition(0);
  // Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
  while (stepper[i].currentPosition() != -200)   {
    stepper[i].setSpeed(-600);
    stepper[i].setAcceleration(1);
    stepper[i].run(); // what is the differnece between run and runspeed as above???
  // stepper[i].runSpeedToPosition(); /don't work
  }
  }
   Serial.print (stepper[0].currentPosition());
}

void testPotarMotor ()  {

   analog_in = analogRead(ANALOG_IN);
   positionX= map ( analog_in, 0, 1023, -400, 400); // 5V

  stepper[4].moveTo(positionX);
  stepper[4].run(); // work perfectly
 
  stepper[3].moveTo(positionX);
  stepper[3].run(); // work perfectly
   
  stepper[2].moveTo(positionX);
  stepper[2].run(); // work perfectly

  stepper[1].moveTo(positionX);
  stepper[1].run(); // work perfectly
 
  stepper[0].moveTo(positionX);
  stepper[0].run(); // work perfectly

  SerialUSB.println (positionX);
}

void TestSpeedSendDataToMax () { //

// Arduino-send datas to -Max with SerialUSB.print
// Arduino receive data-Processing 

   i++;  //

   // WORK VERY WEELL. To test control of motors which different speed.
   //when I don't use datas from Serial Programming port but datas proportional at i
       stepper[4].moveTo(i);
       stepper[3].moveTo(i/3);
       stepper[2].moveTo(i/2);
       stepper[1].moveTo(i/4);
       stepper[0].moveTo(i);

       stepper[4].run();
       stepper[3].run();
       stepper[2].run();
       stepper[1].run();
       stepper[0].run();
 
      SerialUSB.print ("A "); SerialUSB.println (-i);     
      SerialUSB.print ("B "); SerialUSB.println (-i+2);       
      SerialUSB.print ("C "); SerialUSB.println (-i+3); 
      SerialUSB.print ("D "); SerialUSB.println (-i+4);
      SerialUSB.print ("E "); SerialUSB.println (-i*4);
      SerialUSB.println (i-2);

       delay (1);
         
}

Robin2

This line from Processing sends (2*i) to the second motor in Arduino. And just (i) to the others motors.
So the second motors should go twice faster than the other.
But that is not how you are using the data in your Arduino program. This is the relevant code from the program in Reply #32
Code: [Select]
void controlPositionMotor () {   
       stepper[4].moveTo(integerFromPC4);
       stepper[4].run();
       stepper[3].moveTo(integerFromPC3);
       stepper[3].run();
       stepper[2].moveTo(integerFromPC2);
       stepper[2].run();
       stepper[1].moveTo(integerFromPC1);
       stepper[1].run();
       stepper[0].moveTo(integerFromPC0); // First data from Processing reachs here.
       stepper[0].run();
}

it is using the values that come from your PC to set the distance the motor should move, It does not set the speed.

It would be much more usual to put all the stepper.run() statements in loop() - perhaps as the last thing in loop() - rather than inside any function.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

bvking

Thanks it works great with my last Arduino program ; Data come in Arduino as the same frame rate speed from Processing.
I had to rise up the power of drivers controlling my stepper motors too.
Thanks again


Go Up