{"id":2078,"date":"2013-04-12T03:00:15","date_gmt":"2013-04-12T08:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2078"},"modified":"2017-06-07T21:23:15","modified_gmt":"2017-06-08T02:23:15","slug":"creating-driver-blocks-for-arduino-lego-and-other-targets","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/04\/12\/creating-driver-blocks-for-arduino-lego-and-other-targets\/","title":{"rendered":"Creating Driver Blocks for Arduino, Lego, and other targets"},"content":{"rendered":"<p>As you may know, it is easy to run Simulink models on a set of supported target hardware. This has been possible since R2012a.<\/p>\r\n\r\n<p>Even if we add more targets and support more features for each target with every new release, it is still possible that you will need a driver that is not included in the Simulink support package. If you desperately need a driver for your hardware, you can always build it yourself.<\/p>\r\n\r\n<p>Let's see how this works using an example from a LEGO NXT project I worked on.<\/p>\r\n\r\n<p><strong>The LEGO Light Sensor<\/strong><\/p>\r\n\r\n<p>The LEGO NXT kit ships with a Light Sensor including a red LED that can be turned on or off. In the Simulink support package, you can control the light from a checkbox parameter in the block dialog.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/LEGO_light_sensor.png\" alt=\"Dialog of the LEGO Light Sensor driver block\" \/><\/p>\r\n\r\n<p>For our project, we used this sensor to make a line following robot. To make our algorithm more robust, we thought it would be interesting to turn the light on and off while tracking the line, to actively filter out the ambient light.<\/p> \r\n\r\n<p><strong>Step 1: Determine the code to be generated for your driver block<\/strong><\/p>\r\n\r\n<p>In the LEGO MINDSTORMS NXT Support from Simulink, you can find many small examples showing how the robot can be programmed in C. By default, they are located in <tt>C:\\MATLAB\\SupportPackages\\R2013a\\nxtOSEK\\samples_c<\/tt>. In one of those example, I found a function  initializing the robot that looked like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/LEGO_example_code.png\" alt=\"Example code used to program the NXT brick in C\" \/><\/p>\r\n\r\n<p>The function <tt>ecrobot_set_light_sensor_active<\/tt> is exactly what I need to generate from my driver block. I also figured that a corresponding function <tt>ecrobot_set_light_sensor_inactive<\/tt> exists to turn the light off, and I found that those functions are declared in a file named <tt>ecrobot_interface.h<\/tt>.<\/p>\r\n\r\n<p><strong>Step 2 - Create an S-Function<\/strong><\/p>\r\n\r\n<p>Here you have 2 options to create the S-Function<\/p>\r\n\r\n<p><em>Step 2 - Option 1: S-Function Builder Block<\/em><\/p>\r\n\r\n  <p>My colleague <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/76178\">Giampiero Campa<\/a> published a very good submission on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/\">MATLAB Central<\/a> titled <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39354-device-drivers\">Device Drivers<\/a> showing how to use the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sfunctionbuilder.html\">S-Function Builder<\/a> block to include the code you found during step 1 in your model.<\/p>\r\n\r\n<p>His submission contains detailed procedures and screen captures to guide you through the process step-by-step. If you are intimidated by writing an S-function, I recommend using his S-Function Builder technique.<\/p>\r\n\r\n<p><em>Step 2 - Option 2: Writing an S-Function and a TLC file<\/em><\/p>\r\n\r\n<p>If you are like me and want to understand the magic that is happening when you click <strong>build<\/strong> in the S-Function Builder block, this second option is for you.<\/p>\r\n\r\n<p>First, we need to realize that unless you model the interaction of the sensor\/actuator with the environment, in simulation, driver blocks typically do nothing.  All blocks must specify the number of ports and parameters, and their dimensions, even if they do nothing. In my case, I created an s-function with 1 input port of dimension 1 to specify if the light should be on or off. Here is the entire code for my s-function.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/lego_light_sfunction.png\" alt=\"S-Function to control the light of the LEGO light sensor\" \/><\/p>\r\n\r\n<p>To specify the code generated for your block, you need to use the <a href=\"https:\/\/www.mathworks.com\/help\/ecoder\/block-authoring-with-tlc.html\">Target Language Compiler<\/a>.<\/p>\r\n\r\n<p>Concretely, this means that you need to write a TLC file for your block. For that, I recommend starting with examples from <tt><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/sfg\/s-function-examples.html\">sfundemos<\/a><\/tt>.<\/p>\r\n\r\n<p>For this example we need our TLC to do two things: Tell the compiler to include <tt>ecrobot_interface.h<\/tt>, and call <tt>ecrobot_set_light_sensor_active<\/tt> and <tt>ecrobot_set_light_sensor_inactive<\/tt>. Here is what it looks like.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/lego_light_tlc.png\" alt=\"TLC file to control the light of the LEGO light sensor\" \/><\/p>\r\n\r\n<p>Note that I used <tt><a href=\"https:\/\/www.mathworks.com\/help\/rtw\/tlc\/block-target-file-methods.html#f37023\">BlockTypeSetup<\/a><\/tt> to include the header file, and <tt><a href=\"https:\/\/www.mathworks.com\/help\/rtw\/tlc\/block-target-file-methods.html#f37160\">Outputs<\/a><\/tt> to define what the block output method should be.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Download this <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/LEGO_light_sensor.zip\">LEGO example<\/a> or a similar <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/adruino_example.zip\">example for the Arduino target<\/a> and begin creating your own driver blocks!<\/p>\r\n\r\n<p>If you develop custom drivers for the Simulink Target Hardware, share them on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/\">MATLAB Central<\/a> and let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2078&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/lego_light_tlc.png\" onError=\"this.style.display ='none';\" \/><\/div><p>As you may know, it is easy to run Simulink models on a set of supported target hardware. This has been possible since R2012a.\r\n\r\nEven if we add more targets and support more features for each target... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/04\/12\/creating-driver-blocks-for-arduino-lego-and-other-targets\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,87],"tags":[254,318,240],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2078"}],"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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=2078"}],"version-history":[{"count":41,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2078\/revisions"}],"predecessor-version":[{"id":6604,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2078\/revisions\/6604"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}