[Java] [ev3×Java] Control of multiple motors (method, random, probability, time limit, array)

7 minute read

This article is for people who want to operate ev3 with Java. This time I would like to control the tank in various ways.

table of contents

  1. Items to prepare 1 .Multiple motor control 1-0. Program to advance the tank 1-1. Create and control methods 1-2. Create a method that can specify arguments 1-3. Program that uses StopWatch class 1-4. Program that calls a method with a specified probability 1-5. Programs that use arrays Finally

0. Things to prepare

◯ ev3 (tank) ◯ L motor/M motor ◯ PC (eclipse) ◯ bluetooth ◯ microSD ◯ API Documentation(Itisrecommendedtoproceedwhilewatchingthis.)


1. Control of multiple motors

1-0. Program to advance the tank

◯ It is a program that advances the tank for 5 seconds. Although it is a simple one that arranges the required methods, there are parts that do not work well with sequential processing, so it is necessary to devise it.

tank00.java


// Import necessary items (materials)
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;

// create a class
public class tank00 {

// create a static method
    public static void main(String[] args) {

        // create an instance
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
        RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
        //Set speed (unit is [degrees/s])
        l_a.setSpeed(720);
        l_b.setSpeed(720);
        / Move forward for 5 seconds
        l_a.forward();
        l_b.forward();
        Delay.msDelay(5000);
        // turn on the spot (start rotation at the same time)
        l_a.rotate(-2160,true);
        l_b.rotate(2160);
        //Stop two motors at the same time
        l_a.stop(true);
        l_b.stop();
    }
}

Point: public void rotate(int angle,boolean immediateReturn) immediateReturn-if true do not wait for the move to complete

The rotate() method is the second argument, and you can decide whether to wait for the completion of that process before executing the next process. If set to true, the next process will start without waiting for the completion of the process. This allows the above program to rotate both motors A and B simultaneously.


Point: public void stop(boolean immediateReturn) By specifying true as an argument, you can execute the next process immediately without waiting for the completion of the process that stops the motor. [Reference Material]: Class Base Regulated Motor


1-1. Program for creating and controlling methods

◯ Make the method yourself so that it can be reused easily.

tank01.java


// Import necessary items (materials)
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;

// create a class
public class tank01 {
It's a sequel.
// create an instance
private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
//Define the integer variable motorSpeed
private final static int motorSpeed = 200;
It's a sequel.
// create a static method
    public static void main(String[] args) {
        // method to execute
        forward();
        turnRight();
        forward();
        stop();
    }

    // method definition
    public static void forward(){
    l_a.setSpeed(motorSpeed);
    l_b.setSpeed(motorSpeed);
    l_a.forward();
    l_b.forward();
    Delay.msDelay(2000);
    }
    // method definition
    public static void turnRight(){
    l_a.setSpeed(motorSpeed);
    l_b.setSpeed(motorSpeed-100);
    l_a.forward();
    l_b.forward();
    Delay.msDelay(1000);
    }
    // method definition
    public static void stop(){
    l_a.stop(true);
    l_b.stop();
    }
}

Point: Create a method Create methods mainly for “Making the code easier to read” and “Reusing”. Combining related code into one is called modularization. Creating a function can be said to be modular.


1-2. Create a method that can specify arguments

◯ It is a program that creates a method with a higher degree of freedom by specifying arguments.

tank02.java


// Import necessary items (materials)
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;

// create a class
public class tank02 {
It's a sequel.
// create an instance
private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
It's a sequel.
// create a static method
    public static void main(String[] args) {
        // method to execute
        tank_drive(80,80,4);
        tank_drive(-100,-100,2);
        tank_drive(-30,30,3);
        stop();
    }

    // method definition
    public static void tank_drive(int a_speed,int b_speed,int rotations){
    // Make max (a_speed)% or (b_speed)% (absolute) speed
    l_a.setSpeed(Math.abs((int) l_a.getMaxSpeed()*a_speed/100));
    l_b.setSpeed(Math.abs((int) l_b.getMaxSpeed()*b_speed/100));
    It's a sequel.
    // Divide according to positive/negative combination of a_speed and b_speed
    if(a_speed >0) {
                // Both a_speed and b_speed are positive
    if(b_speed >0) {
    l_a.rotate(rotations*360,true);
    l_b.rotate(rotations*360);
                //a_speed is positive, b_speed is negative
    }else {
    l_a.rotate(rotations*360,true);
    l_b.rotate(rotations*360*-1);
    }
    }else {
                //a_speed is negative b_speed is positive
    if(b_speed >0) {
    l_a.rotate(rotations*360*-1,true);
    l_b.rotate(rotations*360);
                // Both a_speed and b_speed are negative
    }else {
    l_a.rotate(rotations*360*-1,true);
    l_b.rotate(rotations*360*-1);
    }
    }
    }

    // method definition
    public static void stop(){
    l_a.stop(true);
    l_b.stop();
    }
}


Point: Absolute value It can be obtained with Math.abs(). [Reference article]: Sample (abs) that acquires Java absolute value

  • –##1-3. Programs that use StopWatch class

◯ This program has a time limit.

tank03.java


import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Stopwatch;
import java.util.Random;
import lejos.utility.Delay;


public class tank03 {
// create an instance
    public static Stopwatch stopwatch = new Stopwatch();
    private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
It's a sequel.
    // create a static method
    public static void main(String[] args) {

    // reset the timer
        stopwatch.reset();
        //infinite loop
        while (true) {
        forward();
            // display elapsed time
        System.out.println(Math.round(stopwatch.elapsed() / 1000));
        / / After 20 seconds, stop the motor and exit the loop
        if(stopwatch.elapsed()> 20 * 1000) {
        stop();
        break;
        }
        }
    }
    // method definition
    public static void forward(){
    // create an instance
    Random r = new Random();
    //0-randomly choose a value from the max speed range to set the speed
    l_a.setSpeed(r.nextInt((int) l_a.getMaxSpeed()));
    l_b.setSpeed(r.nextInt((int) l_a.getMaxSpeed()));
    l_a.forward();
    l_b.forward();
    Delay.msDelay(1000);
    }
    // method definition
    public static void stop(){
    l_a.stop(true);
    l_b.stop();
    }
}

Point: Math.round Method for rounding off. [Reference article]: Java rounding sample (round)


Point: Random class Outputs a random value. [Reference article]: Generate random numbers in specified range in Java: Random.nextInt()


Point: stopwatch.elapsed() Stopwatch class method. Returns the elapsed time.


1-4. Program that calls a method with a specified probability

◯ It is a program intentionally set to increase the probability of moving forward.

.java


import java.util.Random;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;

public class tank04 {
    private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
private final static int motorSpeed = 200;
It's a sequel.
    // create a static method
    public static void main(String[] args) {
    l_a.setSpeed(motorSpeed);
    l_b.setSpeed(motorSpeed);
    It's a sequel.
        / Repeat the process 10 times
    for(int i = 0;i <10;i++) {
            // create an instance
    Random r = new Random();
            // Randomly select a number from the range 0 to 100
    int random_value = r.nextInt(100);
    System.out.println("random_value = "+ random_value);
    It's a sequel.
    / 10% stop
    if(random_value <10) {
        stop();
        //10% chance to turn right
        }else if(random_value <20) {
        turnRight();
        // 30% chance to turn left
        }else if(random_value <50) {
        turnLeft();
        //50% forward
        }else {
        forward();
        }
    }
    }
    // method definition
    public static void forward(){
    l_a.forward();
    l_b.forward();
    Delay.msDelay(2000);
    }
    // method definition
    public static void turnRight(){
    l_a.forward();
    l_b.backward();
    Delay.msDelay(1000);
    }
    // method definition
    public static void turnLeft(){
    l_a.backward();
    l_b.forward();
    Delay.msDelay(1000);
    }
    // method definition
    public static void stop(){
    l_a.stop(true);
    l_b.stop();
    Delay.msDelay(1000);
    }
}

Point: Probability control We’ve covered how to ensure that only the probability of moving forward is high. However, if you write like this, it is not what you intended.

.java


if(random_value <100){
    forward();
} else if(random_value <50){
    turnRight();
} else if(random_value <20){
    turnLeft();
}else{
    stop();
}

This is because the program executes the processing in order from the top. When random_number is decided, the first check to see if the number matches the condition is if(random_value <100) at the top. In this case, all numbers satisfy this condition, so only the forward() method can be executed.


1-5. Programs that use arrays

◯ It is a program that takes out elements from the array and sets the speed.

.java


import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;


public class tank05 {
    private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
It's a sequel.
    // create a static method
    public static void main(String[] args) {
    // create an instance
    // Declare array and determine number of elements
        int[] speed_list = new int[5];
        // element initialization
        speed_list[0] = 500;
        speed_list[1] = 1000;
        speed_list[2] = 1500;
        speed_list[3] = 2000;
        speed_list[4] = 2500;

        //5 processing is repeated, i is incremented by 1 each time
        for (int i = 0;i <5;i++) {
        // use array elements
        //count = 0: speed = 1500
        System.out.println("count = "+ i + ":" + "speed = "+ speed_list[i]);
        l_a.setSpeed(speed_list[i]);
        l_b.setSpeed(speed_list[i]);
        l_a.forward();
        l_b.forward();
        Delay.msDelay(3000);
        }
    l_a.stop(true);
    l_b.stop();
    }
}

Point: How to use arrayThe array is used in the following procedure.

  1. declaration of array (and number of elements determined)
  2. Initialization of array elements
  3. Use of arrays

Point: ** Initialization of array elements **

There are several ways to initialize array elements. If you want to store regular elements in an array, you can combine it with a loop as follows.

.java


for(int i = 0;i <5;i++){
     //speed_list = [500,1000,1500,2000,2500]
     speed_list[i] = 500*(i+1);
}

Finally

Thank you for reading! !! Next time I’ll write about controlling multiple motors!

I want to make it a better article ◯ This is easier to understand ◯ It is difficult to understand here ◯This is wrong ◯I want you to explain more here Thank you for your opinions and suggestions.