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

EV3 to EV3: Using remote methods

ev3toev3

In previous posts we have looked at how we can connect multiple EV3s together into a PAN (Personal Area Network), in this article we will see how we can use the PAN to allow us to write a simple program to control the resources of one EV3 from another. There are several ways to create this type of network program in Java and leJOS, here we will be using a technique that uses remote method calls. Essentially we will write a program that attaches to each of the other EV3s, obtains objects that represent the resources for the remote brick and then calls methods on that object to access the resources. Let’s take a look at the code.

import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;
import lejos.hardware.Button;
import lejos.hardware.LED;
import lejos.remote.ev3.RemoteEV3;
import lejos.utility.Delay;

public class EV3Test2
{

    public static void remoteLEDTest()
    {
        String[] names = {"EV1", "EV2", "EV3", "EV4"};
        Brick[] bricks = new Brick[names.length];
        try {
            bricks[0] = BrickFinder.getLocal();
            for(int i = 1; i < bricks.length; i++)
            {
                System.out.println("Connect " + names[i]);
                bricks[i] = new RemoteEV3(BrickFinder.find(names[i])[0].getIPAddress());
            }
            LED[] leds = new LED[bricks.length];
            for(int i = 0; i < bricks.length; i++)
                leds[i] = bricks[i].getLED();
            int i = 0;
            int pat = 1;
            while(Button.ENTER.isUp())
            {
                leds[(i++) % leds.length].setPattern(0);
                if (i % leds.length == 0)
                {
                    pat = ((pat + 1) % 3) + 1;
                }
                leds[(i) % leds.length].setPattern(pat);
                Delay.msDelay(100);
            }
            for(LED l : leds)
                l.setPattern(0);
        }
        catch (Exception e)
        {
            System.out.println("Got exception " + e);
        }
    }    

    public static void main(String[] args)
    {
        remoteLEDTest();
    }
}

This code runs on EV1 the first EV3. It obtains a local Brick object for that EV3 and then uses the BrickFinder class to find each of the remaining EV3s and create an instance of a RemoteEV3 for each. This class is the key to providing access to the resources on the remote EV3 and can be used in a similar way to the Brick obtained for the local device. We want to access the LEDs on each EV3 so we create an array of objects that provide access to them. Then it is simply a matter of creating a loop that runs through the LEDs setting a color or turning them off to create a pattern. In this case we simply light a single EV3 in turn, after the “light” has moved across all four EV3s we change the color used and start again. The following video shows the results:

As you can see using this approach to networking programming makes it very easy to access the resources of a remote EV3. The same technique can be used to access, the buttons, motors, sensors, screen, sound and other resources. In the next part of this series we will take a look at how we can control motors attached to remote EV3s to allow more than 4 to be used in a robot.

Advertisements

Discussion

6 thoughts on “EV3 to EV3: Using remote methods

  1. the method “find()” does not exist in the BrickFinder class ): what do i do, eh?

    Posted by thais | 2015/07/07, 15:16
    • As you will see if you read the other posts in this series, many of these features are only available in the development version of leJOS (post the 0.9.0 release). However if you just want to use the remote methods between bricks then there are other calls that will allow you to locate other EV3s or you can use the IP address directly. See some of the many other posts on using remote access for details. If you are still struggling then start a thread in the leJOS forum.

      Posted by gloomyandy | 2015/07/07, 15:33
  2. how to know the name of a EV3?When the two EV3s have the same name ,how do I change their names?

    Posted by wpu | 2016/05/17, 08:36
  3. Hi, I do appreciate your work and article. Most of them are really helpful to me and I have a question about connecting between EV3 via bluetooth here. I read your article well and followed the way you did. But It should have worked in the circumstance that all the EV3 have to be installed with lejos. Isn’t there any way I can connect EV3(lejos installed) and EV3(lejos not installed)? If there is, how can it be? Again, thank you for your time and help!

    Posted by Robin | 2017/03/22, 02:37

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: