Skip to main content
Skip to Main Content
Skip to main content
Navigation

Get Your Groove On with VEXcode V5 Python!

In this Live Session, Alaina completes the Groove Machine Challenge from the VEX V5 STEM Lab - Loop, There It Is! She shares the code in VEXcode V5 Python and explains the process of coding loops like for and while True in order to create a dance move for the V5 robot.. 

(upbeat music)

Hello and welcome to the VEX Classroom. My name is Alaina, and we are here today to talk about Coding with Python in this live session. For this session, we are actually going to be using our VEX V5 to compete in the Groove Machine challenge from the Loop There It Is STEM lab. If you're familiar with that at all, you know that we've never really done a Python implementation before. This is going to be our first time doing that Python implementation where we create our own dance move with our robot and have it go through and code it using Python in this case.

The first thing I want to do is go through some of the criteria for this dance move that we're going to be making. We're going to need to create dance moves ourselves and then translate that into some robot behaviors. For the criteria itself, we do have a few things. We know that the dancing needs to continue repetitively until the Stop button on the Brain screen is pressed. We also have a few additional criteria: the arm must be raised and lowered, the claw must open and close, and the Clawbot must turn left, right, and then drive forward and in reverse. We have quite a few things to do, and I've spent some time thinking about a dance. Now bear with me as we go through and plan out our dance moves together here.

What I thought of for this dance move that I want to do for the groove machine challenge is stepping to the right and forward a little bit. It's kind of hard to see since I'm behind a table, but right and forward. Raise your arms and clap. Step to the left and forward. Clap again. Then step back into the right. So you're turning. You can make that repetitive dance where I would go in a circle stepping and clapping if I repeated this process. Let's break that down into robot behaviors. Obviously, turning to the right and moving forward is something that we can create an easy parallel with the drive forward, or drive in reverse, turn right, turn left. But I have to think about what my arms and my hands are doing and how those relate to robot behaviors as well.

In this case, when I raise my arms, I'm going to actually raise the arm of my V5 Clawbot. When I clap, I'm going to close the claw. When I open my hands back up away from the clap, I'll open the claw back up. When I lower the arm, or I lower my arms, those are two also parallel behaviors that we're going to have. Based on our dance move of step forward to the right and clap, step forward to the left and clap again, put our arms back down and turn to the right, we are going to go through and look at some pseudocode in Python in VEXcode V5.

Let me open up VEXcode. I will go ahead and make it a little larger. You can see here in VEXcode V5, there are some different ways to increase your font size. You can decrease it. What I am doing is using the context menu and actually using this font increase, or I can use that shortcut that's provided as well. Here you'll see I have my comments already made in my project. For those of you that are not familiar with comments in Python, those are denoted by the pound symbol and then any sort of wording after that. You can tell when I move the pound symbol, it actually changed some of those commands. I'm going to undo that. Those are all of our comments here: turn right and step forward, raise the arm and clap, turn left, step forward again, clap again, turn right, and step back, put the arms at the side, and wait a second, and do that repetitive movement, which does meet all of the criteria that we saw previously.

The first thing I'm going to do is add in some commands here. The first thing that we're doing is turning right and stepping forward. Now every Python command begins with the object. In this case, we're beginning with our drivetrain, and I am also getting this auto-complete here.

Thank you for joining us in this session. We hope you found it informative and engaging. If you have any questions or need further assistance, please feel free to reach out. Happy coding!

So it's helping me figure out what exactly I want to type. I know I want to turn for a specific distance. I am turning to the right. Now, we separate our parameters in Python with commas. So I'm gonna turn right for 30 degrees. Now I can see right for 30 degrees. My next step is to drive forward. I can use that auto-complete again. So drivetrain.drive_for forward, we'll say 200 millimeters. Now I can actually take this and download this to my robot.

