[ev3 x Java] Single motor control

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

table of contents

  1. What to prepare
  2. Code structure
  3. L motor control 2-0. A program that rotates the L motor for a certain period of time 2-1. Program to rotate the L motor by a certain angle 2-2. Program to accelerate the L motor 3-3. Programs that use for loops 2-4. Programs that use while loops 2-5. Programs that use if and else
  4. Control of M motor Finally

0. What to prepare

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


1. Code structure

◯ The program used this time is roughly like this. First, let's talk about this shape.

templete.java


//Prepare the materials for the program
import package.Class;

//Create a class
public class class name{

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

        //Instance generation
Class object=new class();
        
        //Use method
object.Method();
    }
}

Point :import It is for procuring the materials (code chunks) needed to program. Write as `ʻimport package.Class``.

For example, ʻimport lejos.hardware.motor.EV3LargeRegulatedMotor`` means to procure the ʻEV3LargeRegulatedMotorclass in the package lejos.hardware.motor``.


Point: ** Class ** A collection of related fields and methods. (Example)

public class human{
    //field
float height;
float weight;
float singing ability;
    //Method
public void sleep(){};
public void eat(){};
public void speak(){};
public void sing(){};
}

Point: ** static method ** ** A method (function) that can be called without instantiating a class.

Reference article Explanation of main method, its arguments (args), and return value Carefully explain Java static methods! Let's learn usage examples and ideas together!


Point: ** Instance ** It is a material (object) of a class. The instance has ** fields and methods that the class has **. You can create multiple instances from one class.


Point: ** Instantiation ** Creating an object from a class. The instance is created as follows.

Class object=new class(constructor)

To do. The constructor allows you to ** create an object with individuality ** from a class.

** (Example) ** Making Takeshi Goda from class people

Takeshi Goda=new human(height= 157,body weight= 70,Skill as a singer= 0);

[Reference article]: Implemented interface! How to use implements in Java


Point: ** Dot (.) ** The dot (.) Means ** go inside **. Dots allow you to access fields and methods within an object.

Takeshi Goda.sing();

2. L motor control

2-0. A program that rotates the L motor for a certain period of time

◯ Program to rotate and stop the L motor for 5 seconds

lmotor00.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 lmotor00 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
        
        //Set to a speed that rotates 720 degrees per second
        l_a.setSpeed(720);
        //Rotate the motor forward
        l_a.forward();
        //5000 ms(5 seconds)stand by=Continue processing
        Delay.msDelay(5000);
        //Stop the motor
        l_a.stop();
    }
}

Point: ** Class. Method ** msDelay () is a ** static method **, so you can use it without using an instance.

[Reference article] Carefully explain Java static methods! Let's learn usage examples and ideas together! What are the benefits of static methods


Point : setSpeed() A method that determines the rotation speed of a motor. In the argument, specify the angle you want to rotate in 1 second.


Point : forward() A method that rotates the motor in the positive direction. If there is no method to stop, it keeps spinning.


2-1. Program to rotate the L motor by a certain angle

◯ A program that rotates the motor 1080 degrees

lmotor01.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 lmotor01 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
        
        //Set to a speed that rotates 360 degrees per second
        l_a.setSpeed(360);
        //Rotate the motor 1080 degrees in the negative direction
        l_a.rotate(-1080);
    }
}

Point : rotate() Rotate by the angle specified by the argument Rotation stops when processing is complete


2-2. Program to accelerate the L motor

◯ This is a program that gradually accelerates the L motor.

lmotor02.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 lmotor02 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);

        //Set speed to maximum
        l_a.setSpeed((int) l_a.getMaxSpeed());
        //Set acceleration to 100(The initial velocity is slow and accelerates)
        l_a.setAcceleration(100);
        l_a.rotate(7200);
        //Set acceleration to 9000(Accelerate at once)
        l_a.setAcceleration(9000);
        l_a.rotate(-7200);
    }
}

Point: ** Acceleration ** Acceleration is ** the rate of change of velocity per unit time **. The argument unit ** degrees / s / s ** of the method setAcceleration () indicates how much the angular velocity [degrees / s] changes in ** 1 second [s] **. (Example) When changing by 100 degrees per second

time[s] 0 1 2 3 4 5 6 7 8 9 10
angular velocity[degrees/s] 0 100 200 300 400 500 600 700 800 900 1000
acceleration[degrees/s/s] 0 100 100 100 100 100 100 100 100 100 100

Point : getMaxSpeed() A method that returns the maximum velocity value. The maximum speed also depends on the amount of charge.


Point : setAcceleration() A method that determines the degree of acceleration. The smaller the value, the slower the acceleration.


2-3. Programs that use for loops

◯ A program that increases speed every 2 seconds

lmotor03.java


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

//Create a class
public class lmotor03 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
 
        //i <Increase i by 100 while filling 1000
        for(int i = 10 ; i < 1000 ; i += 100) {
        	//Set speed to i
        	l_a.setSpeed(i);
        	l_a.forward();
        	Delay.msDelay(2000);
        }
        //Stop the motor after exiting the for loop
        l_a.stop();
    }
}

Point: ** for loop ** Repeats the process while ʻi <threshold is true. Every time it is processed, i is added (subtracted).

for(int i =initial value;i <Threshold; i +=Increase / decrease){
processing
}

2-4. Programs that use while loops

◯ It is a program that accelerates the L motor every 2 seconds.

lmotor04.java


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

//Create a class
public class lmotor04 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);

        //Define an integer variable s
        int s = 0; 
        //s <While meeting 1000{}Repeat the process inside
        while(s < 1000) {
        	//Set speed to s
        	l_a.setSpeed(s);
        	l_a.forward();
        	Delay.msDelay(2000);
        	//s = s + 100
        	s += 100;
        }
        //Stop the motor after exiting the while loop
        l_a.stop();
    }
}

Point: ** while loop ** It is a mechanism to set a condition and repeat the process while the condition is true.

while(conditions){
processing;
}

2-5. Programs that use if and else

◯ It is a program that accelerates the L motor every 2 seconds. However, ** if the center button of the intelligent block is pressed **, the loop ends.

lmotor05.java


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

//Create a class
public class lmotor05 {

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

        //Instance generation
        RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);

        //Define an integer variable s
        int s = 0; 
        //s <While meeting 1000{}Repeat the process inside
        while(s < 1000) {
        	//If the center button is pressed
        	if(Button.getButtons() == 2) {
        		//End loop processing
        		break;
        	//If the center button is not pressed
        	}else {
            //Set speed to s
        	l_a.setSpeed(s);
        	l_a.forward();
        	Delay.msDelay(2000);
        	//s = s + 100
        	s += 100;
        	}
        }
        //Stop the motor after exiting the while loop
        l_a.stop();
    }
}

Point : if,else If the condition is true, process 1 is performed, otherwise process 2 is performed.

if(conditions){
Process 1;
}else{
Process 2;
}

There are other statements using if as follows.

if(conditions){
processing;
}
if(Condition a){
Process 1;
}else if(Condition b){
Process 2;
if(Condition a){
Process 1;
}else if(Condition b){
Process 2;
}else{
Process 3;
}

Point : Button.getButtons() A static method that returns an integer value corresponding to the button pressed. Each button on the intelligent block is ** assigned a number **. Button and integer value correspondence table ```if (Button.getButtons () == 2) `` means ** if the center button is pressed ** from the correspondence table.


Point : break A statement to end the loop.


3. Control of M motor

It is the same as the L motor except that it uses the EV3MediumRegulatedMotor class.

//Instance generation
RegulatedMotor m_a = new EV3MediumRegulatedMotor(MotorPort.A);

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 x Java] Single motor control
[LeJOS] Let's control the EV3 motor with Java
[LeJOS] Let's remotely control the EV3 motor with Java
[ev3 × Java] Display, sound, LED control
[ev3 x Java] Intelligent block button
Java control syntax
Selenium x Java
[Java] Control syntax notes
Java version control on macOS
[Java] Multi-thread processing --Exclusive control
Version control Java with SDKMAN
Java version control with jenv
☾ Java / Iterative statement and iterative control statement
[Java] Summary of control syntax