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?
Upgrade to ARC Pro
Get access to the latest features and updates with ARC Early Access edition. You'll have everything that's needed to unleash your robot's potential!
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?
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
Looks like Ill need to learn VS and plugins after all... Oh well, its a DIY kit right ?
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()
I created a simple plugin "Miscellaneous Utility Plugin" with 3 custom functions: Millis, Ticks, TicksToMilliseconds
Sample ez-script:
Code:
Result:
*** EDITED ***
source code:
https://github.com/ppedro74/ezrobot-playground/tree/master/plugins/MiscUtilityPlugin
Correct me if i'm incorrect
PS, awesome quick response time on getting that plugin online! Impressive!
You are correct.
For future reference:
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.
Source code updated.
They're different beasts
I will test to see if that actually works, but ironically the tests I did already showed that it probably is: if you deduct the x times 100ms rounding-up-to-integer error in each iteration of my 100ms loop test, you get believable results for measuring the execution time that vary no more than a few dozen ms, if that.
Its apparently not 15 millisecond like I read somewhere, its significantly better than that:
Code:
Result:
Indeed, who knew, it turns out you can use floats in sleep() ! I expected to see collisions at >10ms, but when 1ms still didnt trigger a collision, I just tried using fractional numbers expecting an error, but much to my surprise it actually works :). When you reduce the sleep time to below 0.6, then the loop is executed fast enough that millis may return the same time on 2 consecutive iterations and you get a print.
So millis() is indeed updated at least every ms, and the underlying datetime object possibly every 15 microseconds and someone, maybe even me, just confused µs with ms.
Whatever the underlying resolution, 1ms is WAY better than what I need, literally 1000x better than what we had, and there is no point even messing with ticks, unless I want to measure how long it takes to execute some piece of code.
Thanks again PTP!
Consider including PTPs plugin in ezb (if he allows it), I cant be the only one needing timers that are more accurate than counting out loud. And even if you dont need the sub second resolution, it is much more convenient to use to determine elapsed time compared to the existing date/time string conversions and custom functions- certainly if you dont need to compare it to the time of day or a date.
Secondly, I would still modify TotalSeconds. The problem is not datetime accuracy, its now() casting strings and the conversion to integer seconds. At the very least make TotalSeconds return an integer rather than float so people dont get confused as I did in to thinking it returns sub second results, when in reality it returns whole seconds plus a random decimal fraction.
Lastly, I mentioned the talk servo plugin. I stand by that, I would check its code, I really suspect there is a similar integer second time rounding issue lurking in there causing the lipsync to be delayed randomly by up to 1 second.
The code is open and the license is Beerware (https://en.wikipedia.org/wiki/Beerware)
No rocket science code
@DJ:
Plugin functions are only available for the desktop, adding them to the ARC core will be useful to mobile applications.
The difference is more significant for short/frequent periods.
the difference was more significant a few years ago, although the differences are relative e.g. hardware, operating system, application code optimization.
Code:
FWIW, this is what I get:
The values are relative.
I can't get less than 50 ms difference, worst case 70 ms.
The script execution: 100 times x 10 ms + script interpretation, overhead etc etc takes between 1300 - 1800 ms.
Either way, I think we can agree its not an atomic clock, but I also find it difficult to come up with EZB scenarios where a sub millisecond errors per measurement is a big problem.