当前位置:   article > 正文

Arduino和Android通过OTG 通信_arduino如何通过usb otg上传程序

arduino如何通过usb otg上传程序

马上就跨年了,写点最近做的东西也算是给自己一个交代。

一直想做一款智能机器人,会看路、会走、会说话、能聊天的机器人。硬件控制采用Arduino软件处理器使用Android Mini Pc俗称安卓盒子或者安卓棒,图像识别采用高通结合OpenCV(这个还在考虑,毕竟自己不太会写算法)、显示器采用车载屏幕。硬件都采购好了,Arduino也基本差不多了,现在就是要做通信以及凸显识别这一块了。

本来打算采用Arduino的蓝牙模块和Android进行通信的,并且通信测试也已经通过了,可以控制小车前进后退操作。但是后来有考虑Arduino本身带有USB接口,为什么弃之不用呢,所以就花了两天时间墙内墙外的找(像我这种编程能力不是特别强的,还是希望找到容易看懂的代码,在修改。。)

找到一国外哥们的,下面给大家简单翻译一下,附带源码

这个例子实现双向通信在USB主机模式下,Android和Arduino Uno之间。Android上的一个按钮,用于开/关Arduino Uno板子上自带的led灯,和Arduino一侧电位计,用于控制 Android的SeekBar

AndroidManifest.xml 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.androidusbhostarduino"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-feature android:name="android.hardware.usb.host" />
  7. <uses-sdk
  8. android:minSdkVersion="12"
  9. android:targetSdkVersion="19" />
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name=".MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. <intent-filter>
  23. <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
  24. </intent-filter>
  25. <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
  26. android:resource="@xml/device_filter" />
  27. </activity>
  28. </application>
  29. </manifest>

res/xml/device_filter.xml, for Arduino Uno. (这个文件夹感觉用处不大,因为在我的测试中,Android直接检测那个usb口是可以双线通信的,如果有,就默认为Arduino的)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- idVendor=2341, idProduct=0043 for Arduino Uno R3 -->
  4. <usb-device
  5. vendor-id="9025"
  6. product-id="0067" />
  7. </resources>

