Build a Real Robot - Episode 11 - Motor Coding

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in episode 11 of the Builder real robot series were still programming the motor controllers I'll fill you some of the test code I've written and also discuss some power considerations when working with the Arduino nanos were coding away today so welcome to the workshop [Music] you [Music] hello and welcome to the workshop into episode 11 of the Builder real robot series I just want to bring you up to speed as to what's happening with db1 right now what I've been doing with the robot is I've been writing the code for the motor controller and as I rate the code I've been doing it sort of piecemeal writing little test scripts in order to test certain aspects of the controller and I'm putting all of those test scripts together into one script and what this becomes is sort of a diagnostic program and I'll use the routines inside this diagnostic program to create the actual code now by doing this I not only end up creating the actual code I end up with a diagnostic program that I can use later on when I'm developing the robot if all of a sudden something doesn't seem to be working right by having diagnostic programs at key locations I can go back quickly and make certain that certain things are still working the way they're supposed to be and use it as a troubleshooting tool to find out what isn't working the way it's supposed to now there's still a lot of code to write but I do want to do a bit of a demo for you today I've got db1 here right now and it is powered on and it's got the two arduino nanos over here which are forming our motor controller and each one is running a demo code so if I press a button the reset button on one of the nanos and I wait a couple of seconds the wheel will start moving forward at a speed of 200 reverse at a speed of 200 then it slowly accelerates up to 255 and stays there for a few seconds and decelerates and then after a few seconds if there's the opposite direction accelerates and decelerates and so that seems to work pretty well so what I want to do today is show you some of the codes specifically the code that has to do with acceleration deceleration and determining the i2c address that the Arduino is plugged into and I also want to talk to you about working with the Arduino nanos while they're on the robot because there are certain considerations regarding power supplies so that's what we're going to cover today now when you're working with the motor controller you are obviously going to want to program it and probably make some changes as you're developing and in order to do that all you need to do since we're using Arduino nanos is to connect a USB cable with a mini USB connector to the nanos from your computer and you can go ahead and program them well that works really great but there is one consideration you have to take into account and that is 5 volt power when you do that when you take a cable like this and plug it into the Nano you're supplying 5 volts from this cable however the robot itself also has a 5 volt power supply so if you are doing this you have the possibility of connecting the two power supplies together and it's debatable whether that's a good thing or not I tend to try to avoid it and so here's a couple of things I've been doing in order to try to avoid it now the first thing of course you can do is simply take the Nano out of its socket you can power everything downward being powered by the umbilical cord right now and you can just pull out the Nano from its socket and go and program it and that's just fine and I've even done that a few times but there are times you want a program that wasn't on the robot especially if you want to use a serial monitor which I'm doing with my code in order to read back from the rotary encoder so the best way to do that is to go and go to the 5 volt fuse over here and remove the fuse temporarily just take the fuse out you'll notice that kill'd are 5 volts but keep in mind that the 12 volts is still there and you can plug your cable into one of the nanos specifically the one that you're interested in and you'll notice that's powering everything up and it's starting everything off - because the two controllers just receive power and they're just starting up so they're gonna go through their sequence right now now one aspect about that of course is that you are powering both of the nanos with the same computer now it's not a great feel with current so at this point in the design that's fine but those of you who are observant will also notice that you are also powering up the Arduino mega you can eliminate that despite pulling this cable over here and now you're just using the computer the power of the two nanos over here which the computer I'm sure is quite capable of now as the design progresses of course there will be more items on the 5 volt line and it might be more than you can really want the program with your Nano but hopefully at that point we'll have the motor controllers all programmed and the need in order to do this will be very very rare now another thing you could do would be to create a USB cable in which you remove the 5 volt line if you want to modify a cable use a break up box that would be another way to connect it and just always use the power on the robot but I just wanted to mention that it is important to consider where the power is coming from when you are programming the nanos now I just want to show you a few aspects of the code that I'm writing this is a work in progress and in actual fact this is not the latest version of it but it has the elements that I want to show you now I'm not going to go through all of the code there's a lot of it over here but I want to take a look at a few things and the first one being how we select an i2c address now as you recall on pin date of 4d 4 on the both arduino nanos i have a connection on 1 nano it is connected to ground and the other it is connected to the 5 volt line so 1 nano will see this as being low and the other one will see it as being high and this is how we determine our i2 see address in this way we can write the same code for both units because the only difference between the two is going to be that address so a couple of things that are involved in there first of all we define pin four as being the one that we are selecting the address from and then we have two different address selections the i2c zero is address nine i2c one is address 10 now I'm using those two addresses right now rather arbitrarily I may change them in the final product depending upon what other i2c devices are going to be on the same bus and so basically these parameters are used in order to determine the address let's go down to a function that I've got to do this actual thing and it's down here and here we are set I to see now the set I to see function basically doesn't have any inputs to it all it does is it creates an integer called the i2c penn state and the integer of the i2c address is going to be the final address and the penn state is just a digital read of that particular pin and then if the pin equals one then we use i2c I sort of depending state a zero or low then we use i2c I address zero or if it is high or a one we use i2c address one have we returned that out of the function and down the set up we actually call that function and so we basically set the i2c address the final variable that we're using to to hold the i2c address as the result of the set I to see function so that's pretty simple now another thing that we're doing in this sketch and I want to show you is acceleration and deceleration because those are going to be used in the final product now in the final product these functions are actually going to be buried in to another function because these functions basically become parameters of that function but I want to show you how it works because I went through a lot of thinking about doing accelerating and decelerating and I came up with something that's quite simplistic but I think actually works quite well now here's the accelerate one I call the function motor excel and you in input three different things you input the lowest speed the speed you want to start accelerating from you input the high speed the one you want to accelerate to and then this one is the direction of the motor because I'm going to do it all in the same function over here now the first thing I do is I just digital write the direction of the motor out to the pin which is this one over here that is the dur pin on the motor drivers and that's basically just passes this over to here now for the actual acceleration what I do is I use a for loop and I go for the interfere I goes from speed low all the way up to speed high it increments by one on every increment I write out to the pwm pin the value of I so it's gonna start off as being the lowest value the next time I write it'll be one more the next time one more etc etc and I'm experimenting with this right now but if placed a small delay of five milliseconds in here just so that the acceleration is gradual and not instantaneous and this number could be played around with a bit but right now I'm pretty happy with it and so it just steps through the entire range and as a result the motor accelerates and deceleration is exactly the same thing it has the same inputs we write out the direction but in this particular case we're going from the high speed to the low speed and instead of incrementing or decrementing but otherwise it's identical and it does seem to work pretty well now there are other ways of determining accelerations some of them are quite complex but I think for most practical purposes that should work fine and let's see is there anything else here in the code that I want to show you well one thing I'm doing in this code that I have done on previous previous codes using PWM is I'm using a function which is not my own function over here let me just get to it this one over here set PWM frequency this lets you set the frequency of the PWM output and as I showed you in an earlier video you can get quite improved performance by raising the frequency so I'm using this function which is not my function as a function I've borrowed from elsewhere to set the PWM frequency and then in the setup I will call that function right over here just to set the PWM frequency and basically I call it with the pin I'm using and the value over here is the divisor amount the PWM frequency is determined by a counter inside the Arduino and this just kink is the default divisor the counter to something else so that the frequency is higher so again there's a lot of code over here and eventually this will become a final product but it's working progress that I just wanted to show to you all right well I don't really have that much more to show you for today so that kind of wraps things up right now I will let you know where I am standing in the code right now now with the test code as you can see I can move the motors which is not that difficult to do and I've also added acceleration and deceleration which I was happy to be able to add to it because those are some of the parameters we need for the motor controller the controller can also recognize its eye to see addresses by looking at the state of pin d4 and so that works well because that way I can put the same code onto both tips and it will just pick up the correct i2c address what isn't working correctly right now is the rotary encoders I am able to get signals from the encoders and read them as interrupts I have a timing problem in my code I know where the problem is I just have to resolve it so I'm not getting the right rpm reading but I don't think that's going to be a difficult thing to resolve and the next thing after that is to do the interrupt for the emergency stop so I'll be working on those this week as well as with the i2c interface aspect of it so we can start controlling this controller through the Arduino mega that sits beside it so next week when we get together I'll show you what the status is on that and hopefully we'll be at the point where we can start sending i2c commands to it now I will create a small article to go along with this video there's not really much else to report but if you wish you can have some of the test code I'm working on right now understand that it's Ana beta version so it will be subject to change but you can perhaps use it for your own project and speaking of your own projects last week I put the question to all of you about having something like a forum or another aspect that we can use in order to dialogue between each other and talk about our robot projects and I got some great responses I'd like to thank you for those I still am leaning toward the idea of the forum and a moderated forum and so what I'll be doing soon is putting on a call for moderators so if that interests you please let me know now if you haven't subscribed to the YouTube channel of course go ahead and do that I would really appreciate that so until the next time that we meet please take care of yourselves please keep working on your robots and I hope to see you here very soon again here in the drone bot workshop supply for now [Music]
Info
Channel: DroneBot Workshop
Views: 9,351
Rating: 4.927835 out of 5
Keywords: build a robot, dronebot workshop, arduino nano, arduino robot
Id: 1aAmUBKdWGQ
Channel Id: undefined
Length: 14min 57sec (897 seconds)
Published: Thu May 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.