how to make an integer calculation in chdkptp as used cam side - LUA Scripting - CHDK Forum

how to make an integer calculation in chdkptp as used cam side

  • 10 Replies
  • 2900 Views
*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Advertisements
was working on a chdkptp side interval timer that would shoot at a specific moment.
f.i. if you use 1 minute interval that it would be shot at the start of every minute.

Code: [Select]
next_shot_moment=((os.time()+secs)/secs)*secs

if this calculation is used in cam, it will always end up on a multiple of [interval] seconds.
but not with a float.
i tried squeezing imath.int in there, but that didn't work out.
prefix 0x?

any solution is appreciated :)

 
« Last Edit: 07 / July / 2022, 16:54:22 by Mlapse »
frustration is a key ingredient in progress

*

Offline reyalp

  • ******
  • 14125
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #1 on: 07 / July / 2022, 20:52:22 »
if this calculation is used in cam, it will always end up on a multiple of [interval] seconds.
but not with a float.
Not completely sure if this is what you want, but you can use util.round in chdkptp to round to the nearest integer.

The Lua math functions math.floor, math.ceil and math.modf also give you an int from a float http://www.lua.org/manual/5.2/manual.html#6.6
Don't forget what the H stands for.

*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #2 on: 08 / July / 2022, 07:03:23 »
math.floor worked for me, however not with the os.time calculation. ??? but did with sys.gettimeofday()

and then i came to realize that sys.gettimeofday() or os.time gave time not in seconds of the day, because then it would only go up to 86400.
so i am assuming this includes year, month, day in seconds as well?

is there a variable that only has the time of day in seconds? (preferrably UTC)
or is there a way to strip it of current year/date?....my best guess is something in this line:
sys.gettimeofday()-(math.floor(sys.gettimeofday()/86400)*86400)

i am assuming sys.gettimeofday() is the local time and not UTC?
 
« Last Edit: 08 / July / 2022, 07:21:36 by Mlapse »
frustration is a key ingredient in progress

*

Offline c_joerg

  • *****
  • 1251
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #3 on: 08 / July / 2022, 12:46:39 »
What about get_day_seconds()?
M100 100a, M3 121a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd

*

Offline c_joerg

  • *****
  • 1251
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #4 on: 08 / July / 2022, 14:01:50 »
That's how I use os.time

Code: [Select]
repeat
    if os.difftime(os.time(),t1)>=0 then
        t1=os.time()+interval
M100 100a, M3 121a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd

*

Offline reyalp

  • ******
  • 14125
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #5 on: 08 / July / 2022, 22:35:38 »
math.floor worked for me, however not with the os.time calculation. ???
If you can clarify what didn't work and how it failed, I might be able to help ;)
Quote
but did with sys.gettimeofday()
This exposes the (confusingly named) gettimeofday libc function. It returns two integers: A timestamp similar to os.time, followed by the microseconds fraction.

Quote
is there a variable that only has the time of day in seconds? (preferrably UTC)
On Windows and Linux, os.time and gettimeofday work in unix time, more or less.
So you if you want to know how many seconds you are into the day, you could get the unix time value for 00:00 in the current day and subtract that from the current time.

You can use *t or !*t with os.date to get a table representing the current date+time (or a specified unix time) in local or UTC time respectively. You can get a timestamp from a similarly constructed table (in local time only) using os.date.
use !return os.date('*t') to see the field names

chdku.ts_get_offset returns the local timezone offset from UTC in seconds.
Don't forget what the H stands for.

*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #6 on: 09 / July / 2022, 04:00:42 »
If you can clarify what didn't work and how it failed, I might be able to help ;)
it is not that important since it works with sys.gettimeofday()
but if you replace currentime in this code with os.time and secs with any interval (in seconds) it will give a one digit fraction in the result, if you use sys.gettimeofday() it doesn't.......in theory there is no difference between theory and real life, in real life there is.
Code: [Select]
next_shot_moment=math.floor((currenttime+secs)/secs)*secs

Quote
This exposes the (confusingly named) gettimeofday libc function. It returns two integers: A timestamp similar to os.time, followed by the microseconds fraction.
obviously doesn't do what it says on the tin :)

Quote
On Windows and Linux, os.time and gettimeofday work in unix time, more or less.
So you if you want to know how many seconds you are into the day, you could get the unix time value for 00:00 in the current day and subtract that from the current time.

UTC and UNIX time are close enough for my purpose, it would be a shame though if i would win the lottery in an UTC leap second while i live in UNIX time ;)
however, getting the unix timevalue for 00:00, i would not know how to to that other than with the 86400 division i posted hereabove and i currently use.

