传感器
电流、应力应变、温度、光等外界因素引起敏感元件磁性能变化转换成电信号,以这种方式来检测相应物理量的器件。
编码器在旋转量的检测控制中起关键作用,它在数控机床、机器人、工厂自动化设备的位置检测、传输速度控制,磁盘、打印机之类的自动化设备通讯设备的旋转量检测中都是不可缺少的重要部件。其检测对象是光磁图形,不受油雾粉尘的影响,因此比目前最先进的光编码器的可靠性高寿命长,尤其适合于自动焊接、油漆机器人和与钢铁有关的位置检测以及各种金属、木材、塑料等加工行业的应用。而仍大量使用光编码器,由于这种器件易受粉尘、油污和烟雾的影响,用在自动焊接、油漆机器人、纺织和钢铁、木料、塑料等的加工中,可靠性极差。应用AMR、GMR 、GMI敏感元件构成的旋转磁编码器,就不存在上述缺点,因此,它们的市场需求年增长率在30%以上。在家用电器和节能产品中也也有其广泛的应用潜力,在节能环保产品中也大有用武之地。若使用微型磁编码器和控制微机一体化,更有利于简化控制系统结构,减少元件数和占空体积,这在精密制造和加工业中意义十分重大。
说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等。当然android手机之所以称为智能手机,少不了这几款传感器的功劳了。下面就学习下了,这里主要学习光照,加速度和磁。
新建工程emSensorStudy,布局如下:
《?xml version=“1.0” encoding=“utf-8”?》
《LinearLayout
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:orientation=“verTIcal”
android:layout_margin=“5dp”
tools:context=“com.jared.emsensorsstudy.MainAcTIvity”》
《TextView
android:text=“Hello Sensors”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“22dp”/》
《Button
android:id=“@+id/startLightSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动LightSensor”
android:textAllCaps=“false”/》
《Button
android:id=“@+id/startAccelerSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动AccelerSensor”
android:textAllCaps=“false”/》
《Button
android:id=“@+id/startMagneTIcSensor”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“启动MagneTIcSensor”
android:textAllCaps=“false”/》
《/LinearLayout》
添加LightSensor,AccelerSensor,MagnetiSensor的Activity,修改MainActivity代码如下:
package com.jared.emsensorsstudy;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startLightSensorBtn;
private Button startAccelerSensorBtn;
private Button startMagneticSensorBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLightSensorBtn = (Button)findViewById(R.id.startLightSensor);
startAccelerSensorBtn = (Button)findViewById(R.id.startAccelerSensor);
startMagneticSensorBtn = (Button)findViewById(R.id.startMagneticSensor);
startLightSensorBtn.setOnClickListener(new myOnClickListener());
startAccelerSensorBtn.setOnClickListener(new myOnClickListener());
startMagneticSensorBtn.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.startAccelerSensor:
Intent intent1 = new Intent(getApplicationContext(), AccelerSensor.class);
startActivity(intent1);
break;
case R.id.startLightSensor:
Intent intent2 = new Intent(getApplicationContext(), LightSensor.class);
startActivity(intent2);
break;
case R.id.startMagneticSensor:
Intent intent3 = new Intent(getApplicationContext(), MagneticSensor.class);
startActivity(intent3);
break;
default:
break;
}
}
}
}
先要实现Light的功能,先修改布局如下:
《?xml version=“1.0” encoding=“utf-8”?》
《LinearLayout 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:layout_margin=“10dp”
tools:context=“com.jared.emsensorsstudy.LightSensor”》
《TextView
android:id=“@+id/light_level”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“22dp”/》
《/LinearLayout》
简单地实现了一个textview用来显示光照强度。接着修改LightSensor代码如下:
package com.jared.emsensorsstudy;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class LightSensor extends AppCompatActivity {
private SensorManager sensorManager;
private TextView lightLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_sensor);
lightLevel = (TextView)findViewById(R.id.light_level);
initWithLight();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(sensorManager != null) {
sensorManager.unregisterListener(listener);
}
}
public void initWithLight() {
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
lightLevel.setText(“Currrent light level is ”+value+“lx”);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};











