Skip to content

Fixing problem with TachoCount, second attempt - #689

Merged
jabrena merged 2 commits into
masterfrom
fixTachoCount2
Dec 2, 2019
Merged

jabrena merged 2 commits into
masterfrom
fixTachoCount2

Conversation

@JakubVanek

@JakubVanek JakubVanek commented Sep 25, 2019 •

Copy link
Copy Markdown
Contributor

Fixes #670
Fixes #663
Supersedes #666
Not yet tested

@JakubVanek

Copy link
Copy Markdown
Contributor Author

Both tests in #680 have passed. The original issue is also fixed:

package example;

import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class Test {
    public static void main(String[] args) {
        LOGGER.info("Initializing motor class");
        RegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);

        LOGGER.info("Set motor speed");
        motor.hold();
        motor.setSpeed(360);

        LOGGER.info("Move motor forward for 1 second");
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        motor.forward();
        Delay.msDelay(1000);
        motor.stop();
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        Delay.msDelay(500);

        LOGGER.info("Calling rotateTo(360)");
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        motor.rotateTo(360, false);
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        Delay.msDelay(500);
        motor.stop();
        Delay.msDelay(500);

        LOGGER.info("Move motor backward for 0,5 second");
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        motor.backward();
        Delay.msDelay(500);
        motor.stop();
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        Delay.msDelay(500);

        LOGGER.info("Calling rotateTo(360) - motor should move forward a little bit");
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        motor.rotateTo(360, false);
        LOGGER.info(Integer.toString(motor.getTachoCount()));
        Delay.msDelay(500);
        motor.stop();
    }
}
[main] INFO example.Test - Initializing motor class
[main] WARN ev3dev.hardware.EV3DevDistros - Failed to detect distro, falling back to Stretch.
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] INFO example.Test - Set motor speed
[main] INFO example.Test - Move motor forward for 1 second
[main] INFO example.Test - 0
[main] INFO example.Test - 361
[main] INFO example.Test - Calling rotateTo(360)
[main] INFO example.Test - 357
[main] INFO example.Test - 365
[main] INFO example.Test - Move motor backward for 0,5 second
[main] INFO example.Test - 360
[main] INFO example.Test - 181
[main] INFO example.Test - Calling rotateTo(360) - motor should move forward a little bit
[main] INFO example.Test - 184
[main] INFO example.Test - 367

@JakubVanek
JakubVanek marked this pull request as ready for review September 25, 2019 21:07
@JakubVanek

Copy link
Copy Markdown
Contributor Author

Also fixes issue #663 (by adding the speed set to rotate() and rotateTo())

@JakubVanek

Copy link
Copy Markdown
Contributor Author

Test 1 of 4: verify #663 fix:

package test;

import ev3dev.actuators.lego.motors.EV3MediumRegulatedMotor;
import lejos.hardware.port.MotorPort;

public class Test663 {
    public static void main(String[] args) throws Exception {
        EV3MediumRegulatedMotor m = new EV3MediumRegulatedMotor(MotorPort.C);
        m.resetTachoCount();
        int dstAngle = 90;
        for (int i = 0; i < 10; i++) {
            m.rotateTo(dstAngle);
            dstAngle = -dstAngle;
            Thread.sleep(1000);
        }
    }
}

Result: motor was rotating back and forth => SUCCESS

@JakubVanek

Copy link
Copy Markdown
Contributor Author

Test 2 of 4: verify #670 fix:

package test;

import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Test670 {

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

        LOGGER.info("MotorEncoderTest");

        RegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);
        motor.resetTachoCount();
        motor.coast();

        ExecutorService executor = Executors.newFixedThreadPool(2);

        class MotorReader implements Runnable {
            private RegulatedMotor motor;

            public MotorReader(RegulatedMotor motor) {
                this.motor = motor;
            }

            public void run() {
                for (int x = 1; x < 50; x++) {

                    LOGGER.info("{} {}", x, motor.getTachoCount());

                    Delay.msDelay(200);
                }
            }
        }

        class MotorWriter implements Runnable {
            private RegulatedMotor motor;

            public MotorWriter(RegulatedMotor motor) {
                this.motor = motor;
            }

            public void run() {

                LOGGER.info("FORWARD");
                motor.forward();
                Delay.msDelay(2000);
                LOGGER.info("BACKWARD");
                motor.backward();
                Delay.msDelay(2000);
                LOGGER.info("FORWARD");
                motor.forward();
                Delay.msDelay(2000);
                LOGGER.info("BACKWARD");
                motor.backward();
                Delay.msDelay(2000);
            }
        }

        executor.execute(new MotorReader(motor));
        executor.execute(new MotorWriter(motor));
        executor.awaitTermination(10, TimeUnit.SECONDS);
        executor.shutdown();

        motor.stop();

        LOGGER.info("End");

        System.exit(0);
    }

}

Result: numbers were following a sort of a "triangle wave" => SUCCESS

@JakubVanek

Copy link
Copy Markdown
Contributor Author

Test 3 of 4: check rotate() regression of the previous fix:

package test;

import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestRotate {
    public static void main(String[] args) {
        LOGGER.info("Initializing motor class");
        RegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);

        LOGGER.info("Set motor speed");
        motor.setSpeed(360);

        LOGGER.info("Move motor forward for 1 second");
        motor.forward();
        Delay.msDelay(1000);

        LOGGER.info("Stop the motor and wait 0,5 seconds");
        motor.stop();
        Delay.msDelay(500);

        LOGGER.info("Calling rotate(360) - should move forward, i.e. in the same direction as the last move");
        motor.rotate(360, false);

        LOGGER.info("Move motor backward for 1 second");
        motor.backward();
        Delay.msDelay(1000);

        LOGGER.info("Stop the motor and wait 0,5 seconds");
        motor.stop();
        Delay.msDelay(500);

        LOGGER.info("Calling rotate(360) - should move forward, i.e. in the oppposite direction to the last move");
        motor.rotate(360, false);
    }
}

Result: the last move was forward too => SUCCESS

@JakubVanek

Copy link
Copy Markdown
Contributor Author

Test 4 of 4: check rotateTo() regression of the previous fix:

package test;

import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestRotateTo {
    public static void main(String[] args) {
        LOGGER.info("Initializing motor class");
        RegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);

        LOGGER.info("Set motor speed");
        motor.setSpeed(360);

        LOGGER.info("Move motor forward for 1 second");
        motor.forward();
        Delay.msDelay(1000);

        LOGGER.info("Stop the motor and wait 0,5 seconds");
        motor.stop();
        Delay.msDelay(500);

        LOGGER.info("Calling rotateTo(360)");
        motor.rotateTo(360, false);
        Delay.msDelay(500);

        LOGGER.info("Move motor backward for 0,1 second");
        motor.backward();
        Delay.msDelay(100);

        LOGGER.info("Stop the motor and wait 0,5 seconds");
        motor.stop();
        Delay.msDelay(500);

        LOGGER.info("Calling rotateTo(360) - motor should move forward a little bit");
        motor.rotateTo(360, false);
    }
}

Result: the last move was forward too => SUCCESS

@jabrena

jabrena commented Dec 2, 2019

Copy link
Copy Markdown
Member

Reviewed changes

@jabrena jabrena closed this Dec 2, 2019
@jabrena jabrena reopened this Dec 2, 2019
@jabrena
jabrena merged commit 0c95178 into master Dec 2, 2019
@jabrena
jabrena deleted the fixTachoCount2 branch December 2, 2019 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseRegulatedMotor.getTachoCount() flips to negative? Large motors work great - medium motor won't move?

2 participants