/res/layout/activity_main.xml 

  1. package com.example.androidusbhostarduino;
  2. import java.nio.ByteBuffer;
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.hardware.usb.UsbConstants;
  7. import android.hardware.usb.UsbDevice;
  8. import android.hardware.usb.UsbDeviceConnection;
  9. import android.hardware.usb.UsbEndpoint;
  10. import android.hardware.usb.UsbInterface;
  11. import android.hardware.usb.UsbManager;
  12. import android.hardware.usb.UsbRequest;
  13. import android.os.Bundle;
  14. import android.widget.CompoundButton;
  15. import android.widget.SeekBar;
  16. import android.widget.ToggleButton;
  17. import android.widget.CompoundButton.OnCheckedChangeListener;
  18. public class MainActivity extends ActionBarActivity implements Runnable{
  19. private static final int CMD_LED_OFF = 2;
  20. private static final int CMD_LED_ON = 1;
  21. SeekBar bar;
  22. ToggleButton buttonLed;
  23. private UsbManager usbManager;
  24. private UsbDevice deviceFound;
  25. private UsbDeviceConnection usbDeviceConnection;
  26. private UsbInterface usbInterfaceFound = null;
  27. private UsbEndpoint endpointOut = null;
  28. private UsbEndpoint endpointIn = null;
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. bar = (SeekBar)findViewById(R.id.seekbar);
  34. buttonLed = (ToggleButton)findViewById(R.id.arduinoled);
  35. buttonLed.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView,
  38. boolean isChecked) {
  39. if(isChecked){
  40. sendCommand(CMD_LED_ON);
  41. }else{
  42. sendCommand(CMD_LED_OFF);
  43. }
  44. }});
  45. usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
  46. }
  47. @Override
  48. public void onResume() {
  49. super.onResume();
  50. Intent intent = getIntent();
  51. String action = intent.getAction();
  52. UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  53. if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  54. setDevice(device);
  55. } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
  56. if (deviceFound != null && deviceFound.equals(device)) {
  57. setDevice(null);
  58. }
  59. }
  60. }
  61. private void setDevice(UsbDevice device) {
  62. usbInterfaceFound = null;
  63. endpointOut = null;
  64. endpointIn = null;
  65. for (int i = 0; i < device.getInterfaceCount(); i++) {
  66. UsbInterface usbif = device.getInterface(i);
  67. UsbEndpoint tOut = null;
  68. UsbEndpoint tIn = null;
  69. int tEndpointCnt = usbif.getEndpointCount();
  70. if (tEndpointCnt >= 2) {
  71. for (int j = 0; j < tEndpointCnt; j++) {
  72. if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
  73. if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) {
  74. tOut = usbif.getEndpoint(j);
  75. } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) {
  76. tIn = usbif.getEndpoint(j);
  77. }
  78. }
  79. }
  80. if (tOut != null && tIn != null) {
  81. // This interface have both USB_DIR_OUT
  82. // and USB_DIR_IN of USB_ENDPOINT_XFER_BULK
  83. usbInterfaceFound = usbif;
  84. endpointOut = tOut;
  85. endpointIn = tIn;
  86. }
  87. }
  88. }
  89. if (usbInterfaceFound == null) {
  90. return;
  91. }
  92. deviceFound = device;
  93. if (device != null) {
  94. UsbDeviceConnection connection =
  95. usbManager.openDevice(device);
  96. if (connection != null &&
  97. connection.claimInterface(usbInterfaceFound, true)) {
  98. usbDeviceConnection = connection;
  99. Thread thread = new Thread(this);
  100. thread.start();
  101. } else {
  102. usbDeviceConnection = null;
  103. }
  104. }
  105. }
  106. private void sendCommand(int control) {
  107. synchronized (this) {
  108. if (usbDeviceConnection != null) {
  109. byte[] message = new byte[1];
  110. message[0] = (byte)control;
  111. usbDeviceConnection.bulkTransfer(endpointOut,
  112. message, message.length, 0);
  113. }
  114. }
  115. }
  116. @Override
  117. public void run() {
  118. ByteBuffer buffer = ByteBuffer.allocate(1);
  119. UsbRequest request = new UsbRequest();
  120. request.initialize(usbDeviceConnection, endpointIn);
  121. while (true) {
  122. request.queue(buffer, 1);
  123. if (usbDeviceConnection.requestWait() == request) {
  124. byte rxCmd = buffer.get(0);
  125. if(rxCmd!=0){
  126. bar.setProgress((int)rxCmd);
  127. }
  128. try {
  129. Thread.sleep(100);
  130. } catch (InterruptedException e) {
  131. }
  132. } else {
  133. break;
  134. }
  135. }
  136. }
  137. }
上面就是Android端的程序,这个有些安卓基础的应该看起来不难。下面是Arduino的代码

  1. int prvValue;
  2. void setup() {
  3. Serial.begin(9600);
  4. pinMode(13, OUTPUT);
  5. prvValue = 0;
  6. }
  7. void loop() {
  8. if(Serial.available()){
  9. byte cmd = Serial.read();
  10. if(cmd == 0x02){
  11. digitalWrite(13, LOW);
  12. }else if(cmd == 0x01){
  13. digitalWrite(13, HIGH);
  14. }
  15. }
  16. int sensorValue = analogRead(A0) >> 4;
  17. byte dataToSent;
  18. if(prvValue != sensorValue){
  19. prvValue = sensorValue;
  20. if (prvValue==0x00){
  21. dataToSent = (byte)0x01;
  22. }else{
  23. dataToSent = (byte)prvValue;
  24. }
  25. Serial.write(dataToSent);
  26. delay(100);
  27. }
  28. }



无耻的把作者的演示视频也下下来了

点击打开链接

下面是我写好的工程

点击打开链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/211541
推荐阅读
相关标签
  

闽ICP备14008679号