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