Build Something! MATLAB and Simulink for Hardware Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to today's session on building hardware projects with MATLAB and Simulink I am Dan tardy and I work in tech marketing and math works and I focus a lot of my attention on using MATLAB and Simulink for hardware programming and as you know working with hardware has really become sort of a foundational skill for today's engineering students if any of you have not had the program in Arduino or a Raspberry Pi or some other kind of hardware yet it's probably just a matter of time before you will and my goal is that every one of you leave here with the knowledge to actually get started programming different types of robots and gadgets with MATLAB and Simulink all right so let's get started so the cornerstone example that I'm going to discuss today is this Arduino self balancing motorcycle and I'll actually have this up and running shortly during today's session but here's a quick taste of what this thing does so you see the motorbike includes an Arduino with a special sensor that measures how much the bike is leaning adjust the speed of the on-board inertial wheel to maintain a lean angle right around zero so the bike remains upright and you can see it's just balanced while moving straight forward it can also move straight back and also make turns while moving forward or backwards the nice thing about the bike too is that it's pretty robust so it can you know handle moderately bumpy terrain other external disturbances such as lightly tapping its side so overall a really cool projects fun to play around with and what's more important is it incorporates a bunch of important engineering concepts now before you can actually get into the fun stuff and build these types of projects you first need to learn the underlying math and engineering concepts so basic mechanics and controls and signal processing and so forth and to help you learn you're generally going to do some programming for example maybe with languages like MATLAB or Python or maybe C or C++ ok so the next question is how do we transition from learning concepts and solving problems on the computer to building real things that exist in the real world and use hardware like the self balancing motorcycle that we just saw or even something much more complex like a wind turbine so this is actually going to take a different kind of programming that involves modeling and simulation and embedded algorithm development to be perfectly honest really hard to do with textual based languages like MATLAB a Python so what I'm going to focus on today is how math works either a core platform product Simulink it can be used for describing and analyzing and building these types of systems so coming back to the wind turbine example the different components of this system and their interactions can be represented as blocks within Simulink for example the turbine blades or generator and electrical grid and so what you do is you build the model in simulink and then simulate the behavior and the nice thing about simulating is that you can adjust your model to try out different design options for example here we're making updates to the pitch system and the generator and the gear train and we're kind of swapping out blocks to represent different design options and for each design we can then rerun the simulation and see how it impacts the system behavior now of course for any real-world system there's generally going to be onboard embedded hardware acting as the brains and we'll simulate you can design your embedded algorithms and actually generate code you can deploy directly on to the hardware so I know there was a lot to unpack in that video but at a high level there's really two key pieces of the Simulink workflow so the first is modeling and simulation so with the wind turbine or really any other large-scale system there's going to be hundreds if not thousands of design parameters that you're dealing with and of course it's not realistic to try them all out in the physical world so simulation becomes absolutely critical for exploring the design space seeing how the system performs under different conditions so once you've converged on a design through modeling and simulation you can then automatically generate code from relevant portions of your model embed it directly onto the hardware so automatic code generation is really key value driver of Simulink it's an incredibly powerful capability because what it does it it eliminates the need to manually write C code for your hardware which of course can be very time-consuming and error-prone Simulink in this workflow that I just described is actually used by companies around the world to design literally some of today's most complex systems so systems that are increasingly smart and connected and autonomous so whether it's Blue Origin designing their space capsules or general motor designing their hybrid vehicles solar impulse designing the first solar-powered aircraft to fly around the world all of these companies rely heavily on Simulink for both simulation as well as cogeneration so for any of you looking to work at these types of companies you know building these innovative products that use some great technology having experience with Simulink and hardware programming is a skill that's definitely going to help you get noticed okay so let's start actually learning how to use Simulink so we're gonna go through four examples today the first is going to be a mobile Rover so this is going to be used sort of as an introduction to Simulink and it's also going to cover the full flow from the modeling and simulation side all the way through to hardware targeting next will be the self balancing motor cycle so there's a little more complex gets a bit deeper into Simulink so we'll spend a bit more time on it probably more like 15 or 20 minutes and following that I'll go through a quick case study of how student teams use simulate to design race cars for their Formula Student challenge this is really a really super interesting example that like the others fans the full flow from modeling and simulation through to hardware targeting however for today's session I'm going to focus on the modeling and simulation side which is why this one's displayed on the left hand side and I'll finish up with an automatic guitar tuner example and and this one's actually different from the others in that in this case the physical design is trivial so the meat of the project is really in the embedded audio processing algorithm so this one focuses more on the hardware targeting side of the flow which is why it's on the right-hand side okay so before we get into these demos I just want to quickly mention that the rover and motorcycle projects are both included as part of the Arduino engineering kit which was developed in partnership between Arduino and MathWorks and includes the Arduino board and all of the hardware components that you need to build these two projects as well as detailed learning materials that basically show you how to program them with MATLAB and Simulink so if you want to work with these projects after today's session or any to do is buy a kit alright with that let's get started so here's our mobile Rover and again I'll use this example mainly as a high-level introduction to Simulink so here are the the key components of this robot so the main things to know are that a it's powered by an Arduino and B it's what we call a differential drive robot and that basically means that each wheel can be operated independently using separate DC motors it also has a servo mode operated forklift and an ultrasonic sensor to help it see its surroundings but I'm really not going to use those today so let's say we want to program this Rover to move from some starting point to an object and then from that object to some drop-off location so if you think about it there's really two basic tasks that are needed to accomplish this the rover has to turn and move forward for example from the starting point it would first turn until it's facing the object and then move forward until it reaches the object then it would do the same thing for the drop-off location now to instruct the rover to move forward and turn we have to give as inputs some desired velocity V and rate of rotation Omega around the some what we call instantaneous center of curvature and using some pretty basic kinematics we can then derive the relationship between how fast each wheel needs to spend so the omega l and omega are to achieve that desired velocity and rate of rotation so a pretty basic equation to denote that relationship okay so the question now becomes how do I use Simulink to program this Rovers movement where do I even begin well I'm going to start from the beginnings with a blank Simulink model so as we talked about Simulink is a block diagram based environment so I'm going to start by adding some blocks so simulant blocks are organized in two intuitive libraries and displayed within this Simulink library browser and in general with Simulink there's going to be some inputs to your model so these are mostly contained within the sources library and then there's the results or the output of your model and these are generally contained within sinks library and in between the inputs and the outputs there's some math that happens there's a library for this too and it's also a library for user-defined functions and this is where you can integrate your MATLAB code or C or C++ code or code that you developed in other languages as you can see there's a host of other libraries there's many more that aren't displayed here but these are just some of the more popular ones and you can actually get pretty far using just these basic libraries so coming back to our earlier equation that relates desired velocity and rate of rotation to wheel velocities this can be represented and Simulink using the simple model that you see here so the inputs to the model are rover velocity and rate of rotation and the outputs are instantaneous wheel speeds to achieve that so basically this model is telling us that if we want the rover to move forward at ten centimeters per second without turning then each wheel should spin at one hundred and twenty seven point three degrees per second and in this case we actually use a MATLAB function block to express the equation and that's one very important thing to realize about Simulink is for those of you that know and love MATLAB you can plug that code right into your model which can make the plunge into Simulink much much easier okay so there it is we just built our first Simulink model but I'm guessing not too many of you are super impressed so we basically just used simulate to solve a pretty simple math equation as I'm sure you realize Simulink is much more than that otherwise I wouldn't be up here telling you about it so the next step is to model the rover movement over time and this is really where Simulink begins to shine it's really what Simulink was designed for is modeling dynamic system behavior so here's this new model and rather than the inputs being singular values for velocity and rate of rotation we define them over a period of 10 seconds so for the first first five seconds we want the rover to move forward at five centimeters per second then for the next five seconds we wanted to turn at 18 degrees per second and if you do the math that turns out to be a 90-degree turn and for output rather than looking at instantaneous wheel speed we instead look at the rover position x and y and theta over time and that can be calculated using the equations that you see here these equations are expressed within this R over R simulation subsystem not going to explain all the details but it's actually pretty straightforward so you see there's blocks for our integrals and sines and cosines and so forth that essentially represent the equations that we see here and then the output goes into a plotting routine and so if we run this simulation we see that simulating solves these equations numerically over time and you see the rover moving forward for five seconds then turning for five seconds until it reaches 90 degrees okay so that's great seem to work as expected so the next question then becomes okay well how do we get our actual Rover in real life to do that well the model here is exactly the same as the previous one except the simulation subsystem is replaced by a driver blocks for the the DC motors that are connected to the rover's wheels so these blocks are available when you install Simulink support package for Arduino which is a free extension to Simulink and similarly provides an option to essentially deploy your model to hardware so with a single click of a button it generates the C code equivalent of the model and downloads it onto the Arduino and then the code runs natively on the rover itself completely independent of Simulink you can see here that our actual Rover is moving forward for 5 seconds and then turning 90 degrees over the next 5 seconds just like it did in simulation except now it's running on the real hardware ok so that's the basics of how to program real hardware with Simulink just wanted to show another quick example of how we were able to program the rover to follow a slightly more complex path that involves multiple waypoints and also moving an object with its forklift so this model actually is a couple of powerful concepts that you'll learn about shortly when we look at the self balancing motorcycle project the first concept is position control ok so just about any real-world sort of mechanical or electromechanical system is going to use some form of position control so in this case just because we tell the rover to move forward 25 centimeters or to turn 90 degrees doesn't mean that it will actually move that precise amount unless some position control is implemented okay so we'll talk a bit more about that the second concept is state transition charts where we define different essentially different operating modes of the rover as unique States for example there's a move forward state and a turn state and then program the rover accordingly based on the particular state that it's in and this is another kind of fundamental extremely powerful concept with Simulink that we're going to see in action shortly okay so now that we have an idea of what Simulink is and how it can be used with hardware let's get into today's main example the self balancing motor cycle and at this point I'm going to hand things over to Swarup who will show how to get it running thanks Dan before getting into how we program this motor cycle let's see a schematic of the different components of the system let's start at the bottom with the IMU sensor connected to the Arduino I am.you stands for inertial measurement unit and this sensor has built-in accelerometers and magnetometers that can measure how much the motorcycle is leaning from its vertical upright position and how fast the lean angle is changing or in other words the rate at which it's falling this data is then fed into the Arduino which uses it to control how fast the inertial wheel rotates to provide an appropriate counter torque to keep the bike upright the motorcycle also has a DC motor connected to the rear wheel to move forward and backward and a servo motor connected to the handlebars for steering there will be three main steps in my workflow I'll start with modeling and simulation to help understand the motorcycles behavior in a safe virtual environment I'll then design and tune my controller until I have the motorcycle balancing in simulation finally I will deploy my control algorithm to we know on configure reading and writing from relevant sensors and actuators and if all goes well we'll get to the motorcycle balancing in the real world let's take a look at the modeling and simulation first simulation is essential in designing all engineering systems this enables quick design iterations at a low cost in some instances simulation is almost necessary to enable design for instance the self balancing motorcycle is inherently an unstable system a control algorithm is necessary to stabilize the system tuning the control gains on the real Hardware for such a system is near impossible in this scenario simulation is helpful in two ways understanding and analyzing the system dynamics designing and tuning algorithms quickly without causing harm to the hardware let's take a closer look at our motorcycle model this is a symbolic model of the motorcycle we our model the complete system the motorcycle sensors and balancing controller we can inject a disturbance and we can enable and tune the balancing controller let's zoom in on the motorcycle block we have modeled the motorcycle as an inverted pendulum the most important variables shown here is the lean angle theta which is equal to zero degrees when the motorcycle is perfectly upright positive when the motorcycle leans counter clockwise when viewed from behind and negative when the motorcycle means clockwise a derived quantity from this is the lean angle rate theta dot another important angle is the rotational displacement of the inertial wheel fee as the inertial wheel rotates relative to the rest of the motorcycle fee varies in value where positive displacement is defined as counter clockwise now the rate at which this displacement occurs free dot is the control variable of the system look into how the subsystem is modelled the model contains physical components for the rear wheel inertial wheel and motorcycle body simile automatically derives the system equations for these components this approach is called physical modeling and it is different from the previous example Dan showed where we represented the rower by implementing our own set of differential equations with simile you can combine these approaches to model electrical mechanical magnetic and other types of system as well note that with physical modeling you can import the system geometry from CAD models as shown in this example let's see the motor cycle response without a balancing algorithm and without external disturbances in the model turn off the balancing and disturbance controls and press the Run button see the value of theta for the unstable system you can see the sinusoidal behavior you can also see the animation here that shows the unstable system now let's use a controller to make the system stable we will use a PD controller to stabilize the motorcycle a PD controller is a modified version of a PID that only includes the proportional and derivative terms let's turn on the balancing and disturbance to the model and press the Run button again to see the motorcycle response with a controller and with external disturbances we see the values here theta seems to symbolize we also see the animation here that shows that the motorcycle is no balancing see the value for theta for the stable system you can see with external disturbances the system is still stable with data hoarding around zero but can we get a better response now let's tune the proportional gain of the controller as the model runs to improve our response this tuning here is done interactively with the knob control in the model sometimes similan crunch simulations too fast to tune parameters or visualize results comfortably as if it were real time fortunately we can control the pace of the simulation to make the visualization of results easier for tuning and visualization we will set the simulation pays to one simulation second per wall clock second press the run button and now we can tune the controller to see the results you can see that initially theta value is changing a whole lot but then as we increase the gain theta becomes more stable so now we have got a better response for our controller using simulation we prove that our balancing algorithm works it is time to shift our focus to the hardware programming can be a challenge for engineers when working with embedded hardware writing device drivers and communication protocols is not an easy task nor is it completely necessary skill when building hardware projects when all we are trying to do is applying algorithms on the hardware similan makes it easy to program hardware with just a click of a button for our example since we will be using the Arduino to program the motorcycle let's look at some of these libraries that are available for us the motorcycle needs the base Arduino library so let's open that up in the Simulink library browser here we see the arduino library it has different types of blocks such as sensors motors etc now let's look at the hardware model and see how it is different from the simulation model the first thing we can see on the sensor side is that now there are two subsystems one subsystem the raw sensors gets the sensor inputs here is where we are using the sensor blocks from the hardware Support Package we see the encoder the IMU and the battery blocks there is also a sensor processing subsystems to get the derived values from the hardware because sensors may not always provide the measurement in the form that you want or need for your algorithms next let's look at the motorcycle subsystem in the hardware model note that the motorcycle subsystem in the hardware model is different than the one in the simulation model in this hardware model we use blocks to send control commands to the motors instead of using a virtual representation of the motorcycle our intention now is to communicate with the actual hardware now let's take a look at the controller the controller subsystem is the same as the one we used in the simulation model so this is where we are going to test the controller we tuned in the simulation model on hardware note the simulation may not always exactly match the hardware behavior so some tuning may be necessary for your controller on the actual hardware the biggest addition in the hardware model when compared to the simulation model is the presence of the enable control logic in Simulink you can implement supervisory control algorithms in a graphical way which can simplify the process of programming debugging and executing that logic this graphical implementation can be used for sequential tasks for example here we are first calibrating the IMU sensor then checking the flywheel saturation speed and finally running the self balancing algorithm we can also use this to write event based logic for example here we are doing actions based on specific events such as if the motorcycle has fallen if the IMU is calibrated etc if we were to implement this supervisory control logic with textual programming then the number of if-else and switch-case statements that you would be using would be quite large with Simulink you can deploy the standalone algorithm on the hardware with a single click of a button you can also run this model in what is called external mode to monitor signals and tomb parameters in real time while the algorithm is running on the hardware with the model set to external mode click on the Run button to automatically generate code for the model and download it to the hardware this simulation mode establishes a communication interface back to our symbolic model so we can monitor the signals from the hardware in the model or also tune the parameters that is running on our hardware let's first look at the enable control logic you can visualize the execution of the Supervisory logic as the algorithm transition between states let's first calibrate our IMU sensor to do this I'm going to roll our motorcycle on several axes here and now we can see that the IMU sensor is calibrated next we see here that the check fallen state is in the fallen state we need to keep the motorcycle in the standing condition to do that we need to keep our motorcycle almost upright and then you start to see that the inertial wheel starts rotating and when it is around the upright position we can leave the motorcycle and you can see that the motorcycle is no balancing the inertial wheel is rotating to provide the counter torque for the falling torque you can also tune parameters in real-time and see the effect of it on the algorithm running on the hardware note that the motorcycle right now is only self balancing in its place recall that we are also feeding in a rear-wheel propulsion command in the model by default this is zero let's use the slider here to set it to a non zero positive value we see that the motorcycle is moving forward I'm going to bring it back to zero put it to a negative value to see that it's moving back so you can see that the balancing algorithm is good enough to balance the motorcycle while it's moving forward and backward as well we also have more controls here to play with the steering angle so changing this control changes the steering you can see that the motorcycle is right now tethered to my laptop and that's how external mode is achieved you can also do external mode via Wi-Fi to see how to do that please refer to the documentation with that I'm gonna hand it back to Dan thanks for Rupe so really cool demo and a great example of how the two pieces of the workflow tied together to help solve a pretty complex problem so at this point I'm going to come back to today's other examples so we'll spend about five minutes each on the final two examples the first being how actual student teams are using Simulink for their formula student design projects in this case I'm going to focus on how they use modeling and simulation to converge on an optimal racecar design I will note again that most of the teams also use Simulink for the code generation and hardware targeting piece but I'm not going to get into that today mainly just for the sake of time so the core tasks in this formula student challenge is to design a racecar that performs well in a number of different categories related to handling and acceleration and energy consumption and there are some set design constraints that all teams need to adhere to but there's also many many other design variables that teams can can tweak to try to get an edge with their design so for example the of components such as battery and engine and motors the location of these components which can affect the center of gravity and have a pretty big impact also suspension geometry just to name a few all told there's really a staggering number of design options so there's no way to evaluate all of the designs in the physical world so as you probably guessed we're going to turn to modeling simulation sort of as our lifeline we're going to use simulating for this so here we have a Simulink model that uses sim scape blocks to represent the various aspects of the physical system such as the rear and front suspensions the powertrain wheels and so forth and we've also defined this scene and connected it to the model so that we get a nice visual reference to interpret how the vehicle behaves in simulation so when we run the simulation it shows the vehicle going around a figure eight track and overall it seems to be doing a pretty reasonable job you know the car staying on the track not fun not swerving off or anything like that but how do we really know if this is is the best design or even necessarily a good design so the answer is that we need to explore the various design options and compare their performance of each so we actually set up our model so that the design for our parameters are adjustable and we can quickly update them for example we can probe into the powertrain system and adjust the accumulator capacity of our battery and you see that in the physical system the battery size then increases and we can also adjust the power of the motor and where it's located and you see those adjust in our physical design - and the final adjustment is to add an aerodynamic package which you see appears on the undercarriage of the race car okay so we can quickly tweak our design and I'm coming back to the original question about how do you do not know which design is best in this case it actually turns out to be pretty complex so there's a diverse set of requirements that these vehicles are going to be judged on related to acceleration and handling and energy consumption and what makes it even more challenges challenging is that these these requirements often conflict with conflict with one another so you know if you improve in one it often takes away from the other there's also an Associated scoring system that's that somewhat complex and has many competing priorities so it's it's pretty complex to you know actually figure out which design is gonna suit your needs the best and we ultimately deal with this by running lots and lots of simulations so in this case for literally hundreds of design alternatives then for each simulation we then score that design based on the relevant criteria and then we see which one gives the highest overall score okay so here you see that the designs as you kind of move from left to right tend to have a higher overall score and so you know we ultimately choose the designs toward the right and for situations like this where you need to run many many simulations teams generally turn to MATLAB to programmatically define the model parameters and run the simulations using a few lines of code rather than manually you know changing parameters within the simulation model itself and clicking the Run button and so forth you can actually do it programmatically through MATLAB also if you have so many simulations that just takes too long to run them all on a single machine you also have the option to offload them to clusters or the cloud so that they can run in parallel to make it to make the process a lot faster and just to kind of close out this piece for the latest Formula Student challenge almost all of the teams so I would say at least 90 percent of them use Simulink to design their vehicles in a manner similar to what I just showed so it's quickly become almost a de-facto standard for this particular competition it's really a great example of how simulation is helping students be successful with their capstone projects okay switching gears the the final example we'll talk about is an Arduino based automatic guitar tuner and this one's really in stark contrast to the Formula Student example since in this case there's really no physical design that needs to be modeled and simulated it's more about the code generation Hardware targeting side of the flow as we develop and deploy an audio processing algorithm onto our hardware so let's take a look and here we have our LED display with a push button so you can select through the six strings and as you can see our guitar is very out of tune right now so we're gonna go ahead and tune it one string at a time and once you play the string and the motor stops turning that's how you know the string is in tune and you can move on okay so Ben did a nice job introducing the problem let's take a look at how this is actually working under the hood so here's the underlying Simulink model and on the left-hand side are the inputs and on the right-hand side are the outputs so the first input is the push button used to select the string so this goes through a state flowchart and lights up the appropriate LED and the second input is the audio input signal from the guitar string so this is going to go through a matlab function that compares the actual signal to a reference signal and turns the servo motor accordingly so that at a high level is how this thing is operating now digging in a little bit further into the string selector or state flowchart it's really pretty straightforward so the way it's set up is that there are six states corresponding to each guitar string and the transition occurs when the button is pressed and depending on the state that you're in it assigns a period range which is then sent as the input to the MATLAB function block and the MATLAB function block is really where the the audio processing happens and is the real meat of the application and again and this function determines the difference between the actual audio signal and the reference for the particular string that's selected and it determines which direction - then turn the servomotor and how much based on the difference between those signals okay so at a high level that's how this project works I just want it to bring attention you know for this particular application a big part of what makes simulants so valuable is the ability to combine the audio processing you get with MATLAB with the system level logic that you get with stateflow and embed all of that onto your hardware so you know Simulink is sort of serving as this integration platform which it turns out is really necessary for just about any real system level design application so with that let's wrap things up so hopefully today's session was a useful starting point for anyone interested in building projects that incorporate hardware I encourage you all the tryouts Simulink soon while it's still fresh in your mind so as you saw it provides a platform for both modeling and simulation and hardware targeting you know some projects again they're going to involve the full flow others may focus on one side of the flow if any of you want to try out the rover or motorcycle projects for yourself I'd encourage you to look into the Arduino engineering kit which can be purchased from Arduino and many of their resellers another thing about Simulink is that it integrates languages so we showed quite a bit about how we can tie in MATLAB we also showed stateflow and sim scape there's other languages that can be tied in as well and you can choose what's right for your particular task Simulink also supports many different types of hardware so today's examples were mostly Arduino based but you can also target virtually any other type of hardware whether it be microcontrollers or DSPs FPGAs mini drones or even mobile devices finally Simulink is used in industry to design some of the most complex systems in the world so it's a really valuable skill to have on the resume one example I'll kind of close with is that NASA used it extensively to launch the Curiosity rover a pictured here to Mars back in 2012 and it relied heavily on simulation since obviously if you think about it they didn't have the to test the physical design in advance they they really only had one shot to get it right so they had to make sure the simulation was spot-on so I'm going to close with the following footage that portrays how this Rover was deployed so first this one-ton object travels 350 million miles through space and as it approached Mars the system deployed a parachute to slow down to around 200 miles an hour and then use rockets to hover above the surface of Mars while lowered the rover to the planet's surface on a tether and then use explosive devices to separate itself from the sky crane and you see the end result is this this beautiful rover on the planet Mars so obviously this is an amazing achievement and you better believe they use all kinds of Simulink and simulation and code generation to make this a reality so thank you
Info
Channel: MATLAB
Views: 26,701
Rating: 4.9670782 out of 5
Keywords: MATLAB, Simulink, MathWorks
Id: 1-f733pjxX0
Channel Id: undefined
Length: 37min 32sec (2252 seconds)
Published: Wed Jul 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.