a495 chdk ptp - page 2 - General Discussion and Assistance - CHDK Forum  

a495 chdk ptp

  • 36 Replies
  • 8217 Views
*

Offline reyalp

  • ******
  • 12208
Re: a495 chdk ptp
« Reply #10 on: 02 / May / 2011, 22:51:39 »
Advertisements
I tried the chdkptp yesterday and it crashed the camera a couple of times, most notably when I hit the display button in the gui, plus there was no obvious way to download the photos.

I think that wrapping up the interop/direct USB stuff inside the console application and then just piping command and responses will be more reliable in the longterm.

I most definitely wasn't suggesting you use the gui. My suggestion was to use chdkptp as the console application, because the console commands are much more flexible and easy to customize. It is also much more convenient for using the script message interface.

You'd want to either customize the existing CLI (which is entirely in lua and quite easy to extend) or roll your own using the chdk.* API and chdku module.

But again, it's in a very early state so it's quite understandable if you don't want to get into that, just putting it out as an option.
Quote
lua press("shoot_half") seemed to focus the camera, but didn't focus lock, that is - it was like the button got half depressed and then released, so it was a click("shoot_half") instead.
The key is implicitly released when the script ends. If you want to hold it down for a set period of time, you need to do something like
lua press("shoot_half") ; sleep(1000) ; ...

click has a small sleep built in.

If you want to have it under under program control from the PC (press half shoot until the PC says to press full shoot), you'd probably to have a long running script that uses the message system instead.
Don't forget what the H stands for.

Re: a495 chdk ptp
« Reply #11 on: 03 / May / 2011, 03:38:20 »
Great answer, thanks reyalp - that clarifies everything.

I was hoping to be able to half-shoot and then full-shoot on PC request. We're shooting 40 cameras simultaneously, and I can do this with a multithreaded windows application and a waithandle to "release" all the threads (which will then signal the cameras via PTP/CHDK/USB).

We were using a long running script and a USB trigger wire (switching +5v) but with this approach, we have no way of getting the images off the camera in real time (well, as close to it as we can)

Can you give me any more detail on the message system? When you say long running script, do you mean a long running lua script, which I start via the CHDK PTP interface (is there some way to invoke a named .lua file?) and that registers an event handle.. and then I can notify that long running script to e.g break a loop?

It's been a crazy day and I'm only just getting down to actually looking into this again now, but I'll likely have to pick it up again when I arrive at work again in 13 or so hours

Gareth

*

Offline reyalp

  • ******
  • 12208
Re: a495 chdk ptp
« Reply #12 on: 03 / May / 2011, 12:21:45 »
Can you give me any more detail on the message system? When you say long running script, do you mean a long running lua script, which I start via the CHDK PTP interface (is there some way to invoke a named .lua file?) and that registers an event handle.. and then I can notify that long running script to e.g break a loop?
This is really documented outside of the source and some comments in the PTP thread. Should go on the wiki somehwere.

From comments in core/luascript.c
Quote
msg = read_usb_msg([timeout])
read a message from the CHDK ptp interface.
Returns the next available message as a string, or nil if no messages are available
If timeout is given and not zero, wait until a message is available or timeout expires
To send a message from ptpcam, use the putm command, or in chdkptp using chdk.write_msg. The message can be any string.
This is the camera side lua script reading a message from the PC. The opposite direction is also available:
Quote
status = write_usb_msg(msg,[timeout])
writes a message to the CHDK ptp interface
msg may be nil, boolean, number, string or table (table has some restrictions, will be converted to string)
returns true if the message was queued successfully, otherwise false
if timeout is set and not zero, wait until message is written or timeout expires
NOTE strings will not include a terminating NULL, must be handled by recipient
These can be read in ptpcam by the getm command, or in chdkptp using chdk.read_msg or in a callback in chdku.exec

You can use these in any lua script, so you could send it all with the lua command, but you can also load and run a lua file using the lua command, like this
Code: [Select]
lua loadfile('A/PATH/TO/FILE.LUA')()
Note that this loadfile()() idiom is used instead of the traditional lua require'module' because code executed within require cannot use functions that yield in our implementation.

I've attached an example script.
Don't forget what the H stands for.

Re: a495 chdk ptp
« Reply #13 on: 03 / May / 2011, 18:31:52 »
Awesome, thank you so much for the help.
I'm coding that up now.

Struggling with a weird issue where after I connect one of the cameras, the USB bus and dev numbers change around - so one of my (two, that i'm testing with) devices never connects.

If the two cameras are plugged in, they're
bus-0/\\.\libusb0-0001--0x04a9-0x31ef   0x04A9/0x31EF   Canon PowerShot A495
bus-0/\\.\libusb0-0002--0x04a9-0x31ef   0x04A9/0x31EF   Canon PowerShot A495

As soon as I start 0001 with ptpcam, I see this with another -l
bus-0/\\.\libusb0-0002--0x04a9-0x31ef   0x04A9/0x31EF   Canon PowerShot A495
bus-0/\\.\libusb0-0003--0x04a9-0x31ef   0x04A9/0x31EF   Canon PowerShot A495





Re: a495 chdk ptp
« Reply #14 on: 03 / May / 2011, 18:37:57 »
Further to this, I can't actually connect to the new bus numbers- it's like I suddenly can't connect to more than one camera with ptpcam simultaneously.
I'm about to test with the older ptpcam exe to determine if the issue is with the new executable

Re: a495 chdk ptp
« Reply #15 on: 03 / May / 2011, 19:06:12 »
Must be a camera/usb issue, the older executable does the same thing. I'll reboot and hopefully this will correct the issue.

Re: a495 chdk ptp
« Reply #16 on: 03 / May / 2011, 19:41:36 »
No dice....
So what I'm seeing is this
1) Turn both cameras on
2) Windows detects both (as libusb)
3) ptpcam -l shows both devices as dev1 and dev2
4) ptpcam --chdk --bus=bus-0 --dev=[dev1] (shortened for brevity)
5) connects
6) mode 1
7) lens extends of cam 1
8) q - quit
9) ptpcam --chdk --bus=bus-0 --dev=[dev1]
10) connects
11) mode 1
12) lens extends of cam 2
13) q - quit

Proves both cameras talk...
Then...

14) ptpcam --chdk --bus=bus-0 --dev=[dev1]
15) connects
16) mode 1
17) lens extends of cam 1
18) ptpcam  --chdk --bus=bus-0 --dev=[dev2]
19) fails to connect
20) ptpcam  --chdk --bus=bus-0 --dev=[dev1]
21) connects
22) mode 1
23) lens extends of cam 2
24) [cam1] lua shoot()
25) [cam1] error
26) [cam2] lua shoot()
27) [cam2] takes picture

What it seems like is that both cameras are trying to be dev1...

Have I missed a step when setting up libusb-win32 or installing the drivers for the device?
I can't seem to reliably connect to both cameras simultaneously


 
« Last Edit: 03 / May / 2011, 19:44:56 by agrath »

Re: a495 chdk ptp
« Reply #17 on: 04 / May / 2011, 00:45:53 »
okay, that problem was just me being an idiot.
I couldn't find it anywere but --bus and --dev are the actual numbers
that means
ptpcam --chdk --bus=0 --dev=1

I've implemented the messaging with the long running script and fleshed out my application so it will take two pictures simultaneously from both cameras using ptp over usb.

Great success.

The images get downloaded to the pc successfully.

I have two or three more problems, but most of them are work-aroundable.

1) I got an out of memory exception after about 3 cycles of shoot-delete, I think I was warned about this. The cameras are hopefully going to be unattended so really hoping this is something I'm doing wrong. I had to write a horrible lua statement to get the most recent filename for the download/delete operations:

" is escaped with \"
lua os.remove(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")]..\"/\"..os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])[#os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])])


2) I can't seem to reliably repeately shoot/cycle.. 50% of the time, either the delete fails, the filename retrieve fails or the script won't start up again after the capture

3) I have to wait an arbitary amount of time for the image to get written to the flash, there doesn't seem to be any way to know when this actually happens?


*

Offline reyalp

  • ******
  • 12208
Re: a495 chdk ptp
« Reply #18 on: 04 / May / 2011, 01:43:48 »
I've implemented the messaging with the long running script and fleshed out my application so it will take two pictures simultaneously from both cameras using ptp over usb.
Just in case it isn't obvious, only one lua script can run at a time. If you use lua... while another script is still running, it will ungracefully terminate whatever was running. I'm not sure just how ungraceful it is, but I'd avoid it. This also includes some ptpcam commands that actually just send lua.

Quote
1) I got an out of memory exception after about 3 cycles of shoot-delete, I think I was warned about this. The cameras are hopefully going to be unattended so really hoping this is something I'm doing wrong. I had to write a horrible lua statement to get the most recent filename for the download/delete operations:
Seems like you should get more than that.

Note that if you have a lua script that runs indefinitely, it can eat up all available memory when it doesn't need to, because of how the garbage collector works. You can use the collectgarbage function to query how much is used or force collection http://www.lua.org/manual/5.1/manual.html#5.1

If each script ends normally and you start a new one, this shouldn't be an issue.
Quote
" is escaped with \"
lua os.remove(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")]..\"/\"..os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])[#os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])])
This look crazy. Rather than try to figure out what it does, I'll suggest some other options
* do listdir once, and then use os.stat to check the timestamps
* as above, but use string functions on the returned names to work out the numbers and go from there
* use get_exp_count to query the cameras file counter directly.
* only allow one file at a time, and delete it before the next shot.
* as above, but in batches, e.g. shoot N shots, download them all, delete them.

Keep in mind that you have lots of images, a single os.listdir might be enough to eat up all available memory. Writing a proper directory iterator that doesn't just dump the whole thing into memory is on my todo list.
Quote
2) I can't seem to reliably repeately shoot/cycle.. 50% of the time, either the delete fails, the filename retrieve fails or the script won't start up again after the capture
There was a bug recently introduced in the "shoot()" command, maybe you are hitting this ? http://chdk.setepontos.com/index.php?topic=6351.msg66002#msg66002

