diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a262307..85cc005 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,12 +1,12 @@ + android:versionName="1.0" package="org.jointheleague.iaroc"> - + diff --git a/res/values/strings.xml b/res/values/strings.xml index 6c02d36..fac605c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,11 +1,11 @@ - iAndroid 2015 - iAndroid 2015 - Waiting for 2015 IOIO to connect... + iARoC 2015 + iARoC 2015 + Waiting for iARoC 2015 IOIO to connect... IOIO connected! - Waiting for 2015 Create to connect... - 2015 Create connected! - 2015 IOIO disconnected! + Waiting for iARoC 2015 Create to connect... + iARoC 2015 Create connected! + iARoC 2015 IOIO disconnected! 0 diff --git a/src/org/wintrisstech/erik/iaroc/Dashboard.java b/src/org/jointheleague/iaroc/Dashboard.java similarity index 91% rename from src/org/wintrisstech/erik/iaroc/Dashboard.java rename to src/org/jointheleague/iaroc/Dashboard.java index d8b402b..69b88de 100644 --- a/src/org/wintrisstech/erik/iaroc/Dashboard.java +++ b/src/org/jointheleague/iaroc/Dashboard.java @@ -1,278 +1,278 @@ -package org.wintrisstech.erik.iaroc; -/************************************************************************** - * Simplified version 140512A by Erik Super Happy Version - * Vic's commit version 140904A - * version 150122A AndroidStudio version - * version 150225B AndroidStudio version - **************************************************************************/ -import ioio.lib.api.IOIO; -import ioio.lib.api.exception.ConnectionLostException; -import ioio.lib.util.IOIOLooper; -import ioio.lib.util.android.IOIOActivity; - -import java.util.Locale; - -import org.wintrisstech.irobot.ioio.IRobotCreateInterface; -import org.wintrisstech.irobot.ioio.SimpleIRobotCreate; - -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; -import android.os.Bundle; -import android.speech.tts.TextToSpeech; -import android.widget.ScrollView; -import android.widget.TextView; - -/** - * This is the main activity of the iRobot2012 application. - * - *

- * This class assumes that there are 3 ultrasonic sensors attached to the - * iRobot. An instance of the Dashboard class will display the readings of these - * three sensors. - * - *

- * There should be no need to modify this class. Modify Lada instead. - * - * @author Erik Colban - * - */ -public class Dashboard extends IOIOActivity implements - TextToSpeech.OnInitListener, SensorEventListener { - - /** - * Text view that contains all logged messages - */ - private TextView mText; - private ScrollView scroller; - /** - * A Lada instance - */ - private Lada kalina; - /** - * TTS stuff - */ - protected static final int MY_DATA_CHECK_CODE = 33; - private TextToSpeech mTts; - /** - * Compass stuff - */ - SensorManager sensorManager; - private Sensor sensorAccelerometer; - private Sensor sensorMagneticField; - - private float[] valuesAccelerometer; - private float[] valuesMagneticField; - - private float[] matrixR; - private float[] matrixI; - private float[] matrixValues; - private double azimuth; - private double pitch; - private double roll; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - /* - * Since the android device is carried by the iRobot Create, we want to - * prevent a change of orientation, which would cause the activity to - * pause. - */ - this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - setContentView(R.layout.main); - - Intent checkIntent = new Intent(); - checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); - startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); - - // Compass stuff - sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); - sensorAccelerometer = sensorManager - .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); - sensorMagneticField = sensorManager - .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); - - valuesAccelerometer = new float[3]; - valuesMagneticField = new float[3]; - - matrixR = new float[9]; - matrixI = new float[9]; - matrixValues = new float[3]; - - mText = (TextView) findViewById(R.id.text); - scroller = (ScrollView) findViewById(R.id.scroller); - log(getString(R.string.wait_ioio)); - - } - - @Override - public void onPause() { - if (kalina != null) { - log("Pausing"); - } - sensorManager.unregisterListener(this, sensorAccelerometer); - sensorManager.unregisterListener(this, sensorMagneticField); - super.onPause(); - } - - @Override - protected void onResume() { - - sensorManager.registerListener(this, sensorAccelerometer, - SensorManager.SENSOR_DELAY_NORMAL); - sensorManager.registerListener(this, sensorMagneticField, - SensorManager.SENSOR_DELAY_NORMAL); - super.onResume(); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == MY_DATA_CHECK_CODE) { - if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { - // success, create the TTS instance - mTts = new TextToSpeech(this, this); - } else { - // missing data, install it - Intent installIntent = new Intent(); - installIntent - .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); - startActivity(installIntent); - } - } - } - - public void onInit(int arg0) { - } - - public void speak(String stuffToSay) { - mTts.setLanguage(Locale.US); - if (!mTts.isSpeaking()) { - mTts.speak(stuffToSay, TextToSpeech.QUEUE_FLUSH, null); - } - } - - @Override - public void onAccuracyChanged(Sensor arg0, int arg1) { - // TODO Auto-generated method stub - - } - - @Override - public void onSensorChanged(SensorEvent event) { - switch (event.sensor.getType()) { - case Sensor.TYPE_ACCELEROMETER: - for (int i = 0; i < 3; i++) { - valuesAccelerometer[i] = event.values[i]; - } - break; - case Sensor.TYPE_MAGNETIC_FIELD: - for (int i = 0; i < 3; i++) { - valuesMagneticField[i] = event.values[i]; - } - break; - } - - boolean success = SensorManager.getRotationMatrix(matrixR, matrixI, - valuesAccelerometer, valuesMagneticField); - - if (success) { - SensorManager.getOrientation(matrixR, matrixValues); - synchronized (this) { - azimuth = Math.toDegrees(matrixValues[0]); - pitch = Math.toDegrees(matrixValues[1]); - roll = Math.toDegrees(matrixValues[2]); - } - } - - } - - /** - * Gets the azimuth - * @return the azimuth - */ - public synchronized double getAzimuth() { - return azimuth; - } - - /** - * Gets the pitch - * @return the pitch - */ - public synchronized double getPitch() { - return pitch; - } - - /** - * Gets the roll - * @return the roll - */ - public synchronized double getRoll() { - return roll; - } - - @Override - public IOIOLooper createIOIOLooper() { - return new IOIOLooper() { - - public void setup(IOIO ioio) throws ConnectionLostException, - InterruptedException { - /* - * When the setup() method is called the IOIO is connected. - */ - log(getString(R.string.ioio_connected)); - - /* - * Establish communication between the android and the iRobot - * Create through the IOIO board. - */ - log(getString(R.string.wait_create)); - IRobotCreateInterface iRobotCreate = new SimpleIRobotCreate( - ioio); - log(getString(R.string.create_connected)); - - /* - * Get a Lada (built on the iRobot Create) and let it go... The - * ioio_ instance is passed to the constructor in case it is - * needed to establish connections to other peripherals, such as - * sensors that are not part of the iRobot Create. - */ - kalina = new Lada(ioio, iRobotCreate, Dashboard.this); - kalina.initialize(); - } - - public void loop() throws ConnectionLostException, - InterruptedException { - kalina.loop(); - } - - public void disconnected() { - log(getString(R.string.ioio_disconnected)); - } - - public void incompatible() { - } - }; - } - - /** - * Writes a message to the Dashboard instance. - * - * @param msg - * the message to write - */ - public void log(final String msg) { - runOnUiThread(new Runnable() { - - public void run() { - mText.append(msg); - mText.append("\n"); - scroller.smoothScrollTo(0, mText.getBottom()); - } - }); - } -} +package org.jointheleague.iaroc; +/************************************************************************** + * Simplified version 140512A by Erik Super Happy Version + * Vic's commit version 140904A + * version 150122A AndroidStudio version + * version 150225B AndroidStudio version + **************************************************************************/ +import ioio.lib.api.IOIO; +import ioio.lib.api.exception.ConnectionLostException; +import ioio.lib.util.IOIOLooper; +import ioio.lib.util.android.IOIOActivity; + +import java.util.Locale; + +import org.wintrisstech.irobot.ioio.IRobotCreateInterface; +import org.wintrisstech.irobot.ioio.SimpleIRobotCreate; + +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.os.Bundle; +import android.speech.tts.TextToSpeech; +import android.widget.ScrollView; +import android.widget.TextView; + +/** + * This is the main activity of the iARoC 2015 application. + * + *

