Pin Assignments:
2: Front left sensor echo (pulse input)
3: Front left sensor trigger
4: Front middle sensor echo (pulse input)
5: Front middle sensor trigger
6: Front right sensor echo (pulse input)
7: Front right sensor trigger
8: Rear left sensor trigger (pin 8 will not work as a pulse input)
9: Rear left sensor echo (pulse input)
10: Rear middle sensor echo (pulse input)
11: Rear middle sensor trigger
12: Rear right sensor echo (pulse input)
13: Rear right sensor trigger
14: Front motion detector input
15: Driver side motion detector input
16: Passenger side motion detector input
17: Rear motion detector input
After a lot of coding, we were able to get three distance senors working in conjunction with the four motion sensors attached to the top of the car. Here is a link to a video showing what we have done.
While we ran out of time to get all six sensors working on the car, we were able to test a board with six sensors in class.
Photos of the in-class sensor test board
Here is our code:
package ioio.examples.carsensors;
//import ioio.examples.carsensor.R;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalInput.Spec.Mode;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIO.VersionType;
import ioio.lib.api.PulseInput;
import ioio.lib.api.PulseInput.PulseMode;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.RadioButton;
/**
* This is the main activity of the HelloIOIO example application.
*
* It displays a toggle button on the screen, which enables control of the
* on-board LED. This example shows a very simple usage of the IOIO, by using
* the {@link IOIOActivity} class. For a more advanced use case, see the
* HelloIOIOPower example.
*/
public class MainActivity extends IOIOActivity {
// Connected
private RadioButton connectedRadioButton_;
// progress bars
private ProgressBar frontleft_;
private ProgressBar frontmiddle_;
private ProgressBar frontright_;
private ProgressBar backleft_;
private ProgressBar backmiddle_;
private ProgressBar backright_;
// variables for echos and distances
private float echosecondsfl_;
private int echoDistanceCmfl_;
private float echosecondsfm_;
private int echoDistanceCmfm_;
private float echosecondsfr_;
private int echoDistanceCmfr_;
private float echosecondsrl_;
private int echoDistanceCmrl_;
private float echosecondsrm_;
private int echoDistanceCmrm_;
private float echosecondsrr_;
private int echoDistanceCmrr_;
private boolean frontmotionvar_;
private boolean drivermotionvar_;
private boolean passengermotionvar_;
private boolean rearmotionvar_;
// motion sensors
private RadioButton frontmotion_;
private RadioButton drivermotion_;
private RadioButton passengermotion_;
private RadioButton rearmotion_;
private TextView textViewfl_;
private TextView textViewfm_;
private TextView textViewfr_;
private TextView textViewrl_;
private TextView textViewrm_;
private TextView textViewrr_;
/**
* Called when the activity is first created. Here we normally initialize
* our GUI.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// assign progress bars to sensors
connectedRadioButton_ = (RadioButton) findViewById(R.id.rbConnected);
frontleft_ = (ProgressBar) findViewById(R.id.pbfl);
frontmiddle_ = (ProgressBar) findViewById(R.id.pbfm);
frontright_ = (ProgressBar) findViewById(R.id.pbfr);
backleft_ = (ProgressBar) findViewById(R.id.pbbl);
backmiddle_ = (ProgressBar) findViewById(R.id.pbbm);
backright_ = (ProgressBar) findViewById(R.id.pbbr);
textViewfl_ = (TextView) findViewById(R.id.textViewfl);
textViewfm_ = (TextView) findViewById(R.id.textViewfm);
textViewfr_ = (TextView) findViewById(R.id.textViewfr);
textViewrl_ = (TextView) findViewById(R.id.textViewrl);
textViewrm_ = (TextView) findViewById(R.id.textViewrm);
textViewrr_ = (TextView) findViewById(R.id.textViewrr);
frontmotion_ = (RadioButton) findViewById(R.id.mfront);
drivermotion_ = (RadioButton) findViewById(R.id.mdriver);
passengermotion_ = (RadioButton) findViewById(R.id.mpassenger);
rearmotion_ = (RadioButton) findViewById(R.id.mrear);
}
/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
class Looper extends BaseIOIOLooper {
/** The on-board LED. */
/* setting up trigger and echo pins for sensors */
private DigitalOutput triggerPinfl_;
private PulseInput echoPinfl_;
private DigitalOutput triggerPinfm_;
private PulseInput echoPinfm_;
private DigitalOutput triggerPinfr_;
private PulseInput echoPinfr_;
private DigitalOutput triggerPinrl_;
private PulseInput echoPinrl_;
private DigitalOutput triggerPinrm_;
private PulseInput echoPinrm_;
private DigitalOutput triggerPinrr_;
private PulseInput echoPinrr_;
private DigitalInput frontmotionin_;
private DigitalInput drivermotionin_;
private DigitalInput passengermotionin_;
private DigitalInput rearmotionin_;
/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.IOIOLooper#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
showVersions(ioio_, "IOIO connected!");
runOnUiThread(new Runnable() {
@Override
public void run() {
connectedRadioButton_.setText("Connected");
connectedRadioButton_.setChecked(true);
}
});
// front left pulse and echo
echoPinfl_ = ioio_.openPulseInput(2, PulseMode.POSITIVE);
triggerPinfl_ = ioio_.openDigitalOutput(3);
// front middle pulse and echo
echoPinfm_ = ioio_.openPulseInput(4, PulseMode.POSITIVE);
triggerPinfm_ = ioio_.openDigitalOutput(5);
// front right pulse and echo
echoPinfr_ = ioio_.openPulseInput(6, PulseMode.POSITIVE);
triggerPinfr_ = ioio_.openDigitalOutput(7);
// rear left pulse and echo
echoPinrl_ = ioio_.openPulseInput(new DigitalInput.Spec(9),PulseInput.ClockRate.RATE_250KHz,PulseInput.PulseMode.POSITIVE,false);
triggerPinrl_ = ioio_.openDigitalOutput(8);
// rear middle pulse and echo
echoPinrm_ = ioio_.openPulseInput(new DigitalInput.Spec(10),PulseInput.ClockRate.RATE_250KHz,PulseInput.PulseMode.POSITIVE,false);
triggerPinrm_ = ioio_.openDigitalOutput(11);
// rear right pulse and echo
echoPinrr_ = ioio_.openPulseInput(new DigitalInput.Spec(12),PulseInput.ClockRate.RATE_250KHz,PulseInput.PulseMode.POSITIVE,false);
triggerPinrr_ = ioio_.openDigitalOutput(13);
frontmotionin_ = ioio_.openDigitalInput(14, Mode.FLOATING);
drivermotionin_ = ioio_.openDigitalInput(15, Mode.FLOATING);
passengermotionin_ = ioio_.openDigitalInput(16, Mode.FLOATING);
rearmotionin_ = ioio_.openDigitalInput(17, Mode.FLOATING);
enableUi(true);
}
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
* @throws InterruptedException
* When the IOIO thread has been interrupted.
*
* @see ioio.lib.util.IOIOLooper#loop()
*/
@Override
public void loop() throws ConnectionLostException, InterruptedException {
try {
if(frontmotionin_.read() == true){
frontmotionvar_ = true;
}
else {
frontmotionvar_ = false;
}
if(drivermotionin_.read() == true){
drivermotionvar_ = true;
}
else {
drivermotionvar_ = false;
}
if(passengermotionin_.read() == true){
passengermotionvar_ = true;
}
else {
passengermotionvar_ = false;
}
if(rearmotionin_.read() == true){
rearmotionvar_ = true;
}
else {
rearmotionvar_ = false;
}
updateViews();
}
catch (ConnectionLostException e) { throw e;}
try{
triggerPinfl_.write(false);
Thread.sleep(1);
triggerPinfl_.write(true);
Thread.sleep(5);
triggerPinfl_.write(false);
echosecondsfl_ = (echoPinfl_.getDuration());
echosecondsfl_ = echosecondsfl_ * 1000 * 1000;
echoDistanceCmfl_ = (int) (echosecondsfl_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
Thread.sleep(20);
try{
triggerPinfm_.write(false);
Thread.sleep(1);
triggerPinfm_.write(true);
Thread.sleep(5);
triggerPinfm_.write(false);
echosecondsfm_ = (echoPinfm_.getDuration());
echosecondsfm_ = echosecondsfm_ * 1000 * 1000;
echoDistanceCmfm_ = (int) (echosecondsfm_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
Thread.sleep(20);
try{
triggerPinfr_.write(false);
Thread.sleep(1);
triggerPinfr_.write(true);
Thread.sleep(5);
triggerPinfr_.write(false);
echosecondsfr_ = (echoPinfr_.getDuration());
echosecondsfr_ = echosecondsfr_ * 1000 * 1000;
echoDistanceCmfr_ = (int) (echosecondsfr_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
Thread.sleep(20);
try{
triggerPinrl_.write(false);
Thread.sleep(1);
triggerPinrl_.write(true);
Thread.sleep(5);
triggerPinrl_.write(false);
echosecondsrl_ = (echoPinrl_.getDuration());
echosecondsrl_ = echosecondsrl_ * 1000 * 1000;
echoDistanceCmrl_ = (int) (echosecondsrl_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
Thread.sleep(20);
try{
triggerPinrm_.write(false);
Thread.sleep(1);
triggerPinrm_.write(true);
Thread.sleep(5);
triggerPinrm_.write(false);
echosecondsrm_ = (echoPinrm_.getDuration());
echosecondsrm_ = echosecondsrm_ * 1000 * 1000;
echoDistanceCmrm_ = (int) (echosecondsrm_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
Thread.sleep(20);
try{
triggerPinrr_.write(false);
Thread.sleep(1);
triggerPinrr_.write(true);
Thread.sleep(5);
triggerPinrr_.write(false);
echosecondsrr_ = (echoPinrr_.getDuration());
echosecondsrr_ = echosecondsrr_ * 1000 * 1000;
echoDistanceCmrr_ = (int) (echosecondsrr_ / 29 / 2);
}catch (ConnectionLostException e) { throw e;}
// update UI
updateViews();
Thread.sleep(5);
}
/**
* Called when the IOIO is disconnected.
*
* @see ioio.lib.util.IOIOLooper#disconnected()
*/
@Override
public void disconnected() {
enableUi(false);
toast("IOIO disconnected");
runOnUiThread(new Runnable() {
@Override
public void run() {
connectedRadioButton_.setText("Searching...");
connectedRadioButton_.setChecked(false);
}
});
}
/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see ioio.lib.util.IOIOLooper#incompatible(IOIO)
*/
@Override
public void incompatible() {
showVersions(ioio_, "Incompatible firmware version!");
}
}
/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
*/
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void showVersions(IOIO ioio, String title) {
toast(String.format("%s\n" +
"IOIOLib: %s\n" +
"Application firmware: %s\n" +
"Bootloader firmware: %s\n" +
"Hardware: %s",
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER)));
}
private void toast(final String message) {
final Context context = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
}
//private int numConnected_ = 0;
private void enableUi(final boolean enable) {
// This is slightly trickier than expected to support a multi-IOIO use-case.
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
private void updateViews() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable()
{
@Override
public void run()
{
textViewfl_.setText(Integer.toString(echoDistanceCmfl_));
frontleft_.setProgress(echoDistanceCmfl_);
textViewfm_.setText(Integer.toString(echoDistanceCmfm_));
frontmiddle_.setProgress(echoDistanceCmfm_);
textViewfr_.setText(Integer.toString(echoDistanceCmfr_));
frontright_.setProgress(echoDistanceCmfr_);
textViewrl_.setText(Integer.toString(echoDistanceCmrl_));
backleft_.setProgress(echoDistanceCmrl_);
textViewrm_.setText(Integer.toString(echoDistanceCmrm_));
backmiddle_.setProgress(echoDistanceCmrm_);
textViewrr_.setText(Integer.toString(echoDistanceCmrr_));
backright_.setProgress(echoDistanceCmrr_);
if(frontmotionvar_ == true) {
frontmotion_.setChecked(true);
}
else {
frontmotion_.setChecked(false);
}
if(drivermotionvar_ == true) {
drivermotion_.setChecked(true);
}
else {
drivermotion_.setChecked(false);
}
if(passengermotionvar_ == true) {
passengermotion_.setChecked(true);
}
else {
passengermotion_.setChecked(false);
}
if(rearmotionvar_ == true) {
rearmotion_.setChecked(true);
}
else {
rearmotion_.setChecked(false);
}
};
});
}
}
- Logan, Elliot, and Thomas