1. Introduction

1.1. EV3 Large Motor

ScreenShot

1.2. EV3 Medium Motor

ScreenShot

1.3. NXT Motor

ScreenShot

2. How to use the regulated motor

2.1. Move a motor

package ev3dev.actuators.lego.motors;

import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class LargeMotorDemo {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Starting motors on A");
        final EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);

        mA.setSpeed(500);
        mA.brake();
        mA.forward();

        System.out.println(String.format("Large Motor is moving: %s at speed %d", mA.isMoving(), mA.getSpeed()));
        Delay.msDelay(2000);

        mA.stop();
        System.out.println("Stopped motors");
    }

}

2.2. Forward & Backward

package ev3dev.actuators.lego.motors;

import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class LargeMotorDemo2 {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Starting motors on A");
        final EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);
        mA.setSpeed(500);
        mA.brake();

        System.out.println("Forward");
        mA.forward();
        System.out.println("Large Motor is moving: " +  mA.isMoving() + " at speed {}" + mA.getSpeed());
        Delay.msDelay(2000);
        mA.stop();
        System.out.println("Stop");
        System.out.println("Backward");
        mA.backward();
        System.out.println("Large Motor is moving: " + mA.isMoving() + " at speed {}" + mA.getSpeed());
        Delay.msDelay(2000);
        System.out.println("Stop");
        mA.stop();
        System.out.println("Forward");
        mA.forward();
        Delay.msDelay(2000);
        mA.stop();
        System.out.println("Stop");

    }

}

2.3. Test the different stop modes

package ev3dev.actuators.lego.motors;

import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class LargeMotorStopModesExample {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Starting motors on A");
        final EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);
        mA.setSpeed(500);

        System.out.println("Testing brake stop mode");
        mA.brake();
        mA.forward();
        System.out.println(String.format("Large Motor is moving: %s at speed %d", mA.isMoving(), mA.getSpeed()));
        Delay.msDelay(2000);
        mA.stop();

        Delay.msDelay(2000);

        System.out.println("Testing hold stop mode");
        mA.hold();
        mA.forward();
        System.out.println(String.format("Large Motor is moving: %s at speed %d", mA.isMoving(), mA.getSpeed()));
        Delay.msDelay(2000);
        mA.stop();

        Delay.msDelay(2000);

        System.out.println("Testing coast stop mode");
        mA.coast();
        mA.forward();
        System.out.println(String.format("Large Motor is moving: %s at speed %d", mA.isMoving(), mA.getSpeed()));
        Delay.msDelay(2000);
        mA.stop();

        System.out.println("Stopped motors");
    }

}

2.4. Multiple motors

package ev3dev.actuators.lego.motors;

import ev3dev.hardware.EV3DevPlatforms;
import ev3dev.sensors.Battery;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class MultipleMotorsDemo extends EV3DevPlatforms {

    public static void main(String[] args) throws InterruptedException {

        MultipleMotorsDemo example = new MultipleMotorsDemo();

        System.out.println("Starting motor on A");
        final EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);
        System.out.println("Starting motor on B");
        final EV3LargeRegulatedMotor mB = new EV3LargeRegulatedMotor(MotorPort.B);

        //To Stop the motor in case of pkill java for example
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                mA.stop();
                mB.stop();
                System.out.println(Battery.getInstance().getVoltage());
            }
        }));

        mA.brake();
        mB.brake();

        mA.setSpeed(100);
        mB.setSpeed(100);
        mA.forward();
        mB.forward();

        Delay.msDelay(4000);
        mA.stop();
        mB.stop();
        System.out.println("Stopped motors");
    }

}

2.5. Motor events

package ev3dev.actuators.lego.motors;


import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;
import lejos.utility.Delay;

public class MotorEventTest {

    public static void main(String[] args) {

        System.out.println("Testing events with Motors");

        // Listener to stop motors if they get stalled
        final RegulatedMotorListener listener = new RegulatedMotorListener() {

            @Override
            public void rotationStarted(
                    final RegulatedMotor motor,
                    final int tachoCount,
                    final boolean stalled,
                    final long timeStamp) {

                System.out.println("Started");
            }

            @Override
            public void rotationStopped(
                final RegulatedMotor motor,
                final int tachoCount,
                final boolean stalled,
                final long timeStamp) {

                System.out.println("Stopped");
            }
        };

        final RegulatedMotor mA = new NXTRegulatedMotor(MotorPort.A);
        mA.addListener(listener);
        mA.forward();
        Delay.msDelay(500);
        mA.stop();

    }


}

2.6. Rotating a motor

package ev3dev.actuators.lego.motors;

import ev3dev.actuators.Sound;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.hardware.EV3DevPlatforms;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class RegulatedMotorRotateDemo extends EV3DevPlatforms {

    public static void main(String[] args) {

        RegulatedMotorRotateDemo example = new RegulatedMotorRotateDemo();

		Sound sound = Sound.getInstance();

    	final int degreesToTurn = 90;

        final EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);
        mA.resetTachoCount();
        mA.setSpeed(100);

        if(example.getPlatform().equals(EV3DevPlatform.EV3BRICK)) {
            mA.brake();

            sound.beep();
            System.out.println("" + mA.getTachoCount());
            mA.rotate(degreesToTurn);
            sound.beep();
            Delay.msDelay(1000);
            System.out.println("" + mA.getTachoCount());
            mA.rotate(degreesToTurn);
            sound.beep();
            Delay.msDelay(1000);
            System.out.println("" + mA.getTachoCount());
            mA.rotate(degreesToTurn);
            sound.beep();
            Delay.msDelay(1000);
            System.out.println("" + mA.getTachoCount());
            mA.rotate(degreesToTurn);
            sound.beep();
            Delay.msDelay(1000);
            System.out.println("" + mA.getTachoCount());

        } else {
            System.out.println("This feature is exclusive of EV3 Brick with accuracy");
        }

    }
}

2.7. Rotating to a specific angle

package ev3dev.actuators.lego.motors;

import ev3dev.actuators.Sound;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.hardware.EV3DevPlatforms;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;

public class RegulatedMotorRotateToDemo extends EV3DevPlatforms {

    public static void main(String[] args) {

        RegulatedMotorRotateToDemo example = new RegulatedMotorRotateToDemo();

        Sound sound = Sound.getInstance();

    	final int degreesToTurn = 90;
    	int currentDegrees =  0;

        EV3LargeRegulatedMotor mA = new EV3LargeRegulatedMotor(MotorPort.A);
        mA.resetTachoCount();
        mA.setSpeed(100);

        if(example.getPlatform().equals(EV3DevPlatform.EV3BRICK)) {
            mA.brake();

            System.out.println("" + mA.getTachoCount());
            currentDegrees += degreesToTurn;
            System.out.println("" + currentDegrees);
            mA.rotateTo(currentDegrees);
            sound.beep();
            Delay.msDelay(1000);

            System.out.println("" + mA.getTachoCount());
            currentDegrees += degreesToTurn;
            System.out.println("" + currentDegrees);
            mA.rotateTo(currentDegrees);
            sound.beep();
            Delay.msDelay(1000);

        } else {
            System.out.println("This feature is exclusive of EV3 Brick with accuracy");
        }

    }
}