Programming for Robotics: Program a Driving Robot | Zoe Lambert | Skillshare

Programming for Robotics: Program a Driving Robot

Zoe Lambert, Programmer, Maker

Programming for Robotics: Program a Driving Robot

Zoe Lambert, Programmer, Maker

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
8 Lessons (34m)
    • 1. Introduction

      1:35
    • 2. Materials

      0:52
    • 3. TinckerCAD Circuits

      5:21
    • 4. The Circuit

      1:41
    • 5. Code Preview

      1:41
    • 6. Basics: Moving Motors Backwards

      11:13
    • 7. Functions and How to Use Them

      11:05
    • 8. Class Project

      0:52
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

130

Students

--

Projects

About This Class

Using TinkerCad Circuits, this class will walk you through the steps to program a robot to drive using 2 motors. This code is vital to building any driving robot. While the main focus for this course is the code, we will be building a simple circuit to demonstrate the code that we make.

The programing language that will be used in the course is the Arduino programming language, which is based on C/C++. If you already know some of the Arduino programming language or some C/C++, then you are already off to a great start. If you don't know any programming, you can still fly through this course but if you start to struggle, I suggest going to my other course; Programming for Robotics: Getting Motion. That course goes over the basics of programming a motor to move.

In the course, You will learn how to take advantage of a free program called tinkercad circuits (https://www.tinkercad.com/circuits) that allows you to program an Arduino and simulate running it without actually having to own one.

46850c96

Meet Your Teacher

Teacher Profile Image

Zoe Lambert

Programmer, Maker

Teacher

Hello,

My name is Zoe Lambert and I have a passion for robotics, art, and programming. I am always working on a project. My favorite ideas to work on involve video game design, crazy robots, and artwork. I also spend much of my free time mentoring middle and high school-aged students on robotics and robotics related topics.

I spend a lot of my time learning new things and look forward to sharing my knowledge with the community.

I plan to teach courses covering a variety of topics including:

1.) Robotics/Programming

2.) Animatronics

3.) 3D modeling/3D printing

4.) Video Game Design

5.) DIY projects

 

 

 

 

 

 

 

 

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

phone

Transcripts

