Ejemplo mioiobot

Este es un ejemplo para el control de los componentes básicos que vienen con el shield mioiobot.

La aplicación ya compilada se puede descargar desde aquí:

En la caja además de la tarjeta de expansión para el control de motores, vienen unos sensores de infrarrojos modelo TCRT5000 que se conectan en las esquinas.

En este ejemplo se utiliza un control de joystick, en concreto el de zerokol: https://github.com/zerokol/JoystickView

Podeis descargarlo desde la página oficial o desde aquí.

Además, el ejemplo muestra la lectura de los sensores en dos progressbar.

Para utilizar el control del joystick hay que importarlo a nuestro proyecto, al igual que se hacia con las librerías ioio.
Yo lo he hecho para la versión 2.3 de android, con versiones más altas no me funcionaba bien en el activity_main.xml

lib_joystick

 

Una vez hecho esto, os debería salir en Custom & Library views, en caso de que no salga, refrescad.

screen-mioiobot

Una vez la librería ha sido importada, ya se puede diseñar el activity_main.xml.
Para este ejemplo se han utilizado:

  • Un toogleButton.
  • Dos Progressbar.
  • y el control JoystickView.

Ahora ya podemos pasar a escribir nuestro código, o copiarlo...

En el archivo MainActivity.java tenemos esto:

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.ToggleButton;
import com.example.mioiom2_manual_info.R;
import com.zerokol.views.JoystickView;
import com.zerokol.views.JoystickView.OnJoystickMoveListener;


public class MainActivity extends AbstractIOIOActivity {

 
 private JoystickView joystick;

 private ToggleButton toggleButton1;
 
 private ProgressBar progressBar1;
 private ProgressBar progressBar2;

 
 
 
 Handler handler;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 
 toggleButton1 = (ToggleButton)findViewById(R.id.toggleButton1);
 joystick = (JoystickView)findViewById(R.id.joystickView1);
 
 progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
 progressBar2 = (ProgressBar)findViewById(R.id.progressBar2);
 
