I think I need an interrupt, but I'm not sure

wapo54001

Senior Member
I am trying to build a program & hardware that will connect to my dslr to enable it to take a series of photos for image stacking with camera focus and image spacing controlled by a Picaxe.

Hardware is a NEMA 8 motor, Sparkfun stepper controller board, and a 20X2 board I've repurposed from an LDR audio preamp.

Below is the code that I have written so far that allows me to control the stepper from a Sony remote control. I'm not happy with it because stepper movement is not smooth and continuous and at maximum speed only perhaps half as fast as it would be if I fed the motor controller a continuous series of pulses.

There are certain requirements:

1. Must have a variable step rate that gives me granular control from single-steps at one end of the range to continuous, smooth high speed stepping at the other end of the range.
2. Must keep track of the total step count (in both directions) at all times (which, I believe, eliminates hpwm as an option here) to avoid overrunning the preset minimum and maximum stepper travel points.

Since the Picaxe is not multi-tasking, I have written the program make a finite number of steps per loop with the number of steps per loop increasing as the number of continuous loops increases (with the remote button held down). Letting go of the remote button resets the number of steps per loop back to a single step. This gives me a range of speeds from a single 1/16 step per loop at the beginning of a continuous press up to some user-definable large number of steps per loop after holding the remote button down for a while.

The problem is, the stepper motor movement is jerky because it's stopping at the end of each loop no matter how long I continuously hold the remote button because the program has to exit the loop to check if the move command is still valid.

If possible, I'd like to eliminate that problem, and I'm wondering if I could use an interrupt somehow to allow me to put the stepper into a continuous loop with gradually decreasing PAUSE periods between steps to give me the same control granularity but without the momentary stopping between finite sets of pulses and have it keep running until an interrupt happens (release of remote control button) stops the loop.

This is the program that currently works, but is not as smooth-running as I would like, just wondering if I could use an interrupt in lieu of exiting the stepper loop:

Code:
#Picaxe 20X2
#No_Data
#No_Table
SETFREQ m32

Main:

'Control Program Loop
DO

'    DO    'wait for command from IR
        DO : LOOP WHILE ptr = hserptr
        Cmd_Code = @ptrinc
'    LOOP UNTIL Cmd_Code <> 255


'Action

IF Cmd_Code = "J" OR Cmd_Code = "K" THEN

    IF Cmd_Code = "J" AND Count_Total < Upper_Limit THEN
        Direction = CCW
        HIGH DirectionPIN
    ELSEIF Cmd_Code = "K" AND Count_Total > 1 THEN
        Direction = CW
        LOW DirectionPIN
    ELSE
        SEROUT C.7,Baud,(254,192," LIMIT  ")
        GOTO EndCode
    ENDIF

    'if first command or position is close to a limit then reset # steps to zero
    IF Timer > 1 THEN
        Count_Target = 0
    ELSEIF Count_Total < Lower_Slowdown AND Direction = CW THEN
        Steps_Remaining = Count_Total - 1
        Count_Target = Count_Target + 1 MAX Steps_Remaining
    ELSEIF Count_Total > Upper_Slowdown AND Direction = CCW THEN
        Steps_Remaining = Upper_Limit - Count_Total - 1
        Count_Target = Count_Target + 1 MAX Steps_Remaining
    ELSE
        Count_Target = Count_Target + 1 MAX Max_Steps  'Set number of steps for this iteration based on duration of key press
    ENDIF
    
    'Do the Stepping
    Count_Stepped = 0
    DO
        PULSOUT C.1,1
        Inc Count_Stepped
    LOOP WHILE Count_Stepped < Count_Target
    
    'Calculate current position of stepper & send information to display
    IF Direction = CCW THEN
        Count_Total = Count_Total + Count_Stepped
    ELSE
        Count_Total = Count_Total - Count_Stepped
    ENDIF   
    SEROUT C.7,Baud,(254,192,"        ",#Count_Total,"-",#Count_Stepped," ")

    'cleanup
    GOTO EndCode
ENDIF


EndCode: 'Prepare for next command and loop to beginning
Timer = 0 'Reset Timer
ptr = hserptr 'clear queued commands to prevent runon
Cmd_Code = 255 'Put null in Cmd_Code   
LOOP ' return to Main
 

Attachments

Buzby

Senior Member
My suggestion would be use a timed interrupt to do the pulsing, with the main loop doing the interface stuff.

MainLoop:
Read remote
Set countvalue
Loop

Interrupt: ( Triggered every 10mS maybe )
Get countvalue
Workout CCW/CW requirements
Send pulse if needed
Return

See 'settimer' and 'setint' in the manual.

Cheers,

Buzby
 

wapo54001

Senior Member
I've been working on a project to allow my semi-obsolete Sony A65 to efficiently take multiple images for the purpose of using the image-stacking technique to allow great in-focus depth of field photographs. This is mostly the purview of 5:1 ~ 10:1 macro photography, but my goal is to make this easy with all of my conventional lenses to be used in a conventional way -- not macro. You can't take a fully in-focus picture of nearby grass, the whole valley, and the mountains beyond with a focusing rail -- you need to turn the focus ring.

After two false starts using first a NEMA 8 stepper which was hugely under powered and then a NEMA 11 which was still under powered, I settled on a NEMA 8 stepper with a 19:1 planetary gear. This had several advantages: first, plenty of power even at 1/2 step (70% torque); second, the reduced travel of the 1/19 of a step gear output per stepper step gave me 9000 full, 18,000 half, steps per full rotation of the focus ring of my lens; third, I could remove power from the stepper to save energy between operations and the planetary kept the lens from moving without exerting considerable force; and last, I could still move the focus ring manually when needed because power was removed most of the time. A 28X2 handles the work nicely by driving a sparkfun stepper driver board.

In case someone is working on something similar, I'll offer the attached photo of the finished hardware setup. Cost of the stepper and rails and brackets was in all about $100, not including the electronics. Everything is off-the-shelf except I used three RC model gears lined up to make the wide driver gear, and attached them via a custom adapter to the 6mm "D" shaft of the planetary gear output.

I gave up on using an interrupt -- I couldn't see how to use an interrupt AND have control of stepper speed between one step and practically full speed. I can post my code if anyone would like to use it; it ain't pretty, but it works.
 

Attachments

Last edited:

hippy

Technical Support
Staff member
In applications such as this I would probably use a separate PICAXE using a HSERIN serial interface to do the stepping. That can be instructed to make a single step, keep stepping at a certain rate until stopped, etc. It can keep on doing that smoothly while the main PICAXE gets on and does something else. It could probably be done using a simple 08M2 in most case.
 
Top