I need to go ahead and plug that in to test out our commands. This is just a best practice. I am testing every time I complete a section of my pseudocode. So I'm gonna download this to the Brain. We'll go ahead and move to our top down. You can see here when I start this project, it should turn to the right and drive forward as the first part of our dance. Perfect, that is exactly what we wanted it to do.

Now we have already coded our robot using Python, which is fantastic to start. Now, we're gonna move into our next set of commands. Rather than having me type all of these, I'm gonna pull up some pieces that are already done. We're gonna raise our arms and clap. Here you'll notice I'm using a spin to position. I chose spin to position for the arm and the claw motor because I know exactly what position in an absolute way that I want these to move to. I went through, used the devices screen on my V5 brain, made sure I know exactly that when this is all the way open, this is my zero because this is where I'm starting from for my claw. When it is closed, we're at roughly 250 degrees. So that's the position I want to move to. Open it back to zero. With my arm, I want it to raise to roughly 450 degrees. That's not all the way raised. You'll remember when I was practicing my dance moves, I didn't raise my arms directly up in the air. They're slightly angled up. So I'm using my robot and slightly angling that arm up, which puts me in about 450 degrees according to this device's screen.

Now I can do the same thing that I did before, and we are going to download this section to my robot. Steps one and two. It works out very well. Just like a dance move you would do normally, you have step one, step two, or your eight count that they do as well. Now we're going to see our robot turn to the right, forward, raise, and close the claw. I know that this is an interesting angle, but you can see it's not raised all the way to the top. We're raising it just slightly up so we can mimic raising the arm. Go ahead and stop the project and now we can move further on into our Python project.

If you do have any questions about anything that I'm doing as we go through, there is someone in the chat who is happy to try and answer any questions. If you have a more complicated Python question, please ask in that community thread that we use for this particular live session, and we'll be able to add anything else or show you different example projects, or even if you wanted to show off your own dance move as well.

Now we have our steps, and we wanna turn left and step forward again. There is another way you can add commands here in VEXcode V5 in Python. That is actually still by dragging over commands. I can grab this command here, our turn_for, drag it over, and then just change this to left, and we'll save 45 degrees, and then do our drivetrain drive_for. This is really easy if you're just getting used to Python. That drag and drop is still there as part of it, which is what you might be familiar with in Blocks as well.

Now that we have our step here, let's go ahead and add our clap again. Just while we're here we can go ahead and do that. We have our claw motor spin to position. I'm gonna bring this over. Claw motor spin to position. This time our claw is already closed. It's at 250 degrees. We want it to go to zero. Then we want it to go back to 250 degrees again.

So we're using that knowledge of our positioning system, our absolute positioning, that's done through the motor encoders on our V5 Clawbot in order to do this. So I'm gonna download this project again. The other thing you can do, if you don't want to move back and forth every time between your computer and your robot, is actually connect this to a controller, and then you can actually download with your controller every time to the brain that's connected there.

Let's troubleshoot where our connection is happening. So I should see that turn green for my brain. One moment. Let's see. Sorry, everyone. You know, every time you're working with any kind of technology, there's always some sort of tactical issues. I have restarted my brain. I'm going to plug that in again. And now it's working just fine. So that's a really great way to troubleshoot if you ever do need to, is just restarting your V5 brain can make a huge difference.

So I'm going to download this. So this is steps one through four. So now we should see turn right and step forward, raise the arm and clap, turn left, step forward again, and clap again. So I'm gonna pull this back. And we are going to go ahead and get started. And you can see here, it ended with it clapping again. Let me show you from that front-facing view, which might be a little bit easier. I wanna make sure every time I reset to having my claw open. So now I have this aligned. We're going to run, turn forward, raise, clap, left forward, clap again. So it's following those dance moves that we've set out. I'll go ahead and stop this, and we'll go back to VEXcode.

