Android Remote Control EV3

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Android Remote Control EV3

Postby tozu » Wed Jun 25, 2014 2:28 pm

Hey,
for my semester project at the university i attend i need do an android application where you can control the lego mindstorm ev3.
Unfortunately i have some problems connecting to the ev3. I used following resources:
https://sourceforge.net/p/lejos/ev3/ci/2c3bd52195576e2f1abcf0e5ced8ec6aee926f49/tree/android/EV3AndroidControl/

i also added the .jar libary to the libs folder of my project so i can atleast start the app somehow:
android.rmi.jar
dbusjava.jar
ev3classes.jar
jna-3.2.7.jar

anyway i can't connect properly and can't send commands to motors and sensors.

Can anybody help me? its quite important for my degree.

Best Regards
Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby gloomyandy » Wed Jun 25, 2014 2:54 pm

I moved your question as this is not really about the future development of leJOS. Anyway if you want help the more details you provide the more likely you will get it...
* What version of leJOS have you installed on the EV3?
* What version of leJOS is the Android application from (please use a tagged version from git).
* What version of Android are you trying to use?
* What sort of connection are you using (WiFi, Bluetooth, Internet), from the Android device, to the EV3
* What error are you getting from the Android code and where is the error being generated (please post a stack trace).

Out of interest when does your project need to be be complete? When did you get the assignment? When did you start?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4001
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Android Remote Control EV3

Postby tozu » Wed Jun 25, 2014 3:13 pm

Thank you for moving the thread...

I use leJOS v0.8.1-beta on the EV3.
I've downloaded the project from the git-repository yesterday(24.06.2014).
My android version i compile the project with is currently 4.4.2(JellyBean). Does this cause the problem?
I'd like to use the wi-fi connection for controlling the ev3. But it's not mandatory, better say if the bluetooth connection is easier to use i would implement bluetooth :P
At the following link you will find a text file with the Logcat output:
https://www.dropbox.com/sh/li2n860ydq348hm/AABI3sb6V3m-VmP7re0JXyhAa

I would like to finish the project as soon as possible ;)

Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby gloomyandy » Wed Jun 25, 2014 8:43 pm

I am pretty sure you should not be linking with any of the following classes:
dbusjava.jar
jna-3.2.7.jar

You should probably take a look at the leJOS blog which contains a number of posts on using Android to control a robot:
http://lejosnews.wordpress.com/2014/05/ ... ntrol-app/

Other than that I can't really help as I'm not familiar with this code. Perhaps another user or dev will see this thread and offer some further input.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4001
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Android Remote Control EV3

Postby tozu » Wed Jun 25, 2014 10:09 pm

Hi gloomyandy, thanks for your answer I will totally will take a look at the page/tutorial.

I just wondering this guy just said something about LeJOS 0.9.0 (!) But there is no version like this..?

Also I'm not sure what you mean by "not linking with any of the following classes"? I add the libraries because I need those so no error will appear...

I totally hope there are more users and devs who nwill answer to this thread so I get new ideas and hints to solve this problem.

Best regards
Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby gloomyandy » Wed Jun 25, 2014 10:19 pm

Well both of those classes talk directly to the EV3 OS and hardware. If you need them then something is very wrong as they simply make no sense in an Android environment!
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4001
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Android Remote Control EV3

Postby tozu » Wed Jun 25, 2014 11:37 pm

Oooh :o okay i will take them out of my android project and will take a look at the project the guy on that page started ;)

but whats up the thing that he used a "leJOS EV3 0.9.0" version??
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby lawrie » Thu Jun 26, 2014 8:02 am

Hi Tozu,

It is a bit hard to get the Android apps to work at the moment as they need code that was added after the 0.8.1 release, but we cannot release the 0.9.0 version yet as we have some issues to resolve with using Java8 before we can do that.

We did add a 0.8.2 tag to the repository and you can use that. If you have the ev3classes and EV3Menu projects from Git, you should switch them to the 0.8.2 tag and then run their build.xml files so the new version get uploaded to the EV3.

You should then put the EV3AndroidControl .classpath file back to how it was before. It references the ev3classes project, not the individual jar files. It is important that it has exported=true set. This means that all the classes that are used from the ev3classes project are included in the exported .apk file.

