{"id":156,"date":"2012-01-11T10:33:11","date_gmt":"2012-01-11T15:33:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=156"},"modified":"2012-01-11T10:33:11","modified_gmt":"2012-01-11T15:33:11","slug":"model-based-design-of-a-dicycle","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2012\/01\/11\/model-based-design-of-a-dicycle\/","title":{"rendered":"Model-Based Design of a Dicycle"},"content":{"rendered":"<p>The other day I found this video:<\/p>\r\n\r\n<p><iframe loading=\"lazy\" width=\"480\" height=\"360\" src=\"http:\/\/www.youtube.com\/embed\/Uf6Gh-hPDeo\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\r\n\r\n<p>If you watch carefully, you will see around 1:50 that they used Simulink to design their controller.<\/p>\r\n\r\n<p>So for this week's post, I thought it would be interesting to see how we can design a simple planar version of a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Diwheel\">dicycle<\/a> in Simulink.<\/p>\r\n\r\n<p><strong>Modeling the plant in SimMechanics<\/strong><\/p>\r\n\r\n<p>Since this system is not very complex, I could have derived the equations of motion by hands. But even if the equations are simple, it is simpler (and more fun) to model it with <a href=\"https:\/\/www.mathworks.com\/products\/simmechanics\/\">SimMechanics<\/a>.<\/p>\r\n\r\n<p>To model this planar dicycle, we need 3 bodies: the ground, a wheel and a center body. The important trick here is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/physmod\/mech\/ref\/velocitydriver.html\">Velocity Driver<\/a> block constraining the motion of the wheel relative to the ground. Note that, to make the SimMechanics visualization look nicer, I added 2 posts acting as starting and stopping references.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q3\/Diwheel.mdl\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q3\/SimMechanics1.png\" alt=\"Modeling a Diwheel using SimMechanics\"><\/a> <em><br>Click on the image to download the model.<\/em><\/p>\r\n\r\n<p> Our system has 2 sensors, a gyroscope returning the angle of the body relative to gravity, and an encoder giving the angle of the body relative to the wheel. On the actuator side, the system has 1 motor applying a torque between the wheel and the body.<\/p>\r\n\r\n<p><strong>Testing the plant motion<\/strong><\/p>\r\n\r\n<p>When developing a plant model, it is important to validate it. To validate the plant, I apply a torque to the actuator and observe if the resulting motion is as expected.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q3\/Diwheel.gif\" alt=\"Uncontrolled motion\"><\/p>\r\n\r\n<p>This behaves as expected... but if I don't find a better controller, the passenger will quickly get sick. Let's see how we can design a controller.<\/p>\r\n\r\n<p><strong>Linearizing the plant<\/strong><\/p>\r\n\r\n<p>Most control techniques I know are based on a linear representation of the system. To obtain this representation, I use <a href=\"https:\/\/www.mathworks.com\/products\/simcontrol\/\">Simulink Control Design<\/a>. I can use the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/slcontrol\/ug\/bso7g6i-2.html\">graphical interface<\/a> or the command-line API. For my model, the command-line version looks like:<\/p>\r\n\r\n<style type=\"text\/css\">\r\npre.codeinput {\r\n  background: #EEEEEE;\r\n  padding: 10px;\r\n}\r\nspan.keyword {color: #0000FF}\r\nspan.comment {color: #228B22}\r\nspan.string {color: #A020F0}\r\nspan.untermstring {color: #B20000}\r\nspan.syscmd {color: #B28C00}\r\n<\/style>\r\n\r\n<p>\r\n<pre class=\"codeinput\">\r\nmdl = <span class=\"string\">'Diwheel'<\/span>;\r\n<span class=\"comment\">% Set the controller to zero during linearization<\/span>\r\nK = [0 0 0 0];\r\n<span class=\"comment\">% Construct linearization input\/output<\/span>\r\nio(1) = linio([ mdl <span class=\"string\">'\/Ctrl'<\/span>],1,<span class=\"string\">'in'<\/span>,<span class=\"string\">'on'<\/span>)\r\nio(2) = linio([ mdl <span class=\"string\">'\/Sensors'<\/span>],1,<span class=\"string\">'out'<\/span>,<span class=\"string\">'on'<\/span>)\r\n<span class=\"comment\">% Linearization<\/span>\r\nsys = linearize(mdl,io);\r\n\r\n<\/pre><\/p>\r\n\r\n<p><strong>Designing a controller<\/strong><\/p>\r\n\r\n<p>Now I will use the <a href=\"https:\/\/www.mathworks.com\/products\/control\/\">Control System Toolbox<\/a> function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/toolbox\/control\/ref\/lqr.html\">lqr<\/a> to design a state-feedback controller. The code looks like this:<\/p>\r\n\r\n<p><pre class=\"codeinput\">\r\n<span class=\"comment\">% Get minimal realization<\/span>\r\n[sys U] = minreal(sys);\r\n<span class=\"comment\">% Design state feedback controller<\/span>\r\nQ = diag([ 100 1 100 1],0);\r\nR = 1;\r\n[K,S,e] = lqr(sys,Q,R);\r\n<span class=\"comment\">% Transformation from y to x<\/span>\r\nK = K\/sys.C;\r\n<\/pre><\/p>\r\n\r\n<p>All I had to tune are the values on the diagonal of matrix Q to give a priority to each state. You can download the complete initialization script <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q3\/init_diwheel.m\">here<\/a>.<\/p>\r\n\r\n<p>Now if I try to command the vehicle an aggressive trajectory of 1m\/s for 10 meters and sudden breaking, I get the following:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2011Q3\/ControlledDiwheel.gif\" alt=\"Controlled motion\"><\/p>\r\n\r\n<p><strong>Next steps<\/strong><\/p>\r\n\r\n<p>I will stop here for this post. If I had to implement this controller on a real system, I would have a lot more steps to go through. This would involve adding to the model actuators limits, sensors resolution, discretizing the controller, maybe converting it to fixed-point, etc.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Let us know what you think of this control design process by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=156&amp;#comment\">comment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>The other day I found this video:\r\n\r\n\r\n\r\nIf you watch carefully, you will see around 1:50 that they used Simulink to design their controller.\r\n\r\nSo for this week's post, I thought it would be... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/01\/11\/model-based-design-of-a-dicycle\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[115,24,30,39,10],"tags":[247,250,79],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/156"}],"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=156"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/156\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}