//
you're reading...
Android, How-to's

Using your Android phone as a steering wheel

This Android needs leJOS 0.9.0 and a Wifi dongle.

You can control your differential drive EV3 robot by holding your Android phone or tablet like a steering wheel and turning it to steer the robot. Tilting it forwards or backwards changes the speed and direction.

Adjust the constants at the start of the program to your preferred angle for the zero position and the sensitivity of the controls. You will also need to change the host address and pilot parameters for your robot.

Remember to request internet access in your Android manifest.

Here is the code:

package org.lejos.android.sample.ev3androidtilt;

import lejos.remote.ev3.RemoteRequestEV3;
import lejos.remote.ev3.RemoteRequestPilot;
import lejos.utility.Delay;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity implements SensorEventListener {
	private static final float ZERO_X = 0f, ZERO_Y = 7.5f, LEEWAY = 0.5f,
			SPEED_FACTOR = 10f, TURN_FACTOR = 20f;

	private SensorManager sensorManager;
	private float x = 0, y = 0, z = 0;
	private TextView xAccel, yAccel, zAccel;
	private RemoteRequestPilot pilot;
	private RemoteRequestEV3 ev3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		xAccel = (TextView) findViewById(R.id.xAccel);
		yAccel = (TextView) findViewById(R.id.yAccel);
		zAccel = (TextView) findViewById(R.id.zAccel);
		new Control().execute("192.168.0.9");
	}

	@Override
	protected void onResume() {
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		super.onResume();
	}

	@Override
	protected void onStop() {
		sensorManager.unregisterListener(this);
		super.onStop();
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// Do nothing
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		x = event.values[0];
		y = event.values[1];
		z = event.values[2];

		xAccel.setText("x = " + x);
		yAccel.setText("y = " + y);
		zAccel.setText("z = " + z);
	}

	private class Control extends AsyncTask<String, Integer, Long> {
		protected Long doInBackground(String... cmd) {
			try {
				ev3 = new RemoteRequestEV3(cmd[0]);
				pilot = (RemoteRequestPilot) ev3.createPilot(3.5f, 20f, "A",
						"B");

				for (;;) {
					pilot.setTravelSpeed(Math.abs(y - ZERO_Y) * SPEED_FACTOR);
					if (y > ZERO_Y + LEEWAY) {
						pilot.backward();
					} else if (y < ZERO_Y - LEEWAY) {
						if (Math.abs(x - ZERO_X) < LEEWAY)
							pilot.forward();
						else
							pilot.steer((x - ZERO_X) * TURN_FACTOR);
					} else
						pilot.stop();
					Delay.msDelay(200);
				}
			} catch (Exception e) {
				return (ev3 == null ? 1l : 2l);
			} finally {
				if (pilot != null) {
					pilot.stop();
					pilot.close();
				}
				if (ev3 != null)
					ev3.disConnect();
			}
		}

		protected void onPostExecute(Long result) {
			if (result == 1l)
				Toast.makeText(MainActivity.this, "Could not connect to EV3",
						Toast.LENGTH_LONG).show();
			else if (result == 2l)
				Toast.makeText(MainActivity.this, "Error accessing EV3",
						Toast.LENGTH_LONG).show();
		}
	}
}
Advertisements

Discussion

2 thoughts on “Using your Android phone as a steering wheel

  1. How about a sample that shows sensors being used?

    Posted by Simon Burfield | 2015/09/30, 15:09
  2. Hi Lawrie,
    this is a very nice project. Unfortunately the setTravelSpeed()-Method is not supported in the newest lejos-release (0.9.1) so that the EV3 responds with an “unknown method”-error. I tried pilot.setLinearSpeed() as a workaround but this results in a funny start-stop-behaviour and simply does not work as I assumed or as it should work. Can you point me to a workaround that makes this android project work with the newest lejos-release just like it worked in 2014 with an older release?
    Thanks
    Jaydee

    Posted by Jürgen Dunker | 2017/01/07, 19:02

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: