supplierdeeply

chdkptp general question - returning chdkptp errorlevels?

  • 4 Replies
  • 1386 Views
*

Offline axman

  • ***
  • 143
chdkptp general question - returning chdkptp errorlevels?
« on: 15 / November / 2015, 16:04:36 »
Advertisements
Hello,

Using linux source-built chdkptp on the ixus160_elph160.  This software, as well as CHDK, is awesome.  Life-changing for my project.  :xmas

I'd like to learn more about how to best utilize exit codes (aka errorlevels) when using CHDK and chdkptp. 

I'm looking for a way to inform my process running on the PC that a chdkptp command succeeded or failed.  I suspect that it can be done (probably has already been done) with lua.  Or maybe the Pixhawk thing..  Reading more at User Contributed scripts and CHDK/LUALIB, trying to get my head around this..  Simple is better.

In my app, the PC drives.  It is good to inform the driver about command success or fail..  Getting closer to what I want by;

set -c cli_verbose=2
set -c core_verbose=1

and then testing a chdkptp command like 'remoteshoot.' 

I'm guessing the output line below indicates success with the trailing 0 - the shot *was* successful.

rc chunk get /home/foo/Pictures/IMG_0117.JPG 1 0

I could write something to parse the redirected stdout/stderr from chdkptp, looking for these kinds of exit codes, but I suspect there's a better way..   Any pointers to where I might learn more about this aspect of chdkptp use are most appreciated.

*

Offline reyalp

  • ******
  • 11543
Re: chdkptp general question - returning chdkptp errorlevels?
« Reply #1 on: 15 / November / 2015, 16:28:13 »
Unfortunately, chdkptp doesn't currently return exit codes. If you run your own Lua code in chdkptp (e.g. with !) you can use os.exit(exit code)

If you want to run cli commands (like 'remoteshoot') and check status in lua, you can use cli:execute. It returns status, followed by an error message or output from the command.

Adding support for return codes (so the exit status will be the status of the last command) is on my TODO list.

Quote
I'm guessing the output line below indicates success with the trailing 0 - the shot *was* successful.

rc chunk get /home/foo/Pictures/IMG_0117.JPG 1 0
Unfortunately, it doesn't. This line comes from chdku.lua chdku.rc_handler_store. The final number indicates the chunk it is trying to get.

rc chunk size:%d offset:%s last:%s

line should indicate whether it got the last chunk, like
Code: [Select]
rc file IMG_0028.jpg 1
rc chunk get IMG_0028.jpg 1 0
rc chunk size:8704 offset:nil last:false
rc chunk get IMG_0028.jpg 1 1
rc chunk size:1784368 offset:nil last:true
Note if you requested more than one data type (raw data, dng header, jpeg) you will get a 'last' for each one. The types are always requested in the same order.
Don't forget what the H stands for.

*

Offline axman

  • ***
  • 143
Re: chdkptp general question - returning chdkptp errorlevels?
« Reply #2 on: 15 / November / 2015, 18:47:18 »
Unfortunately, chdkptp doesn't currently return exit codes. If you run your own Lua code in chdkptp (e.g. with !) you can use os.exit(exit code)

If you want to run cli commands (like 'remoteshoot') and check status in lua, you can use cli:execute. It returns status, followed by an error message or output from the command.


Thanks very much for the help.  I think I see what you mean regarding "using cli commands (like remoteshoot) and check status in Lua."   

With the following command:
Code: [Select]
con 11> !t=cli:execute 'remoteshoot('/home/foo/Videos','-jpeg',{"shots=1"})'
The shot is successful.  Output from the cmd in chdkptp is more verbose, and appears to give a status code of 1

Code: [Select]
ERROR: call failed:[string "t=cli:execute 'remoteshoot('/home/foo/Videos'..."]:1: attempt to perform arithmetic on a boolean value
stack traceback:
[C]: in function 'xpcall'
/usr/local/lib/lua/5.2/cli.lua:654: in function </usr/local/lib/lua/5.2/cli.lua:649>
(...tail calls...)
[C]: in function 'xpcall'
/usr/local/lib/lua/5.2/cli.lua:244: in function 'execute'
/usr/local/lib/lua/5.2/cli.lua:351: in function </usr/local/lib/lua/5.2/cli.lua:342>
(...tail calls...)
/usr/local/lib/lua/5.2/main.lua:274: in main chunk
[C]: in function 'require'
[string "require('main')"]:1: in main chunk

I assume the error is related to bad Lua syntax - but did I understand you correctly about the errorlevel output I should expect to see?

Thanks again.






*

Offline reyalp

  • ******
  • 11543
Re: chdkptp general question - returning chdkptp errorlevels?
« Reply #3 on: 15 / November / 2015, 19:16:46 »
Thanks very much for the help.  I think I see what you mean regarding "using cli commands (like remoteshoot) and check status in Lua."   

With the following command:
Code: [Select]
con 11> !t=cli:execute 'remoteshoot('/home/foo/Videos','-jpeg',{"shots=1"})'
The shot is successful.  Output from the cmd in chdkptp is more verbose, and appears to give a status code of 1

I assume the error is related to bad Lua syntax - but did I understand you correctly about the errorlevel output I should expect to see?
In this case, the there was a lua error() trying to run you command, so you got a stack trace. The :1 at the very end means it was in line 1 of the code you gave to the ! command. This will pretty much always be 1, because you can only enter one line from the console.

The stuff further up the stack trace should give you a better idea of where the error happened. I'm not sure exactly what failed because the line numbers don't appear to match the current chdkptp source, but the use of cli:execute wasn't quite correct.

cli:execute expects a single string, just as you would enter on the command line. So for remoteshoot, you could do something like
Code: [Select]
!return cli:execute'remoteshoot /home/foo/Videos -jpg -shots=1'
This will display the return values.

If you want to exit chdkptp with an error code when it fails, you could do something like
Code: [Select]
!status,msg=cli:execute'remoteshoot /home/foo/Videos -jpg -shots=1'  if msg then print(msg) end if not status then os.exit(1) end
This will print any output or error message from the command, and then exit chdkptp with a status code of 1 if the command failed. It prints msg regardless of status because some cli commands return their output on success.

If you need to build the remoteshoot command with code, getting the path from a variable or something like that, you can use the string concatenation operator ..., or string.format like
Code: [Select]
!return cli:execute(string.format("remoteshoot %s -jpg -shots=%d",output_path,shot_count))
this assumes output_path and shot_count are variables set earlier

Don't forget what the H stands for.


*

Offline axman

  • ***
  • 143
Re: chdkptp general question - returning chdkptp errorlevels?
« Reply #4 on: 15 / November / 2015, 20:31:33 »
This is super-helpful, thanks very much for the guidance.

I believe I'm using chdkptp version 676 from the assembla site.  The failing lua command was me modifying what I saw in USAGE.TXT : line 613, as a quick & dirty test.  But the examples you've just provided are *exactly* what I need to go forward.  Thanks again!

 

Related Topics