How calculate time.ticks_ms for long periods?
How calculate time.ticks_ms for long periods?
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
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
Re: How calculate time.ticks_ms for long periods?
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.
If millisecond precision would suffice, you could just use ticks_ms instead of ticks_us.
Re: How calculate time.ticks_ms for long periods?
rtc.datetime() will give a us resiltion time object. It is based on the same function ticks_ms uses.
try
It returns a tuple with (year, month, day, hour, minute, second, µs)
try
Code: Select all
from machine import RTC
rtc=RTC()
rtc.datetime()
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How calculate time.ticks_ms for long periods?
The fact that you're creating a delay with
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.
Code: Select all
await asyncio.sleep_ms(time)
So I'd seriously question whether microsecond precision is necessary here.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How calculate time.ticks_ms for long periods?
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)
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)
Re: How calculate time.ticks_ms for long periods?
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.
Re: How calculate time.ticks_ms for long periods?
Okay, stupid me. You're right, and it finally works for me
Re: How calculate time.ticks_ms for long periods?
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!
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!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How calculate time.ticks_ms for long periods?
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
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How calculate time.ticks_ms for long periods?
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.
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.