Fast MD with burst/preview mode - Completed and Working Scripts - CHDK Forum supplierdeeply

Fast MD with burst/preview mode

  • 232 Replies

Offline fudgey

  • *****
  • 1705
  • a570is
Fast MD with burst/preview mode
« on: 05 / February / 2008, 17:25:12 »
Edit: go further into this thread for current versions of the script:,471.msg21328.html#msg21328
or here if you run an obsolete version of CHDK for some reason:,471.msg17797.html#msg17797


OK, here's my feeble attempt at making something useful.

We recently got some clarity into the speed of motion detect shooting, with some camera's being more picky about where the half_shoot command can be positioned than others, and we now have scripts (see,459.0.html) that seem to be able to shoot fast as a lightning, literally :)

I wanted faster MD response but not just for lightnings. I needed MD to trigger continuous mode shooting, with the first picture taken as fast as possible. Thus, I basically combined parts from Barney Fife's MD Lightning6 and MLuna's Motion Detect scripts.

My script has both single shot mode and time limited continuous shoot i.e. "burst mode", and I believe that in both modes it shoots as fast as Barney's Lightning6.

The burst mode can alternatively be used as single shoot mode post-shoot "preview", because you need to set your camera to continuous mode yourself. If you don't but still set burst length >0 s, the camera will hold your shutter
pressed down, which (at least in my camera) results in the photo being displayed on screen for about "burst length" seconds. A useful feature when you don't require ultra fast trigger delay.

In single shot mode and Burst Length 0 the script should work as as Barney's MD Lightning6 script. To use burst mode, set your camera into continuous shooting mode and increase burst length.

The "Trigger Delay" parameter is the biggest problem here, because the required amount varies greatly with camera settings. If it is too low, the script will go into an eternal shooting loop, which must be exited by pressing the shutter unless you want the camera to fill your flash card instantly. If you use automatic exposure modes, the script may end up in a loop when you're not watching, especially if the scene lighting dims gradually. Remember this when leaving the camera alone by itself.

I made some experiments about this with my A570IS.

The fastest I could get was in single mode, burst length 0 s, ISO 200, Tv 1/20 (I believe that's fast enough not to affect) in M-mode with RAW off, noise reduction off (I don't know if NR off option does anything when RAW is off) and worked down from there changing a few settings. In the table below, td is the minimum trigger delay setting I could use before the script would get stuck in a loop:

Code: [Select]
c/s BL ISO Tv   td
s   0  200 1/20 19
s   0  400 1/20 24 At ISO400 or greater, continuous mode slows down to a crawl as well!
s   0  800 1/20 24
c   3  200 1/20 38
c  12  200 1/20 38
c  12  400 1/20 38
s   0  200 4    64

The delay was not always exactly the same, for example at the c/bl=3/iso200/td=300 line td=37 did word a couple of times without a loop. Thus the settings above should not be considered safe minima.

Care must be taken in automatic shooting modes, since ISO value and especially long shutter times affect the required trigger delay greatly (look at the last line with 4 second shutter) and compare with 1/20 s shutter... 1.9 s vs 6.4 s trigger delay). Of course, shooting moving objects at slow shutter doesn't sound sane, but an automatic mode may take you there and it isn't fun to have the camera sneakily fill your card and drain your battery while you're away and not looking.

Dark frame reduction will probably increase the required delay as well, I haven't tested that at all.

Also, I had RAW turned off and a suitable fast memory (Sandisk Ultra II), changing that will probably require longer trigger delay as well.

The script really should be improved so that it if the camera is in an automatic mode, it would increase trigger delay to a safe value according to ISO and Tv.

I guess it should periodically (at least after each shoot, when it has pressed shoot_half) check the camera's chosen
shutter speed and ISO and adjust the trigger delay automatically, or maybe it could be programmed to breaking the loop by increasing trigger delay.

One thing I'm a bit puzzled about is why all these scripts work properly when x=30000 i.e. md_detect_motion has a 30 second timeout, after which it supposedly gives up... why don't we get spurious shoots every 30 seconds? Or is the MD documentation incorrect and the option really isn't milliseconds but something slower?

And here's the script (I hope I didn't make any fatal last minute typos, I didn't actually try this exact copy yet):
Code: [Select]
rem Author: fudgey - based on BarneyFive's MD Lightning6 and MLuna's Motion Detect scripts
rem Tested on A570IS, Allbest build 16
rem For bursts, set the camera in continuous shooting mode and set j>0
rem If j>0 in single shot mode, you will see your shot for j seconds on screen.
@title Fast MD with Burst/Preview 080205
@param a Columns
@default a 6
@param b Rows
@default b 4
@param c Threshold (0-255)
@default c 10
@param d Compare Interval (ms)
@default d 1
@param e Trigger Delay (0.1 sec)
@default e 42
@param f Rows to Exclude
@default f 0
@param g Pix-Step(speed/accuracy adj)
@default g 8
@param h Reg Mode(0-no,1-incl,2-excl)
@default h 0
@param i Measure Mode (1-Y,0-U,2-V)
@default i 1
@param j Burst/Preview (sec,0=off)
@default j 0
if a<1 then a=1
if b<1 then b=1
if c<0 then c=0
if g<1 then g=1
if f<1 then f=1
if j<0 then j=0
print ">[";a;",";b;"] threshold: ";c
if j>0 then goto "md_burst"
press "shoot_half"
md_detect_motion a, b, i, x, d, c, 1, t, h, 1, 1, a, f, 0, g, e
until t>0
click "shoot_full"
goto "md_singleshot"
press "shoot_half"
md_detect_motion a, b, i, x, d, c, 1, t, h, 1, 1, a, f, 0, g, e
until t>0
press "shoot_full"
let X=get_tick_count
let U=get_tick_count
let V=(U-X)
if V<j then goto "contloop"
release "shoot_full" 
goto "md_burst"
« Last Edit: 31 / January / 2009, 04:05:56 by fudgey »

Re: Fast MD with burst/preview mode
« Reply #1 on: 05 / February / 2008, 17:54:03 »
« Last Edit: 22 / April / 2008, 10:48:57 by Barney Fife »
[acseven/admin commented out: please refrain from more direct offensive language to any user. FW complaints to me] I felt it imperative to withdraw my TOTAL participation. Nobody has my permission, nor the right, to reinstate MY posts. Make-do with my quoted text in others' replies only. Bye


Offline fudgey

  • *****
  • 1705
  • a570is
Re: Fast MD with burst/preview mode
« Reply #2 on: 05 / February / 2008, 18:32:36 »
In'nerestin', I'll have to look over this when I have some time and inclination. I'm not sure what uses I'd find for this yet but I'm sure there would be. On the S series cameras the burst mode can also be engaged by button presses (see the OMNI-Intervalometer script on the Wikia pages), so it could be engaged automatically too for some cameras.

It's pretty useful for instance for shooting things that go past you quite quickly, like cars, zoomed in pedestrians, airplanes... if you don't know their speed in advance, you can't know exactly when to take the shot and taking more than one is the only option. The old scripts I could find made the a570is focus before shooting, which takes too much time, and the new scripts lacked continuous mode support.

(hey, what's this posting stuff without testing it!!! LOL ... I've done that too, looks good on paper, hoping someone else will find the problem for me. LOL )

Yea well, I only changed some of the comments and default values, it really shouldn't have broken it :D

Re: Fast MD with burst/preview mode
« Reply #3 on: 21 / February / 2008, 10:13:40 »
Very cool fudgey !

I was up to post a new thread on similar issues. I'm glad I found your post first !

I plan to use MD for wildlife photography. e.g. putting the camera (A710 for me) in front of a bird nest. Hence I can use burst mode, too. I have some questions though (chdk beginner here ... :-[ ) :

- I am somewhat concerned about battery life with MD scripts, for example if I want to leave the camera alone for the whole day. Ok it is not possible to turn the LCD off  :P. What about continuous autofocus ? Will it have a cost ?

- if a bird comes in front of the nest, will the camera focus on the "new" foreground with your script ? - seems not  :-[

- is this script compatible with AE or iso bracketing in burst mode ? - yes, great !

Some of these will be tested right now, but I would be glad to have your opinion, too.


vertigo (from france)

edited after first tests
« Last Edit: 21 / February / 2008, 10:46:31 by Vertigo »


Offline Kalli

  • **
  • 51
  • A620, SX10 IS
Re: Fast MD with burst/preview mode
« Reply #4 on: 22 / February / 2008, 02:37:47 »
Since Kamil found out about the very useful propcase 118 (count no. of shots in continous mode) writing of scripts like this one will be much easier.

Kamil's script:
Code: [Select]
@title bracketing in continous mode
@param a shoots
@default a 3

sleep 1000

press "shoot_full"
  get_prop 218 w
until w=a
set_prop 218 0
release "shoot_full"



Offline fudgey

  • *****
  • 1705
  • a570is
Re: Fast MD with burst/preview mode
« Reply #5 on: 22 / February / 2008, 13:23:42 »
- I am somewhat concerned about battery life with MD scripts, for example if I want to leave the camera alone for the whole day. Ok it is not possible to turn the LCD off  :P. What about continuous autofocus ? Will it have a cost ?

- if a bird comes in front of the nest, will the camera focus on the "new" foreground with your script ? - seems not  :-[

You can turn the LCD off by plugging something to your a/v jack (such as an empty plug or adapter or your av cable with nothing connected to the other end). The camera will think you have a television monitor connected and will shut down your LCD but MD will still work.

The camera can't be detecting motion while it's focusing. As is, the script was intended to react quickly to motion and be ready to react quickly for most of the time (I actually use x=900000 for 15 minute timeout, 30 s as in the script above). If the camera is in autofocus mode, it will focus after it has finished shooting. If you want the camera to focus before shooting, use some other script or modify this one so that the camera focuses before shooting, not before it arms motion detection. It will significantly slow down the time from detection of motion to shutter actually opening. Alternatively, you can decrease x (milliseconds) and your camera will autofocus more often, but then it will also spend more time not detecting motion.

For example MLuna's Motion Detect script in the wiki works with continous shooting mode when you set a=2 and b to the length (although I'm not sure whether it actually autofocuses in continuous mode with all camera models).

I'm not actually using this copy of the script any more, I have improved it, but I'm using a custom build with it so it's a bit pointless to publish it until those MD modifications to find their way into releases. I haven't had the time to polish a version I don't actually use.

Most importantly, the trigger time problems are pretty much gone thanks to Anonymous, by doing something like this with the script:

Code: [Select]
if j>0 then goto "md_burst"
press "shoot_half"
  md_detect_motion a, b, i, x, d, c, 1, t, h, 1, 1, a, f, 0, g, e
until t>0
release "shoot_full"
  get_prop 206 p
until p<>1
goto "md_singleshot"
press "shoot_half"
  md_detect_motion a, b, i, x, d, c, 1, t, h, 1, 1, a, f, 0, g, e
until t>0
let X=get_tick_count
let U=get_tick_count
let V=(U-X)
if V<j then goto "contloop"
release "shoot_full" 
  get_prop 206 p
until p<>1
goto "md_burst"


Offline fudgey

  • *****
  • 1705
  • a570is
Re: Fast MD with burst/preview mode
« Reply #6 on: 11 / April / 2008, 17:11:53 »
edit: Note that what you see in this post is an old version, go further in this thread for recent versions:,471.msg17797.html#msg17797

Documentation for MDFB-{VERSION}-Digic{II,III}.bas
Fast motion detector script with burst/review support and optional masking
Version 080411
by fudgey


There are two versions of the script, one for Digic II and one
for Digic III. This is because the script uses the get_prop 206
or get_prop 205 commands depending on the platform to prevent
md shoot looping.

This is my fast motion detection script. It's ment to be as fast as
currently possible (at least for my A570IS), but it also has a "slow
shoot" mode of operation. Both have their pros and cons. By "fast"
I mean the time from motion to shutter opening, a cruicial time in
things such as lightning photography.

As a direct consequence of the speed, this script will not autofocus
right before it shoots (unless it's in the slow shoot mode). In other
words this script does not focus to a moving object when one appears
before the lens. This is because focusing takes a lot of time.

If not in slow shoot mode, this script will autofocus and autoexposure
(if the camera is in automatic focus mode) when it starts and AFTER
every time it has finished shooting AND after a timeout. This means
that if an object appears in front of the camera and stays there,
moving around for a while, the camera will probably detect motion
again and you will get in-focus images too.

The speed that can be achieved is different for each camera model.
There will be significant variation from shot to shot too. Also, changing
parameters may affect speed of detection; at least if you set pixel step
too low. With my A570IS I get motion-to-shutter-opening delays between
80 ms and 150 ms, averaging at about 110 ms, but don't be surprised if
you occasionally get a significantly slower response.

This script will not change camera shooting parameters. You need to
set the camera into the shooting modes you want the script to operate
with. The camera will take images just as it would if it was you
who is keeping the shutter depressed:

  - If in normal single shooting drive mode, a single photo will be
  taken after each time motion is detected. If you set burst/review time
  to >0 seconds, the shot will be reviewed on the LCD for the set time
  before motion detection will be armed again.
  - If in continous shooting drive mode, a burst of images will be taken.
  The shooting time is set by burst/review time in seconds, it must
  be greater than 0 for more than 1 shot to be taken. No extended
  review of the last shot available. The number of shots is determined
  by your camera's shooting speed and depends on things such as
  camera model, shutter speed, ISO value, RAW mode, memory card speed,
  noise reduction setting etc. This is why even a 3 second burst may
  sometime give you no more than one photo.
  - If in custom shooting mode, images will be taken after the delay
  you have set in the camera and the AF light will blink, then 1 to 10
  shots will be taken depending on how many you've selected (your
  camera model may have a different set of parameters), and the AF
  light will blink between the shots. Note that the repetition rate is
  slower than in continous drive mode.

The script lets the camera autoexposure and autofocus unless you prevent
it. This means you can use shooting modes other than M with this script
(but not video mode). Be aware that exposure and focus are set first when
the script starts and after that only after the script shoots or timeouts.
Timeout is by default set to 5 minutes (300 seconds) so even fairly rapid
changes in sunlight will be adjusted to properly.

If you shoot fast objects, make sure focus isn't a problem (for
example by using manual focus to the distance your objects are moving
at by average and/or by shooting from a distance) and that shutter speed
is short enough.

If you shoot objects that move slowly, you can use the slow shoot option
to solve the focus problem.

This script is ment to shoot quickly after something moves on the LCD.
If you also want the script to be detecting motion again as soon as possible
after taking a shot, you should:
  - use manual exposure (camera in M mode), must be set before running the script
  - use manual focus, must be set before running the script
  - set trigger delay to the minimum allowable in your camera in your
    shooting conditions (possibly 0)
  - set burst/review time = 0 unless you want several images in
    continous shooting drive mode
  - disable noise reduction in CHDK RAW menu (if using long exposures)
  - disable RAW saving
  - use a fast, defragmented memory card
Also, it may not be a good idea to enable histogram and zebra in a way
that makes them be displayed during motion detect.



Selects the color space component to monitor for motion detection.
U, Y and V are the YUV space color components. Default is to use luminance
Y (brightness). Alternatively red, green or blue channels of RGB color
space can be used.

Columns, Rows:

Motion detect works by averaging the color space parameter
selected in the "Channel" parameter on the screen. The screen is divided
in to N=columns*rows cells, and average is calculated for each
cell. Smaller cells may make detection more sensitive to smaller object
and smaller movements. The cells are drawn on the LCD when MD
is running. Also study the Pixel Step and threshold parameters.

Pixel Step:

An average is calculated from the contents of each cell.
This pixel step parameter makes this a bit more complicated, because
if it is >1, it makes the motion detector skip pixels and only
use every Nth pixel vertically and horizontally (N is the value you give,
default is every 6th pixel).

Setting this parameter 1 is not recommended, because calculating all
pixels will take a lot of time from the camera and you might end up using
more time than the 10 ms CHDK interval. Odd things might happen. If you
must use a low pixel step value, be instructed that you may need to increase
trigger delay (explained later). Setting this  parameter too big will make
detection of small objects a matter of luck as they may or may not move
around the pixels that are being monitored.


The lower the threshold, the smaller a change in a cell will trigger
motion detection. In low light, higher threshold is required
or noise will cause spurious triggering. Along with decrease in the
number of cells, increasing threshold will make motion detect less
trigger-happy if you have problems with e.g. trees moving in the wind
causing unwanted pictures to be taken. Changing compare interval
will also affect triggering.

Compare interval (milliseconds):

Motion detect takes one sample from the display, then waits for
compare interval until taking another one and then compares these two.
The default is sub-minimal to optimize speed of detection.

If interval is low enough, objects that move slowly or gradual
changes in the amount of sunlight will not trigger motion detection
no matter how big the object or change in light is.

Note that while you can specify an interval as low as 1 ms, only
multiplies of 10 ms will actually have any effect as this is the
finest time step available for motion detection (it comes from the
camera's keyboard polling interval). And even further, 30 ms may be
the shortest you can go if/when the LCD update period is 30 ms.
So with intervals shorter 10 ms, the motion detector is probably
comparing identical pictures two times out of three but it is also
working at maximum available speed of detection because an LCD update
will be noticed during the first CHDK period after the update.

Trigger delay:

How long to wait for camera to stabilize before starting to detect
motion. A low value means the camera will be detecting motion again
promptly after taking a picture. A large value means that after
your shooting (and picture review) the motion detector will be
started and the motion detector grid will be drawn, but it will
not react to any movement until trigger delay has passed. This may
be useful for making sure you don't get too many images.

In past a long trigger delay was needed to prevent motion detector
scripts from entering eternal shooting loops, where the camera's
post-shooting actions (such as adjusting LCD contrast) caused
motion detect to trigger instantly. Increasing trigger delay
helped, but the problem was that a lot of things affected the
length of the required delay (slow shutter speeds, ISO values,
noise reduction setting, memory card speed, RAW saving, low light
to name a few).

This has now mostly been fixed with a few get_prop 206
(or 205) commands and the default trigger delay is 0.5 s. If
you experience loop anyway, try increasing trigger delay.

Burst/Review time:

If 0, take single shots. If >0, how many seconds to keep shutter
depressed. If continuous shooting mode is enabled ("burst mode"),
the camera will shoot using this mode, if it is not enabled, the
camera will show the shot review for the specified time on LCD
(unless your camera doesn't work this way or is configured in a way
that disables this review feature; review by holding the shutter
is a standard feature of your camera when it's in single shot
drive mode).

The script does not switch to continuous shooting mode, the user
must do this before running the script from the camera's own


This is the timeout value for the motion detector. Most of the time
this script sits in the motion detection function waiting for
motion. It exits this function only if motion is detected or
enough time passes (timeout). This timeout is specified in
tens of seconds (default is 30 == 300 seconds == 5 minutes).

The longer the timeout, the more of the script's time will be
spent in detecting motion. However, if you are not running in
the "Slow Shoot" mode, and you have autoexposure or autofocus
enabled, you may want to let the camera do those things every
now and then. The default of 5 minutes should be short enough
to adapt to changes in sunlight but long enough for almost
perfect coverage in time spent detecting motion (if autoexposure
and focus takes 3 seconds, this is 99 % coverage).

Slow Shoot:

When Slow Shoot is disabled, this script acts as a fast motion
detector as described above. This means it first half presses
the shutter (causing autoexposure and autofocus if those are
currently enabled in your camera) and fully presses the shutter
when it detects motion. This way the picture is taken very fast
after motion is detected, but the problem is that many objects
will be out of focus.

If you enable slow shoot, the script will go into motion
detection without pressing the shutter. When motion is detected,
the shutter is pressed and thus exposure and focus are set
and only when that's done with, a the picture is taken by
the camera.


Using the four Mask/Use Columns/Rows Left/Right/Top/Bottom
parameters you can select any rectangular area on your LCD using
your MD cell grid as the resolution of this rectangle. You can
then set the motion detector to ignore everything inside that
rectangular mask (Masking=1, Mask) or to use only the cells in your
mask for motion detection (Masking=2, Use). To disable masking
entirely, set Masking=0 (No).

The green motion detection grid is always up to date with the
masking settings, so it's easy to see if you are getting the mask
you wanted (btw, this masking part was shamelessly copied directly
from Barney Fife's 'MD LightningB3' script).


Note: To conserve batteries, you may plug an A/V cable (any plug
or adapter that fits the jack will do, no need to have any
cables or equipment connected) to your A/V jack. What will
happen is that your LCD will turn off saving significant amounts
of battery power. Motion detection still works, because
the camera thinks you have an external TV monitor connected
and will update it's viewfinder image (which is what MD uses
for detection). Unplug to get your LCD image back (may not
be instant and you may need to interrupt the script, but there
should be no need to reboot the camera).

Note: This script is endless and will never end. This is not
good script coding practice, but checking for button presses
would take precious time from sitting in the motion detection

You can interrupt the script (like any script) by full-pressing
shutter. No camera settings are changed (I think), especially
not if you interrupt while MD is waiting for motion.

Technical note: md_detect_mode "PARAMETERS" argument is always 9.
This means  "make immediate shoot" and the new feature, "no shutter
release on shoot" are enabled. In other words, md_detect_motion
presses the shutter and the script releases it.

Note: It appears that you can turn the mode dial while running
this script... so you can start in M modes with finely tuned
parameters and then try an automatic mode for a while and go
back to M mode without ever interrupting the script.


Thanks go to MX3 for md_detect_motion, Barney Fife for masking
code among other things, Anonymous for solving trigger delay loop
problems and all the other lovely CHDK developers.

Obviously, a standard disclaimer applies i.e. while I believe this script
to be as safe as any part of CHDK, there is no warranty and if this
breaks your camera or your marriage it is of your own responsibility
entirely. In that case you may feel better if you blame the cat.


- Testing. Things I haven't tested much or at all:
    * any camera other than a570is (this is a Digic III model)
    * slow mode hasn't been tested much, especially not for trigger delay
      loops. The current code is almost just a quick guess in that context.
- add option for number of continuous drive mode shoots to take using
  the new commands for this (are they already in allbest or was it just propcase?).
  Somehow avoid getting stuck if we're not in continous drive mode (propcase
  may be used?)
- possibly make an option for not filling the card using the new disk space functions?
  Or maybe an "exit after N*10 photos"? Would be a good safety option when
  leaving the camera alone and fearing it may start triggering too often.
  Maybe even shut down camera at exit.
- think about how to modify the script for video mode. MD doesn't seem to run
  but modes dial changes have been implemented for a570is already (not in trunk
  yet?) burst/review time could be burst/review/video time and slow on/off could
  be fast/slow/video.
  The current dialmode patch can only change mode, not poll (or is that already
  possible using a propcase?). That's why video switching will change dialmode
  and can't know what to return to... I guess it should (for now) use P mode
  (dm_p) for motion detection and click (or press wait release) dm_video...

« Last Edit: 17 / July / 2008, 14:48:54 by fudgey »

Re: Fast MD with burst/preview mode
« Reply #7 on: 11 / April / 2008, 17:33:39 »
« Last Edit: 22 / April / 2008, 10:49:31 by Barney Fife »
[acseven/admin commented out: please refrain from more direct offensive language to any user. FW complaints to me] I felt it imperative to withdraw my TOTAL participation. Nobody has my permission, nor the right, to reinstate MY posts. Make-do with my quoted text in others' replies only. Bye


Offline PhyrePhoX

  • *****
  • 2254
  • make RAW not WAR
    • PhyreWorX
Re: Fast MD with burst/preview mode
« Reply #8 on: 11 / April / 2008, 23:25:06 »
cool. will try it this morning :)

Re: Fast MD with burst/preview mode
« Reply #9 on: 12 / April / 2008, 16:11:06 »
Thanks to all of you! That's the motion detection I was dreaming of! Everything is there!


Related Topics

SimplePortal 2.3.6 © 2008-2014, SimplePortal