Belgium
Asked — Edited

Comparing Times In Ezscipt

Im struggling with using time in ezscript. I want to check how long it has been since a given face was recognized. But all the time functions seem to give strings and I cant really figure out how to convert those to number and compare. Using the script help I came up this this:

Quote:

$lastfacedetection= now() ....

if ( FmtTimeSpan(CDateTime(now()) - CDateTime($lastfacedetection) ), "mm") >5)) do something endif

but that doesnt work, it tells me: Operator '-' invalid for strings.

Help?


ARC Pro

Upgrade to ARC Pro

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.

Belgium
#9  

Dj, this isnt a fundamental limitation of windows; while windows is not a RTOS, surely you can use timers that are more accurate than a full second? I dont need millisecond level assured execution, but I do need to know how much time has elapsed (partially because that time is variable because its not a rtos) and more precisely than a second.

Some googling suggests that In .Net you can use timespan to get 100 nanosecond resolution timers:

https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.7.2

Its not like I need nano second level of resolution, but a thousand milliseconds truly is an eternity and makes it impossible for me to achieve my goals... Im sure youd agree that sleep() uses milliseconds and not seconds for good reason?

Belgium
#10  

As for what I want to do; in a nutshell I want to achieve smooth and "smart" motion tracking, combining eye and neck servos. I am working on some routines that calculate an average absolute position of the tracked face, and estimate its position when the track is lost, and others that steer the head and eyes towards the target or (gu)estimated target. To make the motion smooth and to guess the position when the track is lost, I need to calculate speeds which is distance/time which really doesnt work when time can be off by as much as a full second

PRO
Synthiam
#11  

You can't use a datetime for that - as you discovered using google. As i previously mentioned, a timespan is the only way. A datetime does not provide the accuracy that fits your requirement. Consider creating a plugin that extends the capability of EZ-Script by adding a timespan command. Timespan is not a datetime.

You will probably get the accuracy you're looking for by using a timespan. A simple class that .Net provides for accurate timespan is System.Diagnostic.Stopwatch. It's good for calculating times between events - which sounds like what you're wanting to do.

Keep in mind that your calculation will not know the physical position of the servo because the real-world and software are different beasts. Instructing a servo to move into a position will take an unknown amount of time. I'm sure you thought of that:) Have fun!

Belgium
#12  

Thats why Im "stepping" the servos one position at a time, that way I also gain control over their speed.

Looks like Ill need to learn VS and plugins after all... Oh well, its a DIY kit right ?;)

PRO
Synthiam
#13  

Here's a plugin example with source code that will get you started: https://synthiam.com/redirect/legacy?table=plugin&id=162

I'd create functions that are something like...

StartTimer( ID ) Reset and start a high resolution timer with the respective ID (identifier). The identifier allows you to have multiple timers running

EndTimer( ID ) Stops and returns the timespan since StartTimer()

GetTimer( ID ) Returns the timespan since StartTimer()

PRO
USA
#14  

@Vertigo: I created a simple plugin "Miscellaneous Utility Plugin" with 3 custom functions: Millis, Ticks, TicksToMilliseconds

Sample ez-script:


#V2 
print("IsHighResolution = " + IsHighResolution())

# a long integer representing the tick counter value of the underlying timer mechanism ***EDITED***.
$start_ticks=ticks()

#The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds
$start_ms=millis()

$start_dt=now()

REPEAT ($ix, 1, 7, 1 )
  sleep(123)

  $delta_ticks=ticks()-$start_ticks
  $delta_ms=millis()-$start_ms
  $delta_dt=TotalSeconds(now()-CDateTime($start_dt))

  print("Ix               :" + $ix)
  print("dt ticks         :" + $delta_ticks)
  print("dt ms(from ticks):" + TicksToMilliseconds($delta_ticks))
  print("dt ms            :" + $delta_ms)
  print("dt seconds(ez)   :" + $delta_dt)
  print("------------------")

ENDREPEAT

Result:

User-inserted image

*** EDITED ***

source code: https://github.com/ppedro74/ezrobot-playground/tree/master/plugins/MiscUtilityPlugin

PRO
Synthiam
#15  

Are you extracting the ticks from datetime object? I don't think the datetime.now.ticks has enough precision for the OP's needs. The datetime extracted from system clock is not meant for high precision. That's the purpose of System.Diagnostic.Stopwatch

Correct me if i'm incorrect

PS, awesome quick response time on getting that plugin online! Impressive!

PRO
USA
#16  

Quote:

Correct me if i'm incorrect
You are correct.

For future reference:

Quote:

A good clock should be both precise and accurate; those are different. As the old joke goes, a stopped clock is exactly accurate twice a day, a clock a minute slow is never accurate at any time. But the clock a minute slow is always precise to the nearest minute, whereas a stopped clock has no useful precision at all.

Why should the DateTime be precise to, say a microsecond when it cannot possibly be accurate to the microsecond? Most people do not have any source for official time signals that are accurate to the microsecond. Therefore giving six digits after the decimal place of precision, the last five of which are garbage would be lying.

Remember, the purpose of DateTime is to represent a date and time. High-precision timings is not at all the purpose of DateTime; as you note, that's the purpose of StopWatch. The purpose of DateTime is to represent a date and time for purposes like displaying the current time to the user, computing the number of days until next Tuesday, and so on.

Source: Eric Lippert https://stackoverflow.com/questions/2143140/c-sharp-datetime-now-precision

Only later i noticed the OP wanted to track time (accuracy). I assumed he wanted a flat time representation for calculations.

I'll fix the plugin Ticks() will get the accuracy needed and Millis() a flat time representation.

you can calculate a delta between two Millis() values but is not accurate.