Constant Angular Acceleration of a Stepper Motor with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the first issues that i came across when working on the robotic arm project had to do with the actuation of stepper motors i was using the a4988 stepper motor driver which are extremely common in hobbyist projects however i couldn't find any resource online or any code that explained how to achieve constant acceleration for the stepper motors so i decided to solve this problem myself and i found a way to to do it and that's why i'm making this video to share with you in case anyone else has the same issue that they want to solve and don't know how to approach it i'll try to section this video into different sections the first one will be talking about a simple implementation to achieve some sort of acceleration that method is shown in several other youtube videos and other forums however the second part of the video is a completely novel came up with me and that's gonna be how to achieve constant linear acceleration so without further ado let's get started so the first method i'll be showing you guys is a very simple way to achieve some sort of acceleration and this acceleration will not be linear acceleration but if your application doesn't require linear acceleration this could be a very simple way to implement some sort of acceleration so the stepper motor driver that i'll be using is the a4988 and it's very similar to a lot of the lower end stepper motor drivers if you have something completely different or it's very expensive chances are it might have some sort of angular acceleration implementation in it or a different way to drive it in which case this these methods may not work so i'll show you guys how this uh stepper motor driver works um what you do is you send it pulses and every time the stepper motor driver sees a rising edge it will step the stepper motor by one step and that's just a single fixed angle that's a terrible arrow but basically this angle is a step i'll call it theta and for my stepper motor um it's this theta is equal to 1.8 degrees might depend on your stepper motor what yours is um and so this is how you send you control the stepper motor and this is how the a49888 works so what you see the this series of pulses are happening at a constant frequency so what you get is a constant angular velocity um with no angular acceleration at all and you can kind of see how if we have if you want constant sorry if we want angular acceleration we would have to decrease the pulse width sorry not the pulse width but the distance between the pulses over time i drew this terribly but you can probably see that the distance the time delay between the pulses here is larger than here and we want this to slowly change over time and that will achieve angular acceleration so one easy way to do this is just just to have a for loop and decrease the the delay the time delay by a fixed amount every single time so for example um let's say that this was 6 000 microseconds and the next pulse we would just delay it and maybe that will be 5995 microseconds and every subsequent pulse we can just decrease by five microseconds every single time and this five microseconds is a arbitrary value you could make it one microsecond in which the angular acceleration will be smaller and this is very easy to implement in code so i'll be showing you guys um is just a for loop and you just decrease it every single time and i'll show you guys a video of how it it looks in the actual stepper motor but before that i'm just going to show you guys the caveat with this method and that's that the angular acceleration is not constant and you will be able to see this um in the video of it actually running but what happens is it itch the angular acceleration is very minimal at the start and towards the end you will see a sudden increase in angular velocity and if you wanted to look at the math angular acceleration is equal to the change in angular velocity and angular velocity and then we in this example let's call each of these time delay so that's just t d and then we can say that that's angular the change in angular velocity is equal to your theta divided by um this td minus some sort of constant so in this case the constant would be 5 microseconds and that's just arbitrary minus the same theta divided by regular mole td so that's angular acceleration and as this term gets smaller and smaller as your time delay reaches gets smaller closer to the end of the acceleration cycle this will become will go up very fast this term just because the denominator gets very small and closer and closer to zero it won't reach zero just because um you can't have the let's say the stepper motor step every one microsecond that's impossible so there's a limit to how how much this can decrease to but up to a certain point this will be getting larger and larger and the this term will get larger much faster than this term will be and so overall your acceleration will have a huge spike near the end of the acceleration so i'll show you how to implement this in code and then we'll see it in action okay so over here is the code i just quickly coded this up with the the new arduino ide the 2.0 beta version that just came out and i have to say it looks so much better than the old arduino ide which was really out of date so i'm very glad they did that um there's the code is very simple um i'm only defining two pins the step pin and the direction pin the step pin is the pin that you want to send the signal to every time you step the stepper motor by a single step and the direction pin is only useful if you want to change the direction which i'm actually not doing in this case but i just set it just in case and then in the setup i set both the step pin and the direction pin as output and i set the direction pin to high which i believe corresponds to a clockwise rotation but um i could be wrong we'll see we'll see when it when we actually run it and in the loop we first set the stepper delay to be 3000 and this eventually will be 3000 microseconds and so over here we have a for loop that basically steps the pin the step pin and delays it by whatever the delay is so it starts off at 3000 and then returns it as low and steps it by 3000 sorry weights of 3000 um and then when we're done with the loop we subtract one from the stepper delay so the first time you run it it'll be 3000 the second time you run it this will be 2999 and this minus minus is just the same thing as stepper delay is equal to stepper delay minus one that's just a shorthand and other than that we run it two thousand and three hundred times so by the end of this for loop um it will be at microsecond delay and i experimented and this seemed like um the one of the fastest one of the faster speeds that the stepper motor that i have can run at and then after this this is really unnecessary but i just wanted to continue going at a constant velocity for the last bit so it accelerates from 3000 all the way to 7000 and it continues sorry uh 700 and it continues at 700 for the remainder i set it for 2000 steps and then it stops for one second so let's upload this code and see if it works so i think it's yep so it compiles it and it uploads it and let's check it out alright so i just loaded the program onto the arduino and um if the stepper motor isn't running just because i haven't connected the power supply yet um i just want to mention a few things this electrical circuit that you see in front on the breadboard is a lot more complicated than it should be just because i was testing a bunch of other stuff on the breadboard just a while ago and i'll try to put an overlay on this frame actually showing you the electrical schematic there's a website how to mechatronics they did a great diagram and i think you should be able to follow that and connect everything the way that their diagram has it and you should be fine um as in our program we only had to connect a step pin and the direction pin um actually the direction pin is kind of unnecessary if you want to omit that and no micro stepping so let's hook up the power and see if everything performs the way we want it to okay so it's just ending the last segment so that was the one second pause and it's starting off very slow let me see if it's in frame okay you guys can see it and over here it speeds up very very fast and then for the very last bit it's traveling at a constant angular velocity so i'll just play it again right and so we can tell that the acceleration feels very non-linear and um this is the if this works then um for your application by all means go ahead and you can implement it this way but in the next segment i'm going to talk about how to do constant acceleration so i'll just play this on one more time and all right so now let's take a look at how to achieve constant linear acceleration on a stepper motor and this will probably be the method that i'll be implementing on my robotic arm project and it's a little bit more complicated involves a little bit of math but not not too bad so let's get started um the first things we needed to find um just make sure we understand a few uh concepts so first things is anchor velocity um so the units for angular velocity are radians per second and we can say for our stepper motor i'll just draw one over here that say this is the start and that's theta um we can say that angular velocity is equal to the arc length of a single step so this is that angle which is theta um of a step and then divided by the time delay so we talked about time delay in the previous part um this is the same thing so this is the this the time between a pulse and that's just angular velocity now the other thing we have to talk about is angular acceleration and by definition this is d omega over dt what you can say is the change in angular velocity over the change in time um and that's just the change in time is going to be the ttd essentially so we can simplify that saying that change in omega over the time delay and we can rearrange that for change in angular velocity so that's equal to acceleration times oops times time delay and what's neat about this is the acceleration is kind of arbitrary we it's a constant and since we're using constant linear acceleration so i'll write it alpha c so this is just a random number that we set and time delay we know the first time delay so how this program is going to work is we're going to tell it the first time delay and it should be able to compute the next one um and then during the next step it'll just take the next one and compute the third one and then use the third one to compute the fourth one and so on so this is a it's called a recursive function and um we just need to give it the first one so these two are basically all known at least for the first step and it should be known for the next steps after that um so the third thing that we need to know is um well change in omega is equal to let's say omega 2 minus omega 1. the next one minus the previous one which hopefully makes sense and since we defined up here we can say that change in omega is equal to theta s divided by t d 2 minus theta s divided by t d 1 and t d 2 and t d 1 are just the next time delay versus the previous time delay and based on this we can believe we can simplify this yeah we can rearrange and so let's say we know td1 that's the first value we give it and we want to solve for td2 we should be able to rearrange for td2 so t d2 is equal to theta s think over change in omega plus theta s divided by t d1 okay so now we have a function of td2 in terms of td1 which is kind of what we need um we still have this delta omega term but we actually solved it up here so the final let's combine everything so combine and our final equation should be t d2 is equal to 1 over this is theta c i don't know so our alpha c which is constant angular acceleration times uh t d so this td will actually be the first time delay and then plus theta s oh why did i put one here no this is theta s and then over t d1 so that's the entire equation and what um and this is this would be the answer actually what i'm actually going to do is since i know that theta s which is the step that it takes oh let me go up a little bit more so this is the step that uh the arc length of a single step that and the constant acceleration term are linearly related so if you let's say multiply the you can conceptually kind of see that if you double each step well the arc length for each step you'll basically double angular acceleration since it's constant and i want to absorb the angle into the acceleration um just because to me the the angular acceleration constant is an arbitrary number i'm not actually trying to solve for a particular radian per second value if you do stick with stick with this one um it's actually the better solution probably let me make a little box uh oh here it is so yeah this is this is the solution that you probably want um for me i'm just playing around with the constant acceleration term so i'm gonna use the equation t d2 is equal to one over ac times t d1 plus 1 over td1 and this is it's the same thing um just i'm gonna have a different angular acceleration term then if i were to do it this way but i'm playing around with it see what type of acceleration i want so i don't really care so now let's take a look at the code and see how i programmed this here is the code that i wrote for the constant acceleration and i'll preface this by saying that i wrote this quite a while ago and this actually has micro stepping but i'm too lazy to change it back um to without i have to change some other constants and play around with it um so we're just gonna ignore the micro stepping parts compared to the other acceleration a lot of them is the same the step pin and the direction pin are defined these are for micro stepping there is an led i was just for debugging stuff so here is when we get to the important parts wait time is what i called the time delay delay so um i initialize this with a 0.003 value which is 3 000 microseconds if you read this as milliseconds and we have an acceleration term that is arbitrary and that's i defined that as 4 000 so this is the number that i played around with and 4000 was one that i i guess i i felt um looked pretty smooth or achieved a constant acceleration speed that i want and then there's a rounded wait time which i'll get into later i'll try to put a picture of the formulas that we we just created in in post-processing so that you can see a side-by-side comparison over here the pin modes the important ones are just step in and the direction pin again the the rest of these are all micro stepping and there's one line about the led here is the important part so i created two functions and these two functions are basically the implementation of the final formula that we derived one is for positive acceleration and one is for negative acceleration aka deceleration so the first one positive acceleration it takes in a the previous wait time and it spits out the next wait time essentially and all of this is just the code um of the formula so we have a change in velocity that's the delta omega term and that is uh we derive that as saying that's td times the acceleration term and this is just the final formula td2 this is cd2 essentially and that's equal to 1 divided by delta omega plus 1 over td1 and i just have another clause here saying that if this is too small set it to the minimum amount so this is a very minimum td that we can have before we reach the limits the physical limits of the stepper motor and return the wait time one really cool way uh one cool thing about the method that we did is the same exact formula can be applied just in reverse for the deceleration phase so the only thing you have to change is you have to multiply the acceleration by one you don't need to change any other formula you just have to say that it's the negative acceleration essentially and the reason why you can do that is because it's a constant acceleration if you accelerate it at positive 1 radians per second it's you're applying the formula and if you're using a negative 1 radians per second you apply the same formula so it's it's a really neat thing that is special to the way we derived it and here is the loop um set the direction and i accelerated it for 3000 steps and it only actually takes 1984 steps 19 1984 steps to reach the maximum acceleration which is defined by the wait time over here but since it caps it caps if i say i wrote a class saying that if it is under set it to this number this is the minimum amount so after 1984 steps um everything after that will be a constant velocity it capped out and so um basically this part will accelerate it for eight nine uh 1984 steps and then the remainder steps will be constant um velocity so we we should see it to accelerate keep steady and then i decelerate it all the way to um to the very minimum as well so that that takes 1984 steps and accelerate keep keep a constant speed decelerate and then a delay just so we know it finished a cycle and um that's the code um this code and along with the previous code i will include a link to the github repository so that you can play around with it try it if you want to do some other microstepping or if you want to remove it you can do that and yeah so let's let's run this code and check it out it should feel a lot more smooth than the first one so compiled and uploaded perfect okay so we just uploaded the consonant acceleration program um the wiring is the same as before we're just using more of the actual wires this time i'm not going to go through that again so let's just connect it and see how it works so over here you can see that i mean it is a faster acceleration constant but you can see how it's a lot more smoother it accelerates and decelerates at a constant rate i mean it's a little hard to tell but honestly if you feel it and hear the sound um you you can feel that it's a lot smoother than it was before yeah it feels a lot more natural alright so that concludes um this video um i hope that you enjoyed it and learn something from it and i'll see you again next time
Info
Channel: Sichun Xu
Views: 435
Rating: 5 out of 5
Keywords: linear angular acceleration, stepper motor acceleration
Id: Im2Dnc9_Ifo
Channel Id: undefined
Length: 25min 56sec (1556 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.