1. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Time
Measurement
Dr. Tomy Abuzairi
2. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Why should measure time ?
2
Most embedded applications (discrete system)
are related to time
• Auto power off
• How much elapsed time remaining before
sleep/shutdown
• Playing music / video (Hz/bps)
• Recorded music/video has specification on
Hz/bps, playing with different specification
make it annoying
• Smart watch (Ex: Mi-Band) for measuring
heart rate, deep sleep, step walk/bike
• Measurement shall be compared with
elapsed time
Real-time Issue
• Missile control
• A mistake in time measurement will
explode not in the right time / place
• Autonomous system (ex. Plane, tesla)
• Real-time action on each consecutive time
is required to make it autonomous
3. 30/08/23 3
D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Real Time System
• There are systems that need to respond to a service request within a certain amount
of time: they are called real-time system.
• To a real-time system, each incoming service request imposes a task (job) that is
typically associated with a real-time computing constraint, or simply called its timing
constraint.
3
4. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Timing Constraint
• The timing constraint of a task is normally specified in terms of its deadline, which is the time
instant by which its execution (or service) is required to be completed.
• A timing constraint is hard if the consequence of a missed deadline is fatal. A late response
(completion of the requested task) is useless, and sometimes totally unacceptable.
• A timing constraint is soft if the consequence of a missed deadline is undesirable but tolerable. A
late response is still useful as long as it is within some acceptable range (say, it occurs
occasionally with some acceptably low probability).
• Actual systems may have both hard and soft timing constraints. A system in which all tasks have
soft timing constraints is a soft real-time system. A system is a hard real-time system if its key
tasks have hard timing constraints.
4
5. 30/08/23 5
D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Soft Real-Time System
A soft real-time system offers
best-effort services; its
service of a request is
almost always completed
within a known finite time
(deadline)
Soft timing constraints are
typically expressed in
probabilistic or statistical
terms, such as average
performance and standard
deviation
5
6. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Study Case – Radar System
6
Silahkan Baca Textbook 1
7. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Radar System Discussion
7
Bagaimana konsep
realtimenya ?
01
Apa saja yang
harus di atur
secara real-time ?
02
Sejauh mana
kebutuhannya
bisa diakomodasi
oleh embedded
system ?
03
8. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Time Measurement in Embedded System
8
• Arduino, ESP, etc
• millis()
• micros()
• C/C++ (PC)
• gethrtime();
• Python
• from timeit import default_timer as timer
9. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Delay on Consecutive Commands
9
• Arithmetic/dummy operation in the loop (based on
execution clock)
• for(i=0; i<maxCount; i++) {}
• Internal timer
• Arduino, ESP, etc : delay(), delayMicroseconds()
• C/C++ : sleep(), usleep()
10. 30/08/23 10
D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Delay on Action for Repeated Task
while(true) {
// your program
delay
}
10
Bermasalah jika program anda membutuhkan
waktu lama sehingga total waktu setiap loop
lebih dari waktu delay
mis :
- eksekusi program 20ms, delay 50ms
- total waktu setiap loop 20+50ms
11. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Solusi Mengatasi Konsumsi Waktu Program
yang Berubah-ubah
11
t = hitung waktu
while(true) {
// your program
delay(hitung waktu - t)
t =hitung waktu
}
Delay hanya berdasarkan waktu
tersisa
12. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Timer Interrupt on Arduino
• Basically millis is based on timer
• Timer with interrupt based on timer overflow (sudah mencapai
counter yg diinginkan)
• Initialization
• Timer1.initialize(PERIODE);
• Timer1.attachInterrupt(repeated_task);
• Repeated task declaration
• void repeatedtask() {
• // your program
• }
13. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Timer Interrupt on ESP32
• The ESP32 contains two groups of hardware timers.
• Each group has two general purpose hardware timers
• ESP32 has a total of 4 timers which are numbered 0-3. These are all generic 64-bit based timers.
• Each Timer has a 16-bit Prescaler (from 2 to 65536)
• The Timer uses the processor clock to calculate the elapsed time. It is different for each microcontroller. The quartz frequency
of the ESP32 is 80MHz.
• The prescaler is used to divide the frequency of the base signal (80 MHz for an ESP32), which is then used to increment or
decrement the timer counter
• The timer simply counts the number of Tic generated by the quartz. With a quartz clocked at 80MHz, we will have 80,000,000
Tics
• By dividing the frequency of the quartz by the prescaler, we obtain the number of Tics per second
• 80,000,000 / 80 = 1,000,000 tics / sec
• timer = timerBegin(0, 80, true);
• timerAttachInterrupt(timer, &repeated_task, true);
14. D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Tugas
1. Berikan contoh cuplikan program untuk mengukur seberapa lama waktu telah terlewati
setelah mengeksekusi satu/beberapa perintah
ü Arduino : perintah kirim serial dengan minimal 200 karakter
ü C/C++ : perintah kirim tulisan ke layer dengan minimal 200 karakter
2. Berikan contoh cuplikan program untuk membuat sebuah perintah/program
tereksekusi setiap 1 detik dengan DELAY
ü Arduino : perintah kirim serial dengan minimal 200 karakter
ü C/C++ : perintah kirim tulisan ke layer dengan minimal 200 karakter
3. Berikan contoh cuplikan program untuk membuat sebuah perintah/program
tereksekusi setiap 1 detik dengan INTERRUPT
ü Arduino : perintah kirim serial dengan minimal 200 karakter
ü C/C++ : perintah kirim tulisan ke layer dengan minimal 200 karakter
15. 30/08/23 15
D E P A R T E M E N
TEKNIK ELEKTRO
Universitas
Indonesia
Selamat Belajar
15