AilgorRC : My first RC car with IOIO + L293D
I am novice in Java, IOIO and Robotic.
This is my First Robot : A car RC with IOIO-OTG Board and L293d to drive Motors
Platform :
- IOIO-OTG
- Bridge H : L293D
- 2 motors DC 6V
- Charger Module LiPo Pro POW12059P
- Accu Li-Ion 3,7 V 2000 mAh
Schema
And the code : https://github.com/ailgorbot/AilgorRC
4 buttons (left, right, up, down) to create in MainActivity (see file AndroidManifest.xml in GitHub)
package fr.ailgor.ailgorrc; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.widget.ToggleButton; import ioio.lib.api.DigitalOutput; import ioio.lib.api.PwmOutput; import ioio.lib.api.exception.ConnectionLostException; import ioio.lib.util.BaseIOIOLooper; import ioio.lib.util.IOIOLooper; import ioio.lib.util.android.IOIOActivity; public class MainActivity extends IOIOActivity { private ToggleButton toggleButtonLedSTAT; protected Button buttonLeft; protected Button buttonRight; protected Button buttonUp; protected Button buttonDown; private boolean motorLeft=false; private boolean motorRight=false; private float speed = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggleButtonLedSTAT = (ToggleButton)findViewById(R.id.toggleButtonLedSTAT); buttonLeft = (Button) findViewById((R.id.buttonLeft)); buttonRight = (Button) findViewById((R.id.buttonRight)); buttonUp = (Button) findViewById((R.id.buttonUp)); buttonDown = (Button) findViewById((R.id.buttonDown)); enableUi(false); buttonLeft.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if ((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getActionMasked()!=MotionEvent.ACTION_UP)) { motorLeft = false; motorRight = false; speed = (float)0.5; } else speed = 0; return false; } }); buttonRight.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if ((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getActionMasked()!=MotionEvent.ACTION_UP)) { motorLeft = true; motorRight = true; speed = (float)0.5; } else speed = 0; return false; } }); buttonUp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if ((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getActionMasked()!=MotionEvent.ACTION_UP)) { motorLeft = false; motorRight = true; speed = 1; } else speed = 0; return false; } }); buttonDown.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if ((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getActionMasked()!=MotionEvent.ACTION_UP)) { motorLeft = true; motorRight = false; speed = (float)0.5; } else speed = 0; return false; } }); } // End onCreate private void enableUi(final boolean enable) { runOnUiThread(new Runnable() { @Override public void run() { toggleButtonLedSTAT.setEnabled(enable); // the toggleButton is disable toggleButtonLedSTAT.setChecked(enable); // switch on the toggleButton if (enable) Toast.makeText(MainActivity.this, getString(R.string.IOIOConnected), Toast.LENGTH_LONG).show(); else Toast.makeText(MainActivity.this, getString(R.string.IOIODisconnected), Toast.LENGTH_LONG).show(); } }); } // End enableUi @Override protected IOIOLooper createIOIOLooper() { return new Looper(); } // End IOIOLooper class Looper extends BaseIOIOLooper{ // Declare PIN private DigitalOutput PinDO0; // LED STAT // Motor DC : Right private DigitalOutput PinDIO36; // L293D In 3 private DigitalOutput PinDIO37; // L293D In 4 private PwmOutput PinPWM38; // L293D Enable 2 // Motor DC : Left private DigitalOutput PinDIO40; // L293D In 2 private DigitalOutput PinDIO41; // L293D In 1 private PwmOutput PinPWM39; // L293D Enable 1 @Override protected void setup() throws ConnectionLostException, InterruptedException { // super.setup(); // Assign PIN // LED STAT PinDO0 = ioio_.openDigitalOutput(0, true); // Motor DC : Right PinDIO36 = ioio_.openDigitalOutput(36); PinDIO37 = ioio_.openDigitalOutput(37); PinPWM38 = ioio_.openPwmOutput(38,100); // Motor DC : Left PinDIO40 = ioio_.openDigitalOutput(40); PinDIO41 = ioio_.openDigitalOutput(41); PinPWM39 = ioio_.openPwmOutput(39,100); enableUi(true); // Start IOIO } // End setup @Override public void loop() throws ConnectionLostException, InterruptedException { // super.loop(); PinDO0.write(!toggleButtonLedSTAT.isChecked()); PinPWM39.setDutyCycle(speed); PinDIO41.write(motorLeft); PinDIO40.write(!motorLeft); PinPWM38.setDutyCycle(speed); PinDIO37.write(motorRight); PinDIO36.write(!motorRight); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } // End Loop @Override public void disconnected() { // super.disconnected(); enableUi(false); toggleButtonLedSTAT.setEnabled(false); // the toggleButton is disable toggleButtonLedSTAT.setChecked(false); // switch off the toggleButton Toast.makeText(MainActivity.this,getString(R.string.IOIODisconnected),Toast.LENGTH_LONG).show(); } // End disconnected } //End Looper @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } // End onCreateOptionsMenu @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // End onOptionsItemSelected }