supplierdeeply

Using USB remote to control chdkptp with Raspberry Pi

  • 68 Replies
  • 2763 Views
  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #60 on: 19 / April / 2017, 23:12:54 »
    Advertisements
    What do I need to fix?
    The second occurrence of get_tick_count is missing the brackets? 

    i.e. return get_tick_count() - t0
    Ported : A1200  SD940  G10  Powershot N  G16*

  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #61 on: 21 / April / 2017, 22:23:45 »
    The second occurrence of get_tick_count is missing the brackets? 

    i.e. return get_tick_count() - t0

    Ah, yep. Simple solution. Haha, thank you.


    Finding out when video started in chdkptp is easy enough, but with your current setup getting the information back to your python program may be tricky.

    To wait for video to start recording, you could use something like
    Code: [Select]
    click('video') repeat sleep(10) until get_video_status() == 4

    Is there a way for me to execute this code on the camera and then have it output something that I could return back to my Python program? What I mean is... do you believe it's possible for me to execute this program with chdk like this:
    Code: [Select]
    remote_function.py|chdkptp.sh|remote_function.py

    I realize it is a lot more tricky to do this through Python than if I were using Lua, but I'm in way too deep with everything else involved in this project. If I can get an output back from chdkptp, that would be perfect... Like as soon as my get_movie_status == 4, I could output something to my program and from there start my audio recording.

    Thanks again for all the help. Total life savers.

    *

    Offline reyalp

    • ******
    • 10700
  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #62 on: 22 / April / 2017, 00:16:08 »
    Code: [Select]
    remote_function.py|chdkptp.sh|remote_function.py
    You can't do it like that, since the second instance of the python script would be totally separate from the first. What you would need to do is use python subprocess functions to open chdkptp, with both standard input and output connected. But as I mentioned earlier, this is tricky, and my knowledge of python is minimal, so I couldn't help you without going and reading the python manuals myself.
    Quote
    If I can get an output back from chdkptp, that would be perfect... Like as soon as my get_movie_status == 4, I could output something to my program and from there start my audio recording.
    I did mention a couple of possible ways of dealing with this in my earlier post: https://chdk.setepontos.com/index.php?topic=13062.msg132426#msg132426

    The file hack is probably what I would do if I just wanted to get it done without having to figure too much out. If you can make a loop in python that checks for the existence of a file, you can make it work.  (edit: There are undoubtedly more "correct" ways to do this, but sometimes doing the thing you know how to do is the quickest solution)

    If your audio recording command just needs something standard input, using popen in from chdkptp lua instead of python should be pretty simple too.
    « Last Edit: 22 / April / 2017, 00:18:27 by reyalp »
    Don't forget what the H stands for.

  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #63 on: 27 / April / 2017, 17:27:13 »
    The file hack is probably what I would do if I just wanted to get it done without having to figure too much out. If you can make a loop in python that checks for the existence of a file, you can make it work.  (edit: There are undoubtedly more "correct" ways to do this, but sometimes doing the thing you know how to do is the quickest solution)

    If your audio recording command just needs something standard input, using popen in from chdkptp lua instead of python should be pretty simple too.

    Sorry for the delayed response here... Thank you for re-explaining. I keep underestimating what Lua can do. That file hack works perfectly. But I do have two more questions for you before I am completely done.

    First question: The file hack works great, but sometimes when I merge the audio and video, they are off by like a fraction of a millisecond... it's not the biggest deal, but if I could make that "file hack" even more precise that would be awesome. So my question is, what could I do to shrink that margin of error? I have tried messing with the "repeat sleep(10)" and making that wait time shorter and shorter. Any other thoughts?

    Second question: I am trying to automate this whole project, so as of right now I have it set up to press one button to stop recording both audio and video, and then my program takes the video that is on my camera and saves it to my Raspberry Pi. The issue with this is that if I record a longer video, it'll take a longer amount of time to transfer that file. Is there a way I can use a file hack in the same way to start audio that will create a file I can look for in Python as soon as that video has fully transferred? The reason I ask this is because I have a second button doing the audio and video merging and saving to a flash drive, but I want to be able to press just the one button to stop recording everything, and then as soon as that file is saved to my Raspberry Pi, then I can merge the files.


    *

    Offline reyalp

    • ******
    • 10700
  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #64 on: 27 / April / 2017, 21:48:38 »
    First question: The file hack works great, but sometimes when I merge the audio and video, they are off by like a fraction of a millisecond... it's not the biggest deal, but if I could make that "file hack" even more precise that would be awesome. So my question is, what could I do to shrink that margin of error? I have tried messing with the "repeat sleep(10)" and making that wait time shorter and shorter. Any other thoughts?
    Without knowing what code your are using or what the source of error is, I can't really suggest anything.
    I'm assuming "fraction of millisecond" is just a figure of speech. Have you measure the actual difference between the camera audio and your external audio?

    If you actually need that kind of precision, you'd need some totally different approach, because CHDK, sleep() only has 10 ms resolution. But the first thing to do is figure out how big the actual error is, and where it is coming from.
    Quote
    Second question: I am trying to automate this whole project, so as of right now I have it set up to press one button to stop recording both audio and video, and then my program takes the video that is on my camera and saves it to my Raspberry Pi. The issue with this is that if I record a longer video, it'll take a longer amount of time to transfer that file. Is there a way I can use a file hack in the same way to start audio that will create a file I can look for in Python as soon as that video has fully transferred?
    Sure, just send chdkptp the commands to do the download and create the file all at once, with a newline in between, like
    sys.stdout.write('imdl -d=${name}\n!fh=io.open("video_downloaded","wb") fh:close()')
    The second command won't run until the first one is finished.
    Don't forget what the H stands for.

  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #65 on: 01 / May / 2017, 15:55:18 »
    I'm assuming "fraction of millisecond" is just a figure of speech. Have you measure the actual difference between the camera audio and your external audio?

    If you actually need that kind of precision, you'd need some totally different approach, because CHDK, sleep() only has 10 ms resolution. But the first thing to do is figure out how big the actual error is, and where it is coming from.
    Sorry, I meant fraction of a second. I am using the piece of code that you wrote for me to use, but if the sleep() only has a 10 ms resolution then I am content with what is working now. I just believe that since the delay between when the button is pressed and when the camera is actually recording isn't constant, sometimes I will have a perfectly synchronized audio/video, and sometimes it is just a tad bit off. But for the sake of what I have now, it works. So just ignore that previous question. Thanks for the insight, though.

    Quote
    Sure, just send chdkptp the commands to do the download and create the file all at once, with a newline in between, like
    sys.stdout.write('imdl -d=${name}\n!fh=io.open("video_downloaded","wb") fh:close()')
    The second command won't run until the first one is finished.

    Yep, this works perfectly. Thank you. My question was definitely a simple question, and the solution makes perfect sense. Would you mind explaining what exactly
    Code: [Select]
    imdl -d=${name} does? I had been simply using
    Code: [Select]
    mdl A/DCIM/115___05 /home/pi/Video to download whatever is in the current folder on the camera to the specified /Video folder on my Pi, but your approach is 1000x better, as the name of the folder on my camera could change at any point.

    EDIT: I just realized that your approach works perfectly for downloading the video to where my chdkptp.sh program lives on my Raspberry Pi, but how would I go about using that method to download that video to a specified location?
    « Last Edit: 01 / May / 2017, 15:56:54 by smartzer »

    *

    Offline reyalp

    • ******
    • 10700
  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #66 on: 01 / May / 2017, 17:00:53 »
    Would you mind explaining what exactly
    Code: [Select]
    imdl -d=${name} does?
    imdl is specifically made for downloading image files, while mdl is for any kind of file. imdl is aware of the normal camera DCIM directory structure, and lets you do substitutions (like the ${name}) in the downloaded file path.

    If you haven't already, try
    help imdl

    If something wasn't clear from the help, let me know and I'll try to improve it.

    Quote
    EDIT: I just realized that your approach works perfectly for downloading the video to where my chdkptp.sh program lives on my Raspberry Pi, but how would I go about using that method to download that video to a specified location?
    imdl -d=/home/pi/Video/${name}

    ${name}
    is replaced with the image or video file name. Note that if you have a lot of files, the number can wrap around, so the file names might not be unique.

    If you only work with one file at a time, you could just make it go to a fixed name instead, like

    imdl -d=/home/pi/Video/video.mp4

    You might be tempted to use the video file to notify python instead of creating a separate file with io.open, but unless you have a way to verify that it's finished writing, you might end up with a partial file.

    You can use include -rm in your imdl command to remove the files from the camera after they are downloaded.
    Don't forget what the H stands for.

  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #67 on: 06 / May / 2017, 08:54:25 »
    Hey reyalp, that worked perfectly. Thank you so much. Okay I have one final question, and then I should be 100% content.

    I tried something like:
    Code: [Select]
    sys.sydout.write("=set_zoom(64)\n=press('shoot_half')")
    so that whenever I would press a button to zoom in, I would press shoot half to focus in... But the camera isn't really focusing. Is there a better approach to something like this?


    *

    Offline reyalp

    • ******
    • 10700
  • Publish
    Re: Using USB remote to control chdkptp with Raspberry Pi
    « Reply #68 on: 06 / May / 2017, 16:16:17 »
    I tried something like:
    Code: [Select]
    sys.sydout.write("=set_zoom(64)\n=press('shoot_half')")
    so that whenever I would press a button to zoom in, I would press shoot half to focus in... But the camera isn't really focusing. Is there a better approach to something like this?
    The reason this doesn't work is that CHDK script key press functions only stay in effect when the script that called them is running. The chdkptp '=' command runs one script each time you use it, so in your example, the key is pressed and released immediately.

    If you want to wait for the camera to focus, you could do something like
    =press'shoot_half' repeat sleep(10) until get_shooting()

    It's possible get_shooting() could never become true in some case, e.g. if autofocus fails, so it's safer to include a timeout like

    Code: [Select]
    =press'shoot_half' t0=get_tick_count() repeat sleep(10) until get_shooting() or get_tick_count() - t0 > 1000

    Once the script ends and half shoot is released, the camera will still try to focus again the next time you try to record video or shoot, but focus should be pretty close if the scene doesn't change much.

    If you want half shoot to stay held down until you start recording video, you have to take a different approach. The simplest way is probably to use canon "logical events" http://chdk.wikia.com/wiki/Lua/Lua_Reference/Levent, like

    Code: [Select]
    =post_levent_to_ui'PressSwOne'
    This will remain in effect until you call
    Code: [Select]
    =post_levent_to_ui'UnpressSwOne'
    To press full shoot, you'd use 'PressSwTwo'

    Note on cameras with a video button, you probably can't use click'video' to start recording while half press is held down. If your camera has dedicated video mode where you can start recording with the shutter button, you could use that.
    Don't forget what the H stands for.

     

    Related Topics