Feature Request - hardware.millis() in Agents

I noticed that the hardware class doesn’t appear to be available to the Agent code. I’d like to request that hardware.millis() (hardware.micros() would be nice too, but not required) be added to Agents. I can get close with time() but having greater accuracy on the imp just doesn’t seem fair! :wink:

It should be noted that imps have rather more exclusive use of CPU than agents; what are you trying to do with millis here?

I was using a slider to send AJAX Post requests to the imp - they were coming in fast enough to flood the imp’s memory to the point where it crashed and restarted (each device.send triggers an agent.send as a status report back which only made the issue worse).

The goal was to effectively “debounce” post requests. As I said, using time() I get centisecond accuracy which is good enough in this case and probably most cases - I just figured if centisecond accuracy was doable in the Agents then millisecond accuracy shouldn’t be too much trouble but it is a low priority request - I don’t have a specific need where I need that level of precision right now.

How are you getting centisecond resolution using agents? time() returns seconds, not centiseconds. I am trying to time how long a remote http call takes, and time() is not ideal. I’d like at least 1/10 second resolution. Is this possible?

It would be really cool if hardware.millis() started on a RTC second boundary. Or if we knew what the offset was. This way the Imp could get the absolute time of an event to the millis level. By recording both time() and millis() together. Is there any way to sync up millis() with the real world? Determine the offset?

Maybe like this? Pseudocode

was=time();
while(was==time){};
offset=hardware.millis()%1000;

I believe this would at least be consistent across different Imps. It might be off from the official time by about 100-500ms? My question:

Would time() change to the next second at the same time on different imps after they are freshly synchronized with the server? Within 10ms?

With an Arduino Uno and GPS I’m able to store sub microsecond absolute times, even with loss of GPS signal. Nearly enough to measure the speed of light! The PPS signal has 10ns jitter, that’s 10e-8 sec. Would love to see 10e-3 without GPS here?

@katmandu - I’m actually using the clock() function.
According to the Squirrel Documentation it returns a float representing the number of seconds elapsed since the start of the process (Agent boot time in this case)

Looks like clock() is not working for me on the Agent side. This ugly code:

local transactionTime = clock(); local transactionTimeS = time(); imp.sleep(3); transactionTime = clock() - transactionTime; transactionTimeS = time() - transactionTimeS; server.log(format("Server transaction time: %02fs", transactionTime)); server.log(format("Server transaction time: %ds", transactionTimeS));

Produces what I would expect on the device:
Tue Apr 02 2013 17:19:32 GMT-0700 (PDT): Server transaction time: 3.000000s Tue Apr 02 2013 17:19:32 GMT-0700 (PDT): Server transaction time: 3s

But this, which I do not understand, on the agent side:
Tue Apr 02 2013 17:20:02 GMT-0700 (PDT): Server transaction time: 0.080078s Tue Apr 02 2013 17:20:02 GMT-0700 (PDT): Server transaction time: 3s