 enableUi(false);
 }
 
 class IOIOThread extends AbstractIOIOActivity.IOIOThread {
 private AnalogInput input_1;
 private AnalogInput input_2;
 
 private DigitalOutput led_;
 private PwmOutput pwmOutput_d1;
 private PwmOutput pwmOutput_d2;
 private PwmOutput pwmOutput_i1;
 private PwmOutput pwmOutput_i2;
 private PwmOutput pwmOutput_1;
 int i=0;
 
 
 
 
 
 @Override
 public void setup() throws ConnectionLostException {
 try {
 input_1 = ioio_.openAnalogInput(31);
 input_2 = ioio_.openAnalogInput(46);
 
 led_ = ioio_.openDigitalOutput(IOIO.LED_PIN, true);
 
 pwmOutput_d1 = ioio_.openPwmOutput(38, 100);
 pwmOutput_d2 = ioio_.openPwmOutput(37, 100);
 pwmOutput_i1 = ioio_.openPwmOutput(39, 100);
 pwmOutput_i2 = ioio_.openPwmOutput(40, 100);
 
 
 pwmOutput_1 = ioio_.openPwmOutput(35, 50);
 enableUi(true);
 } catch (ConnectionLostException e) {
 enableUi(false);
 throw e;
 }
 }
 
 @Override
 public void loop() throws ConnectionLostException {
 try {
 
 final float reading1 = input_1.read();
 final float reading2 = input_2.read();
 
 led_.write(!toggleButton1.isChecked());
 
 //Tarea para el joystick
 MiThread thread = new MiThread(pwmOutput_d1,pwmOutput_d2,pwmOutput_i1,pwmOutput_i2);
 //Tarea para los sensores
 MiThread2 thread2 = new MiThread2(reading1,reading2);
 
 
 thread.execute(); 
 
 thread2.execute();
 
 
 Thread.sleep(20);
 
 } catch (InterruptedException e) {
 ioio_.disconnect();
 } catch (ConnectionLostException e) {
 enableUi(false);
 throw e;
 }
 }
 }

 /**
 * A method to create our IOIO thread.
 */

 @Override
 protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
 return new IOIOThread();
 }
 //Habilitamos los controles una vez se ha conectado la tarjeta con el telefono
 private void enableUi(final boolean enable) {
 runOnUiThread(new Runnable() {
 @Override
 public void run() {
 
 toggleButton1.setEnabled(enable);
 joystick.setEnabled(enable);
 
 }
 });
 }
 class MiThread2 extends AsyncTask<Void, Integer,Integer> {

 
 private int str1;
 private int str2;


 public MiThread2(float reading1,float reading2) {

 this.str1 = (int)(reading1*100);
 this.str2 = (int)(reading2*100);
 progressBar1.setProgress(str1);
 progressBar2.setProgress(str2);
 }

 public void run() {

 runOnUiThread(new Runnable() {

 @Override public void run() {
 
 }

 });

 }

 @Override
 protected Integer doInBackground(Void... params) {
 // TODO Auto-generated method stub
 runOnUiThread(new Runnable() {
 
 @Override public void run() {
 
 
 }

 });
 return str1;
 }

 }
 
 class MiThread extends AsyncTask<Void, Integer,Boolean> {

 
 private PwmOutput pwmOutput_d1;
 private PwmOutput pwmOutput_d2;
 private PwmOutput pwmOutput_i1;
 private PwmOutput pwmOutput_i2;
 public MiThread( final PwmOutput pwmOutput_d1, final PwmOutput pwmOutput_d2,
 final PwmOutput pwmOutput_i1, final PwmOutput pwmOutput_i2) {
 
 
 this.pwmOutput_d1 = pwmOutput_d1;
 this.pwmOutput_d2 = pwmOutput_d2;
 this.pwmOutput_i1 = pwmOutput_i1;
 this.pwmOutput_i2 = pwmOutput_i2;
 

 }



 public void run() {
 runOnUiThread(new Runnable() {

 @Override public void run() {
 
 }

 });
 }



 @Override
 protected Boolean doInBackground(Void... params) {
 // TODO Auto-generated method stub
 
 //Event listener that always returns the variation of the angle in degrees, motion power in percentage and direction of movement
 joystick.setOnJoystickMoveListener(new OnJoystickMoveListener() {

 
 public void onValueChanged(int angle, int power, int direction) {
 
 // TODO Auto-generated method stub
 
 try {
 if(angle==0){
 pwmOutput_d1.setPulseWidth((int) ((Math.cos(angle*Math.PI/180)*power*100)));
 pwmOutput_d2.setPulseWidth(0);
 
 pwmOutput_i1.setPulseWidth((int) (Math.cos(angle*Math.PI/180)*power*100));
 pwmOutput_i2.setPulseWidth(0);
 
 }
 if(angle<90 && angle>0){
 pwmOutput_d1.setPulseWidth((int) (power*100));
 pwmOutput_d2.setPulseWidth(0);
 
 pwmOutput_i1.setPulseWidth((int) (Math.cos((angle)*Math.PI/180)*power*100));
 pwmOutput_i2.setPulseWidth(0);
 }
 if(angle<180 && angle>90){
 pwmOutput_d2.setPulseWidth((int) (power*100));
 pwmOutput_d1.setPulseWidth(0);
 
 pwmOutput_i2.setPulseWidth((int) (-Math.cos((angle)*Math.PI/180)*power*100));
 pwmOutput_i1.setPulseWidth(0);
 }
 if(angle>-90 && angle<0){
 pwmOutput_d1.setPulseWidth((int) (Math.cos(angle*Math.PI/180)*power*80));
 pwmOutput_d2.setPulseWidth(0);
 
 pwmOutput_i1.setPulseWidth((int) (power*100));
 pwmOutput_i2.setPulseWidth(0);
 }
 if(angle>-180 && angle<-90){
 pwmOutput_d2.setPulseWidth((int) (-Math.cos(angle*Math.PI/180)*power*100));
 pwmOutput_d1.setPulseWidth(0);
 
 pwmOutput_i2.setPulseWidth((int) (power*100));
 pwmOutput_i1.setPulseWidth(0);
 }
 } catch (ConnectionLostException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 
 
 }
 }, JoystickView.DEFAULT_LOOP_INTERVAL);
 return null;
 }

 } 
 
}

En el archivo activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ToggleButton" /> 
<ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="400dp" android:max="100" android:progress="50" /> 
<ProgressBar android:id="@+id/progressBar2" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/progressBar1" android:layout_alignParentRight="true" android:max="100" android:progress="50" /> 
<com.zerokol.views.JoystickView android:id="@+id/joystickView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/toggleButton1" android:layout_below="@+id/toggleButton1" /> 
</RelativeLayout>

Puedes descargar el proyecto completo desde aquí:

Ejemplo1: MIOIOBOT_Manual+IR

Deja un comentario

Tu dirección de correo electrónico no será publicada.