pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 10:20 am

Hoooray .......

I had my doubts as it had only run once without crashing.....

So I will try and get all the time sorted and the displaying text in a readable state , I will include a simple 1,2,3,4 type counter so you can use that as part of the picture numbering.

if all goes well hope to post finished code by the end of today.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 10:24 am

Sounds great.
So no don't worry about the camera bit yet, you will be adding it in place of the time.sleep that you currently do on stepper X when you reach a stop so it wont affect the stepping.
Would you mind highlighting with a comment this time.sleep to be replaces with the camera line?

Thanks!

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 12:15 pm

How does it go one step forwards 2 steps back.

so what I thought was a timing error turns out to be a missing stop on the stepper x counter clockwise travel, even tough we use the same list for both directions. :(

so had to go back to one of the test program to solve it , I hope

so please can you test this to make sure the stepper stops at each of the stop points ( 6 in total each way).

Code: Select all


# version 1.1

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]

# Stops X
stopsX = [0,546,1091,1637,2182,2728]
stop_countX = 0

# Stops Z
stopsZ = [0,114,228,341,455,568,683,796,910,1024,1252,1365,1479,1593,1707,1820,1934,2048,2162,2276,2389,2503,2617,2731,2844,2958,3072,3186,3300,3413,3527,3755,3864,3982,4096]
stop_countZ = 0

for pin in control_pinsX:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0)
 
for pin in control_pinsZ:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0) 

halfstep_seq = [
 [1,0,0,0],
 [1,1,0,0],
 [0,1,0,0],
 [0,1,1,0],
 [0,0,1,0],
 [0,0,1,1],
 [0,0,0,1],
 [1,0,0,1]
]
halfstep = 0
start = time.time()

# stepper Z starting Halfstep
Halfstep = 0

# Stepper X
cycle = 1

for L in range(6): #loop for 18 times clockwise 18 counter clockwise

    # Clock wise rotation  including stops
    for i in range(2729):  # this now the number of steps for a 240 degrees rotation
        print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

        time.sleep(0.001)
        halfstep = halfstep + 1
        if halfstep == 8:
            halfstep = 0

        #stops
        if i == (stopsX[stop_countX]):
            print("cwstop at ", (stopsX[stop_countX]))
            stop_countX = stop_countX + 1
            time.sleep(5)
        # End of Clockwise rotation

    #stop and wait 5 seconds
    print ("End of Clockwise rotation cycle ", cycle )
    #time.sleep(5)  # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
    cycle = cycle + 1
    stop_countX = 5 # reset stops

    # rotate back to start with stops
    for i in range(2728, -1, -1):  # this now the number of steps for the required rotation
        print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

        time.sleep(0.001)
        halfstep = halfstep - 1
        if halfstep == -1:
            halfstep = 7

        #stops
        if i == (stopsX[stop_countX]):
            print("ccwstop at ", (stopsX[stop_countX]))
            stop_countX = stop_countX - 1
            time.sleep(5)

    #End of reverse
    print ("End of Counter Clockwise rotation cycle ", cycle)
    time.sleep(5)  # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
    cycle = cycle + 1
    stop_countX = 0 # reset stops



GPIO.cleanup()
end = time.time()
print (end - start)
it will only do 6 cycles.

thanks
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 1:03 pm

Yeih, well spotted!

It works good, 6 stop points (including 0) 6 cycles (6 cw + 6 ccw).

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 1:07 pm

Great thanks for that

I can now add the fix to the 2 stepper code and try testing again.

that's what actually takes he time now 23 min 30 seconds per full run :shock: and that's without the actual picture taking........
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 1:20 pm

ahah in Italy we have a say: "the hasty cat, makes blind kittens" :D

23 minutes is an ok amount of time compared to how long this technique usually takes. But true that it will be annoying having to wait all the time to check the results, it'll be an excitement killer. I should start thinking about 23 mins tasks :mrgreen:

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 1:46 pm

So while waiting I have been looking up your proposed camera command -o a%04d.raw,

It would appear that this is used with the raspiraw command

like this raspiraw -md 7 -t 2000 -sr 1 -o /dev/shm/out.%04d.raw

I think you will find that is a command you would use at the command line not from within a python program.

