{"id":533,"date":"2013-05-25T05:37:21","date_gmt":"2013-05-24T19:37:21","guid":{"rendered":"http:\/\/legoeng.local\/?p=533"},"modified":"2017-09-07T16:13:25","modified_gmt":"2017-09-07T06:13:25","slug":"program-structure","status":"publish","type":"post","link":"http:\/\/legoeng.local\/program-structure\/","title":{"rendered":"Program structure – simulating publish and subscribe"},"content":{"rendered":"
Many times I see teams I’m supporting decide to run their GUI-based programs in a straight line from start to finish. But I’m not sure that’s always a great idea.<\/p>\n
Consider the program structure outlined below:<\/p>\n In this model, the first thing that happens after the blue start block is the definition of variables and setting their first values as determined by the programmer. Don’t limit the concept to just writing variables. If calibration for a sensor was required, then the initialization phase of the program could check for existing calibration values or files, and if they did not exist, the program would request the operator to perform the calibration.<\/p>\n In this first phase of the program, the robot physically does nothing. It sits still while the variable initialization runs its course.<\/p>\n After the initialization is done, the program starts parallel loops to monitor sensors and write the sensor values to variables. Some considerations:<\/p>\n This enables the main program branch to act based on the variables, or as shown in the diagram, to Act(values) or Act, as a function of variable values.<\/p>\n It also gives some flexibility and modularity to the program. If a sensor is replaced, then only that sensor’s loop will have to be changed, and it will be easier to find all the parts of the program that need changed.<\/p>\n If a new program action is created, then another action can be added to the main program branch, without risking the introduction of error by disturbing the sensor program elements providing the data.<\/p>\n Finally, if desired or important, data can be logged in parallel with the program – this allows the robot to do what the operator wants, while keeping the file management or data sharing complexities away from the main program structure. Again, this should reduce the opportunity for programming error introduction.<\/p>\n By running the sensors in their own loops, the robot simulates a publish and subscribe<\/em> architecture. In other words, the sensor loops read and smooth the data and publish it to the variable. The main program subscribes to the variables and reads each of them when required. By executing the loops in parallel, the robot can do more because it’s not limited to acting on only one sensor.<\/p>\n If you continue in more advanced robotics, it’s likely you’ll encounter machines using finite state machines<\/em> to publish data to which the main CPU and core program subscribe to prior to taking action.<\/p>\n Of course, the single CPU of LEGO Robots doesn’t really work in parallel, but it’s not a bad simulation.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Many times I see teams I’m supporting decide to run their GUI-based programs in a straight line from start to finish. But I’m not sure that’s always a great idea. Consider the program structure outlined below: In this model, the first thing that happens after the blue start block is the definition of variables and […]<\/p>\n","protected":false},"author":6,"featured_media":534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[88,453,89],"_links":{"self":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts\/533"}],"collection":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/comments?post=533"}],"version-history":[{"count":0,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/media\/534"}],"wp:attachment":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/a>
\n
\n
Discussion<\/h2>\n
Pitfalls<\/h2>\n
\n
Further reading<\/h2>\n
\n