So now we've done a lot of these positions. We've worked through a lot of these basic commands as we go through, and we know what we can do in order to continue to add to this project. So we're going to turn to the right again and step backwards. So I'm just gonna take some other pieces of our code and use that, except for we wanna make a larger right-hand turn, and we wanna drive in reverse. Let's go ahead and drive reverse for a little bit longer. And then we wanna put your arms at the side just so you have everything back together. So we need our claw motor to open, and we need our arm motor to move back down to its zero position, which is on the bottom. So arm motor spin to position zero. So now we have all of these dance moves ready to go for our robot.

We will connect this. My goodness, my brain today. There we go. We're gonna go ahead and download this project. And we'll come back here so you can see the full dance move. And don't worry, I'll do it right behind the robot just so we know that our pseudocode matches the actions of our robot. So we're gonna go start this, turn right, clap. Now, I hope you noticed what my mistake was in this particular case, is I didn't open my claw again. So I'm gonna stop my project. I'll reset the claw. These are all things to keep in mind, especially when you're working with students. What are some of the common mistakes is that you're not resetting your claw to that specific starting position that you know it needs to be at. It can't close 450 degrees if the arm, or 250 degrees, if the arm, or the claw, excuse me, is already closed. So we have to make sure everything is back in its proper position before we start our projects as well.

Go ahead and hit the run. Turn, step forward, raise the arms, clap, turn, forward, clap again. Now we're gonna turn, back up, put our arms at our side, and now we're ready for our next step. So we've made it through, and we've coded our robot to go through all of these steps. The next thing that we need to do, according to our criteria, is to make this a repetitive process, or a repetitive project. There's a couple different ways we can do that. The first one that we're gonna use is something called a for loop.

So let's go ahead back to VEXcode and take a look.

Now, for this, what we can do is go to our control section and look at a for loop. In Blocks, to open help, you would use the question mark and then select a command. In this case, all you need to do is select this question mark next to a command. So I can see here the help for my for loop, my for command. And in this case, there's a lot of information about the different pieces or parameters that are a part of my for loop.

So what I'm going to do is I'm gonna pull in a for loop so we can take a look at this together. Just like how a for loop wraps around Blocks in VEXcode V5 Blocks, in Python, the for loop wraps around commands. Let me show you what I mean by that. If I go back to my slideshow, you can see here in my repeat, there's that line on the left-hand side that makes that a C block. In the for loop, that's what we call an indentation. It's shown using this gray line, and I can see that everything inside is going to be indented and be within that gray loop, that gray line which makes it indented and makes it part of the for loop, or inside of our for loop.

So I'm gonna go back to VEXcode. What we need to do is put all of these commands inside of our for loop. I'm going to select all of this, and I'm just gonna tab it in so it's within my loop. I can tell that it's all within the loop because of the gray indentation line, except for this last piece that we need to add. So I'm going to indent this last comment, and we need to add in a wait. We'll say half of a second before it begins the next loop.

Now let's take a look at the actual syntax of our for loop, which is up here. Our for loop is made up of four main parts. That's not meant to be a joke. That is actually intentional. There are four parts. You have the for. You have this section which is repeat count, or it can be any other variable that you want to set. I could just use the letter I if I wanted to, but in this case, I think that repeat count makes sense because it's a very clear name for my variable. So for this variable in, and then your range. The range is the number of times that you want this to repeat. So in this case, I want it to repeat, let's say, three times. So I'm gonna change that to three.

What is happening is that inside of this loop, it is going to say for however long it takes for this variable to reach the number three, I will repeat the inside. That variable is counting up by one every time we run that loop. If we download this to our robot, we can see that this is going to repeat a total of three times to create a repetitive project.

So we'll go back up here. Move my cord out of the way. We're gonna move it back, and we should see this repeat a total of three times all of our dance moves. So move, raise, clap, left, forward, clap, back up. Now we're going back to our beginning. So now this is our next loop. We're seeing our third clap, a fourth clap. Little too big for my table. And now we should see our fifth and sixth clap. So there's number five. And there's clap number six. And there we can see that it has repeated a total of three times. I count by the number of claps because to me that's the easiest way to tell that all of the behaviors are completed. We have two claps per loop, so a total of six claps if we loop through three times.

So now, that's how we have our for loop. There is another type of loop. If we remember, our criteria wanted us to run this project continuously until the Stop button was pressed. Now, right now I haven't pressed the Stop button. I just pressed it, and that robot was still not repeating the dance moves.

What we can do now is we can go back in and change this to be a forever loop in Python. In Python, let me go back. I'm gonna close this also so we can see more of our code. In Python, a forever loop is known as a while true.

So while the condition is true, repeat all of the things that are indented within this condition or within this command. Excuse me. So while true. Now what we're doing here is we're actually setting this to be true continuously. So we're going to say while true. Instead of saying the condition needs to be true, we are moving to true as part of that. So we're saying this is always true, so it will always repeat. So it is always part of that. So while true, it is going to repeat everything that is inside of this particular command.

Now what we can do is we can come back. We're going to download this to our robot, and we should see this repeat all the way through until it is, while the condition is reported as true, which is consistent 'cause that is what we typed. One moment, I'm gonna switch out my battery. It's part of that, so we'll snap that back in. All part of testing a robot. And now we're going to download this to the brain. So we should see this as a continuous project that meets all of the criteria.

[Music Cue]

So real quick, let's go through all of this criteria again. We need to make sure that our robot continues until I press Stop. That the arm is raised and lowered. That the claw opens and closes, and that the Clawbot goes left, right, forward, and in reverse. So now we can go ahead and test our robot. So we're going to see it go left, or right, forward, close, to the left, forward, close, to the right, and now the arm is lowered. So we've met all of those conditions. Now we can just watch to make sure that our dance continues to repeat as part of it. Just making sure that it's okay.

So now it's going through. This is the third time it's doing its dance. And now what we need to see is that it's going to happen continuously for a fourth time as well because that's different from what has happened before. Make sure it doesn't run into my computer. Now, this would be better if you ran this on a floor, or a larger field, but for just this particular case, we're using the table so that you can see how we are running the robot. But we could see it continue to repeat as it went through.

[Music Cue]

So let's go ahead and cover, recap what we've covered so far in Python. We have talked about our basic drivetrain commands, but really how to use the auto complete to finish those commands. We talked about how to use comments with that pound sign. You'll notice here I have additional gaps in my code as well. These extra spaces don't cause anything to happen for the robot, but you'll notice it makes it a lot easier for me to read my code to know where I'm at in any of the different places for my project.

We also talked about the difference between a for loop and a while true. So for is going to repeat the number of, or that range, for the number of times in the range, excuse me, versus the while true will repeat forever, will repeat continuously. We also talked about how for both of those, they need to be all of the comments that are the commands that are going to be run as part of a for loop or a while true need to be indented within.

Now the last thing I wanna cover that I missed at the beginning, I skipped part of it, is the configuration. So we can see here, I can see what the configuration is. I used a template for this, but it actually hides the code within. So we could expand that and see left, right, drivetrain, claw and arm. So you'll notice when you configure different devices in Python, it looks like the same configuration, but that configuration then is added to your code and hidden up in that robot configuration section, which is really helpful. You don't need to know all of the ins and outs of your robot config or how to code that in Python. You can go ahead and get started coding with specific commands instead.

I know it's a little early, but we have completed our groove machine challenge as part of this.

Again, if you have any questions, please post them in our community thread. If you have your own robot dance moves that you want to show off, I would love to see them. We're also looking for more ideas for continued live sessions from here on out.

If you have any specific challenges you would like to see completed in VEXcode Python, whether that be in IQ 2nd gen, EXP, V5, or in VEXcode VR, please let me know. This session is by no means limited to VEX V5, and we would love to expand to other platforms and show you more about Python in those platforms as well.

With what we've completed today, you could do this also with your IQ or EXP robot as part of it. So please, even if you are using those particular platforms, create your own robot dance move, and I would love to see it.

Thank you, and hopefully I will see you next month. Bye.

(upbeat music)

Share

Like this video? Share it with others!