//
you're reading...
Connecting robots, How-to's, leJOS Features

EV3 to EV3: Motor control

ev3toev3motor

In the previous article we saw how leJOS can be used to control the LEDs on remote EV3s, but what about motors? Can the same technique be used for those? Let’s take a look.

Like before we are using 4 EV3s all connected together in a PAN using Bluetooth, this time each EV3 has two LEGO motors attached (we could have used more but I don’t have that many motors!). Again we will be using remote method calls to allow a small program running on one EV3 to control both the local and remote motors. The process is very similar to the previous example. Connect to the remote devices, obtain an object for each of the motors that we wish to control and them simply use the standard leJOS motor API to control the motors. Here is a fragment of the code:

    public static void remoteMotorTest()
    {
        String[] names = {"EV1", "EV2", "EV3", "EV4"};
        RemoteRequestEV3[] bricks = new RemoteRequestEV3[names.length];
        try {
            for(int i = 1; i < names.length; i++)
                bricks[i] = new RemoteRequestEV3(BrickFinder.find(names[i])[0].getIPAddress());
            RegulatedMotor[] motors = new RegulatedMotor[bricks.length*2];
            motors[0] = new EV3LargeRegulatedMotor(BrickFinder.getLocal().getPort("A"));
            motors[1] = new EV3LargeRegulatedMotor(BrickFinder.getLocal().getPort("B"));
            for(int i = 1; i < bricks.length; i++)
            {
                motors[i*2] = bricks[i].createRegulatedMotor("A", 'L');
                motors[i*2+1] = bricks[i].createRegulatedMotor("B", 'L');
            }

            for(RegulatedMotor m : motors)
            {
                m.setAcceleration(900);
                m.setSpeed(600);
            }

            for(RegulatedMotor m : motors)
                m.rotate(720);
            for(RegulatedMotor m : motors)
                m.rotate(-720);
            for(RegulatedMotor m : motors)
                m.rotate(720, true);
            for(RegulatedMotor m : motors)
                m.waitComplete();

            for(RegulatedMotor m : motors)
                m.rotate(-720, true);
            for(RegulatedMotor m : motors)
                m.waitComplete();

            for(int i = 0; i < 4; i++)
                for(RegulatedMotor m : motors)
                    m.rotate(90);
            for(RegulatedMotor m : motors)
                m.rotate(-720, true);
            for(RegulatedMotor m : motors)
                m.waitComplete();

            for(int i = 0; i < motors.length; i++)
            {
                motors[i].setSpeed((i+1)*100);
                motors[i].setAcceleration((i+1)*100);
            }
            for(int i = 0; i < motors.length; i++)
                motors[i].rotate((i+1)*360, true);
            for(RegulatedMotor m : motors)
                m.waitComplete();

            for(RegulatedMotor m : motors)
                m.close();

            for(int i = 1; i < bricks.length; i++)
                bricks[i].disConnect();
        }
        catch (Exception e)
        {
            System.out.println("Got exception " + e);
        }
    }    

We can see this code in action:

But what if we wanted to use USB or WiFi? The great thing about the leJOS API is that exactly the same code will fork for any connection method, all we need to do is hook up the EV3s to WiFi or group them into a PAN and away we go. We can even run very similar code on a PC!

Using remote methods makes controlling remote resources very easy, but sometimes they may not work so well. In the next two articles I’ll take a look at an example where a simple remote method call does not work and at an alternative solution that does.

Advertisements

Discussion

8 thoughts on “EV3 to EV3: Motor control

  1. I am moving my codebase from an nxt to new ev3 brick – but want to support both the nxt and the ev3 at the same time- The new API makes me a little clueless – if I start from brickfinder.discover – I get BrickInfo objects – but where to go from there?

    In the old world (lejos 0.9.1 API for NXT) I used

    final NXTConnectionManager connManager = new NXTConnectionManager();
    NXTInfo [] robots = connManager.search();
    NXTInfo robotInfo = robots [0];

    NXTConnector connector = new NXTConnector ();
    boolean isConnected = connector.connectTo(robotInfo, 1);

    if (!isConnected) throw new Exception (“Could not connect to NXT ” + robotInfo.name);

    nxtCommand = new NXTCommand(connector.getNXTComm());

    Posted by clemens | 2015/03/01, 20:15
    • Hi,
      The examples in this and the previous article show how you use the return from BrickFinder to create a RemoteEV3 object which can be used to access the remote resources. Things are different on the EV3 to the NXT, so the old way of doing things is not used for EV3 to EV3 communications. This is not the best place to discus code in detail. If you still have questions the best place to ask them is on the leJOS forum.

      Posted by gloomyandy | 2015/03/01, 21:07
  2. Hi,
    So in this example you use the class “RemoteRequestEV3” on line 7, but in the previous article (https://lejosnews.wordpress.com/2015/02/26/ev3-to-ev3-using-remote-methods/) you used “RemoteEV3”. Also I noticed in the docs that there is also “RMIRemoteEV3”, can you clarify the differences between all three of these?

    Posted by KH | 2016/10/28, 00:09
  3. Hi,

    On line 7 you use the “RemoteRequestEV3” class however in your previous article, you utilized “RemoteEV3”. I also noticed in the docs there is also “RMIRemoteEV3”. Could you clarify the differences between all three of these classes?

    Thanks,
    KH

    Posted by KH | 2016/10/28, 01:54
    • RemoteRequestEV3 and RemoteEV3 are basically two different ways to do remote calls to an EV3 running leJOS. One (RemoteEV3) uses RMI the other uses a hand coded remote API using a serialized object. Why are there two? Well Android does not support RMI so Lawrie implemented the RemoteRequest version to allow him to use remote calls from his Android device. The two mechanisms are similar but may have slightly different implementations and may also have different performance characteristics. We should probably just settle on one mechanism but to be honest I’m not very interested in using remote APIs so it is not something I’m going to do! As for RMIRemoteEV3 and the rest of the classes/interfaces that begin RMI these basically form the client and server side RMI implementation of the RMI version of the remote interface and should probably not be used directly.

      Posted by gloomyandy | 2016/10/28, 09:11
      • Thanks for the prompt response GloomyAndy! (sorry about the duplicated comment, wasn’t sure if it got submitted properly the first time)

        Posted by KH | 2016/10/28, 13:27
  4. Hello, I just wanted to ask, how do you switch bluetooth for USB? You said it is really simple, but I can’t find any documentation on internet… Could you please give me some sample of this code, but with USB? I would be really thankful. πŸ™‚ Thank you. πŸ˜‰

    Posted by Vojtech Bestak | 2017/06/16, 14:13

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: