CHDK Wiki
Advertisement

Multipurpose Motion Detector

Written for/on: Powershot S3IS
Also works on:Powershot A460, A530, A550, A570, A590, A630, SD700, SD870, SD1000, S2IS, S5IS (works on A620 with AllBest build as well)
Required CHDK build: Fingalo's build 119 or Allbest

This is a script for motion detection based on the samples provided by MX3. The aim is just to make the life of people that don't know how to program easier.

Documentation/Help (save as a small "Motion Detection.txt" file to your /CHDK/SCRIPTS/ folder)


Tested on S3IS only Requires Fingalo's build v 119 Use with caution!

For better performance, turn off the shot review in camera menu. This script uses luminance variation to detect motion. The timeout is set to 600 sec. It means that if nothing is detected in 10 min, the detection "trap" is re-armed. To stop the script, press the shutter button anytime.

[ INPUTS ]

Shot (0=nf/1=f/2=c/3=t)

0 -> shoot without auto-focusing. It's like pressing the shutter button fully. Use with manual focus to improve speed.

1 -> shoot with auto-focusing. It's like pressing the shutter button half way to the camera find the proper settings and then fully to take the shot.

2 -> shoot holding the shutter button for a while. It needs to be in continuos mode.

3 -> test mode. It doesn't shoot, it just shows information on how many cells detected movement. Use it to tweak the settings.

Continuous shoot (secs)

How long the shutter button will be pressed in continuous mode (in seconds).

Threshold (0-255)

Sensitivity of motion detection. It seems the lower the value, the more sensitive it becomes.

Compare Interval (msecs)

This is the sample rate. It tests if the image changed in the given interval, if so, a motion is detected.

Compare Interval (secs)

Same as above, but for longer test time. Just to make easier to input longer times. More than 600 secs is useless due to the 600 secs timeout.

Begin Delay (secs)

This defines the time, between the start of the script or the last shoot, and the next shoot triggered by detection. (If you have trouble with your cam making continuous shoots, so increase this value!)

Pix step (speed/accuracy adj)

I'm not sure how this works. It seems the lower the value, the more sensitive it becomes.

Columns

Number of columns sensitive to motion (luminance variations) in the MD grid.

Rows

Number of rows sensitive to motion (luminance variations) in the MD grid.

Dead frame

This is the width of a frame that is not sensitive to motion. If you want that the motion is detected only when it occurs at the center of your camera display, put a value greater than zero here. Just alter the value and see how the detection grid changes in your camera display.


Script Code (save as "Motion Detection.bas" to your /CHDK/SCRIPTS/ folder)

rem Author: MLuna - based om MX3 sample script
rem Tested on S3IS only
rem Requires Fingalo's build v 119
rem Use with caution!

@title Motion Detection

rem Shot without auto-focus/with auto-focus/continously (nedd to put in continous mode manually)
rem T implies test mode with MD cells drawing and no shots taken
@param a Shot (0=nf/1=f/2=c/3=t)
@default a 1

rem How long the shutter button will be pressed in continous mode
@param b Continuos shoot (secs)
@default b 10

@param c Threshold (0-255)
@default c 5

@param d Compare Interval (msecs)
@default d 20

@param e Compare Interval (secs)
@default e 0

rem If this value is too small, the camera goes continously shooting after the 1st shot.
rem Experiment with this value to find one fitted to your needs
@param f Begin Delay (secs)
@default f 5

@param g Pix step(speed/accuracy adj)
@default g 5

@param h Columns 
@default h 6

@param i Rows 
@default i 6

rem Frame width in which no MD is performed (in cell units)
@param j Dead frame 
@default j 0

if a<0 then let a=0
if a>3 then let a=3
if c<0 then let c=0
if d<0 then let d-0
if e<0 then let e=0
if g<1 then let g=1
if h<1 then let h=1
if i<1 then let i=1
if j<0 then let j=0

rem Conversions secs to msecs
let b=b*1000
let e=e*1000
let f=f*1000

let d=d+e

rem This is the timeout in msecs. After this period, the motion trap is rearmed.
let T=600000

rem Parameters for the Dead Frame
let J=j+1
let H=h-j
let I=i-j

let t=0

print "press Shutter Button to Stop"

:repete

	md_detect_motion h, i, 1, T, d, c, 1, t, 1, J, J, H, I, 0, g, f

	if a=0 and t>0 then click "shoot_full"
	if a=1 and t>0 then shoot
	if a=2 and t>0 then goto "continuos"
	if a=3 then goto "test"

	let t=0

goto "repete"

:continuos
	let X=get_tick_count
	press "shoot_full"

	:contloop
		let U=get_tick_count
		let V=(U-X)
	if V<b then goto "contloop"

	release "shoot_full"
goto "repete"

:test
	if t>0 then print "Detected cells: ",t else print "No detection in 10 min!"
	let t=0
goto "repete"

end

--201.20.117.111

Note: I have an SD700 and I find that even when I lock focus and exposure and use mode 0 in the script, the camera still meters, as noted in this post http://chdk.setepontos.com/index.php/topic,405.msg3216.html#msg3216 . So I added

press "shoot_half"

right after the :repete line. This causes the camera to focus and meter right away before motion detection, yielding super fast snap times once motion is found. You would *not* want to do this if your application requires focusing/metering the shot when motion has occurred. I.e. to take a pic of the cat when it walks in front of the camera, you need to focus on the cat after motion is found. My change would give you only blurry cats unless you have guessed the focal distance correctly. But I am using this to catch lightning strikes, so focal length is inf., and shutter speed is locked.

--ferrix

There is just the same problem with A590, and I've solved it almost the same way:

if a=0 then press "shoot_half"

after the :repete line. Then the camera meters right before arming motion detection. You only need it when a==0, as this mode was initially intended to skip metering. This also enables you to get ultra-fast response without manual mode.

Advertisement