Quote
You can use *t or !*t with os.date to get a table representing the current date+time (or a specified unix time) in local or UTC time respectively. You can get a timestamp from a similarly constructed table (in local time only) using os.date.
use !return os.date('*t') to see the field names

chdku.ts_get_offset returns the local timezone offset from UTC in seconds.

on my list: if i can improve the calculation, but since it works for now i am going forward.
now i finally can read and understand part of multicam i am eager to add the functionality i need or want ;)

since the interval was a need for my use and more or less works, i will dive into a want.
because it's hard to check things on the fly and i did not find this in the existing code. next thing on my mind is making a csv log of the terminal output i get from multicam, either on the pc or on each seperate cam....i'm leaning towards on the cam, because if file names, cam names and timestamps are the same on both cams seperating data on the pc might pose a challenge to my skills or mind.

 
« Last Edit: 09 / July / 2022, 04:40:08 by Mlapse »
frustration is a key ingredient in progress

*

Offline reyalp

  • ******
  • 14125
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #7 on: 09 / July / 2022, 16:56:42 »

it is not that important since it works with sys.gettimeofday()
Well, the first return value of sys.gettimeofday() and the return value of os.time should identical.

Quote
but if you replace currentime in this code with os.time and secs with any interval (in seconds) it will give a one digit fraction in the result, if you use sys.gettimeofday() it doesn't.......in theory there is no difference between theory and real life, in real life there is.
Code: [Select]
next_shot_moment=math.floor((currenttime+secs)/secs)*secs
There must be something else involved
Code: [Select]
!local t1=os.time() local t2=sys.gettimeofday() local secs=86400 local r1=math.floor((t1+secs)/secs)*secs r2=math.floor((t2+secs)/secs)*secs return r1,os.date('!%c',r1),r2,os.date('!%c',r2)
=1657411200,"07/10/22 00:00:00",1657411200,"07/10/22 00:00:00"
Above is chdkptp r1179 built with Lua 5.3, but nothing relevant should have changed in chdkptp for a long time.
Don't forget what the H stands for.

*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #8 on: 09 / July / 2022, 17:32:53 »
after you helped me through that ordeal of me compiling i'm using 1167....will take a few revisions before i try that again ;)

and like you write, in theory there is no difference :) but since your code shows the calculations end up the same for now i can only assume it was a typo on my side.
btw, by using the interval code with a value of 86400 you actually end up with 00:00 next day, i'm seeing a timelapse of stargazing at midnight :)

maybe i'll figure it out when i get back to that, but math.floor should never end up with a fraction, that's the whole point isn't it?
i'm still adding functionality at this moment, so a working interval is all i need for testing. finetuning code is for later.

i'm very satisfied with the <7ms timing difference i appear to have between cams and impressed by the nice code that makes it possible, thanks. it already gave me some good looking shots.

i have some trouble that one of the 2 cams i use for this setup is a s110-103a and because that has no dual support yet it's a fair bit slower than the other s110-102b.

but since i'm still testing my software changes i feel like i'm overasking philmoz by requesting yet another tailor made build before i have made it into a working rig.
« Last Edit: 09 / July / 2022, 18:15:29 by Mlapse »
frustration is a key ingredient in progress

*

Offline reyalp

  • ******
  • 14125
Re: how to make an integer calculation in chdkptp as used cam side
« Reply #9 on: 09 / July / 2022, 19:10:51 »
after you helped me through that ordeal of me compiling i'm using 1167....will take a few revisions before i try that again ;)
FWIW, I was not suggesting you should update, just noting which version I tested.

Quote
maybe i'll figure it out when i get back to that, but math.floor should never end up with a fraction, that's the whole point isn't it?
Correct. If floor didn't return an integer value, something would be very, very wrong. IMO a bug elsewhere in your code changing the value or using the wrong variable name is more likely.
Quote
i have some trouble that one of the 2 cams i use for this setup is a s110-103a and because that has no dual support yet it's a fair bit slower than the other s110-102b.

but since i'm still testing my software changes i feel like i'm overasking philmoz by requesting yet another tailor made build before i have made it into a working rig.
Phil checked in the multipartition changes for all canon firmware versions of s110 (and g1x), so it should be available from the trunk autobuild 6109 and later.

This is the normal practice, most optional CHDK features have to be supported by all firmware subs to be enabled.
Don't forget what the H stands for.

 

Related Topics


SimplePortal © 2008-2014, SimplePortal