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

This article is for anyone who wants to work with ev3 in Java. This time I would like to control the tank in various ways.

table of contents

  1. What to prepare
  2. Control of multiple motors 1-0. Program to move the tank forward 1-1. Create and control methods 1-2. Create a method that can specify arguments 1-3. Programs that use the StopWatch class 1-4. A program that calls a method with a specified probability 1-5. Programs that use arrays Finally

0. What to prepare

◯ ev3 (tank) ◯ L motor / M motor ◯ Personal computer (eclipse) ◯ bluetooth ◯ microSD ◯ API Documentation (It is recommended to proceed while watching this.)


1. Control of multiple motors

1-0. Program to move the tank forward

◯ This is a program that advances the tank for 5 seconds. It's as simple as arranging the necessary methods, but there are some parts that do not work with sequential processing, so some ingenuity is required.

tank00.java


//Things necessary(material)To import
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) {

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
        RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
        //Set speed(Units[degrees/s])
        l_a.setSpeed(720);
        l_b.setSpeed(720);
        //Advance for 5 seconds
        l_a.forward();
        l_b.forward();
        Delay.msDelay(5000);
        //Change direction on the spot(Start rotating 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, which allows you to ** wait for the process to complete before deciding whether to execute the next process **. If set to true, the next process will start without waiting for the process to complete. This allows both motors A and B to rotate at the same time in the program above.


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


1-1. A program that creates and controls methods

◯ Create your own method so that it can be reused easily.

tank01.java


//Things necessary(material)To import
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;

//Create a class
public class tank01 {
	
	//Instance generation
	private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
	private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
	//Definition of integer variable motorSpeed
	private final static int motorSpeed = 200;
	
	//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 "** code to make it easier to read " and " reuse **". Bringing together 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


//Things necessary(material)To import
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;

//Create a class
public class tank02 {
	
	//Instance generation
	private final static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
	private final static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
	
	//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){
    	//Max's(a_speed)% or (b_speed)%of(Absolute value)Get 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));
    	
    	//a_speed and b_Cases are classified according to the combination of positive and negative speed
    	if(a_speed > 0) {
                //a_speed、b_Both 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);
                //a_speed、b_Both 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 to get Java absolute value (abs)


1-3. Programs that use the StopWatch class

◯ This is a program that sets 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 {
	//Instance generation
    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);
	
    //Create a static method
    public static void main(String[] args) {     

    	//Reset 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(){
    	//Instance generation
    	Random r = new Random();
    	//0-Randomly select a value from the max speed range and set it to 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 This is a method for rounding. [Reference article]: Java rounding sample (round)


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


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


1-4. A program that calls a method with a specified probability

◯ This program is intentionally set so that the probability of moving forward is high.

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;
	
    //Create a static method
    public static void main(String[] args) {     
    	l_a.setSpeed(motorSpeed);
    	l_b.setSpeed(motorSpeed);
    	
        //Repeat processing 10 times
    	for(int i = 0;i < 10;i++) {
            //Instance generation
    		Random r = new Random();
            //Randomly select a number from the range of 0 to 100
    		int random_value = r.nextInt(100);
    		System.out.println("random_value = " + random_value);
    		
    		//10%Stop with a probability of
    		if(random_value < 10) {
        		stop();
        	//10%Turn to the right with a probability of
        	}else if(random_value < 20) {
        		turnRight();
        	//30%Turn to the left with a probability of
        	}else if(random_value < 50) {
        		turnLeft();
        	//50%Probability of moving 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 odds of moving forward are high. However, if you write it as below, it will not be what you intended.

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 processes in order from the top. When the random_number is determined, the first thing to check if the number matches the condition is the top ʻif (random_value <100). In this case, all the 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 an array and sets them to speed.

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);
	
    //Create a static method
    public static void main(String[] args) {     
    	//Instance generation
    	//Declaration of array and determination of 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;

        //The process is repeated 5 times, and i is added by 1 each time.
        for (int i = 0;i < 5;i++) {
        	//Use array elements
        	//count = 0 : speed =Displayed as 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 arrays ** Use the array in the following procedure.

  1. ** Declaration ** of the array (and determination of the number of elements)
  2. ** Initialization ** of array elements **
  3. Use of array **

Point: ** Initialization of array elements **

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

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 would like to write about controlling multiple motors!

I want to make a better article ◯ This is easier to understand ◯ This is difficult to understand ◯ This is wrong ◯ I want you to explain more here We appreciate your opinions and suggestions.

Recommended Posts

[ev3 × Java] Control of multiple motors (method, random, probability, time limit, array)
[Java] Random method
[Java] Random number generation method (Random)
Benefits of Java static method
[Java Silver] Array generation method
[Java] Summary of control syntax