Position / rotation conversion

Hello,

I run motors in Processing with position data from 0 to 4096 for a revolution.
I have a stepper motor in Arduino which follows its positions very well. :grinning:

The problem is when the simulated motor arrives at its starting point, the program sends the data 0, then 2048 when it arrives at the middle of the second rotation.

While to properly inform my engine in the Arduino, it would be necessary that at the start of the second lap, I send the data 4096 + 1 when it starts the second revolution.

When it reaches the middle of the second revolution, it should have
4096 + 1 + 2048.

If you have brilliant ideas, please enlighten me.

Here the position of my motor

I think I have to compare an actual data with last data but I can’t program save data, so I did this
I don’t work, because I don’t have all the resolution of data. It goes from 4056 directly to 10 for exemple.
`

float pos0 = map ((net.phase[0]), 0, TWO_PI, 0, 4096); 
int Pos0 = int (pos0);
int positionMax = 4096
 if (( Pos0 - positionMax > -1) || ( positionMax- Pos0 < 1))  { 
     
            int VirtualPosition = Pos0+positionMax+1;
1 Like

Position is from 0 to 4095 as you will get a 12 bit resolution (2^12).

float pos0 = map ((net.phase[0]), 0, TWO_PI, 0, 4096); this line is always constrained between 0 and 4096. How do you want to detect full rotations? Is that what you want to do? Keep track how many rotations you have completed? If so, then you will need to implement some sort of memory concept where you keep track of the previous position. If the position changes from 4095 to 0, then it will count as a full revolution. I am assuming every cycle the position is constrained to change only by steps of 1.

Kf

final int POSITION_MAX= 4096
int previousPos=0;  //Init value 
int currentPos=0;  //Init value
int lapCounter=0;  //keeps track of revolutions clockwise or counterclockwise
...
...
...
float pos0 = map ((net.phase[0]), 0, TWO_PI, 0, 4096); 
int currPos = int (pos0);
int deltaPos=currPos-previousPos;
if (deltaPos !=1 ){
  int lapSense = abs(deltaPos)/deltaPos; // Normalize to 1 or -1 for CW or CCW sense
  lapCounter+=lapSense;
}
previousPos=currentPos;  //Update previous position
// ACTUAl absolute position would be: POSITION_MAX * lapCounter + currentPos
1 Like

Yes, I would like that if the position goes from 4095 to 0, it will count as a complete revolution.

In this way, we could send Arduino the ActualAbsolutePos: 0 + 4095 * 1 + 1, so that the motor moves to position 4096. this would mean that the motor starts its first revolution and advances of one step, the +1. (From the data int currPos pos = 0)

In the second round, you should send the position
0 + 4095 * 2 + 1. (Always from int currPos = 0).

And when it reaches the middle of the second round, you should send 2048+ 4095 * 2 + 1 (from int currPos = 2048).

If the motor changes direction since the ActualAbsolutePos = 2048+ 4095 * 2 + 1. This ActualAbsolutePos should be decremented.

The problem in your program is the “lapsens” (+ or -) is triggered only when the engine pass the threshold of position 0 to -1.

L’ActualAbsoluePos ne se dĂ©cremente pas avant.

Strangely lapCounter add or substract the LapCount with too much big numbers

Morever, I should have told you that the program sends the position -1 to -4095 when the motor turns in the opposite direction.

And continuing in this direction, it sends 0, -1, 


Lately is it normal that the given currentPos is always equal to 0?

Thank you, thank you very much.