Find a tip to inform the position to the stepper motor

n Processing, I made a simulation where I run an engine over several revolutions at a certain speed and in both directions.

I can track the position of the simulated motor using a stepper motor.

By giving the position information of the simulated motor (on a scale from 0 to 4095) to the stepper motor (which therefore makes a revolution in 4096 steps), the stepper motor follows the simulated movement very well. 8); D.

Everything is fine when the simulated engine does not exceed 1 revolution in one direction or the other.

But, when the simulated motor exceeds a revolution, when it starts a first revolution, Processing always sends positions between 0 and 4095.

Processing therefore sends at the start of the first lap, position 0 to my stepper motor on Arduino and therefore (instead of continuing forward) it goes back to go to position 0.

So to solve this problem, when the information of the simulated engine goes from 4095 to directly 0 or 1 or 23, Processing would have to send a virtual position equal to position 4096 + 0, 4096 + 1 or 4096 + 23.

Similarly, if at the start the simulated motor turns in the other direction, Processing sends the positions 0, 4096, 4085 for example and the stepper motor goes forward (from 0 to 4096) then back (from 4096 to 4085 ).

When I should send virtual positions 0, -1, -11.

Likewise if the second back turn is the same as the first, instead of Processing sending 0, 4096, 4085, Processing should send virtual positions
0- 4096, -1- 4096, -11 - 4097. So, -4096, -4097, -5008.

Hoping to have been clear, I thank you with my greatest gratitude. :muscle:

here what I tried but I don’t have all the data of position. It can to go straight to 4090 to 10 for exemple

 float pos0 = map ((net.phase[0]), 0, TWO_PI, 0, 4095); 
 
int positionMax=4095;
int Pos0= int (pos0);
    
 print ("Pos0    "); println (Pos0);

 // if motor goes forword
 if (( Pos0 - positionMax > -1) || ( positionMax- Pos0 < 1))  { 
     
            int VirtualPosition = Pos0+positionMax+0;
   
   } 
      
     String pos = (10.0)+"*"+(10.0) +"*"+(10.0)+"*"+(10.0)+"*"+(VirtualPosition) +"*"; 
     
 println (pos);
 
 arduinoport.write(pos); 
1 Like

Hello,

You have a related post here already:

I have worked with stepper motors.

I provided a minimal example of an approach I took to this:

  • Processing is sending integer “steps” and direction (this can be positive or negative) to the Arduino
  • “Position” can be translated to steps and direction; you will have to do this correctly relative to the current initial and final position. I did not do this in this example.
  • The Processing “simulation” should follow the expected motion of the stepper motor from the instructions you are sending.
  • The motor must complete its motion (code is blocking on Arduino side) so that must also be given consideration. This is not in this code example.

I did this simulation (rotating line) using:

  • A integer counter for steps.
  • Drawing a line that was rotated by steps*angle/step
  • Using modulo operator for the zero-crossing (0/360/720 deg… etc.) to determine the rotation and plot the line.
    Examples:
    4096%4096 = 0
    (4096 + 1024)%4096 = 1024
    And so on…

I left this uncommented and removed variable names so you can work through it:

float a;
int b, c;

void setup() 
  {
  size(400, 400, P2D);
  b = 32;
  }

void draw() 
  {
  background(64); 
  translate(width/2, height/2);    
  
  float d = TWO_PI/b;   
  a = (c%b)*d;
  rotate(a);
  strokeWeight(3);
  stroke(255, 0, 0);
  line(0, 0, 150, 0);
  
  println(a, degrees(a));
  }
  
void mousePressed()
  {
  if (mouseButton == LEFT) c++;
  if (mouseButton == RIGHT) c--;
  }

I used the modulo operator because floating point errors accumulated if I rotated the motor a few hundred thousand times; this kept the rotations to between 0 and 2π.

References:
https://processing.org/reference/modulo.html
:slight_smile:

1 Like