2010年3月24日 星期三

將感測器模擬軟體的數值傳送到車用虛擬儀表


今天教導學生如何安裝SensorSimulator的軟體,並將感測器的模擬訊號當成汽車的時速值,我還修改了SensorDemo部份程式,讓它能顯示虛擬儀表,其程式如下(粗體字為新增部份、斜體字為刪除部份):

package org.openintents.samples.SensorDemo;

import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class SensorDemoActivity extends Activity implements SensorListener {

private SensorManagerSimulator mSensorManager;

TextView mTextView1;
TextView mTextView2;
TextView mTextView3;

private float[] Oritation;

private VirtualMeterView mVirtualMeterView;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Oritation = new float[3];
mVirtualMeterView=new VirtualMeterView(this);
setContentView(mVirtualMeterView);

/* mTextView1 = (TextView) findViewById(R.id.text1);
mTextView2 = (TextView) findViewById(R.id.text2);
mTextView3 = (TextView) findViewById(R.id.text3);*/


////////////////////////////////////////////////////////////////
// INSTRUCTIONS
// ============

// 1) Use the separate application SensorSimulatorSettings
// to enter the correct IP address of the SensorSimulator.
// This should work before you proceed, because the same
// settings are used for your custom sensor application.

// 2) Include sensorsimulator-lib.jar in your project.
// Put that file into the 'lib' folder.
// In Eclipse, right-click on your project in the
// Package Explorer, select
// Properties > Java Build Path > (tab) Libraries
// then click Add JARs to add this jar.

// 3) You need the permission
//
// in your Manifest file!

// 4) Instead of calling the system service to obtain the Sensor manager,
// you should obtain it from the SensorManagerSimulator:

//mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);

// 5) Connect to the sensor simulator, using the settings
// that have been set previously with SensorSimulatorSettings
mSensorManager.connectSimulator();

// The rest of your application can stay unmodified.
////////////////////////////////////////////////////////////////

}



@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, SensorManager.SENSOR_ACCELEROMETER
| SensorManager.SENSOR_MAGNETIC_FIELD
| SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_FASTEST);
}

@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}

public void onAccuracyChanged(int sensor, int accuracy) {
}

public void onSensorChanged(int sensor, float[] values) {
switch(sensor) {
/* case SensorManager.SENSOR_ACCELEROMETER:
mTextView1.setText("Accelerometer: "
+ values[0] + ", "
+ values[1] + ", "
+ values[2]);
break;
case SensorManager.SENSOR_MAGNETIC_FIELD:
mTextView2.setText("Compass: "
+ values[0] + ", "
+ values[1] + ", "
+ values[2]);
break;*/

case SensorManager.SENSOR_ORIENTATION:

/* mTextView3.setText("Orientation: "
+ values[0] + ", "
+ values[1] + ", "
+ values[2]);*/


for(int i=0; i<3; i++)
Oritation[i]=values[i];
mVirtualMeterView.invalidate();
break;
}
}
private class VirtualMeterView extends View{
private static final String TEXTONMETER = "0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300";
private Path mPath;

public VirtualMeterView(Context context) {
super(context);
// TODO Auto-generated constructor stub

mPath = new Path();
RectF oval = new RectF(40,20,260,240);
mPath.addArc(oval , 120, 300);



}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();;
RectF oval = new RectF(50,30,250,230);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval , 120, 300, true, paint );

paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(20);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawTextOnPath(TEXTONMETER, mPath, 0, 0, paint);

float angle;
if(Oritation[0]>300)
angle = 300;
else
angle = Oritation[0];
float x = (float) (105 * Math.cos((120+angle)/180*3.14));
float y = (float) (105 * Math.sin((120+angle)/180*3.14));
canvas.drawLine(150+x, 130+y, 150, 130, paint);
}

}

}

8 則留言:

  1. 搞個...藉由"進氣的空氣流量"and"供油曲線"and"點火正時"and"活塞運作後所產生的引擎轉速模擬表
    喝喝....這個比較好玩啦!

    另案,時速表的設定值內可有藉由熱交換而降低時速之設定

    謝謝!

    回覆刪除
  2. 我的油表剛好壞了,幫我換一個...:)

    回覆刪除
  3. 謝謝vmabt的指導,我會請學生研究目前的ECU是不有提供這些資訊,如果可行可以列入本次比賽的項目之一。
    對於rala只能說愛莫能助。

    回覆刪除
  4. 想把orientation 設定在0.00,-90.0,0.00
    這樣在-90.0~0.00就跟車子踩油門ㄧ樣

    我想應該是在這邊做修改?


    for(int i=0; i<3; i++)
    Oritation[i]=values[i];

    然後有values[0], values[1], values[2]

    現在不知道怎樣把0跟2的數值固定在0
    然後控制1的部分 -90.0 ~ 0.00

    回覆刪除
  5. 為何畫第一個和第二個儀表板的時候,儀表板的數字會正常出現,但畫第三個和第四個儀表板的時候,儀表板的數字不會出現。

    回覆刪除
  6. 找個時間大家來討論看看

    回覆刪除