+ * This class assumes that there are 3 ultrasonic sensors attached to the + * iRobot. An instance of the Dashboard class will display the readings of these + * three sensors. + * + *

+ * There should be no need to modify this class. Modify Main instead. + * + * @author Erik Colban + * + */ +public class Dashboard extends IOIOActivity implements + TextToSpeech.OnInitListener, SensorEventListener { + + /** + * Text view that contains all logged messages + */ + private TextView mText; + private ScrollView scroller; + /** + * A Main instance + */ + private Main kalina; + /** + * TTS stuff + */ + protected static final int MY_DATA_CHECK_CODE = 33; + private TextToSpeech mTts; + /** + * Compass stuff + */ + SensorManager sensorManager; + private Sensor sensorAccelerometer; + private Sensor sensorMagneticField; + + private float[] valuesAccelerometer; + private float[] valuesMagneticField; + + private float[] matrixR; + private float[] matrixI; + private float[] matrixValues; + private double azimuth; + private double pitch; + private double roll; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + /* + * Since the android device is carried by the iRobot Create, we want to + * prevent a change of orientation, which would cause the activity to + * pause. + */ + this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + setContentView(R.layout.main); + + Intent checkIntent = new Intent(); + checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); + startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); + + // Compass stuff + sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); + sensorAccelerometer = sensorManager + .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + sensorMagneticField = sensorManager + .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); + + valuesAccelerometer = new float[3]; + valuesMagneticField = new float[3]; + + matrixR = new float[9]; + matrixI = new float[9]; + matrixValues = new float[3]; + + mText = (TextView) findViewById(R.id.text); + scroller = (ScrollView) findViewById(R.id.scroller); + log(getString(R.string.wait_ioio)); + + } + + @Override + public void onPause() { + if (kalina != null) { + log("Pausing"); + } + sensorManager.unregisterListener(this, sensorAccelerometer); + sensorManager.unregisterListener(this, sensorMagneticField); + super.onPause(); + } + + @Override + protected void onResume() { + + sensorManager.registerListener(this, sensorAccelerometer, + SensorManager.SENSOR_DELAY_NORMAL); + sensorManager.registerListener(this, sensorMagneticField, + SensorManager.SENSOR_DELAY_NORMAL); + super.onResume(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == MY_DATA_CHECK_CODE) { + if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { + // success, create the TTS instance + mTts = new TextToSpeech(this, this); + } else { + // missing data, install it + Intent installIntent = new Intent(); + installIntent + .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); + startActivity(installIntent); + } + } + } + + public void onInit(int arg0) { + } + + public void speak(String stuffToSay) { + mTts.setLanguage(Locale.US); + if (!mTts.isSpeaking()) { + mTts.speak(stuffToSay, TextToSpeech.QUEUE_FLUSH, null); + } + } + + @Override + public void onAccuracyChanged(Sensor arg0, int arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void onSensorChanged(SensorEvent event) { + switch (event.sensor.getType()) { + case Sensor.TYPE_ACCELEROMETER: + for (int i = 0; i < 3; i++) { + valuesAccelerometer[i] = event.values[i]; + } + break; + case Sensor.TYPE_MAGNETIC_FIELD: + for (int i = 0; i < 3; i++) { + valuesMagneticField[i] = event.values[i]; + } + break; + } + + boolean success = SensorManager.getRotationMatrix(matrixR, matrixI, + valuesAccelerometer, valuesMagneticField); + + if (success) { + SensorManager.getOrientation(matrixR, matrixValues); + synchronized (this) { + azimuth = Math.toDegrees(matrixValues[0]); + pitch = Math.toDegrees(matrixValues[1]); + roll = Math.toDegrees(matrixValues[2]); + } + } + + } + + /** + * Gets the azimuth + * @return the azimuth + */ + public synchronized double getAzimuth() { + return azimuth; + } + + /** + * Gets the pitch + * @return the pitch + */ + public synchronized double getPitch() { + return pitch; + } + + /** + * Gets the roll + * @return the roll + */ + public synchronized double getRoll() { + return roll; + } + + @Override + public IOIOLooper createIOIOLooper() { + return new IOIOLooper() { + + public void setup(IOIO ioio) throws ConnectionLostException, + InterruptedException { + /* + * When the setup() method is called the IOIO is connected. + */ + log(getString(R.string.ioio_connected)); + + /* + * Establish communication between the android and the iRobot + * Create through the IOIO board. + */ + log(getString(R.string.wait_create)); + IRobotCreateInterface iRobotCreate = new SimpleIRobotCreate( + ioio); + log(getString(R.string.create_connected)); + + /* + * Get a Main (built on the iRobot Create) and let it go... The + * ioio_ instance is passed to the constructor in case it is + * needed to establish connections to other peripherals, such as + * sensors that are not part of the iRobot Create. + */ + kalina = new Main(ioio, iRobotCreate, Dashboard.this); + kalina.initialize(); + } + + public void loop() throws ConnectionLostException, + InterruptedException { + kalina.loop(); + } + + public void disconnected() { + log(getString(R.string.ioio_disconnected)); + } + + public void incompatible() { + } + }; + } + + /** + * Writes a message to the Dashboard instance. + * + * @param msg + * the message to write + */ + public void log(final String msg) { + runOnUiThread(new Runnable() { + + public void run() { + mText.append(msg); + mText.append("\n"); + scroller.smoothScrollTo(0, mText.getBottom()); + } + }); + } +} diff --git a/src/org/jointheleague/iaroc/Main.java b/src/org/jointheleague/iaroc/Main.java new file mode 100644 index 0000000..7d9abf3 --- /dev/null +++ b/src/org/jointheleague/iaroc/Main.java @@ -0,0 +1,168 @@ +package org.jointheleague.iaroc; + +/************************************************************************** + * iARoC 2015 + **************************************************************************/ +import ioio.lib.api.IOIO; +import ioio.lib.api.exception.ConnectionLostException; +import org.wintrisstech.irobot.ioio.IRobotCreateAdapter; +import org.wintrisstech.irobot.ioio.IRobotCreateInterface; +import org.jointheleague.iaroc.sensors.UltraSonicSensors; + +import java.util.Random; + +/** + * An implementation of the IRobotCreateInterface, inspired by Vic's + * awesome API. It is entirely event driven. + * @author From the Erik Simplified version 140512A + */ +public class Main extends IRobotCreateAdapter { + private final Dashboard dashboard; + Random r = new Random(); + public UltraSonicSensors sonar; + private boolean firstPass = true;; + private int commandAzimuth; + private int leftSignal; + private int rightSignal; + private int leftFrontSignal; + private int rightFrontSignal; + private int wheelSpeed = 50; + private int relativeHeading = 0; + private int irSensorThreshhold = 2000; + public int turnSpan; + private int[] beep1 = {72, 15}; + private int[] beep2 = {80, 15}; + private int[] beep3 = {65, 15}; + + public Main(IOIO ioio, IRobotCreateInterface create, Dashboard dashboard) + throws ConnectionLostException { + super(create); + sonar = new UltraSonicSensors(ioio); + this.dashboard = dashboard; + } + + public void initialize() throws ConnectionLostException { + dashboard.log("iARoC-2015"); + driveDirect(wheelSpeed, wheelSpeed); + } + + public void loop() throws ConnectionLostException { + driveDirect(wheelSpeed, wheelSpeed); + readSensors(SENSORS_GROUP_ID6); // Reads all sensors + leftFrontSignal = getCliffFrontLeftSignal(); + rightFrontSignal = getCliffFrontRightSignal(); + leftSignal = getCliffLeftSignal(); + rightSignal = getCliffRightSignal(); + dashboard.log(leftFrontSignal + "");//for testing only to calibrate threshhold + + /*************************************************************************************** + * Handling left IR sensors. + ***************************************************************************************/ + if (leftFrontSignal > irSensorThreshhold) // Seeing left front IR + // sensor. Too far right. + { + song(1, beep1); + playSong(1); + turnAngle(5); // Turn left 5 degrees. + } + if (leftSignal > irSensorThreshhold) // Seeing left front IR sensor. Too + // far right. + { + song(2, beep2); + playSong(2); + turnAngle(20); // Turn left 20 degrees. + } + + /*************************************************************************************** + * Handling right IR sensors. + ***************************************************************************************/ + if (rightFrontSignal > irSensorThreshhold) // Seeing right front IR + // sensor. Too far left. + { + song(3, beep3); + playSong(3); + turnAngle(-5);// Turn right 5 degrees. + } + if (rightSignal > irSensorThreshhold) // Seeing right front IR sensor. + // Too far left...turn right. + { + song(1, beep1); + playSong(1); + turnAngle(-20); // Turn right 20 degrees. + } + + /*************************************************************************************** + * Checking for bumps. Turns right on left bump. Turns left on + * right bump. Back up and turn right for head-on. + ***************************************************************************************/ + boolean bumpRightSignal = isBumpRight(); + boolean bumpLeftSignal = isBumpLeft(); + + if (bumpRightSignal) { + song(1, beep1); + playSong(1); + driveDirect(wheelSpeed, 0);// turn left + } + + if (bumpLeftSignal && bumpRightSignal) // Front bump. + { + song(1, beep1); + playSong(1); + driveDirect(-wheelSpeed, -wheelSpeed / 2); // Back up. + turnAngle(-30); // Turn right 30 degrees. + driveDirect(wheelSpeed, wheelSpeed); // Continue forward. + } + + } + + public void turnAngle(int angleToTurn) throws ConnectionLostException // >0 means left, <0 means right. + { + if (angleToTurn > 0) { + driveDirect(wheelSpeed, 0); // turn left + relativeHeading = 0; + while (relativeHeading < angleToTurn) { + readSensors(SENSORS_GROUP_ID6); + relativeHeading += getAngle(); + } + } + + if (angleToTurn < 0) { + driveDirect(0, wheelSpeed);// turn right + relativeHeading = 0; + while (relativeHeading > angleToTurn) { + readSensors(SENSORS_GROUP_ID6); + relativeHeading += getAngle(); + } + } + + driveDirect(wheelSpeed, wheelSpeed); // Go straight. + } + + public void turn(int commandAngle) throws ConnectionLostException // Doesn't + // work + // for + // turns + // through + // 360 + { + int startAzimuth = 0; + if (firstPass) { + startAzimuth += readCompass(); + commandAzimuth = (startAzimuth + commandAngle) % 360; + dashboard.log("commandaz = " + commandAzimuth + " startaz = " + + startAzimuth); + firstPass = false; + } + int currentAzimuth = readCompass(); + dashboard.log("now = " + currentAzimuth); + if (currentAzimuth >= commandAzimuth) { + driveDirect(0, 0); + firstPass = true; + dashboard.log("finalaz = " + readCompass()); + } + } + + public int readCompass() { + return (int) (dashboard.getAzimuth() + 360) % 360; + } +} diff --git a/src/org/wintrisstech/sensors/UltraSonicSensors.java b/src/org/jointheleague/iaroc/sensors/UltraSonicSensors.java similarity index 98% rename from src/org/wintrisstech/sensors/UltraSonicSensors.java rename to src/org/jointheleague/iaroc/sensors/UltraSonicSensors.java index 22f5810..8e7c108 100644 --- a/src/org/wintrisstech/sensors/UltraSonicSensors.java +++ b/src/org/jointheleague/iaroc/sensors/UltraSonicSensors.java @@ -1,4 +1,4 @@ -package org.wintrisstech.sensors; +package org.jointheleague.iaroc.sensors; /************************************************************************** * Simplified version 140512A by Erik Super Happy Version