{"id":263,"date":"2019-12-20T07:30:34","date_gmt":"2019-12-20T06:30:34","guid":{"rendered":"https:\/\/blogs.mathworks.com\/student-lounge\/?p=263"},"modified":"2019-12-20T14:58:07","modified_gmt":"2019-12-20T13:58:07","slug":"walking-robot-modeling-and-simulation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/student-lounge\/2019\/12\/20\/walking-robot-modeling-and-simulation\/","title":{"rendered":"Walking Robot Modeling and Simulation"},"content":{"rendered":"<p><!--introduction--> In this post, I will discuss robot modeling and simulation with Simulink\u00ae, Simscape\u2122, and Simscape Multibody\u2122. To put things in context, I will <em>walk<\/em> you through a walking robot example (get it?).<!--\/introduction--><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3703 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/walking_robot.gif\" alt=\"\" width=\"633\" height=\"404\" \/><\/p>\n<h1>Motivation<\/h1>\n<p>First of all\u2026 why simulate? I\u2019ve broken down the benefits into two categories.<\/p>\n<ul>\n<li><strong>Safety:<\/strong>\u00a0Robots will fall. Prototypes will break. You can verify that controls algorithms are at a good starting point in simulation before moving to hardware. Simulation lets you test your robot and controller design under multiple scenarios without building prototypes. In simulation, you also get the benefit of intentionally generating unsafe conditions, as well as discovering unexpected issues.<\/li>\n<li><strong>Efficiency:<\/strong>\u00a0Physical experiments take time and effort to set up and reset between runs. With simulation, you get a programmatic environment\u00a0to automate experiments and walk away from your desk. If your robot is controlled by an embedded system, simulation lets you test algorithm changes without having to port and rebuild the code on hardware every time. This separation of algorithm and implementation can also help you determine whether new issues are due to algorithm changes or physical limitations.<\/li>\n<\/ul>\n<h1>Robot Simulation Components<\/h1>\n<p>We will now look at a typical robot simulation architecture, which consists of multiple layers. Depending on your goals, you may only need to implement a subset of these for your simulation.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-271 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/robot_model_schematic-e1507136654371.png\" width=\"719\" height=\"243\" \/><\/p>\n<h1>Robot Mechanics<\/h1>\n<p><a href=\"https:\/\/www.mathworks.com\/products\/simmechanics.html\">Simscape Multibody<\/a> lets you model the 3D rigid body mechanics of your robot. There are two ways to do this.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/physmod\/sm\/multibody-modeling.html\"><strong>Build from scratch:<\/strong><\/a> It may take some initial time to build a model from scratch. However, if set up correctly, you can easily change properties such as dimensions, cross-sections, masses, etc. If you are still in the conceptual design phase, this can be useful as you sweep through different parameters and validate your design.<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/physmod\/sm\/cad-import.html\"><strong>Import from CAD:<\/strong><\/a> Useful if you have already created a robot model and want to simulate its dynamics using more realistic geometric and inertial properties. So long as the kinematics of the CAD model remain the same, you can make changes in CAD and reimport the parameters into your model. For more information, look at our <a href=\"https:\/\/blogs.mathworks.com\/racing-lounge\/2017\/05\/03\/import-cad-assemblies-to-simscape-multibody\/\">blog post on importing CAD assemblies<\/a>.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3715 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/walking_robot_model.png\" alt=\"\" width=\"1414\" height=\"399\" \/><\/p>\n<p>Regardless of how you create the robot model, the next step is to add dynamics to it.<\/p>\n<ul>\n<li><strong>Internal mechanics:<\/strong> Every Joint block (translational or rotational) in the model can be assigned mechanical stiffness, damping, and initial conditions.<\/li>\n<li><strong>External mechanics:<\/strong> First, you can set up the direction and magnitude of gravity. Equally important for legged robots, you need to model contact with the ground. Starting with R2019b, you can do this using the <a href=\"https:\/\/www.mathworks.com\/help\/physmod\/sm\/ug\/modeling-contact-force-between-two-solids.html\">Spatial Contact Force block<\/a> in Simscape Multibody. However, in previous releases you can use the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47417-simscape-multibody-contact-forces-library\">Simscape Multibody Contact Forces Library on File Exchange<\/a>.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1367\" height=\"406\" class=\"aligncenter size-full wp-image-3717\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/walking_robot_contact.png\" alt=\"\" data-wp-editing=\"1\" \/><\/p>\n<h1>Actuator Dynamics and Control<\/h1>\n<p>As shown in the simulation architecture diagram earlier, the actuator is the \u201cglue\u201d between the algorithm and the model (or robot). Actuator modeling consists of two parts: one on the controller side, and one on the robot side.<\/p>\n<ul>\n<li><strong>Actuator control:<\/strong> By prescribing motion to an actuator model in Simscape, you can first perform actuator sizing. This lets you determine the power (for example, current, torque, etc. for electric actuators) needed for your actuator to perform as desired. Once you have an actuator model, you can use Simulink to\u00a0design\u00a0a controller and test it in simulation before deploying it.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3705 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/walking_robot_servo.jpg\" alt=\"\" width=\"735\" height=\"307\" \/><\/p>\n<ul>\n<li><strong>Actuator dynamics:<\/strong> You can use <a href=\"https:\/\/www.mathworks.com\/products\/simscape.html\">Simscape<\/a> to build more detailed actuator models. This allows you to connect the 3D mechanical motion of the robot to other physical domains \u2013 for example, the electrical domain for <a href=\"https:\/\/www.mathworks.com\/help\/physmod\/elec\/ug\/example-modeling-a-dc-motor.html;jsessionid=3e51fe1c3644304dbdd9a13db230\">motors<\/a> or the fluid domain for <a href=\"https:\/\/www.mathworks.com\/videos\/modeling-a-hydraulic-actuation-system-68833.html\">piston actuators<\/a>.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1028\" height=\"449\" class=\"aligncenter size-full wp-image-3711\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/walking_robot_motor.jpg\" alt=\"\" \/><\/p>\n<p>Different design tasks may need different model detail. Depending on this,\u00a0simulation speed could range from much faster than real-time to much slower than real-time, and this is an important tradeoff.\u00a0Let&#8217;s take the following example. Suppose you&#8217;re designing a robot which has both a high-level motion planning algorithm\u00a0and a low-level electronic motor controller with high-frequency pulse-width modulation (PWM).<\/p>\n<ul>\n<li>A motion planning mission may require <em>minutes<\/em> of simulation, whereas a motor control response may be in the order of <em>milliseconds<\/em>.<\/li>\n<li>To test the motion planner, you can assume that the low-level actuators just work; for electronics design, you may need to dig all the way into the actuator current transients to make sure individual components will not fail.<\/li>\n<\/ul>\n<p>Ideally, you\u2019d like to have reusable and configurable model components for different scale simulations. Simulink facilitates this with modeling features such as <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-variant.html\">variants<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/about-block-libraries.html\">block libraries<\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/overview-of-model-referencing-1.html\">model referencing<\/a>.<\/p>\n<p>To see how this was done with the walking robot actuator models, watch the video below.<\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/videos\/modeling-and-simulation-of-walking-robots-1576560207573.html\"><strong>[Video]\u00a0<\/strong>Modeling and Simulation of Walking Robots<\/a><br \/>\n<div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"6116338561001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div><\/p>\n<h1>Motion Planning<\/h1>\n<p>Motion planning can be an <em>open-loop<\/em> or <em>closed-loop<\/em> activity.<\/p>\n<p>You can read more about motion planning and control for walking robots in our <a href=\"https:\/\/blogs.mathworks.com\/racing-lounge\/2019\/04\/24\/walking-robot-control\/\">next blog post<\/a>. In this example, we already designed an initial open-loop walking pattern that makes our simulated robot walk stably. To further improve this walking pattern, you can add closed-loop components for stability and\/or reference tracking, or use techniques such as optimization to refine the walking pattern.<\/p>\n<p>Optimization tools are useful in many aspects of robot modeling and simulation, such as<\/p>\n<ul>\n<li><strong>Robot design:<\/strong> Determining optimal geometry (size, position, cross-section, etc.) or dynamics (mass\/stiffness\/damping, or equivalents in electrical or fluid actuators). For example, see <a href=\"https:\/\/www.mathworks.com\/videos\/estimating-parameters-of-a-dc-motor-68856.html\">Estimating Parameters of a DC Motor<\/a>.<\/li>\n<li><strong>Control design: <\/strong>Tuning control gains, thresholds, rate limits, etc. to meet performance and safety requirements. For example, see <a href=\"https:\/\/www.mathworks.com\/videos\/optimizing-system-performance-dc-motor-68851.html\">Optimizing System Performance: DC Motor<\/a>.<\/li>\n<li><strong>Motion planning:<\/strong> Finding a sequence of motion inputs that satisfy overall planning goals. This approach is shown in the animation and video below, in which a <a href=\"https:\/\/www.mathworks.com\/discovery\/genetic-algorithm.html\">genetic algorithm<\/a> is used to optimize the robot walking trajectory.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-282 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2017\/10\/optimization_animation.gif\" alt=\"\" width=\"525\" height=\"300\" \/><\/p>\n<p>Designing an open-loop motion profile through optimization can be a good start, but this may not be robust to variations in the physical parameters, terrain, or other external disturbances. In theory, you could use optimization and simulation to test against scenarios that cover all the challenges you expect in the real world. In practice, a closed-loop system &#8212; or a system that can react to the environment &#8212; is\u00a0better suited to handle these challenges.<\/p>\n<p>Closed-loop motion controllers require information about the environment through sensors. Common sensors for legged robots include joint position\/velocity sensors, accelerometers\/gyros, force\/pressure sensors, cameras, and range sensors. An overall control policy can then be determined using model-based methods like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Internal_model_(motor_control)\">Internal Model Control<\/a>, or with machine learning techniques like <a href=\"https:\/\/www.mathworks.com\/videos\/series\/reinforcement-learning.html\">reinforcement learning<\/a>.<\/p>\n<p>The video below shows how you can repeatedly simulate a model and collect results to optimize open-loop trajectories for a walking robot. Running simulations in batch can similarly help you perform tasks such as tuning controller or motion planning algorithms using optimization and machine learning.<\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/videos\/matlab-and-simulink-robotics-arena-walking-robots-part-3-trajectory-optimization-1506440520726.html\"><strong>[Video]\u00a0<\/strong>Optimizing Walking Robot Trajectories<\/a><br \/>\n<div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"5589115643001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div><\/p>\n<h1>Conclusion<\/h1>\n<p>You have now seen how simulation can help you design and control a legged robot.<\/p>\n<p>For more information, watch the videos above and read our <a href=\"https:\/\/blogs.mathworks.com\/racing-lounge\/2019\/04\/24\/walking-robot-control\/\">next blog post on walking robot control<\/a>. You can download the example files from the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64227-matlab-and-simulink-robotics-arena--walking-robot\">File Exchange<\/a> or <a href=\"https:\/\/github.com\/mathworks-robotics\/msra-walking-robot\">GitHub<\/a>. You can also find a four-legged <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64237-running-robot-model-in-simscape\">Running Robot example<\/a> on the File Exchange.<\/p>\n<p>Are you working on legged robot locomotion? We\u2019d be interested in hearing from you.<\/p>\n<p>&#8211; Sebastian<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/student-lounge\/2019\/12\/20\/walking-robot-modeling-and-simulation\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/daee9f26-1e03-44f6-a901-036c3a2bfc58\/855ea591-407a-4156-8eec-6c33e319038e\/1280x720\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/><\/p>\n<div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">21:19<\/time><\/span>\n      <\/div>\n<p>      <\/a><\/div>\n<p> In this post, I will discuss robot modeling and simulation with Simulink\u00ae, Simscape\u2122, and Simscape Multibody\u2122. To put things in context, I will walk you through a walking robot example (get&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/student-lounge\/2019\/12\/20\/walking-robot-modeling-and-simulation\/\">read more >><\/a><\/p>\n","protected":false},"author":155,"featured_media":271,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,9,8,11,12],"tags":[23,21,20,22,17,19,15,18,16],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/users\/155"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":23,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":3719,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/263\/revisions\/3719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/media\/271"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}