In order to build a tank, a physical setup with two motors is required. Here is a simple design consisting 1 touch sensor, 1 colour sensor, and 2 large tacho motors for an EV3. It is designed using the LDD (Lego Digital Designer), and its download is available.
For motors, please connect output/motor port B to the left motor, and output/motor port C to the right motor.
For sensors, please also connect input/sensor port 1 to the touch sensor and input/sensor port 2 to the colour sensor.
We have to use two Motor
class instance to achieve driving an ev3dev tank. To begin with, we will import two following classes:
import org.ev3dev.hardware.motors.Motor; import org.ev3dev.hardware.ports.LegoPort;
With the following code snippets, we will create two Motor
instance to drive the tank.
/* LegoPort.OUTPUT_* assigns the Motor to a specific port on the EV3. Available output constants are: - LegoPort.OUTPUT_A -> Assigns to Port A - LegoPort.OUTPUT_B -> Assigns to Port B - LegoPort.OUTPUT_C -> Assigns to Port C - LegoPort.OUTPUT_D -> Assigns to Port D */ Motor leftMotor = new Motor(new LegoPort(LegoPort.OUTPUT_B)); Motor rightMotor = new Motor(new LegoPort(LegoPort.OUTPUT_C));
We will set the speed of two motors using the following method:
//Speed ranges from -100 to 100 //Negative speed makes the motor to runs backwards leftMotor.setSpeed_SP(100); rightMotor.setSpeed_SP(100);
Next, we will tell the two motors to run forward! And wait for 5 seconds (5000 ms).
leftMotor.runForever(); rightMotor.runForever(); try { Thread.sleep(5000); //Tell the thread to sleep 5000 ms } catch (InterruptedException ignore){} //We don't need to handle it currently
Nevertheless, we will tell the two motors to run backwards! And wait for 5 seconds (5000 ms).
//Negative speed makes the motor to runs backwards leftMotor.stop(); //Stop it first rightMotor.stop(); leftMotor.setSpeed_SP(-100); rightMotor.setSpeed_SP(-100); leftMotor.runForever(); //Restart rightMotor.runForever(); try { Thread.sleep(5000); //Tell the thread to sleep 5000 ms } catch (InterruptedException ignore){} //We don't need to handle it currently
Turn left! And wait for 5 seconds!
leftMotor.stop(); //We have to stop it first leftMotor.setSpeed_SP(-100); //Negative speed means let the motor runs in a reverse direction leftMotor.runForever(); //Restart it! try { Thread.sleep(5000); } catch (InterruptedException ignore){}
Turn right! And wait for 5 seconds!
leftMotor.stop(); leftMotor.setSpeed_SP(100); //Set back to positive speed to let the motor to run forward leftMotor.runForever(); //Restart rightMotor.stop(); rightMotor.setSpeed_SP(-100); //Let the motor to run backwards rightMotor.runForever(); //Restart try { Thread.sleep(5000); } catch (InterruptedException ignore){}
Finally, we stop it.
leftMotor.stop(); rightMotor.stop();
You have completed a simple task to drive a tank! Here's the full example.
/* LegoPort.OUTPUT_* assigns the Motor to a specific port on the EV3. Available output constants are: - LegoPort.OUTPUT_A -> Assigns to Port A - LegoPort.OUTPUT_B -> Assigns to Port B - LegoPort.OUTPUT_C -> Assigns to Port C - LegoPort.OUTPUT_D -> Assigns to Port D */ Motor leftMotor = new Motor(new LegoPort(LegoPort.OUTPUT_B)); Motor rightMotor = new Motor(new LegoPort(LegoPort.OUTPUT_C)); // // Set speed to 100 (Forward, Max Speed) // leftMotor.setSpeed_SP(100); rightMotor.setSpeed_SP(100); // // Run Forward // leftMotor.runForever(); rightMotor.runForever(); try { Thread.sleep(5000); //Tell the thread to sleep 5000 ms } catch (InterruptedException ignore){} //We don't need to handle it currently // // Run Backwards // //Negative speed makes the motor to runs backwards leftMotor.stop(); //Stop it first rightMotor.stop(); leftMotor.setSpeed_SP(-100); rightMotor.setSpeed_SP(-100); leftMotor.runForever(); //Restart rightMotor.runForever(); try { Thread.sleep(5000); //Tell the thread to sleep 5000 ms } catch (InterruptedException ignore){} //We don't need to handle it currently // // Turn Left // leftMotor.stop(); //We have to stop it first leftMotor.setSpeed_SP(-100); //Negative speed means let the motor runs in a reverse direction leftMotor.runForever(); //Restart it! try { Thread.sleep(5000); } catch (InterruptedException ignore){} // // Turn Right // leftMotor.stop(); leftMotor.setSpeed_SP(100); //Set back to positive speed to let the motor to run forward leftMotor.runForever(); //Restart rightMotor.stop(); rightMotor.setSpeed_SP(-100); //Let the motor to run backwards rightMotor.runForever(); //Restart try { Thread.sleep(5000); } catch (InterruptedException ignore){} // // Stop both motors // leftMotor.stop(); rightMotor.stop();