{"id":217,"date":"2011-12-30T14:30:54","date_gmt":"2011-12-30T14:30:54","guid":{"rendered":"http:\/\/www.emtronics.co.uk\/wp_blog\/?p=217"},"modified":"2017-04-16T15:31:34","modified_gmt":"2017-04-16T15:31:34","slug":"testing-realtime-pid-in-simulink-on-hardware","status":"publish","type":"post","link":"http:\/\/www.emtronics.co.uk\/wp_blog\/?p=217","title":{"rendered":"Testing realtime PID in Simulink on Hardware"},"content":{"rendered":"<p><strong>This information was contained in my Quatbot project post, but I have moved it here instead.<\/strong><\/p>\n<p>I decided to test out some PID control of the speed of a wheel in a project I was making. I thought it might be interesting to interface directly with Simulink in Matlab and try and use the provided Toolbox.<\/p>\n<p>This allowed the ability to capture data, control speed, and used the Simulink control block &#8216;live&#8217; in real time with the hardware.<\/p>\n<p>&nbsp;<\/p>\n<p>So in order to do this I connect the motor and optical sensor to an FPGA, the FPGA then connected via a serial link to the computer.<\/p>\n<p>Below show the setup used:<\/p>\n<p><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_flow.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-219\" title=\"simulink_flow\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_flow.png\" alt=\"\" width=\"738\" height=\"624\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_flow.png 738w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_flow-300x253.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p align=\"left\">Simulink allows the creation of custom blocks called S-functions. I created 2 S-functions, one for reading serial data &#8211; this acted as a Simulink source; and the other which wrote serial data &#8211; this acted as a Simulink sink.<\/p>\n<p align=\"left\">These 2 files are here:<\/p>\n<p align=\"left\"><a href=\"..\/..\/projects_OLD\/quad_bot\/files\/s_func_serial_input.m\">s_func_serial_input.m<\/a><\/p>\n<p align=\"left\"><a href=\"..\/..\/projects_OLD\/quad_bot\/files\/s_func_serial_output.m\">s_func_serial_output.m<\/a><\/p>\n<p align=\"left\">A picture of the hardware is show below:<\/p>\n<p align=\"left\"><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-222\" title=\"simulink_pic\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pic-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pic-300x225.jpg 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pic-1024x768.jpg 1024w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pic.jpg 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p align=\"left\"><strong>Wheel speed VS PWM duty<\/strong><\/p>\n<p align=\"left\">Below shows actual wheel speed vs ramped input PWM duty. The flat bit at the start is a programmed dead zone, this was to try and make it slightly more linear. But as you can see, it is still very non-linear!<\/p>\n<p align=\"left\"><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-224\" title=\"pwmvspeed\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed-300x224.png\" alt=\"\" width=\"300\" height=\"224\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed-300x224.png 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed.png 999w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p align=\"left\"><strong>Wheel speed vs ramped input<\/strong><\/p>\n<p align=\"left\">In order to make the wheel speed linear w.r.t PWM duty, I created a lookup table in Matlab as follows:<\/p>\n<ul>\n<li>Captured measured speed to Matlab array<\/li>\n<li>Reversed X and Y on captured data<\/li>\n<li>Used reversed data in Matlab Curve Fitting Tool (cftool)<\/li>\n<li>Created polynomial of curve (used 9th order)<\/li>\n<li>Generate Lookup table from polynomial function<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/curve_fitting.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-227\" title=\"curve_fitting\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/curve_fitting-300x269.png\" alt=\"\" width=\"300\" height=\"269\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/curve_fitting-300x269.png 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/curve_fitting.png 688w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The same test as above was performed again, but this time with the lookup table inserted after the input data. As you can see the wheel speed is now much more linear. This is useful when using any control loop.<\/p>\n<p><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed_linear.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-228\" title=\"pwmvspeed_linear\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed_linear-300x226.png\" alt=\"\" width=\"300\" height=\"226\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed_linear-300x226.png 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/pwmvspeed_linear.png 993w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p align=\"left\"><strong>PID Control<\/strong><\/p>\n<p align=\"left\">Using the standard PID control block in Simulink, I connected up the closed loop system. The output of the PID is sent though the Lookup Table created above. The desired speed (Input Speed) was defined as an array in Matlab.<\/p>\n<p align=\"left\">Below shows the Simulink diagram, and the results after a run:<\/p>\n<p><em>Input Speed<\/em> &#8211; The desired speed of the wheel<\/p>\n<p><em>Output Speed<\/em> &#8211; The actual measure speed of the wheel<\/p>\n<p><em>PID Output<\/em> &#8211; The PWM duty being sent to the motor<\/p>\n<p><em>Error Signal<\/em> &#8211; The difference between Input Speed and Output Speed<\/p>\n<p>The update rate was about 50ms. The real time delays in the system are:<\/p>\n<p>Laptop CPU (low), Serial transmission(medium), USB-&gt;Serial latency(high).<\/p>\n<p>When running, the simulation will simply run as fast as possible, and has no real concept about time. In fact Simulink doesn&#8217;t even know it is running with a real system in real time.<\/p>\n<p><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-229\" title=\"simulink_pid\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid-300x229.png\" alt=\"\" width=\"300\" height=\"229\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid-300x229.png 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid-1024x784.png 1024w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid.png 1228w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Below shows a run when the PID parameters are incorrectly set, rendering it unstable.<\/p>\n<p><a href=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid_unstable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-230\" title=\"simulink_pid_unstable\" src=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid_unstable-300x229.png\" alt=\"\" width=\"300\" height=\"229\" srcset=\"http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid_unstable-300x229.png 300w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid_unstable-1024x784.png 1024w, http:\/\/www.emtronics.co.uk\/wp_blog\/wp-content\/uploads\/2011\/12\/simulink_pid_unstable.png 1225w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This information was contained in my Quatbot project post, but I have moved it here instead. I decided<\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"_links":{"self":[{"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=217"}],"version-history":[{"count":10,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":246,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions\/246"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.emtronics.co.uk\/wp_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}