In this tutorial, we would build a simple program for controlling the Tetrix robot motors with the GamePads. We use the stick and when the stick is pushed forward the motors move forward. When it is pushed backwards the robot moves backwards.
- 15 Apr 2017
The phones are connected
For this tutorial, I would assume that the phones are already connected to each other. If you have any problem connecting them then write us in the comments section and we would try to help you.
Control with the stick
The program that we are developing controls the motors. If you push or pull the stick this will rotate the wheels. If you slightly push or pull the stick than this will slightly rotate the wheels. We are directly setting the power of the motors based on the push/pull of the stick.
In this episode we'll build a simple program for the gamepad and we'll control the motors of our FIRST Technical Challenge robot. How? We have this stick and if I move up, you can see the motors moving. Now, the robot is not moving because I don't have enough space for the camera and you won't be able to see this because it's kind of a large robot but we move and we control the motors.
A few things to notice here. To start from the beginning. First, we have the software and it's probably a good idea to disconnect the joystick, connect it again. Now, we have the driver station and we have the robot station. The driver station is here and the robot station is on the robot. They are both connected and we can see that they are communicating. We would build a couple of other videos how exactly we connect them but there are a number of other tutorials so I assume that you have both phones connected. If you don't have them, drop a comment below and we'll try to help you. Probably with another video for connecting or just in the comment sections to figure out how you can connect both phones. So, the phones are connected then I press start A and we see a small icon right here on the phone. And then what we do
we've developed a program and this program is My Op Mode. Probably you don't see it from the camera but it's the only program on the software and I select this only program and click on it, then play. The robot is placed on some objects so that the wheels are in the air. They are not touching the table and the robot will not be moving but we'll rotate the motors. And now with the left stick if I move up, the wheels rotate in this direction. If I move back, the wheels rotate in reverse. And we can call this backward or this forward or we can have other definitions of forward and backward for this robot. Interesting: When you use the stick you can slightly move the stick up
and this is directly the power that you are setting to the motors.
and larger power. And in reverse.
This is the program that we want to have in the end. Let's go and develop it in the Android studio.
In the Android studio we have the FtcRobotController project and the team robot project and we have everything set up for these projects. So, this is again subject for other videos. I suppose that most of you have done this. Now, in the team code this is the place where we put the code for our team and this is the place where we put our operation mode. What I'll do is go to the FtcRobotController. And in the FtcRobotController there are a number of examples that we can use and these are all examples of how to use different modes and the different sensors and motors, etc. Here we have one template Op Mode Linear. I'll take the template Op Mode Linear, copy it and paste it in our project.
And this is a starting point for developing our program. This is how the code looks like. Now, with your view it won't look like this. What I've done is I've increased the font here so that you can see it actually on the screen and it will be easier to follow the tutorial. There are a number of comments, you can of course read the comments. The important parts of the template Op Mode are the following: First, we have a number of annotations here and the first thing that we should change is the name of our operation mode. This is what we'll see at the software. And the name will be My New Op Mode like this. Then we have a disable annotation and this means that the Op Mode is disabled and you can use these annotations if you have many Op Modes and you'd like to keep them in the same project but only part of them are visible in the sofware so we must comment this. This is the second step. So, first change the name to something that you are familiar with and then comment the disable annotation. Then we must control the motors. And we have two motors. We have a DcMotor that's called left motor and DcMotor that's called right motor. As you uncomment this there will be an error. And this error is that DcMotor cannot be resolved. How do we resolve this? It is even said in the suggestion we press Alt and Enter. And here we select import class. The DcMotor is part of the library provided by the FIRST Technical Challenge project and we must import this library. We click and now we can resolve them. Technically what we've done is right here in the import section there is a new import DcMotor. This import previously was not here. If I do an undo and then a redo, you can see that the redo - can't do a redo.
I'll again import it, import class and I have the DcMotor two variables declared here. Then we have the public void method and that's the Op Mode. And here I'll just remove the telemetry. I don't want to discuss telemetry currently. But it's used to display things on the screen. Remove the telemetry.
And then we must initialize our left and right motor. The way we do this. We even have it in the template and we initialize the variable leftMotor to hardwareMap.dcMotor.get("left_drive") and rightMotors is harwareMare.dcMotor.get("right_drive"). I want you to remember these two names because these two names - left_drive and right_drive are the same names that we've given to our motors in the robot controller software. So, these are actually the same names. And at the end what we'd like to do is we are waiting for a start, we press play, we reset runtime while the operation mode is active. This means while we are working. I would like to remove the telemetry again. And what I would like to do is to just set leftMotor its power to be equal to -gamepad1.left_stick_y. This means the 'y' access of the left stick. And right motor is rightMotor setPower -gamepad1.right_stick_y. And with the plus and minus you control the direction. Let's leave the program like this. And what we need to do now is to just recap this program and then download this program to the robot controller. Again, for a recap we name our Op Mode then we mark disabled as a comment because we don't want this operational mode to be disabled. Then we declare two variables leftMotor and rightMotor. There will be an error for DcMotor that's not important. We must import it with Alt+Enter. Then I've removed the telemetry because we don't need it now. Then we initialize leftMotor variable and rightMotor variable with using this special code hardwareMap.DcMotor.get which actually assigns the left motor to a special object that can control the left motor. And the important thing here is that we look for name left_drive and this must be the same name as in the robot controller.
Finally, we have the opModeIsActive() and we have leftMotor.setPower to something in this case we use the left stick and setPower something we use the right stick. Now, this program will work but will not work in the way we want to. And we see on more comment in the template. And this comment is right here. Because of the way we've placed our motors one of the motors should be set as a direction - forward while the other is set as a direction - reverse. And this is strictly because of the construction. You can do another construction where you don't need to do this depending on how your motors are positioned. But in our care the direction of our left motor is forward while the direction of the right motor is reverse. And these are the last two things we should do. Finally, we connect.
I would assume again that you have the phone correctly connected to your operating system and that's whole lot of subject for another video and even for these phones when I was doing it it took us about 3 hours to correctly set up the phone to be visible from the operating system. So, there might be problems but again this is a subject for another video and now I assume that you have them connected correctly when you click start
we'll see our phone and for this phone press okay and this will download the project.
We are now waiting.
And this will download the software to the robot controller. Let's see how this works. We downloaded the software right here and I'll unplug this is the USB cable from the computer and I have to plug the other USB cable.
Then I'll take the robot, place it over our supporting objects. If there are some errors or something that's not connected correctly what we do is self inspect and then back and now the robot station is correctly working. On the driver station we see the software again and we select right here on the right arrow and you can see the My New Op Mode here and we can select it.
Press on it. Press play. Leave the phone. Take the gamepad and we are controlling only one of the motors.
For the second motor we use the other stick on the gamepad and this is the right stick and if I move the right stick, we can see the right motor. Again, this is our left motor, this is our right motor if this is the front of the robot. And we can even rotate it back and with different power - slowly
With this I hope that you have a basic understanding on how you develop a program on the Android studio, download it on the phones and then control the robot with the gamepad. In the next few episodes we'll enter into more details and specifics about the programs.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons