{"id":56,"date":"2009-05-22T23:30:28","date_gmt":"2009-05-22T23:30:28","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/05\/22\/robotic-arm-models-and-repairing-hubble\/"},"modified":"2009-05-22T23:30:28","modified_gmt":"2009-05-22T23:30:28","slug":"robotic-arm-models-and-repairing-hubble","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/05\/22\/robotic-arm-models-and-repairing-hubble\/","title":{"rendered":"Robotic Arm Models and Repairing Hubble"},"content":{"rendered":"<p>As you may know, Space Shuttle Atlantis is about to\r\nreturn this weekend from man\u2019s last visit to the Hubble Telescope.\u00a0 Those\r\nastronauts took some major risks to repair our favorite and most famous scientific\r\ninstrument.\u00a0 One of the tools they used during space walks is the robotic\r\nmanipulator from the Canadian Space Agency (the robotic arm).<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/hubbleSTS125_robotArm.png\" alt=\"Image Source: Nasa - STS125 Hubble above the space shuttle Atlantis cargo bay\"><\/p>\r\n\r\n<p><em>Image Credit: NASA<\/em><\/p>\r\n\r\n<p>I have never had a chance to operate a CSA robotic arm,\r\n    but my colleague <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31651\">Guy Rouleau<\/a> has!\u00a0 Guy told me about a Simulink S-function he\r\ncreated to help in controlling a robotic arm in Simulink.<\/p>\r\n\r\n<strong>Changing the color of blocks during simulation<\/strong>\r\n\r\n<p>By Guy Rouleau<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/guy_rouleau.png\" alt=\"Roboticist Guy Rouleau\"><\/p>\r\n\r\n\r\n<p>I have many good memories from my internship at the\r\nCanadian Space Agency when I was studying for my bachelor degree.\u00a0 During this\r\ninternship, I was developing control logic for a robotic arm. Simulink was used\r\nto develop the controllers and as a user interface to command the robot. <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image003.png\" alt=\"Using a joystick to control inputs to a Simulink model of a robotic arm.\"><\/p>\r\n\r\n<p>When driving a robotic arm simulation using a joystick,\r\nit is not always easy to visualize if you are close to a joint limit or a\r\nsingularity. It is possible to display the state values in the model, but hard\r\nto know if you are about to lock out control of the arm.\u00a0 To help the person\r\ndriving the robot, we though it might be cool to make blocks in the Simulink\r\ndiagram turn red when close to a limit. \u00a0This is what it looks like in action with\r\nthe SimMechanics mech_robot_vr.mdl model.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/Robot.gif\" alt=\"Animation of a virtual reality robotic arm with red and green displays to indicate joint state.\"><\/p>\r\n\r\n<p>Here is a quick explanation of how to changing the color\r\nof a block with a simple Level 2 M-file S-Function.<\/p>\r\n\r\n<p>As a template, I suggest starting with the demo M-file <em>msfcn_times_two.m<\/em>.\r\nIn your MATLAB installation, you can open this file using the following\r\ncommand:<\/p>\r\n\r\n<p><code style=\"font-size: 11pt;\">&gt;&gt; edit msfcn_times_two.m<\/code><\/p>\r\n\r\n<p>Only a few modifications are required to make the file change\r\nthe color of a block based on the value of the signal connected to the\r\ns-function. <\/p>\r\n\r\n<p>To begin, save the file as <em>ChangeColor.m<\/em><strong> <\/strong>for\r\nthis example. At the minimum, you need to modify the code of the <em>Output<\/em>\r\nsection. To start, I created a model named \u201cTest\u201d where we will change the\r\ncolor of the block named \u201cDisplay\u201d<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image005.png\" alt=\"Simulink test model to show the S-function working.\"><\/p>\r\n\r\n<p>The simplest implementation can look at the input value, and\r\nthen use <em>set_param<\/em> to modify the block <em>BackgroundColor<\/em>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image006.png\" alt=\"Outputs code for the S-function\"><\/p>\r\n\r\n<p>In the above code, the color of the block \u201cTest\/Display\u201d\r\nis set to green or red depending if the input is larger or smaller than zero.<\/p>\r\n\r\n<p>In order to make the code reusable, I may want to determine\r\nautomatically which block is connected to the S-Function. We can also add a\r\nparameter to the block to set the value at which the color switches. In that\r\ncase, we add these lines to the setup function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image007.png\" alt=\"Block setup code\"><\/p>\r\n\r\n<p>Notice that the handle to the destination block is stored\r\nin the \u201cUserData\u201d of the S-function. UserData is often useful for storing any\r\ndata you need to persist with the S-function, but doesn\u2019t fit the definition of\r\na continuous or discrete state. The output function now looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image008.png\" alt=\"Block outputs using User Data to get the block handle\"><\/p>\r\n\r\n<p>Finally, if you want the block to return to white at the\r\nend of the simulation, you can add these lines to the terminate function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/image009.png\" alt=\"Terminate function that sets the block back to white background.\"><\/p>\r\n\r\n<p>You can find this example in my File Exchange Submission:\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23127-changing-the-color-of-a-block-while-the-simulation-runs\">Changing\r\nthe color of a block while the simulation runs<\/a>.<\/p>\r\n\r\n<p><strong>Now it is your turn<\/strong><\/p>\r\n\r\n<p>Do you work on a robotic arm or on the Hubble?\u00a0 Have you\r\nused an S-function to customize what Simulink looks like?\u00a0 Share your\r\nexperience and leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=56&amp;#comment\">comment\r\nhere<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>As you may know, Space Shuttle Atlantis is about to\r\nreturn this weekend from man\u2019s last visit to the Hubble Telescope.\u00a0 Those\r\nastronauts took some major risks to repair our favorite and most famous... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/05\/22\/robotic-arm-models-and-repairing-hubble\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[65,87,10],"tags":[90,91,459],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/56"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}