How calculate time.ticks_ms for long periods?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
plugowski
Posts: 6
Joined: Tue Apr 10, 2018 5:35 am
Location: Bangkok, Thailand
Contact:

How calculate time.ticks_ms for long periods?

Post by plugowski » Tue Apr 10, 2018 6:01 am

Hi,

I build photo slider based on ESP32 and I use PWM + sleep_ms to define distance which has to be reached by stepper motor.
The case is to show user actual distance which dolly moved, so I need to know how long actually motor works.

I use something like that (pseudo code):

[code]
freq = # calculate correct frequency for PWM
time = # time how long motor should work with frequency above
PWM.freq(freq)
PWM.init()
start_time = utime.ticks_us()
await asyncio.sleep_ms(time)
PWM.deinit()
[/code]

And there is second task which should display on oled current status:

[code]
time_elapsed = utime.ticks_diff(utime.ticks_us(), self.motor.start_time)
[/code]

The problem is that maximum value for utime.ticks_us() is 536870911 (about 13.5 minutes) and after that value has been reached, ticks_us return value -536870911...

I want to be able set slider to work about 3hrs, any idea how can I achieve that? Using simple timestamp can be not enough accurate :(

rsmith
Posts: 6
Joined: Mon Sep 25, 2017 9:36 pm

Re: How calculate time.ticks_ms for long periods?

Post by rsmith » Tue Apr 10, 2018 10:13 pm

The question is, do you really need microsecond precision here?

If millisecond precision would suffice, you could just use ticks_ms instead of ticks_us.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How calculate time.ticks_ms for long periods?

Post by Roberthh » Wed Apr 11, 2018 5:51 am

rtc.datetime() will give a us resiltion time object. It is based on the same function ticks_ms uses.
try

Code: Select all

from machine import RTC
rtc=RTC()
rtc.datetime()
It returns a tuple with (year, month, day, hour, minute, second, µs)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How calculate time.ticks_ms for long periods?

Post by pythoncoder » Wed Apr 11, 2018 6:52 am

The fact that you're creating a delay with

Code: Select all

await asyncio.sleep_ms(time)
The nature of cooperative multi-tasking limits the precision of this delay. It will be -0 +Nms where the value of N depends on the design of your coroutines. In practice, even with careful design, N is likely to be > 3, if only because of the effect of garbage collection.

So I'd seriously question whether microsecond precision is necessary here.
Peter Hinch
Index to my micropython libraries.

plugowski
Posts: 6
Joined: Tue Apr 10, 2018 5:35 am
Location: Bangkok, Thailand
Contact:

Re: How calculate time.ticks_ms for long periods?

Post by plugowski » Wed Apr 11, 2018 7:36 am

True, I don't really need microseconds precission, but if I understand documentation, TICKS_MAX is the same for us as well as for ms.

I rewrite a code using RTC, but convert tuple to microseconds costs couple milliseconds. Mistake of couple ms should be fine.

btw. tuple of rtc.datetime() is (year, month, day, WEEK, hour, minute, second, µs) :)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How calculate time.ticks_ms for long periods?

Post by Roberthh » Fri Apr 13, 2018 7:39 am

Yes, TICKS_MAX is the same for ticks_ms() and ticks_us(), but obviously the time span of TICKS_MAX milliseconds (~298 hours) is by a factor of 1000 long than TICKS_MAX microseconds (~18 Minutes); using ticks_diff() for the calculation.

plugowski
Posts: 6
Joined: Tue Apr 10, 2018 5:35 am
Location: Bangkok, Thailand
Contact:

Re: How calculate time.ticks_ms for long periods?

Post by plugowski » Wed Apr 25, 2018 5:07 pm

Okay, stupid me. You're right, and it finally works for me :D

Zolee
Posts: 14
Joined: Thu Jul 27, 2017 12:09 pm

Re: How calculate time.ticks_ms for long periods?

Post by Zolee » Sat Mar 16, 2019 7:25 am

Hi!

Can I use rtc long period? (esp32)
I want to measure the seconds in a infinity loop. Every 1 minute, i want send data to my server.
Like so: check the second in the tuple, if second=0, send the data to server.
Can this work long period?
Thanks!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How calculate time.ticks_ms for long periods?

Post by pythoncoder » Sat Mar 16, 2019 7:35 am

Code: Select all

while True:
    t = utime.ticks_ms()
    if utime.ticks_diff(utime.ticks_ms(), t) >= 60000:
        t = utime.ticks_ms()
        # get data and send to server
This will handle rollover: see https://micropython.readthedocs.io/en/l ... utime.html.
Peter Hinch
Index to my micropython libraries.

Zolee
Posts: 14
Joined: Thu Jul 27, 2017 12:09 pm

Re: How calculate time.ticks_ms for long periods?

Post by Zolee » Sat Mar 16, 2019 9:12 am

Thank you!
I want to use this, to count the impulse on electric power measure device. 10000 imp/kwh. I hope is enough fast to send the data to the server and counting precisely, with one microcontroler.

Post Reply