Skip to content

BaseRegulatedMotor.getTachoCount() flips to negative? #670

Closed

Description

@salamanders

I was continually polling the motor's getTachoCount, while steering my robot around using

bb.motorLeft.forward() // is a EV3LargeRegulatedMotor
bb.motorRight.backward() // is a EV3LargeRegulatedMotor
delay(1_000)

and noticed the tachoCount suddenly flip negative:

ms	tachoRight    
9853	786    
9874	786    
9897	786    
9918	786    
10003	786    
10020	786    
10039	786    
10059	786    
10080	-786    
10100	-785    
10127	-780    
10143	-775    
10164	-769    
10184	-762    

Anyone else encounter this?

Activity

  1. JakubVanek commented on Jun 18, 2019

    @JakubVanek
    Contributor

    Hi,

    this seems to be caused by the use of the polarity parameter:

    public void forward() {
    this.setStringAttribute(POLARITY, POLARITY_NORMAL);
    this.setSpeed(this.speed);
    if (!this.regulationFlag) {
    this.setStringAttribute(COMMAND, RUN_DIRECT);
    } else {
    this.setStringAttribute(COMMAND, RUN_FOREVER);
    }

    public void backward(){
    this.setStringAttribute(POLARITY, POLARITY_INVERSED);
    this.setSpeed(this.speed);
    if (!this.regulationFlag) {
    this.setStringAttribute(COMMAND, RUN_DIRECT);
    } else {
    this.setStringAttribute(COMMAND, RUN_FOREVER);
    }

    Instead of doing the inversion (which should be set to 'normal'), a negative speed_sp/duty_cycle_sp should be written.

    Regards,

    Jakub

  2. self-assigned this
    on Aug 18, 2019
  3. added this to the v1.0.0 milestone on Aug 18, 2019
  4. jabrena commented on Sep 1, 2019

    @jabrena
    Member

    Hi @salamanders, I was checking your information. The information from tacho is returned by the runtime.

    /**
    * @return the current tachometer count.
    * @see lejos.robotics.RegulatedMotor#getTachoCount()
    */
    public int getTachoCount() {
    return getIntegerAttribute(POSITION);
    }

    Next week, I am going to review the history about the usage of polarity and I will reply.
    Maybe exist a way to improve the quality of data, but data data is provided by the EV3Dev runtime.

  5. reopened this on Sep 1, 2019
  6. removed this from the v1.0.0 milestone on Sep 1, 2019
  7. 10 remaining items

  8. jabrena commented on Sep 2, 2019

    @jabrena
    Member

    It is a Bug in the implementation.
    I have fixed it. I am going to verify the solution with a System Test with the EV3.

    The reason to use Polarity is historical. In the code exist 2 classes: BaseRegulatedMotor & BasicMotor. In Basic motor, exist a protected method to upgrade the state.

    http://www.lejos.org/ev3/docs/lejos/hardware/motor/BaseRegulatedMotor.html
    http://www.lejos.org/nxt/nxj/api/lejos/nxt/BasicMotor.html
    http://www.lejos.org/nxt/nxj/api/lejos/nxt/BasicMotor.html#updateState(int)

    Forward has a polarity
    Backward has inverse polarity

    Another reason is about the consistency. The speed in many examples in LeJOS are positive. Using polarity, always the speed is positive.

    Tomorrow, I will create a small example to verify that tachoMotor has a consistent value when the motor is running forward or backward.

    Thanks @salamanders & @JakubVanek

    I continue

  9. salamanders commented on Sep 3, 2019

    @salamanders
    Author

    Fantastic - thank you so much!

  10. jabrena commented on Sep 3, 2019

    @jabrena
    Member

    Defining a TestSample to review the changes in branch: #670

  11. jabrena commented on Sep 3, 2019

    @jabrena
    Member

    @salamanders

    Take a look the logs:

    2019-09-03 19:17:26 [main] INFO  example.MotorEncoderTest - MotorEncoderTest
    2019-09-03 19:17:31 [main] INFO  e.a.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
    2019-09-03 19:17:34 [pool-1-thread-2] INFO  example.MotorEncoderTest - FORWARD
    2019-09-03 19:17:34 [pool-1-thread-1] INFO  example.MotorEncoderTest - 1 0
    2019-09-03 19:17:34 [pool-1-thread-1] INFO  example.MotorEncoderTest - 2 46
    2019-09-03 19:17:35 [pool-1-thread-1] INFO  example.MotorEncoderTest - 3 135
    2019-09-03 19:17:35 [pool-1-thread-1] INFO  example.MotorEncoderTest - 4 223
    2019-09-03 19:17:35 [pool-1-thread-1] INFO  example.MotorEncoderTest - 5 315
    2019-09-03 19:17:35 [pool-1-thread-1] INFO  example.MotorEncoderTest - 6 404
    2019-09-03 19:17:36 [pool-1-thread-1] INFO  example.MotorEncoderTest - 7 493
    2019-09-03 19:17:36 [pool-1-thread-1] INFO  example.MotorEncoderTest - 8 583
    2019-09-03 19:17:36 [pool-1-thread-1] INFO  example.MotorEncoderTest - 9 672
    2019-09-03 19:17:36 [pool-1-thread-2] INFO  example.MotorEncoderTest - BACKWARD
    2019-09-03 19:17:36 [pool-1-thread-1] INFO  example.MotorEncoderTest - 10 758
    2019-09-03 19:17:37 [pool-1-thread-1] INFO  example.MotorEncoderTest - 11 657
    2019-09-03 19:17:37 [pool-1-thread-1] INFO  example.MotorEncoderTest - 12 567
    2019-09-03 19:17:37 [pool-1-thread-1] INFO  example.MotorEncoderTest - 13 470
    2019-09-03 19:17:37 [pool-1-thread-1] INFO  example.MotorEncoderTest - 14 375
    2019-09-03 19:17:38 [pool-1-thread-1] INFO  example.MotorEncoderTest - 15 286
    2019-09-03 19:17:38 [pool-1-thread-1] INFO  example.MotorEncoderTest - 16 199
    2019-09-03 19:17:38 [pool-1-thread-1] INFO  example.MotorEncoderTest - 17 101
    2019-09-03 19:17:38 [pool-1-thread-2] INFO  example.MotorEncoderTest - FORWARD
    2019-09-03 19:17:38 [pool-1-thread-1] INFO  example.MotorEncoderTest - 18 21
    2019-09-03 19:17:39 [pool-1-thread-1] INFO  example.MotorEncoderTest - 19 114
    2019-09-03 19:17:39 [pool-1-thread-1] INFO  example.MotorEncoderTest - 20 205
    2019-09-03 19:17:39 [pool-1-thread-1] INFO  example.MotorEncoderTest - 21 305
    2019-09-03 19:17:39 [pool-1-thread-1] INFO  example.MotorEncoderTest - 22 413
    2019-09-03 19:17:40 [pool-1-thread-1] INFO  example.MotorEncoderTest - 23 509
    2019-09-03 19:17:40 [pool-1-thread-1] INFO  example.MotorEncoderTest - 24 603
    2019-09-03 19:17:40 [pool-1-thread-1] INFO  example.MotorEncoderTest - 25 697
    2019-09-03 19:17:40 [pool-1-thread-2] INFO  example.MotorEncoderTest - BACKWARD
    2019-09-03 19:17:40 [pool-1-thread-1] INFO  example.MotorEncoderTest - 26 749
    2019-09-03 19:17:41 [pool-1-thread-1] INFO  example.MotorEncoderTest - 27 648
    2019-09-03 19:17:41 [pool-1-thread-1] INFO  example.MotorEncoderTest - 28 559
    2019-09-03 19:17:41 [pool-1-thread-1] INFO  example.MotorEncoderTest - 29 465
    2019-09-03 19:17:41 [pool-1-thread-1] INFO  example.MotorEncoderTest - 30 375
    2019-09-03 19:17:42 [pool-1-thread-1] INFO  example.MotorEncoderTest - 31 287
    2019-09-03 19:17:42 [pool-1-thread-1] INFO  example.MotorEncoderTest - 32 199
    2019-09-03 19:17:42 [pool-1-thread-1] INFO  example.MotorEncoderTest - 33 111
    2019-09-03 19:17:42 [pool-1-thread-1] INFO  example.MotorEncoderTest - 34 23
    2019-09-03 19:17:43 [pool-1-thread-1] INFO  example.MotorEncoderTest - 35 -70
    2019-09-03 19:17:43 [pool-1-thread-1] INFO  example.MotorEncoderTest - 36 -163
    2019-09-03 19:17:43 [pool-1-thread-1] INFO  example.MotorEncoderTest - 37 -252
    2019-09-03 19:17:43 [pool-1-thread-1] INFO  example.MotorEncoderTest - 38 -340
    2019-09-03 19:17:44 [pool-1-thread-1] INFO  example.MotorEncoderTest - 39 -429
    2019-09-03 19:17:44 [pool-1-thread-1] INFO  example.MotorEncoderTest - 40 -518
    2019-09-03 19:17:44 [pool-1-thread-1] INFO  example.MotorEncoderTest - 41 -607
    2019-09-03 19:17:44 [main] INFO  example.MotorEncoderTest - End
    

    And sourcecode:

    package example;
    
    import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    import lejos.hardware.port.MotorPort;
    import lejos.robotics.RegulatedMotor;
    import lejos.utility.Delay;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class MotorEncoderTest {
    
        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);
        }
    
    }
  12. jabrena commented on Sep 3, 2019

    @jabrena
    Member

    Creating a PR.

  13. jabrena commented on Sep 3, 2019

    @jabrena
    Member

    Example refactored with Java 8+ syntax:

    import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.function.Consumer;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    import lejos.hardware.port.MotorPort;
    import lejos.robotics.RegulatedMotor;
    import lejos.utility.Delay;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class MotorEncoderTest {
    
        Consumer<RegulatedMotor> readMotor = motor -> {
    
            IntStream.rangeClosed(1,50).boxed()
                .forEach(i -> {
                    LOGGER.info("{} {}", i , motor.getTachoCount());
                    Delay.msDelay(200);
                });
        };
    
        Consumer<RegulatedMotor> writeMotor = motor -> {
    
            IntStream.rangeClosed(1, 2).boxed()
                .forEach(i -> {
                    LOGGER.info("FORWARD");
                    motor.forward();
                    Delay.msDelay(2000);
                    LOGGER.info("BACKWARD");
                    motor.backward();
                    Delay.msDelay(2000);
                });
        };
    
        Consumer<RegulatedMotor> processTasks = motor ->  {
    
            final ExecutorService executor = Executors.newFixedThreadPool(2);
    
            List<CompletableFuture<Void>> motorTasks = Arrays.asList(
                    CompletableFuture.runAsync(() -> this.readMotor.accept(motor), executor),
                    CompletableFuture.runAsync(() -> this.writeMotor.accept(motor), executor)).stream()
                    .collect(Collectors.toList());
    
            motorTasks.stream().forEach(CompletableFuture::join);
    
            executor.shutdown();
    
            motor.stop();
    
            LOGGER.info("End");
        };
    
        public static void main(String[] args) throws Exception{
    
            LOGGER.info("MotorEncoderTest");
    
            MotorEncoderTest example = new MotorEncoderTest();
    
            example.processTasks.accept(new EV3LargeRegulatedMotor(MotorPort.A));
    
            System.exit(0);
        }
    }

  14. JakubVanek commented on Sep 4, 2019

    @JakubVanek
    Contributor

    Note to thread pools: I think we will have to cache and/or share them, because native threads could be quite expensive regarding both their startup time and memory footprint. However I have no data yet to back this up. Maybe the JVM is clever and caches this behind the scenes and the effect also may not be dramatic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions