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.
◯ ev3 (intelligent block) ◯ L motor / M motor ◯ Personal computer (eclipse) ◯ bluetooth ◯ microSD ◯ API Documentation (It is recommended to proceed while watching this.)
◯ 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();
◯ 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.
◯ 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
◯ 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.
◯ 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
}
◯ 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;
}
◯ 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.
It is the same as the L motor except that it uses the EV3MediumRegulatedMotor class.
//Instance generation
RegulatedMotor m_a = new EV3MediumRegulatedMotor(MotorPort.A);
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