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. While we could try adding each motor individually, Blocks has a dual motor block available already for just this purpose.
To access the dual block you will need to select the actuators dropdown menu.
Any code from Part 1 should be moved to the side of the workspace or deleted before continuing this section. Alternatively, you may choose to create a new program.
Add the Dual Motor block to op mode while loop.
Use the variable drop down menu on the block to change from arm to rightmotor.
Before running your code for the first time, pause and think about the following:
Now save your OpMode and give your program a go!
Did the robot move as you expected?
You may have expected your robot to move in a straight line forwards or backwards. Instead, your robot likely spun in a circle. When motors run at different speeds they spin along their center pivot point. But the motors are both set to a power of 1 here so what else could be the cause?
[DANGER] 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.
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:
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.
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 simply reverse our motor's direction during initialization.
Add Motor - rightmotorDirection to your program, under the "Put initialization blocks" comment block. Blocks placed in this section will run AFTER the initialization button is pressed on the Driver Hub, but BEFORE the play button is clicked.
Now with the Motor - rightmotorDirection block added the direction for the rightmotor will always be reversed for this program. Our power values do not need to be changed.
This guide teaches programming a robot to respond to gamepad inputs for drivetrain control using the "Arcade Style" approach. Forward and back movements will be controlled using the y-axis of the joystick while turning will be controlled by the x-axis.
The educational content emphasizes using variables to establish calculations that determine motor power based on gamepad input. How much power each motor receives changes how the robot will move, making it important to understand this relationship while coding.
Part 1 covered single motor control, and this section addresses controlling two motors simultaneously for drivetrain functionality.
Before proceeding, users should delete previous code from Part 1 or create a new program. Add motor power commands to the OpMode while loop:
while (opModeIsActive()) {
rightmotor.setPower(1);
leftmotor.setPower(1);
}
What do you expect your robot to do once the program is activated?
You may have expected your robot to move in a straight line forwards or backwards. Instead, your robot likely spun in a circle. When motors run at different speeds they spin along their center pivot point. But the motors are both set to a power of 1 here so what else could be the cause?
[DANGER] 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.
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.
The motors on your robot are currently mirrored from each other as part of the drivetrain. While they may both be rotating clockwise, the direction they know to be as clockwise is opposite.
Two solution approaches: applying negative power values or reversing motor direction during initialization. The recommended method:
rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);
This line should be added before waitForStart();
@Override
public void runOpMode() {
control_Hub = hardwareMap.get(Blinker.class, "Control Hub");
arm = hardwareMap.get(DcMotor.class, "arm");
leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
test_motor = hardwareMap.get(DcMotor.class, "test_motor");
test_servo = hardwareMap.get(Servo.class, "test_servo");
test_touch = hardwareMap.get(TouchSensor.class, "test_touch");
telemetry.addData("Status", "Initialized");
telemetry.update();
//Reverse the rightmotor
rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);
// Wait for the game to start (driver presses PLAY)
waitForStart();
// run until the end of the match (driver presses STOP)
while (opModeIsActive()) {
rightmotor.setPower(1);
leftmotor.setPower(1);
telemetry.addData("Status", "Running");
telemetry.update();
}
}This guide teaches programming a robot to respond to gamepad inputs. Forward and back movements will be controlled using the y-axis of the joystick while turning will be controlled by the x-axis.
The tutorial introduces variables and demonstrates how calculations determine motor power based on gamepad input.
The Java code demonstrates:
The code captures right stick input, inverts the y-axis for intuitive control, and distributes power to both motors using simple arithmetic calculations that enable simultaneous forward/backward and turning motion.