在我的移動機器人項目,該項目有2個伺服電機在兩個平面旋轉的相機,我想通過旋轉來控制他們在我的Android應用程序的智能手機(通過讀數偏航/俯仰/從智能手機的加速度計滾動)。週期性任務(消息通過藍牙發送)
爲此,我必須通過藍牙向Arduino發送關於這三個角度的數據。因此,例如,樣本數據分組的樣子:
「Y:100,P:20,R:45」 其中[Y-偏航,P-間距,R輥。 >
bl.sendData("A:"+String.format("%.0f",azimut) + ",P:" + String.format("%.0f",pitch) + ",R:" + String.format("%.0f",roll) + ".");
欲被在每200ms發送環狀此消息(當我在此活動林) - - >(現在它是負責發送該命令/消息的Arduino
代碼每次點擊按鈕「發送」:D時發送)。
我還必須提到的是,除了這個消息Arduino的應用程序發送另一個消息,當我觸摸虛擬搖桿在這同樣的活動來控制我的移動機器人的方向,我希望這是同時發送郵件)
之間沒有衝突那我該如何得到它?一些示例代碼?
這是從Android Studio中的代碼(這個活動):
package com.samplecompass;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private cBluetooth bl = null;
private String address = "00:11:35:94:61:19";
private StringBuilder sb = new StringBuilder();
private Button send;
Float azimutF;
Float pitchF;
Float pitchMax=0.0f;
Float azimut; // View to draw a compass
Float pitch;
Float roll;
int zgoda=0;
int zgodaa=1;
int znak=1;
private TextView mPitch;
private SensorManager mSensorManager;
Sensor accelerometer;
Sensor magnetometer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bl = new cBluetooth(this, mHandler);
bl.checkBTState();
mPitch = (TextView) findViewById(R.id.Pitch);
send = (Button) findViewById(R.id.send);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
zgodaa=0;
//bl.sendData("Hello");
}
});
}
private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case cBluetooth.BL_NOT_AVAILABLE:
Log.d(cBluetooth.TAG, "Bluetooth is not available. Exit");
Toast.makeText(getBaseContext(), "Bluetooth is not available", Toast.LENGTH_SHORT).show();
finish();
break;
case cBluetooth.BL_INCORRECT_ADDRESS:
Log.d(cBluetooth.TAG, "Incorrect MAC address");
Toast.makeText(getBaseContext(), "Incorrect Bluetooth address", Toast.LENGTH_SHORT).show();
break;
case cBluetooth.BL_REQUEST_ENABLE:
Log.d(cBluetooth.TAG, "Request Bluetooth Enable");
BluetoothAdapter.getDefaultAdapter();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
break;
case cBluetooth.BL_SOCKET_FAILED:
Toast.makeText(getBaseContext(), "Socket failed", Toast.LENGTH_SHORT).show();
finish();
break;
case cBluetooth.RECIEVE_MESSAGE: // if message is recieved
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
//txtArduino.setText("Data from Arduino: " + sbprint); // update TextView
}
break;
}
};
};
protected void onResume() {
super.onResume();
bl.BT_Connect(address);
//bl.sendData("Hello World");
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
}
protected void onPause() {
super.onPause();
bl.BT_onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
if(zgoda==0) {
azimutF = -orientation[0] * 360/(2 * 3.14159f) - 90;
pitchF = orientation[1]*360/(2*3.14159f);
}
zgoda=1;
azimut = -orientation[0]*360/(2*3.14159f) - azimutF; // orientation contains: azimut, pitch and roll
pitch = -1*(orientation[1]*360/(2*3.14159f));
if(pitchMax<pitch){ pitchMax=pitch;}
/**if (Math.round(pitch) == 0) {
if(znak==1) znak=-1;
else if(znak==-1) znak=1;
}**/
roll = -orientation[2]*360/(2*3.14159f);
mPitch.setText(String.valueOf("Azimut: " + String.format("%.0f",azimut)) + String.valueOf("Znak:" + pitchMax) + String.valueOf("Pitch:" + String.format("%.0f",pitch)) + String.valueOf("Roll:" + String.format("%.0f",roll)));
if(zgodaa== 0) {bl.sendData("A:"+String.format("%.0f",azimut) + ",P:" + String.format("%.0f",pitch) + ",R:" + String.format("%.0f",roll) + ".");}
zgodaa=1;
}
}
}
}
https://guides.codepath.com/android/Repeating-Periodic-Tasks – jdv
@jdv我不知道什麼時候應該將這部分代碼插入到MyActivity/code - > // //我們需要使用這個處理程序包 import android.os.Handler; //創建Handler對象(默認在主線程中) Handler handler = new Handler(); //定義代碼塊被執行 私人的Runnable runnableCode =新的Runnable(){ ' –
@jdv CD'@覆蓋 公共無效的run(){// 這裏做什麼主線程 Log.d上(「處理程序」,「主線程調用」); //重複此同一可運行代碼塊再次2秒鐘 //「這個」被引用Runnable對象 handler.postDelayed(這一點,2000); } }; //通過處理程序 handler.post(runnableCode)發佈啓動初始運行的任務;' –