of course I could be wrong , but if I am right this line won't work directly from python program.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 1:52 pm

I think you're right, because the way I've been testing the camera so far, was directly from the terminal, without creating a .py specific file for it.

Edit:

It'll basically be this one:
https://picamera.readthedocs.io/en/rele ... -sequences

But once, I'll get the definitive camera module + lens, I'll be adding specific settings according to the new camera features - such as brightness, sharpness etc.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 3:00 pm

Don't think that will work as the Capturing timelapse sequences uses its own time delay,
where as you need to capture an image once you have moved the stepper motors which will be triggered by the stepper motor program, trying to get the 2 time delays to line up would be difficult if not impossible.

To capture consistent images you may want to look at section 3.5 of the doc but instead of capturing a sequence you just capture a single image each time.

if you can't find a away to do it with python code , you may be able to call the command line instruction using os.system() or subprocess.call()

I would suggest writing a short test python program to test this if you go down that route.( willing to try and help if you should need it.)

I have finished testing the two stepper motor program so its ready for hardware testing.

So bit of information about it first.

I have tried to make adjusting some common timings as easy as possible by using variables which you will find at the beginning of the program , with comments so you know what the do.

I would be careful about lowing the stops between the steppers moving ( zstop , picdelay ) as depending upon how stable your set up is you may need time for things to steady.

I have added a comment in the function were you need to add your picture taking code, along with a print and sleep line, both of thes can be removed or modified as you wish.

So here it is.........

Code: Select all


# final 2 stepper version 3.2

import RPi.GPIO as GPIO
import time

# setup gpio
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]

for pin in control_pinsX:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0)
 
for pin in control_pinsZ:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0) 

# steps lists
# Stops X
stopsX = [0,546,1091,1637,2182,2728]
stop_countX = 0

# Stops Z
stopsZ = [0,114,228,342,455,569,683,797,910,1024,1138,1252,1365,1479,1593,1707,1820,1934,2048,2162,2275,2389,2503,2617,2730,2844,2958,3072,3185,3299,3413,3527,3640,3754,3868,3982,4096]
stop_countZ = 0

# gpio ouput values list
halfstep_seq = [
 [1,0,0,0],
 [1,1,0,0],
 [0,1,0,0],
 [0,1,1,0],
 [0,0,1,0],
 [0,0,1,1],
 [0,0,0,1],
 [1,0,0,1]
]

# stepper x starting halfstep value do not change
halfstep = 0

# stepper Z starting Halfstep value do not change
position = 0

# Stepper X cycles starting value
cycle = 1

# stop time after moving stepper Z you can reduce this if you want but not below 0.5
zstop = 5

# cmaera picture count start value
campic = 1

#step timer value  adjust this value higher to slow stepping of motors   typical values 0.001 0.001007
steptime = 0.001007 

# delay for picture taking stop can be reduced once you have your picture taking code added but not below 0.5
picdelay = 5

def take_picture(campic):
    print("place holder Take picture", campic)
    # ----put you camera code in these lines to take pictures----
    # campic is variable name for picture number counter
    #
    # --------------add more lines if need be -------------------    
    time.sleep(0.5)


def stepperz(Halfstep,stop_countZ):
    # Stepper Z
    start = stopsZ[stop_countZ]
    if stop_countZ > 0:
        start = start + 1
    stop = stopsZ[stop_countZ + 1] + 1
    
    for I in range(start, stop):  # this now the number of steps for one move
        #print(I)
        for pin in range(4):
            GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
            
        time.sleep(steptime) 

        Halfstep = Halfstep + 1
        if Halfstep == 8:
            Halfstep = 0


    #stop
    print("Z moved to", (stopsZ[stop_countZ + 1]))
    time.sleep(1)
    return Halfstep
    

