position and velocity measuring by several encoders

hi
I'm using 3 encoders to read the position and the velocity two of them have 4000 cpr and the other one has 48 cpr,
the code which I've used is :

#include <Encoder.h>
#include <stdio.h>
Encoder myEncoder1(2,3);
Encoder myEncoder2(4,5);
Encoder myEncoder3(8,9);
double Position1;
double Position2;
double Position3;

void setup() {
 Serial.begin(9600);

}
long oldposition1 = -999;
long oldPosition2 = -999;
long oldPosition3 = -999;

void loop() {
 long newPosition1 = myEncoder1.read();
 long newPosition2 = myEncoder2.read();
 long newPosition3 = myencoder3.read();

if (newposition1 != oldPosition1) {
oldPosition1 = newPosition1;
Position1 = (double)(newPosition1*360)/4000;
}
if (newPosition2 != oldPosition2) {
oldPosition2 = newPosition2;
Position2 = (double)(newPosition2*360)/4000;
}
if (newPosition3 != oldPosition3) {
oldPosition3 = newPosition3;
Position3 = (double)(newPosition3*360)/48;
}
String p1=";";
Serial.println(Position1+p1+Position2+p1+Position3);
}
}

and the arduino board is DUE
but I have no idea how to write the code for measuring the 3 velocities of the encoders .could you help me please thanks

Please use Autoformat, or Ctrl T, in the IDE before copying. That makes the code a lot easier to read.
Writing comments telling the intention of the lines is good practise. Now it looks like a djungle of positions.

ok

#include <Encoder.h>
#include <stdio.h>
Encoder myEncoder1(2, 3);  //define encoder1 
Encoder myEncoder2(4, 5);  //define encoder2 
Encoder myEncoder3(8, 9);  //define encoder3 
double Position1;
double Position2;
double Position3;

void setup() {
  Serial.begin(9600);

}
long oldposition1 = -999;
long oldPosition2 = -999;
long oldPosition3 = -999;

void loop() {
  long newPosition1 = myEncoder1.read();
  long newPosition2 = myEncoder2.read();
  long newPosition3 = myencoder3.read();
  // write the code that if the position of the encoder is changed , the position set to the new one
  if (newposition1 != oldPosition1) {
    oldPosition1 = newPosition1;
    Position1 = (double)(newPosition1 * 360) / 4000; // convert position1 unit to degree
  }
  if (newPosition2 != oldPosition2) {
    oldPosition2 = newPosition2;
    Position2 = (double)(newPosition2 * 360) / 4000; // convert position2 unit to degree
  }
  if (newPosition3 != oldPosition3) {
    oldPosition3 = newPosition3;
    Position3 = (double)(newPosition3 * 360) / 48;   // convert position3 unit to degree
  }
  String p1 = ";";
  Serial.println(Position1 + p1 + Position2 + p1 + Position3); // showing position of encoders
}
}

Thanks. I wonder if it compiles. The two last rigjt curly brackets on the same column is a suspect.
For time measuring use the same technic as for positions. When You make a reading of the encoder note the time like this:

encoder1CurrentTime = millis();

Calculating speed will be like:

encoder1speed = encoder1Distance / (currentTime -previousTime);
previousTime = currentTime;

Railroader:
Thanks. I wonder if it compiles. The two last rigjt curly brackets on the same column is a suspect.
For time measuring use the same technic as for positions. When You make a reading of the encoder note the time like this:

encoder1CurrentTime = millis();

Calculating speed will be like:

encoder1speed = encoder1Distance / (currentTime -previousTime);

previousTime = currentTime;

thanks for your answer I know how to write the formula to calculate the speed as you mentioned it is distance / delta time but for distance I need delta position / delta time and in my code the delta position is :

(newPosition1-oldPosition1)

and in my code I set oldPosition to newPosition so distance equals to ZERO and the speed equals to ZERO too!

Squeeze in the speed calculation just before that line! Then delta distance exists.

Railroader:
Squeeze in the speed calculation just before that line! Then delta distance exists.

Railroader:
Squeeze in the speed calculation just before that line! Then delta distance exists.

Railroader:
Squeeze in the speed calculation just before that line! Then delta distance exists.

thanks its working

Fine. You probably saw You need to wait setting previousTime = currentTime until position 3 is done.
Declaring the vaiables You did as well....

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.