{"id":1187,"date":"2014-01-19T11:45:40","date_gmt":"2014-01-19T16:45:40","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code\/?p=1187"},"modified":"2014-01-19T11:45:40","modified_gmt":"2014-01-19T16:45:40","slug":"what-is-real-time-anyway","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/misc\/1187\/","title":{"rendered":"What is &#8220;Real-Time&#8221;, Anyway?"},"content":{"rendered":"<p>Recently, a colleague of mine was working on a project with an Arduino Y\u00fan that involved reading a lot of sensor data using the Y\u00fan&#8217;s Arduino processor and writing it to the microSD card via the board&#8217;s Linux processor to be served to other devices via HTTP. She found that it took anywhere from 20 milliseconds \u00a0to several seconds to get the data from the sensors written to the SD card. &#8220;Why is it not real-time?&#8221; she asked me.<\/p>\n<p>Welcome to the world of embedded operating systems. They are not realtime.\u00a0&#8220;Realtime&#8221; can mean many things. In automotive systems, for example, your car&#8217;s braking system had better react in &#8220;realtime&#8221; or you&#8217;re dead. That might mean only a couple of milliseconds. When measuring high-speed rotation, it might even mean microseconds.<\/p>\n<p>My colleague was measuring her function&#8217;s response time in tens to hundreds of milliseconds. That function read one controller&#8217;s analog input pin, sent the result via asynchronous serial to another controller, and then stored the result on an SD card. I haven\u2019t measured it, but I\u2019d wager you\u2019ll see the same response times on a BeagleBone or Raspberry Pi, or any embedded Linux board. \u00a0Here&#8217;s why:<\/p>\n<p>All computers run programs. In a microcontroller like the Arduino, there&#8217;s only one program running. That program is made up of instructions stored in memory, in a particular order. The computer moves through those instructions one at a time. Sometimes it jumps around in the<\/p>\n<p>At the electrical level, all computers are made up of transistors, so in computing, the fastest version of &#8220;realtime&#8221; means &#8220;how fast can you read and act on a changing voltage on a transistor?&#8221; Some of the input\/output pins of a microcontroller are usually <strong>hardware interrupt<\/strong> pins, meaning that they can be programmed such that if there&#8217;s a change on that pin, the program running on the controller is immediately interrupted and a special function is run. This function, called an <span style=\"color: #000000;\"><b>interrupt service routine<\/b> or\u00a0<strong>ISR<\/strong>, is typically very short. Normally an ISR simply stores the result of the interrupt in a variable. Then the processor returns to the main program. \u00a0<strong><br \/>\n<\/strong><\/span><\/p>\n<p>Operating systems, both on small boards like this and on servers and personal computers, do not guarantee a minimum response time to physical I\/O. They are written to optimize processor sharing between programs, so the scheduler, a core part of the operating system, doles out processor time to each program. Even before the programs you run get time, there are OS tasks that take time. \u00a0Disk writing is one of the most time-intensive tasks. Perhaps the only longer task is a network transaction, because the data is going through many computers, each with its own operating system.<\/p>\n<p>In systems that have to have real-time response, you typically use one of two options:<\/p>\n<p>1) no operating system. Your processor runs one program only. Like Arduino<\/p>\n<p>2) a \u201crealtime operating system\u201d or RTOS. RTOSes are stripped-down operating systems that leave many core functions out. As of yet, all RTOSes are custom packages, not very user friendly, though there has been some work lately on realtime linux. I haven\u2019t seen one running on any of the hobbyist boards, but i wouldn\u2019t be surprised if we don\u2019t see one in the next year or so.<\/p>\n<p>When we designed the Yun, we decided we\u2019d give users the benefits of both real-time and an operating system. What you do on the 32U4 (the Arduino processor) is real-time because there is no operating system. What you do on the linino side is not, because it\u2019s running linux, an operating system. The typical approach to a networked project (whether a Yun or other system) is to do all the real-time operations on a non-OS micro controller, then send the results to an operating system computer in non-real-time, after the action has happened.<\/p>\n<p>Here\u2019s a use case that illustrates the use of real-time and an operating system together:<\/p>\n<p>Let\u2019s say you\u2019re using a rotary encoder to measure the speed of a wheel on a remote control vehicle. You want to display the speed on a dashboard screen that\u2019s networked to the vehicle over Wifi.<\/p>\n<p>Rotary encoders measure rotation by counting pulses generated by a rotating shaft. When the rotation is fast, the pulses happen VERY fast, and you need real-time to catch them. On a micro controller, this is typically sensed using hardware interrupts. These are sub-circuits of the micro controller that can interrupt the program flow when a change happens on a given I\/O pin. They force the program to jump to a function to handle the interrupt. You usually want that function to be very short \u2014 typically all it does it to increment or decrement a variable counting the changes. The Arduino Uno has two hardware interrupts, and Paul Stoffregen\u2019s encoder library allows you to use them for reading encoders.<\/p>\n<p>The way you\u2019d build this app is to have the rotary encoder attached to the hardware interrupts of a micro controller. This controller is your physical I\/O controller. You\u2019d write a program for the micro controller that calculates the speed based on the pulse rate and sends that serially to another controller connected to a display. One function counts the pulses. That\u2019d be done by the function called by the interrupt (these are called Interrupt service routines, or ISRs). Another function calculates a running speed based on the changing count. A third function might control the movement of the vehicle\u2019s steering based on the value. A fourth function sends the calculated value over a serial connection to the display computer.<\/p>\n<p>The serial connection of the physical controller be connected to a networked modem like the Wifi shield or a Bluetooth radio, but that modem is just another single-function controller. That transmission takes time, and you don\u2019t want to take processor time away from counting the pulses, so your physical I\/O controller doesn\u2019t handle this transmission, it only counts pulses and sends the value on. The radio controller handles the network connection. It transmits the data on to a display computer or a server. That server is typically running an operating system, and not working in real time, but that\u2019s okay, because humans aren\u2019t going to react in more than a half-second or so most of the time. \u00a0 What you see onscreen is typically a report of the sensor readings, but it\u2019s an average or aggregated reading, not the raw, realtime reading. \u00a0The delay depends on the transmission time of the data. Perhaps you have a virtual steering wheel onscreen that then directs the card, but this is not real-time either. The user gets to steer the car, but what she&#8217;s really doing is affecting the gross movement of the steering, not the fine control over the axle. She\u2019s changing the overall balance, but the physical I\/o controller is the only part acting in real-time to the sensors.<\/p>\n<p>It is possible for a controller that\u2019s running an operating system to have hardware interrupts, and for it to have interrupt service routines. But those are typically part of the operating system kernel, not part of the user programs. You have to write custom kernel routines and restart the OS in order to use them. So unless you\u2019re a kernel programmer, you typically won\u2019t use these on BeagleBone, Raspberry Pi, or Arduino Yun. This is why there are many projects that combine the Pi or the Bone with an Arduino or other non-OS controller to do real-time sensing, and it\u2019s why the Yun has a separate processor for that as well.<\/p>\n<p>A typical personal computer is made up of several controllers, in fact. There\u2019s normally a CPU, a network controller, a controller inside any disk drive (if it\u2019s a drive with a motor), a graphics processor, a hardware bus controller, and more. These are usually tied together with synchronous serial communication like SPI or I2C or some other means of communication. The dedicated controllers handle their subsystems in realtime, and report back to the CPU, which reports in user time (not real time).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, a colleague of mine was working on a project with an Arduino Y\u00fan that involved reading a lot of sensor data using the Y\u00fan&#8217;s Arduino processor and writing it to the microSD card via the board&#8217;s Linux processor to be served to other devices via HTTP. She found that it took anywhere from 20 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/misc\/1187\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What is &#8220;Real-Time&#8221;, Anyway?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,1],"tags":[],"class_list":["post-1187","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-misc"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=1187"}],"version-history":[{"count":1,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1187\/revisions\/1204"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}