A correct example of using utime.ticks_add and utime.ticks_diff

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

A correct example of using utime.ticks_add and utime.ticks_diff

Post by ihornehrutsa » Sat Oct 10, 2020 6:49 pm

It seems to me that the official manual is not fully clear, especially for new users.

To all: Could you publish here parts of code from real projects where ticks_add and/or ticks_diff are used?

Thanks.

Divergentti
Posts: 67
Joined: Fri Sep 04, 2020 9:27 am
Location: Hanko, Finland
Contact:

Re: A correct example of using utime.ticks_add and utime.ticks_diff

Post by Divergentti » Sun Oct 11, 2020 8:27 am

Example:

Code: Select all

def check_uptime(topic, message):
    global mqtt_last_seen
    mqtt_last_seen = utime.ticks_ms()

in main loop:

client.set_callback(check_uptime)
client.subscribe("$SYS/broker/bytes/#")

...

print(utime.ticks_diff(utime.ticks_ms(), mqtt_last_seen))
time.sleep(1.1)

prints:

1108
2210
3312
4413
5515
6616
7718
8820
9921
0 (this when mqtt-message is seen)

Code is now here too: https://github.com/divergentti/kotiauto ... in/main.py

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

Re: A correct example of using utime.ticks_add and utime.ticks_diff

Post by pythoncoder » Sun Oct 11, 2020 10:48 am

Or at the REPL issue:

Code: Select all

import time
t = time.ticks_ms()
Then wait five seconds and issue

Code: Select all

print(time.ticks_diff(time.ticks_ms(), t))
and you'll see the number of ms you waited (about 5000).
Peter Hinch
Index to my micropython libraries.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: A correct example of using utime.ticks_add and utime.ticks_diff

Post by jimmo » Tue Oct 20, 2020 6:41 am

ihornehrutsa wrote:
Sat Oct 10, 2020 6:49 pm
It seems to me that the official manual is not fully clear, especially for new users.
I agree it's not the most obvious explanation of what's going on. Before I raise a bug, can you explain what you think was specifically unclear or how you'd improve it?

(i.e. is the issue that it's confusing why you need ticks_add and ticks_diff in the first place, or how to actually use it)

Ref: http://docs.micropython.org/en/latest/l ... ticks_diff

ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Re: A correct example of using utime.ticks_add and utime.ticks_diff

Post by ihornehrutsa » Wed Oct 21, 2020 8:18 pm

Thanks, Divergentti
But if your MQTT client lasts ~ 12 days(not rarely for IoT), you will get negative values unexpected for newbies.

Thanks, Peter pythoncoder
But if your code

Code: Select all

import time
t = time.ticks_ms()
time.sleep(5) # or do something
print(time.ticks_diff(time.ticks_ms(), t))
lasts ~ 12 days, you also may get negative values unexpected for newbies.

If time.ticks_us() used

Code: Select all

import time
t = time.ticks_us()
time.sleep(5) # or do something
print(time.ticks_diff(time.ticks_us(), t))
we may get negative values every 18 minutes.

Counter overflow happens.

Only one example from the http://docs.micropython.org/en/latest/l ... ticks_diff

Code: Select all

# This code snippet is not optimized
now = time.ticks_ms()
scheduled_time = task.scheduled_time()
if ticks_diff(scheduled_time, now) > 0:
    print("Too early, let's nap")
    sleep_ms(ticks_diff(scheduled_time, now))
    task.run()
elif ticks_diff(scheduled_time, now) == 0:
    print("Right at time!")
    task.run()
elif ticks_diff(scheduled_time, now) < 0:
    print("Oops, running late, tell task to run faster!")
    task.run(run_faster=true)
is strictly correct, others are not fully. :(

The question is not closed...
Thanks to all.

Post Reply