The goal is to operate the CHDK with motion detection and then every minute (or some interval) poll for a new image with CHDKPTP. Ideally there would be some channel/trigger between a creating new image and CHDKPTP to transfer.
You have a few options, all with some complications and some script coding required
1) Use remoteshoot like a I suggested above.
Images will be saved over USB instead of going to the SD. On many cameras, saving over USB is about as fast as saving to SD, so there would be negligible performance impact. Needs a custom script or glue script, and if anything goes wrong, images aren't backed up on the SD card. A few ports don't support remoteshoot.
2) Periodically run a chdkptp command to check for images.
The complication here is that CHDK can only run one script at a time, and listing files requires a script, so you can't just use the standard imdl or mdl commands while your MD script continues to run.
2a) You could make your MD script include a check for usb messages (
read_usb_msg) and respond with a list of files. The chdkptp side script would send a message, get back a list of filenames, and download them.
2b) Alternately, the camera side script could write a list of names to a file at some predictable location, and the chdkptp side could download that, and then download the listed images. There would be some complication avoiding downloading while the list was being updated.
2c) Have the MD script check for a quit message. Similar 2a, you'd use read_usb_msg, but on receiving the message, the MD script would end and then the chdkptp side script could download with normal commands and restart the MD script.
3) chdkptp side script wait for images to be available
3a) Make the camera side script send a message with the filename after each shot. The chdkptp side script would listen, and download each image as it became available. One complication is that there isn't a direct way to know when camera jpeg is finished saving, so you'd some logic like rlib_wait_file used by the shoot command.
3b) Make the camera side script quit when images are available to download. The chdkptp side script would check script_status until the script was not running, download the files, and restart the MD script.
#2c and #3b stop motion detection while the files are downloading. This is a disadvantage if you want to capture every possible shot, but it's possible you could run into stability issues downloading asynchronously with the shooting process as in #2a, #2b and #3a. I've done this a few times without problems, but it's hard say if it's entirely safe. Option #1 avoids this, since it redirects the normal file save.
If you want to delete files after downloading, beware that chdkptp can't delete without running a script, so #2a, #2b and #3a would need additional logic to handle that.
I can provide some code suggestions if you have a preferred approach.
My last experience with CHDK was EyeFi days and image transfers took to long over 54MBPS, speculating USB CHDKPTP transfer speed is 1MByte/SEC? maybe better?
This depends on the camera, what models(s) are you planning to use? Some very old cameras that only support USB 1 might be 1MByte/s. Modern cameras are more like 20-30.