# main program loop
print ("Program Start")
start = time.time()
for L in range(18): #loop for 18 times clockwise 18 counter clockwise

    # Clock wise rotation  including stops
    for i in range(2729):  # this now the number of steps for a 240 degrees rotation
        #print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
                
        time.sleep(steptime)
        halfstep = halfstep + 1
        if halfstep == 8:
            halfstep = 0

        #stops
        if i == (stopsX[stop_countX]):
            print("X Moved to", (stopsX[stop_countX]))            
            time.sleep(picdelay)
            #call to take picure
            take_picture(campic)
            campic = campic + 1
            stop_countX = stop_countX + 1
            
            
    # End of Clockwise rotation
 
    print ("End of camera clockwise rotation cycle ", cycle )
       
    # call stepper Z
    stop_countZ = cycle - 1
    Halfstep = position
    position = stepperz(Halfstep,stop_countZ)
    
    cycle = cycle + 1
    stop_countX = 5 # reset stops
    time.sleep(zstop)  
    
    # rotate back to start with stops
    for i in range(2728, -1, -1):  # this now the number of steps for the required rotation
        #print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

        time.sleep(steptime)
        halfstep = halfstep - 1
        if halfstep == -1:
            halfstep = 7

        #stops
        if i == (stopsX[stop_countX]):
            print("X moved to", (stopsX[stop_countX]))
            time.sleep(picdelay)
            #call to take picure
            take_picture(campic)
            campic = campic + 1
            stop_countX = stop_countX - 1
            
            
     

    #End of counter clockwise rotation
    print ("End of counter clockwise camera rotation cycle ", cycle)
    
    # call stepper Z
    stop_countZ = cycle - 1
    Halfstep = position
    position = stepperz(Halfstep,stop_countZ)
    
    cycle = cycle + 1
    stop_countX = 0 # reset stops
    time.sleep(zstop)  



GPIO.cleanup()

end = time.time()
runT = (end - start)
a=str(runT//3600)
b=str((runT%3600)//60)
c=str(round(((runT%3600)%60),3))
print ("End of program run it took {} hours {} mins {} seconds".format(a, b, c))
oh and about the time it takes to run once you add the code to take the pictures you may find it take even longer than 25 mins so you might need to find longer distraction tasks :mrgreen:
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 3:47 pm

Yehhhh!! It's perfect! Woooow.

Ok great, now I'll make a copy and try playing around with the camera. Thanks for the latest tips, I'll try making it with python first, as It'd be great to have all consistent and in the same script.

I'll try by myself first, so that in case I'll need to knock again, at least I'll know what we're talking about and I can be more precise asking the right questions.

I'll let you know how it goes anyway. Thanks soooo muuuuch! :mrgreen:

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 4:00 pm

I am happy it worked and does what you want.

I have enjoyed the problem solving/code writing , though I think my bald spot might be bigger now :lol:

Good luck with your project look forward to hearing how it goes. :D
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Mon Apr 20, 2020 4:08 pm

Just had a proper look to the way you wrote the code, and comments and tips. It's super clear, and understandable, plus the pics marker is also very useful to keep track if you fall asleep while waiting ahah; thanks so so so much! This is a super gift.

Will def keep you posted, can wait to put all in place and start shooting.

p.s. Go bold. It's fashionable!!

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 10:33 am

Morning Bob, sorry to bother again!

I’m trying to write the camera script but I’m not able to add a delay between the shots to camera.capture_sequence.

Starting from the script suggested at point 3.5 of the picamera library, I firstly thought about adding and indented sleep(5) # wait 5 seconds as they do below for camera.capture_continuous, but didn't work. When running, in fact, this was giving no error, but it seemed like this line had no effect.

Therefore I thought about splitting this last line in two:

Code: Select all

camera.capture_sequence(['image%02d.jpg' % i for i in range(10)])
So that I could have declared the number of pics I need to take through a for i in range, and then have a specific section for camera.capture_sequence where to declare the sleep.time between each shot.

For this latter option, I tried following Dave Jones’s answer who seems like doing something similar, or at least this looked to me like the closest approach I found around:
https://raspberrypi.stackexchange.com/q ... era-module

But didn’t manage to convert it for what I need. This is what I wrote:

Code: Select all

import time
import picamera

def outputs():
    for i in range(10): # let's start with 10 pics to try

with picamera.PiCamera() as camera:
    camera.resolution = (1920, 1080)
    time.sleep(2)
    start = time.time()
    camera.capture_sequence(outputs(), 'rgb')
    finish = time.time()
    print('Captured 10 images at %.2ffps' % (10 / (finish - start)))

for filename in camera.capture_sequence('img{counter:03d}.jpg'):
    print('Captured %s' % filename)
    sleep(3) # wait 3 seconds
My version pasted above is giving an indentation problem, but even by trying Dave Jones’s actual script, the result only prints out Captured 10 images at 56.49fps without actually shooting nor storing the pictures.

Hope this was a clear explanation? Thanks!!

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 11:04 am

I am a bit busy right now with a project of my own outside while the weather is good , will have a look at this hopefully later today.

But a couple of questions after looking at what you have posted.

You seem to be using a loop to take 10 pictures , so I am assuming you are expecting to take single pictures on each pass of the loop,
but you have a capture sequence command within the code which its self is taking 10 pictures.

So if you were saving the pictures you would end up with 100 pictures.

As I know this is to go in to the rig control program , what do you actually want ?

is it just a single picture at each camera position for each object position ?

or do you actually want 10 pictures for each camera position for each object position ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 11:22 am

Hey!!

Of course, no rush. If your outside project is somehow related to solar power my brother is a renewable energy engineer, maybe he can help somehow to give you back for all your help. If this supposition is wrong ahah never mind, just a good to know thing.

Ok then. Yes, you're right: I need to take a single picture at each camera position. So please disregard the capture sequence command you mentioned.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 1:03 pm

I do actually have solar panels on my house , but I was busy changing a CCTV camera that had failed, it had not done bad built the system 15 years ago and this was the first camera failure.

So getting back to your project.

Here is an example program using the same function call method as you main rig program.

Code: Select all

import time
import picamera
campic = 1



def take_picture(campic):
#----- copy this in to the function in the main program over writing what's there -------
    print("Take picture", campic)
    # the following line contains the path and file name that will be use to save file 
    filename = "/home/pi/insect" + str(campic) + ".jpg"
    
    with picamera.PiCamera() as camera:
        camera.resolution = (1920, 1080)
        camera.start_preview()
        time.sleep(2)
        camera.capture(filename)
        time.sleep(0.5)
        camera.stop_preview()
#--------end of copy --------------------------------------------------------------------       
    
for i in range(3): # let's start with 3 pics to try   
    
    take_picture(campic)
    time.sleep(5)
    campic = campic + 1
    
So this will take 3 pictures which you will find in the directory /home/pi ( used this as it should exist on all pi's running raspbian,
they will be called insect1.jpg, insect2jpg, insect3.jpg .

So this line in the function defines the path and the file name.

Code: Select all

filename = "/home/pi/insect" + str(campic) + ".jpg"
So you can change the path to the directory in which the file will be saved by changing this part /home/pi/

As long as the directory exists you just need to change this line to point to the directory ,

So for example if you had a directory called pictures in the default pi directory you would change path in the line to this /home/pi/pictures/

after the path comes the file name in this example I just used insect but you could use any thing you want but don't use spaces or special characters as that could cause problems.

then line then automatically adds the current picture number from our existing code and finally the .jpg extension.

So I have include 2 markers so if you want to try this in the rig program you just need to copy the code from between the markers and place it in the same function on the rig program over writing what's already there.

One final point if you want to change the path or file name you have to edit the program at the moment , but we could add some more code so it asks you for a path and file name each time the program is run.

Enjoy and let me know how you get on.... :D
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 2:38 pm

This code is goooold!

It's great! I tried merging this camera bit with the full code, and it's all perfectly synced. Amazing!

I have two questions left:

1 - to shoot a .raw picture:
Should I change this line, to something like:
camera.capture(filename, rgb)
Then change the filename line to:
filename = "/home/pi/insect" + str(campic) + ".raw"
?
but we could add some more code so it asks you for a path and file name each time the program is run.
2 - Would this prevent overwriting pictures in case I forget to move the old ones to a different folder? Because this sounds so much like something I'll be doing at some point eheh So yep, might be useful.

Lastly, do you have a website you'd be happy for me to quote in the program?

Thanks!

P.s. if you're curious, it took 0.0 hours 37.0 mins 26.248 seconds to run it all :)) It's great!!!

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 3:58 pm

As I said I am no expert on the pi camera side of things but I will read the documents and see if I can figure it out.
When I tried earlier to capture a raw image file it did not produce an image .

Simply having you enter a file name each time , as long as it was different, would stop you over writing the existing files, or you could just use the date & time when the program starts in the file name, your choice.

No I don't have a web site.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 4:57 pm

Ok great! This is something I can sort out later on and will change the name as suggested.

Thanks for everything once again. Will keep you posted! Byeee

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 5:12 pm

Do you want me to add some code so it prompts you for a new file name each time the code runs ?

I still have to read the docs on the camera and try testing what I find , not had time yet was busy cooking .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 5:29 pm

As you wish, but no rush as I need to wait for the definitive components. They should arrive next week. Thanksss

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 6:22 pm

Well I have tried both the "rgb" and "yuv" methods listed in the doc to capture raw images , both produce output file but the are either just scrambled or part images in black & while .

Now it might be the fact that I have a version 1 pi came but even looking a the document for has the same code.

So here is the code I tried may be it wil work on your set up.

Code: Select all


import time
import picamera
campic = 1

def take_picture(campic):
#----- copy this in to the function in the main program over writing whats there -------
    print("Take picture", campic)
    # the following line contains the path and file name that will be use to save file 
    filename = "/home/pi/insect" + str(campic) + ".raw"
    
    with picamera.PiCamera() as camera:
        camera.resolution = (1920, 1088)
        camera.start_preview()
        time.sleep(2)
        camera.capture(filename, 'rgb')
        time.sleep(0.5)
        camera.stop_preview()
#--------end of copy --------------------------------------------------------------------       
    
for i in range(3): # let's start with 3 pics to try   
    
    take_picture(campic)
    time.sleep(5)
    campic = campic + 1
    
if it does not work I suggest you start a new thread in the camera board sub forum , I keep an eye out for it and follow it in the hope of learning something.

will work on the stepper code to add a request for a file name on program run.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 6:54 pm

So I have added the request for a file name at program run,

You can put what you like it there within reason, as I said try and avoid spaces and special characters, if you just press return it produces a default name based on day number and current hours minutes.

code has jpg picture taking code in it so it can be tested.

Code: Select all


# final 2 stepper version 4

import RPi.GPIO as GPIO
import time
import picamera

# setup gpio
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]

for pin in control_pinsX:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0)
 
for pin in control_pinsZ:
 GPIO.setup(pin, GPIO.OUT)
 GPIO.output(pin, 0) 

# steps lists
# Stops X
stopsX = [0,546,1091,1637,2182,2728]
stop_countX = 0

# Stops Z
stopsZ = [0,114,228,342,455,569,683,797,910,1024,1138,1252,1365,1479,1593,1707,1820,1934,2048,2162,2275,2389,2503,2617,2730,2844,2958,3072,3185,3299,3413,3527,3640,3754,3868,3982,4096]
stop_countZ = 0

# gpio ouput values list
halfstep_seq = [
 [1,0,0,0],
 [1,1,0,0],
 [0,1,0,0],
 [0,1,1,0],
 [0,0,1,0],
 [0,0,1,1],
 [0,0,0,1],
 [1,0,0,1]
]

# stepper x starting halfstep value do not change
halfstep = 0

# stepper Z starting Halfstep value do not change
position = 0

# Stepper X cycles starting value
cycle = 1

# stop time after moving stepper Z you can reduce this if you want but not below 0.5
zstop = 5

# cmaera picture count start value
campic = 1

#step timer value  adjust this value higher to slow stepping of motors   typical values 0.001 0.001007
steptime = 0.001007 

# delay for picture taking stop can be reduced once you have your picture taking code added but not below 0.5
picdelay = 5

def take_picture(campic):
    print("Take picture", campic)
    # the following line contains the path and file name that will be use to save file 
    filename = "/home/pi/" + name + str(campic) + ".jpg"
    
    with picamera.PiCamera() as camera:
        camera.resolution = (1920, 1080)
        camera.start_preview()
        time.sleep(2)
        camera.capture(filename)
        time.sleep(0.5)
        camera.stop_preview()


def stepperz(Halfstep,stop_countZ):
    # Stepper Z
    start = stopsZ[stop_countZ]
    if stop_countZ > 0:
        start = start + 1
    stop = stopsZ[stop_countZ + 1] + 1
    
    for I in range(start, stop):  # this now the number of steps for one move
        #print(I)
        for pin in range(4):
            GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
            
        time.sleep(steptime) 

        Halfstep = Halfstep + 1
        if Halfstep == 8:
            Halfstep = 0


    #stop
    print("Z moved to", (stopsZ[stop_countZ + 1]))
    time.sleep(1)
    return Halfstep
    

# file name entry
print ("Program Start")
print (" ")
print ("please input a file name for this program run")
name = input ("> ")
print(" ")
if name =="":
    name = (time.strftime("%d%H%M_"))

# main program loop  
start = time.time()
for L in range(18): #loop for 18 times clockwise 18 counter clockwise

    # Clock wise rotation  including stops
    for i in range(2729):  # this now the number of steps for a 240 degrees rotation
        #print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
                
        time.sleep(steptime)
        halfstep = halfstep + 1
        if halfstep == 8:
            halfstep = 0

        #stops
        if i == (stopsX[stop_countX]):
            print("X Moved to", (stopsX[stop_countX]))            
            time.sleep(picdelay)
            #call to take picure
            take_picture(campic)
            campic = campic + 1
            stop_countX = stop_countX + 1
            
            
    # End of Clockwise rotation
 
    print ("End of camera clockwise rotation cycle ", cycle )
       
    # call stepper Z
    stop_countZ = cycle - 1
    Halfstep = position
    position = stepperz(Halfstep,stop_countZ)
    
    cycle = cycle + 1
    stop_countX = 5 # reset stops
    time.sleep(zstop)  
    
    # rotate back to start with stops
    for i in range(2728, -1, -1):  # this now the number of steps for the required rotation
        #print(i)
        for pin in range(4):
            GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])

        time.sleep(steptime)
        halfstep = halfstep - 1
        if halfstep == -1:
            halfstep = 7

        #stops
        if i == (stopsX[stop_countX]):
            print("X moved to", (stopsX[stop_countX]))
            time.sleep(picdelay)
            #call to take picure
            take_picture(campic)
            campic = campic + 1
            stop_countX = stop_countX - 1
            
            
     

    #End of counter clockwise rotation
    print ("End of counter clockwise camera rotation cycle ", cycle)
    
    # call stepper Z
    stop_countZ = cycle - 1
    Halfstep = position
    position = stepperz(Halfstep,stop_countZ)
    
    cycle = cycle + 1
    stop_countX = 0 # reset stops
    time.sleep(zstop)  



