mawyatt
Posts: 10
Joined: Tue Sep 04, 2018 6:38 pm

Help with For Loops

Mon Nov 05, 2018 1:01 am

Hello,

I'm new to Python, using a Raspberry Pi 3B, and also not a programmer.

I've got a setup with 3 stepper motors on X ,Y and Z axis driven by a Pololu Tic500 on each axis, controlled by USB from the Raspberry. Separate X, Y and Z setup routines to allow each axis to define a Start, End and Increment size work and allows the motors to increment Y, then X and finally Z with predefined Start, End and Increments. This is a camera/lens setup where the camera/lens is attached to a Z linear rail, X & Y linear rails move the subject. All the linear rails are driven my stepper motors, which turn motor rotation into linear movement.

All works well, however I need to go to each X position, at each Y position and setup a specific Z axis Start, End and Increment size for each X position at each Y position, so a specific Z Start, End and Increment for each predefined X of Y position is required.

A nested "for" loop of Y overall, with X and then Z runs fine and positions all 3 stepper motors at the correct positions with the predefined setup Start, End and Increment size for X, Y and Z. I've tried using the same nested stepper motor position routines "for X loop" inside a "for Y loop" to move to the desired Y and X positions , but using the Z Start, End, Increment setup routine to capture the Z parameters at each Y and X position. The Z axis Start, End, Increment routine only gets run once then bypassed, as if it's ignored for the rest of the Y and X "for" loops. Here's the basic general concept;

Code: Select all

for y in range (y number of increments ):
    move to y position
    y position = y position + y increment
   for x in range (x number of increments):
      move to x position
      x position =  x position + x increment 
      z axis start end increment routine
      print z parameters
    print x parameters
print y  parameters
This is the same sequence that runs everything fine with "z axis start end increment routine" replaced with;

Code: Select all

 for z in range (z number of increments):
      move to z position
     z position =  z position + z increment 
Any help is greatly appreciated,

Best,

Mike

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Help with For Loops

Mon Nov 05, 2018 10:38 am

Mike, I'm not sure that your question makes complete sense (or even if it is a question) Are you saying that you already have some code that works to some extent but has some incorrect behavior (in which case post that code). Or that you want to write some code to do what your pseudo code does(n't ?) do but would(n't ?) do if you put the second code snippet in. Do you want to make your z_start, z_end, z_increment depend on a function of x and y? In which case what function?

One issue with your pseudo code is that you don't seem to reset your x and z values back to the start before each loop which you need to do as in

Code: Select all

x_start, x_incr, x_n = 10, 2, 7
y_start, y_incr, y_n = 12, 2, 9
z_start, z_incr, z_n = 4, 1, 5
y_posn = y_start
for y in range(y_n):
    y_posn += y_incr
    x_posn = x_start
    for x in range(x_n):
        x_posn += x_incr
        # if the z start, end, increment depend on x and y then calculate them here
        z_posn = z_start
        for z in range(z_nr):
            z_posn += z_incr
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hippy
Posts: 7908
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Help with For Loops

Mon Nov 05, 2018 11:39 am

I think your problem may be with having three nested X, Y and Z for loops. That should work fine when there is only a change in one axis, will behave as if just a single for loop for that axis, but with multiple axis changes you either need to use a single loop to traverse from start to end positions, or run the axis changes one after another.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Help with For Loops

Mon Nov 05, 2018 12:03 pm

hippy wrote:
Mon Nov 05, 2018 11:39 am
I think your problem may be with having three nested X, Y and Z for loops. That should work fine when there is only a change in one axis, will behave as if just a single for loop for that axis, but with multiple axis changes you either need to use a single loop to traverse from start to end positions, or run the axis changes one after another.
I was thinking the same thing. What happens if there is only movement on one axis for example the Y axis, If there is no increment specified for X then the Y axis for loop won't even be reached.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”