Quote
3) I have to wait an arbitary amount of time for the image to get written to the flash, there doesn't seem to be any way to know when this actually happens?
Nope, not at this time.
Don't forget what the H stands for.

Re: a495 chdk ptp
« Reply #19 on: 04 / May / 2011, 18:36:57 »
I've implemented the messaging with the long running script and fleshed out my application so it will take two pictures simultaneously from both cameras using ptp over usb.
Just in case it isn't obvious, only one lua script can run at a time. If you use lua... while another script is still running, it will ungracefully terminate whatever was running. I'm not sure just how ungraceful it is, but I'd avoid it. This also includes some ptpcam commands that actually just send lua.

This was the main problem, it was pretty ungraceful. Once I ensured the long running script was shut down after the actual capture, all the crashes and memory problems went away.

1) I got an out of memory exception after about 3 cycles of shoot-delete, I think I was warned about this. The cameras are hopefully going to be unattended so really hoping this is something I'm doing wrong. I had to write a horrible lua statement to get the most recent filename for the download/delete operations:

Seems like you should get more than that.

Note that if you have a lua script that runs indefinitely, it can eat up all available memory when it doesn't need to, because of how the garbage collector works. You can use the collectgarbage function to query how much is used or force collection http://www.lua.org/manual/5.1/manual.html#5.1

If each script ends normally and you start a new one, this shouldn't be an issue.

Thanks


" is escaped with \"
lua os.remove(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")]..\"/\"..os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])[#os.listdir(\"A/DCIM/\"..os.listdir(\"A/DCIM\")[#os.listdir(\"A/DCIM\")])])
This look crazy. Rather than try to figure out what it does, I'll suggest some other options
* do listdir once, and then use os.stat to check the timestamps
* as above, but use string functions on the returned names to work out the numbers and go from there
* use get_exp_count to query the cameras file counter directly.
* only allow one file at a time, and delete it before the next shot.
* as above, but in batches, e.g. shoot N shots, download them all, delete them.


Keep in mind that you have lots of images, a single os.listdir might be enough to eat up all available memory. Writing a proper directory iterator that doesn't just dump the whole thing into memory is on my todo list.

That builds and returns a A\DCIM\IMG02___05\IMG000023.jpg path by using os.listdir and the last item in the arrays for the DCIM and the IMG????? folders.
Yes it's horrible.
I might be able to use the sequencing number get_exp_count and simplify this down, but figuring out what the IMG???? bit is will be tricky without a statement like above.

2) I can't seem to reliably repeately shoot/cycle.. 50% of the time, either the delete fails, the filename retrieve fails or the script won't start up again after the capture
There was a bug recently introduced in the "shoot()" command, maybe you are hitting this ? http://chdk.setepontos.com/index.php?topic=6351.msg66002#msg66002

Think this was the multiple script thing instead, not using shoot()

3) I have to wait an arbitary amount of time for the image to get written to the flash, there doesn't seem to be any way to know when this actually happens?
Nope, not at this time.

No worries.


To summarise, I now have 4xA495 and they're all being controlled by a bit of C# software. We're doing our final testing before purchasing the entire batch of 40.
We're using a long running lua script (based off the ptpmsg.lua script) which makes available half shutter and full shutter messages.
When a full shutter message is received it quits the script.
This script is started up over the ptp interface using loadfile, and then a message is sent using putm to lock the focus and then when a button is pressed in the winforms app, a putm capture causes the shutter to be fully depressed and the script to exit.
The most recent file is then located, downloaded and deleted from the camera and the script is started, focus lock applied ready for the next shot.
We're getting about 10 seconds per sequence, which isn't super fast but I have to introduce lots of sleeps/delays to ensure commands have executed correctly before I do the next step.
e.g. jpg saved to flash

I'm personally quite happy but as the A495 has no manual mode, there's no easy way to set the TV and AV. I tried using proptable and set_prop but I either used the wrong magic numbers (for the values) or it's not implemented in the a495 alpha chdkde firmware i'm using.
Most of the time I got an error from the camera.
Same with trying to turn the flash off with a setprop of flashmode - errors.
I *think* the iso might have worked.

Unfortunately, despite my best efforts of having an application with multiple threads and a pulseall to signal those threads to send the putm capture, we're still getting too much delay.
I can't rely on the camera timestamps, so I timestamp the files once they're received. There is 300ms between the first shot and the last shot, but not in order. It's kinda scattered.
I think the actual photos are closer, but the USB transfers (after which I timestamp the files) is causing this number to appear bigger than it actually is.
The subject's foot visibly moves though.

Anyone got any ideas how I can speed up the release at all?
We were using the wired release over USB but then there's no way of getting the photos off.
At the very least, the PTP option here gives me a way of programatically getting the photos off the camera.
We were hoping to not have to dismantle the cameras to wire the shutter button.

I'll post another update shortly with 4 jpgs resized into one square of the results of my timing tests from last night.

 

Related Topics