Skip to content

Two EV3Motor/Sensor instances for the same Motor/Sensor #740

Closed

Description

@mrkrizic

Hello, I am trying to read a motor on the same port that has already been instantiated in another class I don't have access to. One class should control the hardware and the other should keep track of the motor's and sensor's state. Both classes should not know about each other and should be able to run on their own as well as simultaneously.

Now my problem is that when I instantiate two motors it is looking at the wrong port on the Brick. Each time I run the jar file again the port number it is looking for increases by two. This means I need to restart the brick each time the program is terminated.

I have looked through the EV3 Motor classes and noticed an unused offset parameter, but since it is not used, I doubt this is the issue. Can anybody explain me why this is happening and point me into a direction what I could do to accomplish this? I would like to stay away from dependency injection if possible.

Any help would be appreciated.

Here is the code I wrote to test the behaviour, since my application did not work using both components:

public static void main(String[] args) {
        EV3MediumRegulatedMotor motor1 = new EV3MediumRegulatedMotor(MotorPort.A);
        EV3MediumRegulatedMotor motor2 = new EV3MediumRegulatedMotor(MotorPort.A);
        System.out.println("Motor1="+motor1.getPosition());
        System.out.println("Motor2="+motor2.getPosition());
        EV3LargeRegulatedMotor motor3 = new EV3LargeRegulatedMotor(MotorPort.D);
        EV3LargeRegulatedMotor motor4 = new EV3LargeRegulatedMotor(MotorPort.D);
        System.out.println("Motor3="+motor1.getPosition());
        System.out.println("Motor4="+motor2.getPosition());
        EV3ColorSensor colorSensor1 = new EV3ColorSensor(SensorPort.S3);
        EV3ColorSensor colorSensor2 = new EV3ColorSensor(SensorPort.S3);
        System.out.println("Sensor1=" + colorSensor1.getColorID());
        System.out.println("Sensor2=" + colorSensor2.getColorID());
        EV3TouchSensor sensor1 = new EV3TouchSensor(SensorPort.S2);
        EV3TouchSensor sensor2 = new EV3TouchSensor(SensorPort.S2);
        System.out.println("Sensor1=" + sensor1.isPressed());
        System.out.println("Sensor2=" + sensor2.isPressed());
    }

My log output from the test i wrote in case it helps:

[main] INFO ev3dev.hardware.EV3DevDistros - Providing an EV3DevDistros instance
[main] INFO ev3dev.hardware.EV3DevPlatforms - Providing a EV3DevPlatforms instance
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] ERROR ev3dev.utils.Sysfs - Problem reading path: /sys/class/tacho-motor/motor8/position
java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor8/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Exception in thread "main" java.lang.RuntimeException: Problem reading path: /sys/class/tacho-motor/motor8/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:80)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Caused by: java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor8/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        ... 5 more
robot@ev3dev:~/build/libs$ java -jar TwoMotorsAtSamePortTest-1.0-SNAPSHOT.jar
[main] INFO ev3dev.hardware.EV3DevDistros - Providing an EV3DevDistros instance
[main] INFO ev3dev.hardware.EV3DevPlatforms - Providing a EV3DevPlatforms instance
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] ERROR ev3dev.utils.Sysfs - Problem reading path: /sys/class/tacho-motor/motor10/position
java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor10/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Exception in thread "main" java.lang.RuntimeException: Problem reading path: /sys/class/tacho-motor/motor10/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:80)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Caused by: java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor10/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        ... 5 more
robot@ev3dev:~/build/libs$ ^C
robot@ev3dev:~/build/libs$ java -jar TwoMotorsAtSamePortTest-1.0-SNAPSHOT.jar
[main] INFO ev3dev.hardware.EV3DevDistros - Providing an EV3DevDistros instance
[main] INFO ev3dev.hardware.EV3DevPlatforms - Providing a EV3DevPlatforms instance
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] ERROR ev3dev.utils.Sysfs - Problem reading path: /sys/class/tacho-motor/motor12/position
java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor12/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Exception in thread "main" java.lang.RuntimeException: Problem reading path: /sys/class/tacho-motor/motor12/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:80)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Caused by: java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor12/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        ... 5 more
robot@ev3dev:~/build/libs$ java -jar TwoMotorsAtSamePortTest-1.0-SNAPSHOT.jar
[main] INFO ev3dev.hardware.EV3DevDistros - Providing an EV3DevDistros instance
[main] INFO ev3dev.hardware.EV3DevPlatforms - Providing a EV3DevPlatforms instance
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] INFO ev3dev.actuators.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
[main] ERROR ev3dev.utils.Sysfs - Problem reading path: /sys/class/tacho-motor/motor14/position
java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor14/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Exception in thread "main" java.lang.RuntimeException: Problem reading path: /sys/class/tacho-motor/motor14/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:80)
        at ev3dev.utils.Sysfs.readInteger(Sysfs.java:90)
        at ev3dev.hardware.EV3DevDevice.getIntegerAttribute(EV3DevDevice.java:106)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getTachoCount(BaseRegulatedMotor.java:117)
        at ev3dev.actuators.lego.motors.BaseRegulatedMotor.getPosition(BaseRegulatedMotor.java:130)
        at TwoMotorsTest.main(TwoMotorsTest.java:14)
