{"id":679,"date":"2017-02-06T20:01:00","date_gmt":"2017-02-07T01:01:00","guid":{"rendered":"http:\/\/sunapi386.ca\/wordpress\/?p=679"},"modified":"2023-02-07T17:27:23","modified_gmt":"2023-02-07T22:27:23","slug":"lessons-from-real-time-programming-class","status":"publish","type":"post","link":"https:\/\/sunapi386.ca\/wordpress\/lessons-from-real-time-programming-class\/","title":{"rendered":"Lessons from Real-Time Programming class"},"content":{"rendered":"\n<p id=\"4005\">This post is about lessons I learnt in&nbsp;<a href=\"http:\/\/www.cgl.uwaterloo.ca\/~wmcowan\/teaching\/cs452\" rel=\"noreferrer noopener\" target=\"_blank\">CS452 Real-Time Programming<\/a>, a 4th year undergraduate class at the University of Waterloo. Better known as \u201ctrains\u201d or \u201crealtime\u201d class.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"14d7\">\u201cEasy stuff! You play with trains all day (literally, maybe even night). All you need to do is write one simple program to control multiple trains so they don\u2019t crash!\u201d<\/p>\n\n\n\n<p id=\"e50b\">\u2014 chko on&nbsp;<a href=\"https:\/\/birdcourses.com\/course.php?courseid=144\" rel=\"noreferrer noopener\" target=\"_blank\">BirdCourses.com<\/a><\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*gaxGu5bLYzy5u_4ZS4a2uw.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The trains lab. Room 3018, 3rd floor Math &amp; Computer building.<\/figcaption><\/figure>\n\n\n\n<p id=\"8f8f\">The class is a bit unconventional because it indirectly teaches other topics, like time management and teamwork, by providing a large amount of work \u2014 so that you have to learn these skills. I\u2019ll talk about these lessons, hoping to address a more general audience.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"8577\">History<\/h1>\n\n\n\n<p id=\"d94d\">This class has been around since at least the 80\u2019s. Currently&nbsp;<a href=\"http:\/\/www.cgl.uwaterloo.ca\/wmcowan\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">Bill Cowan<\/a>&nbsp;teaches this class, and has been for over 20 years.<\/p>\n\n\n\n<p id=\"c504\"><em>The equipment have evolved since then, but the underlying challenges have not.<\/em><\/p>\n\n\n\n<p id=\"c7d0\">For example, teamwork, setting priorities, and dealing with real (imperfect) systems.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*Fxb_Ko7T3ilA4U2uVacVRw.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Prof. Bill Cowan explains the why sensor pickup sometimes have trouble detecting slow moving trains. The gap between the contact points may cause the pickup switch to flip twice.<\/figcaption><\/figure>\n\n\n\n<p id=\"1523\">The early version of the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/QNX\" rel=\"noreferrer noopener\" target=\"_blank\">QNX real-time operating system<\/a>&nbsp;took off from a pair of students taking this class, Gordon Bell and Dan Dodge. They decided to&nbsp;<a href=\"http:\/\/www.qnx.com\/developers\/docs\/660\/index.jsp?topic=%2Fcom.qnx.doc.neutrino.getting_started%2Ftopic%2Fpreface_History.html\" rel=\"noreferrer noopener\" target=\"_blank\">expand the class project into a full-fledged commercial RTOS<\/a>&nbsp;(real time operating system).<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"ddc9\">\u201cYou should try to find a partner for this class, it\u2019ll be hard without one. Generally I\u2019ve seen two outcomes:<br>(1) you become friends with your partner, or<br>(2) you stop being friends and hate each other.<br>I recommend not starting with a partner who is your friend, because you are already friends. Don\u2019t risk it.\u201d<\/p>\n\n\n\n<p id=\"ab8a\">\u2014 Prof. Bill Cowan<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"da30\">Curriculum<\/h2>\n\n\n\n<p id=\"689f\">About 65% of the class involves building a small embedded kernel, with the other 35% working on the train management application.<\/p>\n\n\n\n<p id=\"7323\">You can choose to program in whatever language you want, because there aren\u2019t any standard libraries provided. People generally program in C, because it\u2019s known as a systems language, and the programming examples given in class. But there\u2019s no reason why you can\u2019t do it in Rust language, for example \u2014 although godspeed, you\u2019re venturing into uncharted territory!<\/p>\n\n\n\n<p id=\"990d\">The kernel runs on a&nbsp;<a href=\"https:\/\/www.embeddedarm.com\/products\/TS-7200\" rel=\"noreferrer noopener\" target=\"_blank\">TS-7200 embedded system<\/a>, and it talks to a&nbsp;<a href=\"https:\/\/www.student.cs.uwaterloo.ca\/~cs452\/manual\/trains.html\" rel=\"noreferrer noopener\" target=\"_blank\">M\u00e4rklin Train System<\/a>&nbsp;over RS232 type serial. The firmware is loaded on via&nbsp;<a href=\"https:\/\/sourceware.org\/redboot\/\" rel=\"noreferrer noopener\" target=\"_blank\">RedBoot<\/a>, a bootstrap environment for embedded systems.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*YAnG6Y1Nzgabz3-xiEE3gQ.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The&nbsp;<a href=\"https:\/\/www.student.cs.uwaterloo.ca\/~cs452\/manual\/trains.html\" rel=\"noreferrer noopener\" target=\"_blank\">M\u00e4rklin Train System<\/a>. You can see the center console where the RS232 is attached. Off to the right is the grey box that houses the TS-7200.<\/figcaption><\/figure>\n\n\n\n<p id=\"afdf\">The final exam is a take home 24 hour long exam, where you can use any resources you want, and you only need to do 3 of the 5 questions. The assignments and kernel make up 70% of the grade.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f108\">Optional Technical Info<\/h2>\n\n\n\n<p id=\"12bc\">The microkernel did very little; it<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>only kickstarts a couple of initial processes.<\/li>\n\n\n\n<li>handled message passing between processes.<\/li>\n\n\n\n<li>provided very basic interrupt handler and clock.<\/li>\n\n\n\n<li>supports realtime operations; the scheduler is predictable (through priorities, and pre-emption).<\/li>\n\n\n\n<li>avoided disk IO by loading kernel and programs into RAM directly.<\/li>\n\n\n\n<li>needed to provide serial output to a PC terminal, to display graphics relating to the real-time actions being controlled, such as displaying position of trains on the track showing movement.<\/li>\n<\/ul>\n\n\n\n<p id=\"f808\">The realtime program is probably more work than the kernel. Because initially there were many constraints, but later on, assignments were of more guidelines than objectives.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"5712\">Lessons<\/h1>\n\n\n\n<p id=\"0806\">I\u2019ll talk about some of the lessons I learnt, and provide some reasoning or backstory to them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7956\">Don\u2019t trust anything. Don\u2019t make any assumptions.<\/h2>\n\n\n\n<p id=\"cf4a\">One assumption that was made was while building a acceleration and deceleration profile for the train \u2014 that the train would stop where we expected to. Calibration was done by running the train at full speed around the tracks, and when it hits a sensor, tell it to stop.<\/p>\n\n\n\n<p id=\"fab1\">That assumption turned out false: at max speed, the train varies its stopping distance quite a bit, up to \u00b13 cm. Not only that, the train accelerates and decelerates non-linearly!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*kVf9s0MoRdLr7rlr3YDkfQ.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Real systems are often imprecise. For example, look at the stopping distance of a train. In our experiments, it never stops in the same place \u2014 for a variety of reasons: such as buffering, interrupt handling, incorrect system timing.<\/figcaption><\/figure>\n\n\n\n<p id=\"31d3\">This meant that over the course of several start and stop move commands, there can accumulate quite a difference between where you&nbsp;<em>think<\/em>&nbsp;the train is, and where it&nbsp;<em>actually<\/em>&nbsp;is.<\/p>\n\n\n\n<p id=\"8747\">Consequently, our model couldn\u2019t be trusted. We\u2019d assume it was safe to perform a certain operation, and it of course, wouldn\u2019t be. And that causes accidents like multi-track drifting to occur.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*KqvqmDz_al4zmcAQ9sNXMA.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Multi-track drifting. Happens when a track switch flips direction while the train has yet to clear both axles.<\/figcaption><\/figure>\n\n\n\n<p id=\"df56\"><a href=\"http:\/\/knowyourmeme.com\/memes\/multi-track-drifting\" rel=\"noreferrer noopener\" target=\"_blank\">Multi-track<\/a>&nbsp;drifting is when you thought it was safe to flip a switch, and then it wasn\u2019t. The poor train comes to a sudden stop because the front axles and rear axles are going to different directions.<\/p>\n\n\n\n<p id=\"5d3a\">When your model of the world and the real world itself are different, you get problems. We couldn\u2019t trust our model. We\u2019d assume it was safe to perform a certain operation, thinking we knew where the train was, and it wasn\u2019t safe.<\/p>\n\n\n\n<p id=\"c9b9\">This explains the comic that is pasted on the walls beside the train set. Now I get it!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*iD5f7uqzhqCx_sED5B5G7A.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A printed out version of this comic was taped to the wall beside the train tracks. The original multi-track drifting.<\/figcaption><\/figure>\n\n\n\n<p id=\"4e38\">If this was a real train, this would result it some serious accidents. A lesson we learnt after trying to figure out how this happened.<\/p>\n\n\n\n<p id=\"8737\"><em>Assumptions are going to be wrong! So it\u2019s important to build in error correction and recovery.<\/em><\/p>\n\n\n\n<p id=\"87db\">Often assumptions are wrong, or become wrong during the course of real life usage. Real systems don\u2019t always run in your ideal situations. You can\u2019t always think of all the edge-cases. Errors are going to happen. Your best chance is to try to realize the error as early as possible and correct for it.<\/p>\n\n\n\n<p id=\"4dde\">Instead of eagerly switching tracks for the next train, we switched tracks only if the train had ownership of the track.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*jWZ66KYCz7qox4P-2u-t4Q.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Map of the train switches and segments. We had two tracks, A and B, and they have different configurations. We hard coded the map to build the train track reservation system.<\/figcaption><\/figure>\n\n\n\n<p id=\"bdbe\">To solve this, we ended up building in margin of error around where we think the train is, assigned the train to have ownership to tracks, and made sure when trains approach tracks that they didn\u2019t own, it would stop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7a76\">You are as only good as your tools.<\/h2>\n\n\n\n<p id=\"9bab\">Building the path\/route finding portion of the project, we were manually reading a map and trying to reason about what the shortest ways were. This took a long time, and felt complicated.<\/p>\n\n\n\n<p id=\"c23a\">Route a train from point&nbsp;<em>A<\/em>&nbsp;to point&nbsp;<em>B&nbsp;<\/em>was not difficult, but factoring in what paths are currently, or will be, blocked by other trains made it harder. We often stared at the layout map for a long time, trying to figure out whether this was the quickest path through this or not.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*nlK8hZTX3uooqq93h_5eeQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">This is the track layout map. It\u2019s easy to find the shortest path visually, for one train. Now try imagine putting more trains on here. After two trains, becomes pretty difficult. Trains probably need to negotiate with each other, and it\u2019s not enough to try to all take the shortest path.<\/figcaption><\/figure>\n\n\n\n<p id=\"4853\">After staring at this for a long time, and it was clear that reading a map like felt a lot of work. Time to make our life easier.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*srm7EnePDo3YMjDz4ITisw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">This map contains the same information as the track layout map, but represented as a tree. This way, we can easily figure out what were shortest paths by counting the least number of moves. This tree factored in backwards movements as well. Because point A and point B may be most easily reached by reversing direction.<\/figcaption><\/figure>\n\n\n\n<p id=\"e8bb\">This tree map didn\u2019t take much more effort, it was generated by&nbsp;<a href=\"http:\/\/www.graphviz.org\/content\/dot-language\" rel=\"noreferrer noopener\" target=\"_blank\">GraphViz in the Dot language<\/a>.<\/p>\n\n\n\n<p id=\"2a62\">Tools empower you to be more productive. As a programmer, you\u2019re always thinking about how to make things automated, and easier. A commonly seen trait that programmers have is to be lazy, a desire to automate things and make their own lives easier. Determine how much time you spent doing some work, and see if you can build a tool to make it easier.<\/p>\n\n\n\n<p id=\"3b90\"><mark><em>Invest in building better tools when you find yourself repeating a task, as a rule of thumb, more than three times.<\/em><\/mark><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ce13\">It\u2019s ok to take on technical debt. Keep it simple, stupid.\u2122<\/h2>\n\n\n\n<p id=\"d0e0\">For example, having fancy colorful terminals. Ours was initially very pretty, but we noticed a lot of things had to be changed. Towards the end of the projects, we had avoided building fancy graphics because they took a lot of work and are likely to be changed later.<\/p>\n\n\n\n<p id=\"c1b8\">If you wanted to have pretty looking terminals, lot of time had to be dedicated for it. If you\u2019re really bored, and have time to kill, I suppose it\u2019s fine.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*c7eRIxb9AMiywKiAUCVUXA.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Our colorful terminal. It was all pretty, initially. Later on, we couldn\u2019t afford much time to make things look good.<\/figcaption><\/figure>\n\n\n\n<p id=\"c8d3\">Later on, we took on more technical debts with screen drawing. It no longer looked as nice, but it worked.<\/p>\n\n\n\n<p id=\"c424\"><em>If there are technical debts with higher priority, they should be addressed before diving into a lesser problem.<\/em><\/p>\n\n\n\n<p id=\"5779\">This sounds intuitive, but sometimes people become irrational and just spend time on low priority issues. Like making things look pretty.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"e05d\">Pair programming is effective teamworking.<\/h2>\n\n\n\n<p id=\"9882\">Initially we dividing up projects, and mostly worked separately. This was okay for well-defined problems, as it was the case for initial kernel implementation. But later on, the problems we needed to solve became so inter-dependent that it was no longer feasible to build something in the dark.<\/p>\n\n\n\n<p id=\"49f1\">Many quick idea iterations were needed, and we needed each other to bounce ideas around. Having a comfy couch is important!<\/p>\n\n\n\n<p id=\"48b7\">And towards the end, we were doing pair programming out of despair; we had to write working code, and we had to write it fast.<\/p>\n\n\n\n<p id=\"f350\"><em>Pair programming produces quality code that is better thought out, and a rate that is comparable, if not faster, to separating out tasks and doing code reviews.<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a325\">Summary<\/h1>\n\n\n\n<p id=\"a60a\">I think taking this class was very valuable, and was a very nice way to wrap up my Computer Science undergrad.<\/p>\n\n\n\n<p id=\"57e4\">These lessons are ingrained in my memories, and helped shape my behavior.<\/p>\n\n\n\n<p id=\"373f\">For example, after pair programming, I subconsciously debate about solutions to problems and bounce ideas around. To an outsider, it may seem argumentative, but it\u2019s really an effective way to solve problems.<\/p>\n\n\n\n<p id=\"1d12\">Thank you for reading.<\/p>\n\n\n\n<p id=\"e372\">PS.<\/p>\n\n\n\n<p id=\"8be3\">Feel free to reach out to me with any thoughts, and follow the discussion on&nbsp;<a href=\"https:\/\/www.reddit.com\/r\/programming\/comments\/5sk17i\/lessons_from_uwaterloos_realtime_programming_class\/\" rel=\"noreferrer noopener\" target=\"_blank\">reddit<\/a>.<\/p>\n\n\n\n<p><a href=\"https:\/\/medium.com\/tag\/programming?source=post_page-----b64544717348---------------programming-----------------\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about lessons I learnt in&nbsp;CS452 Real-Time Programming, a 4th year undergraduate class at the University of Waterloo. Better known as \u201ctrains\u201d or \u201crealtime\u201d class. \u201cEasy stuff! You play with trains all day (literally, maybe even night). All you need to do is write one simple program to control multiple trains so they &hellip; <a href=\"https:\/\/sunapi386.ca\/wordpress\/lessons-from-real-time-programming-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Lessons from Real-Time Programming class<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-679","post","type-post","status-publish","format-standard","hentry","category-thoughts"],"_links":{"self":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":1,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":1055,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/679\/revisions\/1055"}],"wp:attachment":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}