This is an example for the control of the basic components that come with the mioiobot shield.
The application already compiled can be downloaded from here:
In the box in addition to the expansion board for motor control, there are infrared sensors model TCRT5000 that are connected in the corners.
This example uses a joystick control, specifically zerokol: https://github.com/zerokol/JoystickView
You can download it from the official website or from here.
In addition, the example shows the reading of the sensors in two progressbar.
To use the control of the joystick you have to import it to our project, just like with the yoio libraries.
I have done it for version 2.3 of android, with higher versions I did not work well in the activity_main.xml
Once done, you should exit in Custom & Library views, in case it does not come out, refresh.
Once the library has been imported, the activity_main.xml can already be designed.
For this example we have used:
A toogleButton.
Two Progressbar.
And the JoystickView control.
Now we can go on to write our code, or copy it ...
In the MainActivity.java file we have this:
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; } } }
File 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>
You can download the complete project from here:
Example 1: MIOIOBOT_Manual+IR