Programming Drivetrain Motors
In Part 1 we learned how to control a single motor by giving it power or input from a joystick. For controlling a drivetrain, we need to be able to control two motors simultaneously to help the robot move.
Programming Drivetrain Motors
Any code from Part 1: Tackling the Basics within our loop should be deleted before continuing this section. Alternatively, you may choose to create a new program.
Since the focus of this section is creating a functional drivetrain in code, lets started by adding rightmotor.setPower(1);
and leftmotor.setPower(1);
to the OpMode while loop.
Quick Check!
Before running your code for the first time, pause and think about the following:
What do you expect your robot to do once the program is activated?
Now save your OpMode using the button in the upper lefthand corner and give your program a go!
Always keep the Driver Hub within reach in the case of the event that a robot does not perform as expected. When in doubt, disable your robot to keep you and it safe.
Mirroring Motors
DC Motors are capable of spinning in two different directions depending on the current flow provided. When a positive power value is applied the motors will spin in a clockwise direction. The opposite will happen when using a negative power value, meaning the motors will spin in a counter clockwise direction.
But how does that help with our current spinning robot? Let's take a closer look at our physical robot to find out:
Notice how the motors on your robot are currently mirrored from each other as part of the drivetrain. Now think about how we learned that when giving the motors a positive value they should turn clockwise. This is still how, however while they may both be rotating clockwise, the direction they know to be as clockwise is opposite.
Try activating your robot's code again, but this time watching which direction the wheels turn. You may consider supporting the robot's frame so the wheels are suspended to make this easier to see.
Reversing a Motor:
There are a couple ways we could adjust our program to help our robot not to be a spinning top. For example, we could make sure the power is set to a negative value whenever one of our motors is called. Or we could simple reverse our motor's direction during initialization.
Before our waitForStart();
we will add the line rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);
. This sets our right motor to always be in reverse once it initializes.
Last updated