1. Introduction: Hi, I'm Julie Lambert and I love robotics. I believe anyone can build a robot. Yes, it can seem intimidating at first, but really, it's easy once you get started. This class is a second class in my programming for robotic Siri's. Even though this classes for beginners I do suggest you go watch my first class called programming for robotics getting motion. That class will walk you through getting motion out of a motor and a server using king or cut circuits. This class will walk you through the steps to program the drive for two motor robot. While the main focus of this course is the code, we will be making a simple circuit to demonstrate the code that we make. So I'm gonna walk you through, creating a backwards function, a left term function and a stop motors function in the class and using what you learn for making those for the class project, you will be making a Ford function and a right turn function that will follow the same steps. Completing the class project will be a piece of cake. Once you've run through the class, I will be providing some resource is for this class and the project resource is all you really need is a computer with Internet. But if you'd like to do the circuit with real life, I go over what you need to do that in the material section. Also, I have links to where you can buy the materials online in the project description. Well, now that you know what this course is all about, let's get to work. 2. Materials: the materials you'll need for this course are just a computer with Internet to do Kinka cat circuits optionally You may want to try out your code in real life for this you'll need and are we know with the programming cable. So that way you can plug it into the computer. You will also need a H bridge to DC Motors, a nine volt battery and the cables to go along with it. Like everything it You also need the bread board that the H bridge will be mounted on. Now that you know what you need for the project, we can get started. 3. TinckerCAD Circuits : in this video, I will be explaining what Tinker Cat Circuits is and how to use it. Tinker Cat is a free program that allows you to simulate circuits made up of basic components. This is great for seeing your ideas come to life without having to buy any equipment. To get the Tinker Cat circuits, open up your Internet browser and go to tinker cat dot com slash circuits. There will also be a link in the course description. If you would like to just click on that once you get to the Tinker Cad Circuits page, scroll down to the launch Tinker Cat Circuits button. Click it and you will be prompted. The sign it. If you do not already have an account, then you will need to make one. Once you are signed in, you'll be brought to this page, which shows you all the previous circuits that you've seen. If you haven't made any yet, well, this page is going to be blinked. So to get started on a new circuit, you go to this button that says, create new circuit and it brings you to the simulation software. Now when you sign in, it may automatically bring you to this page with a random name for your project. I'm gonna go ahead and change this project, to be example Project. So that way I will be able to find it easily later. Tinker Cad Circuits has lots of components available to you over here on the components bar . There's many things for you to choose from. If you go to all, you will be able to see everything that's available to you, including general categories for them. So let's go ahead and try out an example in Tinker Cat circuits. We're just going to get a motor to spin using a battery. So I'm going to go to the output section of the components panel and grab a hobby gear motor. Then I'm going to go to where the batteries are, which are under power. Down at the bottom, I'm gonna grab a nine volt battery and place it into my workspace. Now I want to connect this part of the nine volt battery to this part of the motor. To do that, I just click where I want a wire to start, and I can click in every spot that I want it the wire depend until I make it over to where I want to connect it to, this little red box will pop up when you can connect that wire there and ended. Now, I just connected the negative on the battery to the negative on the motor, and that is usually represented for the black wire. So that's what I'm going to do now. I'm gonna connect the positive on the battery to the positive on the motor, and I'm going to make that wire, right? No, this components bar over here is kind of in my way. So I'm gonna go ahead and close that using this arrow. Now we can hit the start simulation button up in the top right corner. As you will see, our motor has started spinning and were given the rpm's of the motor, which is how many times a shaft of the motor is rotating per minute. Lastly, what I want to cover is how you would program something if we brought in a Arduino and we wanted to use that Well, we can program in Arduino, so we click on code and I like to use the text code. So I change it to text. It gives you a warning that your blocks are gonna be deleted. But that's OK. So now this code is already written for us because this was a pre made circuit and ah, we can just start the simulation. But this is where you would normally type your code for whatever circuit that you made. So this code just makes us led Blink on and off. That has been a basic overview of what Tinker cad circuits is. 4. The Circuit: before we get to the code. Let's first set up our circuit. If you took my other course, that doctor, about how to get one motor to move using Tinker cut circuits, then you'll already know the process it takes to hook up a motor to NH Bridge. Hooking up the second motor is just a ZZ as hitting the 1st 1 hooked up. You just used the opposite pins from the one on the first motor site. You also will mean to hook up your power wine to the nine volt battery. Also, I have the nine volt battery eight positive lead hooked up to a row of pins so I can hook up both the power one and the power to to the positive point on the battery. I also have the nine volt batteries. Negative lead onto the negative strip on the bread board. Make sure you also have the inputs and the enable pins for your second motor hooked up to the Arduino. There's a wiring diagram in the project. Resource is tab for you to look at to make sure that you get your motors hooked up the same way that I have mine hooked up here, Since this course is more about the code than it is about getting the motors actually hooked up, I'm not going to go through the entire set up of the cables here. If you want to learn more about how to do this, check out my first class that I did programming for robotics getting motion. This will go over the details of how to hook everything up to the Arduino and get a single motor moving. 5. Code Preview: the program that we're making in this course will be the code that you need to make a driving robot. We will be making two functions a backwards function and a left turn function. The class project will be to take what you have learned in this course and create a forwards and a right turn function for your robot. As you can see, the circuit that we're using has two motors and h bridge nine volt battery, an ARD. We know you don't need this to write your code, but it is nice to be able to visualize what you are doing. I'm going to start the simulation and you will see the motors move. So that way the robot would turn left and that will run for two seconds, then a role run backwards for two seconds. As you can see when we go backwards, both our motors or negative when we turn left are left motor is negative and are right. Motor is positive. If this were a real robot, that would cause the robot the turn left and go backwards. Now that you got a little sneak peek at what the code will be like when you're finished, let's get started. Actually making the code 6. Basics: Moving Motors Backwards: first program we're going to make. It's going to run both the motors backwards. First, we need to define all the pins that were using. We do this by saying Pound define the name that we want to use and then the pin. We need to do this for the inputs of each motor and the enable pins for each motor. I'm going to start with Motor one. So I say Define input wine. I'm going to define input one as pain number nine because that's where it's plugged in on the Arduino. I'm going to continue to do this until I'm done. Defining All depends for Motor one. Next, I'm going to do the same thing for motor to. I'm making sure to add comments to my code. So that way, in the future, when I come to look back at my code, I know what everything does. I have labeled the enable pin for motor to as unable to since it's far second motor after we have defined dollar pins, it's time to work on the set up coat. This code runs once at the beginning of the program. In the set up function, we want to set Alor pins toe outputs. I'm going to start by setting up motor one. We say them mode, the pin that we want to set. So I'm going to start with the enable one pin and then we say output or input for this older pins air outputs. So we just say output in all caps. We don't forget our semicolon at the end. I'm going to go ahead and run through this for all depends for motor one and motor, too. Now I'm done with the set of function and I can move on to the main loop in the mane loop. I want the motors to run backwards for two seconds at 50 rpm. Decode. This will start by setting the motor rpm's. We will use the enable one and two just set the motor rpm's to set the motor Rpm's. We just type analog, right? We say the name that we want to do so enable wine and then we see how fast we wanted to go . So 50 rpm again. This is something that I'm just going to copy and paste for motor to and just change the name. Since it's the exact same thing. Except for with a different label after we have set the motor rpm next week. Need to set what direction we want each motor to spend. I'm going to start by setting the first motor to run backwards. We do this with digital rate, then we need input one and then put two. Since those go to motor one when we say whether it's high or look to get the motor to spend , you have to say in put one toe high and input to too low or the opposite, depending on which direction you want the motor to go. The motor will not move if you set both the high or both. Too low the high and low in this well, be in all caps. This requires a little bit of testing to know which way will actually make your motor turn the direction you want. So I'm just going to set it in, put one toe high and input to toe low for now and then I'm gonna run my coat and see if the motor runs negative or positive. I'm gonna run my code real quick to test and see which way the motors moving as we can see the motor is moving in the positive direction, which is not what I want. So I need to switch the low and the high on my inputs. Now that I switched. Which input is the low in which input is the high? The motor should be moving in the direction that I want. Great. Now let's move on to making the second motor move. We're gonna just copy what we did for motor one and change the labels toe work with motor, too. There's no point and remaking code that you've already made. This saves you time and effort. We'll have to run the same test again to see if the next motor is hooked up the same way. Great. We're lucky they are hooked up the same way. So that means they're motors are now both moving backwards. I want this to delay for two seconds before continuing to move backwards. This command is easy as it's just delay. 2000 delays air done a millisecond. So 2000 represents two seconds. Let's start a simulation and see what happens. As you can see, both our motors are moving backwards, but they're not actually stopping tohave are motors stop after the two seconds we need to add in setting the motors to not run. So this is where, after our delay, we will add in the zero rpm's. So it'll stop trying to run our motors. I'm going to copy where we said our rpm's for motors before and put it in below our delay. Obviously, we don't want to keep this at 50. Will want to change it to zero now that we have changed our enable pins. Zero last thing we need to do without in another delay for how long we want the motors to be stopped. I want them to be stopped for one second. So we do another delay and do 1000. Now when we start our simulation, our motor shabu backwards for two seconds, be at zero for one second and then move backwards for two seconds again, and I'll go on and off between these two activities until we stop the simulation. The reason why it starts moving backwards again is because our code moves from the top to the bottom. So we starter code and it sets are motors to 50 rpm's and then tells them which direction to spend then it does this for two seconds. Sets are motors to zero. So they're not running anymore. Does this for one second? And as soon as that one seconds up, this code has already moved back to the top of this look from there, it will continue this loop until we stop the program. Now, this is a lot of code for just saying to move our motors backwards and then stop for one second. If we want to move left, we would have to do all this code again. Specifying are exact speed that we want to go and which direction each will needs to go. They'll make our code much longer than we want it to be. A nice way to make our code clean is to use functions just like our set up function and our loop function that are already provided for us. We can create our own functions that allow us the type backwards, specifying our speed and for how long, while only writing the backwards code once we're going to learn how to do that next 7. Functions and How to Use Them: to get started on her functions. I'm first going to create a section in the code that is specifically for the functions for driving. You want this section to be before your loot? A function has a name for the function just like our set up. But it can also take in parameters that I can be used within the coat. An example of this would be if we made a drive backwards function. We would want it toe have both the speed. We want the motors to move and for how long. These two things are the parameters and we can use them instead of the numbers that we have in our loop. So let's make this backwards function. Since our function does not return anything, we start out by saying void. Next we put in the label for function so backwards. Now, both our speed and our time are integers which you're just whole numbers. So we need to specify this by saying ent and then the name for our parameter. So we have speed and int time. Now Arco goes inside the two curly brackets, so we basically can copy and paste our code from our loop because this is where we made our motors go backwards. We only want the backwards code, though. So Onley copy that you don't need the code that sets motor of PM 20 or delays for one second. All right, now, this is when our parameters come into play. Instead of typing the 50 here, you wanted to be whatever speed was specified when the function was called. So we just put in speed and then time will be used for earthy delay. So instead of 2000 we wanted to delay for However, long time is that is our first function. Now you may be wondering we created this function. But how do you use it? Well, use it by going down into your loop, we'll get rid of the backwards. Could we had before and user function this time. So to use our function, we just type backwards, which is the name of our function. And then what we want for our two parameters. So we had the motor moving at 50 or PM's 42 seconds. This is the same thing as we had before. This just calls the code that we have up here and it runs it exactly as if this code was in the loop itself. Now, if you run the code, let's pull it to the side, will be able to see what our motors do. If we start the simulation, they're doing exactly what they did before running backwards for two seconds, stopping for one second and then going backwards for two seconds again. This process has just repeated for all the rest of the functions that you want to make a backwards function, the four words the right turn and left turn. And then we also need a stop button to tell. Now you just repeat this for all the functions that you want to create, a backwards forwards left and right turn functions. Also, you will need to stop function to be able to tell the motors when to stop moving. Let's go ahead and make that stop function. Stop the simulation. Let's make our stock motors function Above are backwards function, so stop motors, and we're gonna want to specify how long so the stock motors function is only gonna have the parameter time. Let's go ahead and make that we have void stop motors and this is again an integer So we say int time, but our curly brackets in and fill in our coat. We can go ahead and copy the code from our loot function. That was our stop code before and place it up in our stop motors function, and then we just need to change the delay to use time instead of one doesn't. All right, so now we have both are backwards and our stop motors functions. Let's go ahead and add in our stop motors function. And when we tested, it should be the same as it was before reaching to the coat. We want to stop our motors for one second. Let's make it so we can see our motors again and starter simulation and our code is working . So there's one more function that I want to show you how to make, because our motors are going to be moving in opposite directions, and that can seem like it's gonna make creating the function harder. But it's really not. It's the exact same way as we laid out or backwards function, except for we're going to be turning so one motor is gonna be going forward and one motor is going to be going backwards. We'll do the left. Turn on my left turn again is going to need speed and time as parameters. So void left, turn and speed, comma and time and then our curly brackets. All right, so I am again going to copy the code from our backwards function and put it into our left term function and just change the parts that need to be changed. This is basically everything that we need to dio, except for we need to turn one of our motors toe, turn in the forge direction. So we need our right motor to turn forwards and are left motor to turn backwards. My murder one is the left motor, and my motor to is the's right motor. So since we already know that running the right motor with input three at low and input for it high makes it run backwards to make it run forwards, we just switch him. So input three runs high and input. Four runs Look now are left turn function is complete em. We can add it into our loop. So left turn at Let's do 20 rpm's for to second. All right, I'm going to put another stop motors function under that, and we're going to stop him again for one second. All right, let's run this code. If everything goes correct, we should see our motors both run negative, go to zero, are left motor, run negative and are right motor run forwards and then stop motors again. And then the cycle repeats until we stop our program. So let's see if we did everything correctly. Ah, see, this is where mistakes can happen. I labeled this sped instead of speed. So let's just fix that and try running our simulation again. Spirit spelling errors like that can happen. Commonly. Don't let it get you down, all right? It looks like our code is writing exactly the way we want you now have working coat 8. Class Project: in this class recreated the backwards, the left turn and the stop motors functions. The class project will be to create the forward and right term functions for your program. Thes functions will complete your code for robot Dr Functionality. Your function should take in speed and time, just like we did with the backwards and left term functions in the class. When you have completed your project, make sure to take a screenshot or upload the code to a project and share it with the class . You can check the solution for your project by uploading it to the class for feedback. Or you can look at the solution code that I'm going to post in the project. Resource is, I look forward to seeing everyone's projects, if you have any questions, pushed them in the discussion.