{"id":7742,"date":"2018-10-11T19:00:55","date_gmt":"2018-10-12T00:00:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=7742"},"modified":"2018-10-11T19:00:55","modified_gmt":"2018-10-12T00:00:55","slug":"arduino-engineering-kit","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2018\/10\/11\/arduino-engineering-kit\/","title":{"rendered":"Arduino Engineering Kit"},"content":{"rendered":"<p>Have you heard of the <a href=\"https:\/\/www.mathworks.com\/campaigns\/products\/arduino-kit.html\">Arduino Engineering Kit<\/a>?<\/p>\n<p>If you are interested in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Do_it_yourself\">DIY<\/a> projects and are looking for a fun way to apply some control and robotics principles to a real system, you probably want to read this post.<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>If you have been following this blog for some time, you are probably aware that I like using MATLAB and Simulink for various projects at home. For an example, see my <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/02\/21\/sous-vide-cooking-with-simulink-part-1\/\">sous-vide cooking machine posts<\/a>.<\/p>\n<p>When I heard that Arduino is now offering an Arduino Engineering Kit and that we have support packages to program it with Simulink, I contacted the team that put it together and asked if I could borrow one to play with it a bit.<\/p>\n<p>When you buy the kit on <a href=\"https:\/\/store.arduino.cc\/arduino-engineering-kit\">the Arduino Store<\/a>, you receive a box full of mechanical and electronic parts, including a <a href=\"https:\/\/store.arduino.cc\/usa\/arduino-mkr1000\">Arduino MKR1000<\/a>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/store-cdn.arduino.cc\/uni\/catalog\/product\/cache\/1\/image\/500x375\/f8876a31b63532bbba4e781c30024a0a\/u\/n\/unbox.jpg\" alt=\"The Arduino Kit\" \/><\/p>\n<p>The kit includes instructions to build three possible projects: a motorcycle, a rover, or a drawing robot.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/ThreeProjects.png\" alt=\"The 3 projects in the Arduino kit\" \/><\/p>\n<p>I decided to go for the motorcycle. I thought the inertia wheel making it self-balancing would be an interesting control project.<\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Once you have the kit pieced together, you will need to install a few packages in MATLAB:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/40312-simulink-support-package-for-arduino-hardware\">Simulink Support Package for Arduino Hardware<\/a>: This is the basic package you need to deploy Simulink models on the family of Arduino boards.<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/66568-arduino-engineering-kit-hardware-support\">Arduino Engineering Kit Hardware Support<\/a>: this package contains the drivers and example models for all the sensors and actuators available in the kit.<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/66569-arduino-engineering-kit-project-files\">Arduino Engineering Kit Project Files<\/a>: This package contains Simulink models specific to the three projects, both for simulating the systems and deploying on the hardware.<\/li>\n<\/ul>\n<p>The easiest way to install those is through the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018a\/matlab\/add-ons.html\">Add-ons<\/a> menu in the MATLAB toolstrip.<\/p>\n<p>After installing those three packages, I went in the <tt>examples<\/tt> folder of the Arduino Engineering Kit Hardware Support package and tried the examples one by one, to be sure I was able to utilize all the sensors and actuators. For example, here is my test of the IMU, where I tilted the motorcycle by 90 degrees back and forth along all axes. This allowed me to conclude that the axis I want to control, the roll angle of the motorcycle, is the second element of the Euler angles output, the Y axis.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/IMUtest.png\" alt=\"Testing the IMU\" \/><\/p>\n<p><strong>My Model<\/strong><\/p>\n<p>The Engineering Kit Project Files also contain a complete model that balances the motorcycle and allows you to control its speed and steering angle. However I thought it would be more fun to build mine from scratch. If you are interested, you can <a href=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/motoSys2_balance_Guy2.slx\">download the final version here<\/a>.<\/p>\n<p>Here is a picture of the top level of the final result.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/topMotoModel.png\" alt=\"Top final model\" \/><\/p>\n<p>Let's look at each of the components. To begin, on the left I created a subsystem with all the sensors I needed and combined them in a Bus.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/SensorRaw.png\" alt=\"Top final model\" \/><\/p>\n<p>Next I process each sensor to extract the relevant data in convenient engineering units.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/processSensor.png\" alt=\"Top final model\" \/><\/p>\n<p>Now it is time for the Stateflow chart acting as the brain of my model. On the left, I check for three conditions: That the IMU is calibrated, that the motorcycle has not fallen, and that the flywheel does not reach maximum speed. When all those conditions are met, I can then click the Start button at the top level of the model. Once this is clicked, I have 4 seconds of calibration where I hold the motorcycle perfectly vertically for calibration before I start the balancing. If the motorcycle falls, I can hit the button again to reset the controller and try again.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/ArduinoSFChart.png\" alt=\"Top final model\" \/><\/p>\n<p>When the chart enables the balancing, it turns on an Enabled Subsystem that activates the controllers. For the balancing fly-wheel, the controller is a PID that takes as input the roll angle of the motorcycle and generates a voltage command to the fly-wheel. For the motorcycle speed, this is a simple proportional controller, which takes as input the speed of the motorcycle, integrates it and compares it with the wheel encoder.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/Ctrls.png\" alt=\"Top final model\" \/><\/p>\n<p>Note that this balancing controller is not perfect. The sensors on the fly-wheel motor do not allow direct measurement of the fly wheel velocity, making it difficult to control the fly-wheel motion perfectly. We are working on that!<\/p>\n<p>Finally, the output of the controllers is sent to the output driver blocks.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/output.png\" alt=\"Top final model\" \/><\/p>\n<p><strong>And it works!<\/strong><\/p>\n<p>Here is a video showing the motorcycle in action:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/b5UCQ5jrwOI\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p>Visit the <a href=\"https:\/\/www.youtube.com\/channel\/UCaPRtmtzWXQkbo9D-OxMN1w\">Arduino Engineering Kit YouTube channel<\/a> to see more examples.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Let us know what you think of the Arduino Engineering Kit in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/output.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Have you heard of the Arduino Engineering Kit?<br \/>\nIf you are interested in DIY projects and are looking for a fun way to apply some control and robotics principles to a real system, you probably want to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2018\/10\/11\/arduino-engineering-kit\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,34,71,16],"tags":[542,544],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7742"}],"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=7742"}],"version-history":[{"count":25,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7742\/revisions"}],"predecessor-version":[{"id":8093,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7742\/revisions\/8093"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=7742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=7742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=7742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}