GPIO.cleanup()

end = time.time()
runT = (end - start)
a=str(runT//3600)
b=str((runT%3600)//60)
c=str(round(((runT%3600)%60),3))
print ("End of program run it took {} hours {} mins {} seconds".format(a, b, c))

Have fun :D :D
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ceciliab
Posts: 56
Joined: Thu Apr 02, 2020 2:51 pm
Location: London

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 7:00 pm

Super! Thanks x 1000

Meanwhile I tested the .raw version. It's seems working but I've no application on my pi to try open them and check how they visually look.

Tomorrow I'll start by moving the .raw pics into my mac and trying opening them with Photoshop first to see what I get.
Then I'll have a look at the two softwares I'll be using for the photogrammetry to understand what's best between rgb and yuv. I might open a thread to ask, but might have to do so in a photogrammetry forum perhaps. Will see.

Anyway, in case I find out something new I'll paste it here.
Have a great evening!!

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper 288yj-48 - stop and restart from the latest position

Tue Apr 21, 2020 7:09 pm

So I did find another format option called raw but its depreciated.

but for completeness here is a list of them all.


'jpeg' - Write a JPEG file
'png' - Write a PNG file
'gif' - Write a GIF file
'bmp' - Write a Windows bitmap file
'yuv' - Write the raw image data to a file in YUV420 format
'rgb' - Write the raw image data to a file in 24-bit RGB format
'rgba' - Write the raw image data to a file in 32-bit RGBA format
'bgr' - Write the raw image data to a file in 24-bit BGR format
'bgra' - Write the raw image data to a file in 32-bit BGRA format
'raw' - Deprecated option for raw captures; the format is taken from the deprecated raw_format attribute


So just change the last bit in the line

camera.capture(filename, "png")

and then change the file extension in this line

filename = "/home/pi/" + name + str(campic) + ".png"

I did try raw but it still did not produce a usable file/image

look forward to hearing what the answer might be... :D :D
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”