Caused by: java.io.IOException: Problem reading path: /sys/class/tacho-motor/motor14/position
        at ev3dev.utils.Sysfs.readString(Sysfs.java:77)
        ... 5 more

Activity

  1. JakubVanek commented on Nov 26, 2020

    @JakubVanek
    Contributor

    Hi @mrkrizic,

    I suspect what is happening is this: one of the constructors in EV3LargeRegulatedMotor class hierarchy (BaseRegulatedMotor) is always sending a port mode switch command to the kernel, so the kernel has to recreate the motor device. This results in the original device "handle" being lost (hence the exceptions) and a new one being created.

    A workaround for this has been proposed in #655, but it needs to be cleaned up first.

    With regards,

    Jakub

  2. jabrena commented on Nov 27, 2020

    @jabrena
    Member

    Hi @mrkrizic, the library binds with the runtime.

    • Why do you need to instanciate 2 objects about the same device?
    • Why not use the same object to get information about the same device?

    Juan Antonio

  3. self-assigned this
    on Nov 27, 2020
  4. mrkrizic commented on Dec 2, 2020

    @mrkrizic
    Author

    I am using an actor system where one controls the hardware and the other publishes data. My solution was to create the hardware and then pass the object through the constructor. Guess I expected an exeption when creating the same object twice and then got confused when it showed unexpected behaviour.

  5. jabrena commented on Dec 2, 2020

    @jabrena
    Member

    Well, the whole library is pretty imperative and not designed for that scenario.
    Do you have a repo to review that interaction?
    it seems pretty interesting, not everyday, people use Actor Model for Robotics.

    Juan Antonio

  6. reopened this on Dec 2, 2020
  7. jabrena commented on Dec 2, 2020

    @jabrena
    Member
  8. mrkrizic commented on Dec 4, 2020

    @mrkrizic
    Author

    Yes indeed. I wanted to send you a smaller example in the next few days.
    Although only the Turntables were actually programmed using Java, the rest is running on 4diac forte

  9. jabrena commented on Dec 4, 2020

    @jabrena
    Member

    hi @mrkrizic,

    Interesting. Please, share with us the project.
    I saw that you used Akka & Milo. I would like to learn the use cases and everything.
    I saw your customizations for Motors.

    Cheers

  10. mrkrizic commented on Dec 11, 2020

    @mrkrizic
    Author

    Hello @jabrena,

    As part of my bachelor's thesis i created a version of the Turntable using my framework. You can find it here: https://github.com/mrkrizic/FIABTurntable

    The wiki contains some more information about the project. Feel free to ask any questions I would be glad to answer them.

    The hardware wrapper I wrote is used to test the same program on a windows machine first before deploying it on a BrickPi.

  11. jabrena commented on Dec 13, 2020

    @jabrena
    Member

    I will take a look next week.
    The videos are pretty promising.

    Cheers

  12. added this to the v1.0.0 milestone on Dec 13, 2020
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