If you do all that, the app should work for you.

Lawrie
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Re: Android Remote Control EV3

Postby tozu » Thu Jun 26, 2014 5:02 pm

hi lawrie, thanks for you help! ;)

I updated to the current leJOS EV3 0.8.2 beta build and everything is working.

...well almost: Accessing the motors does, but now i would like to use the sensors aswell. But on requesting the data with fetchSample()-method it throws a lejos.remote.ev3.RemoteRequestException

My Implemention of the sensor is this:
Code: Select all
EV3TouchSensor touchSensor = new EV3TouchSensor(ev3.getPort("S1"));
SampleProvider sp = touchSensor.getTouchMode();
float[] samples = new float[sp.sampleSize()];
sp.fetchSample(samples, 0);


The problem exists with all the sensors: Touch, IR and Color.
Is this implemention for accessing the Sensors remotly via WiFi correct?

The logcat output:
Code: Select all
06-26 18:55:53.774: I/Control - doInBackground(29384): connected to IP: 192.168.19.102
06-26 18:56:03.755: I/Control - doInBackground(29384): forward
06-26 18:56:06.468: W/dalvikvm(29384): threadid=1: thread exiting with uncaught exception (group=0x416bfe18)
06-26 18:56:06.508: E/AndroidRuntime(29384): FATAL EXCEPTION: main
06-26 18:56:06.508: E/AndroidRuntime(29384): Process: at.fhooe.mc.lego_mindstorms, PID: 29384
06-26 18:56:06.508: E/AndroidRuntime(29384): lejos.remote.ev3.RemoteRequestException: android.os.NetworkOnMainThreadException
06-26 18:56:06.508: E/AndroidRuntime(29384):    at lejos.remote.ev3.RemoteRequestAnalogPort.sendRequest(Unknown Source)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at lejos.remote.ev3.RemoteRequestAnalogPort.getPin6(Unknown Source)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at lejos.hardware.sensor.EV3TouchSensor.fetchSample(Unknown Source)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at at.fhooe.mc.lego_mindstorms.Drive.onClick(Drive.java:93)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.view.View.performClick(View.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.view.View$PerformClick.run(View.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.os.Handler.handleCallback(Handler.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.os.Handler.dispatchMessage(Handler.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.os.Looper.loop(Looper.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.app.ActivityThread.main(ActivityThread.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.lang.reflect.Method.invoke(Method.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at dalvik.system.NativeStart.main(Native Method)
06-26 18:56:06.508: E/AndroidRuntime(29384): Caused by: android.os.NetworkOnMainThreadException
06-26 18:56:06.508: E/AndroidRuntime(29384):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at libcore.io.IoBridge.recvfrom(IoBridge.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at libcore.io.Streams.readSingleByte(Streams.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.io.FilterInputStream.read(FilterInputStream.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.io.ObjectInputStream.checkReadPrimitiveTypes(ObjectInputStream.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java)
06-26 18:56:06.508: E/AndroidRuntime(29384):    ... 15 more
06-26 18:56:06.598: I/Control - doInBackground(29384): stopp


Regards Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby tozu » Sat Jun 28, 2014 1:11 am

okay guys i managed to run the sensors aswell ;)

...but now i get a ArrayIndexOutOfBoundsException after a while :(

I would like to display the data from the sensors the whole time. Therefore i created a thread that receive them and displays it in some textViews.
The control of the motors are handled with an AsyncTask. Everytime a user hits a button (like 'rotate left') new Control().execute("left) is called.

But when i clicked stop the thread with the sensors crashs because of an ArrayIndexOutOfBoundsException, but i don't know why :?:

This is inside the onClick() of the Button "Stop":
Code: Select all
...
case R.id.button_stop:
     new Control().execute("stop");
     break;


my Implementation of the AsyncTask "Control":
Code: Select all
private class Control extends AsyncTask<String, Integer, Long> {

      @Override
      protected Long doInBackground(String... cmd) {
         if (cmd[0].equals("connect")) {
            try {
               ev3 = new RemoteRequestEV3(cmd[1]);
               Log.i("Control - doInBackground", "connected to IP: "
                     + cmd[1]);

               runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                     Toast.makeText(getApplicationContext(),
                           "Connected - wait for the Audio-Signal",
                           Toast.LENGTH_SHORT).show();
                  }
               });

               // Connect to Motors at the declared Ports
               left = ev3.createRegulatedMotor("A", 'L');
               right = ev3.createRegulatedMotor("B", 'L');

               // Connect to Sensors at the declared Ports
               touchSensor = new EV3TouchSensor(ev3.getPort("S1"));
               colorSensor = new EV3ColorSensor(ev3.getPort("S2"));
               irSensor = new EV3IRSensor(ev3.getPort("S3"));

               spTouch = touchSensor.getTouchMode();
               spColor = colorSensor.getColorIDMode();
               spIR = irSensor.getDistanceMode();

               // play confirm sound
               audio = ev3.getAudio();
               audio.systemSound(3);

               // now allow the User to interact
               activateButtons();

               // Start Sensor start
               sensorThread.start();

               return 0l;
            } catch (IOException e) {
               return 1l;
            }
         } else if (cmd[0].equals("disconnect") && ev3 != null) {
            Log.i("Control - doInBackground", "disconnect");
            // play sound as signal to close
            audio.systemSound(2);
            left.close();
            right.close();

            // stop thread
            stopThread = true;
            try {
               sensorThread.join();
            } catch (InterruptedException e) {
               e.printStackTrace();
            }

            // close sensors
            touchSensor.close();
            colorSensor.close();
            irSensor.close();
            touchSensor = null;
            colorSensor = null;
            irSensor = null;

            ev3.disConnect();
            ev3 = null;
            return 0l;
         }

         if (ev3 == null)
            return 1l;

         ev3.getAudio().systemSound(1);

         if (cmd[0].equals("stop")) {
            Log.i("Control - doInBackground", "stopp");
            left.stop(true);
            right.stop(true);
         } else if (cmd[0].equals("forward")) {
            Log.i("Control - doInBackground", "forward");
            left.forward();
            right.forward();
         } else if (cmd[0].equals("backward")) {
            Log.i("Control - doInBackground", "backward");
            left.backward();
            right.backward();
         } else if (cmd[0].equals("rotate left")) {
            Log.i("Control - doInBackground", "rotate left");
            left.backward();
            right.forward();
         } else if (cmd[0].equals("rotate right")) {
            Log.i("Control - doInBackground", "rotate right");
            left.forward();
            right.backward();
         }

         return 0l;
      }

      protected void onPostExecute(Long result) {
         if (result == 1l) {
            Toast.makeText(getApplicationContext(),
                  "Could not connect to EV3 - try again...",
                  Toast.LENGTH_LONG).show();
            // Back to Main
            Intent toMain = new Intent(getApplicationContext(),
                  MainActivity.class);
            startActivity(toMain);
            finish();
         }
      }

   }


The Implementation of Sensor Thread:
Code: Select all
   protected class SensorData implements Runnable {

      @Override
      public void run() {
         while (true) {
            if (spColor != null && spTouch != null && spTouch != null) {
               Log.i("Thread Sensor", "run() - receive new data");
               final float[] sampleColor = new float[spColor.sampleSize()];
               final float[] sampleTouch = new float[spTouch.sampleSize()];
               final float[] sampleIR = new float[spIR.sampleSize()];

               try {
                  Thread.sleep(1000);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }

               spTouch.fetchSample(sampleTouch, 0);

               if (sampleTouch.length > 0) {
                  tvTouch.post(new Runnable() {
                     @Override
                     public void run() {
                        if (sampleTouch[0] == 1) {
                           Log.i("Touch-Sensor", "Pressed");
                           tvTouch.setText(R.string.touched);
                        } else {
                           Log.i("Touch-Sensor", "Not Pressed");
                           tvTouch.setText(R.string.not_touched);
                        }
                     }
                  });
               }

               spColor.fetchSample(sampleColor, 0);
               if (sampleColor.length > 0) {
                  tvColor.post(new Runnable() {
                     @Override
                     public void run() {
                        if (sampleColor.length > 0) {
                           Log.i("Color-Sensor", "" + sampleColor[0]);
                           colorToText(sampleColor[0]);
                        }
                     }
                  });
               }

               spIR.fetchSample(sampleIR, 0);

               if (sampleIR.length > 0) {
                  tvDistance.post(new Runnable() {
                     @Override
                     public void run() {
                        Log.i("IR-Sensor", sampleIR[0] + " cm");
                        tvDistance.setText((int) sampleIR[0] + " cm");
                     }
                  });
               }
            }
         }
      }


The logcat i get:
Code: Select all
06-28 02:51:57.392: W/dalvikvm(28647): threadid=12: thread exiting with uncaught exception (group=0x416bfe18)
06-28 02:51:57.402: E/AndroidRuntime(28647): FATAL EXCEPTION: Thread-12584
06-28 02:51:57.402: E/AndroidRuntime(28647): Process: com.example.legomindstormsv2, PID: 28647
06-28 02:51:57.402: E/AndroidRuntime(28647): java.lang.ArrayIndexOutOfBoundsException: length=8; index=54
06-28 02:51:57.402: E/AndroidRuntime(28647):    at lejos.hardware.sensor.EV3ColorSensor.getColorID(Unknown Source)
06-28 02:51:57.402: E/AndroidRuntime(28647):    at lejos.hardware.sensor.EV3ColorSensor.fetchSample(Unknown Source)
06-28 02:51:57.402: E/AndroidRuntime(28647):    at com.example.legomindstormsv2.Drive$SensorData.run(Drive.java:253)
06-28 02:51:57.402: E/AndroidRuntime(28647):    at java.lang.Thread.run(Thread.java)


i just need to slove this and my application for my semester project is done. please help me :o :|

Best Regards
Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby lawrie » Sat Jun 28, 2014 1:33 pm

You seem to be getting a value of 54 for the color id from the EV3 Color sensor when values are only supposed to be 0 to 7. Perhaps your program is getting the value from the wrong sensor for some reason. I am not sure what may be causing it, but the remote access code you are using is new and not tested much.

You might be able to trap the exception as a temporary solution.

There could be a bug in the remote request API that for one of the methods the server sends a reply that the client is not expecting and the reply is wrongly sent to the next request. When I get a chance, I will look through the code to see if I can spot a case like that. Do you know what sequence of remote requests causes it to go wrong?

There is some diagnostics in EV3Menu that is currently commented out. If you put it back in you could see what requests are being sent to the EV3 and what eplies were being returned.
Last edited by lawrie on Sat Jun 28, 2014 2:05 pm, edited 1 time in total.
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Re: Android Remote Control EV3

Postby tozu » Sat Jun 28, 2014 2:05 pm

Oh okay... Could be the distance the ir sensor gets.... But but why is the color sensor complaining then? Why does it gets from the other sensors? I've created arrays for the values of each sensor...

Hmm anybody else who has an suggestion?
Is my implementation okay? Are there any improvements I could do on the code? (Who could maybe solve the problem)


Any suggestions how I catch the exception and still get the values?

Regards Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby gloomyandy » Sat Jun 28, 2014 2:43 pm

There seems to be a lot of threads in this code. My guess is that there is some sort of threading issue and that the 54 is actually the response to some other request.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4001
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Android Remote Control EV3

Postby tozu » Sat Jun 28, 2014 3:09 pm

Yeah its true There are lot of threads in my code^^
I put the control in an asynctask because i saw that the guy who coded the control App at http://lejosnews.wordpress.com/2014/05/ ... ntrol-app/ also used it. And I created the class with the runnable because I would like to receive the data Of the sensors the whole time to print it out at the Android UI.

How would you suggest to implement it with the requirements I got from my advisor that he I described above?

Regards Tozu
tozu
New User
 
Posts: 10
Joined: Wed Jun 11, 2014 11:15 pm

Re: Android Remote Control EV3

Postby gloomyandy » Sat Jun 28, 2014 3:37 pm

Threads are fine, but you may want to add some form of synchronization so that you are not attempting to read the sensors at the same time as issuing commands to the motors. I'm sure you can work it out, after all it is your assignment!
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4001
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Next

Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff