上一篇文章講了Android Things之閃爍的LED燈俱饿,不明白的請(qǐng)查看http://www.reibang.com/p/09a6a66a339c
本節(jié)內(nèi)容講PWM如何進(jìn)行控制驻啤,在講之前先了解一些概念
- 頻率計(jì)算公式(f=1/T)坎背,f代表頻率逊桦,單位是HZ郑象;1代表1秒绿语;T是周期
- 總周期属百,一個(gè)完整脈寬(沖)所耗費(fèi)的總時(shí)間
- 工作周期记劝,通電周期相對(duì)總周期的比例
電路連接圖
電路連接圖
獲取開(kāi)發(fā)板名稱(chēng)
請(qǐng)看這里http://www.reibang.com/p/09a6a66a339c
獲取PWM引腳名稱(chēng),為PWM0
/**
* Return the preferred PWM port for each board.
*/
public static String getPWMPort() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO6";
case DEVICE_EDISON:
return "GP12";
case DEVICE_JOULE:
return "PWM_0";
case DEVICE_RPI3:
return "PWM0";
case DEVICE_IMX6UL_PICO:
return "PWM7";
case DEVICE_IMX6UL_VVDN:
return "PWM3";
case DEVICE_IMX7D_PICO:
return "PWM1";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
連接到PWM引腳并初始化配置
//聲明設(shè)備管理器對(duì)象
PeripheralManagerService service = new PeripheralManagerService();
//獲取PWM引腳名稱(chēng)
String pinName = BoardDefaults.getPWMPort();
//連接PWM引腳
Pwm pwm = service.openPwm(pinName);
//初始化屬性族扰,包括頻率和工作周期
pwm.setPwmFrequencyHz(50);
pwm.setPwmDutyCycle(1);
//可以獲取pwm
mPwm.setEnabled(true);
動(dòng)態(tài)改變PWM
/**
* 脈沖寬度增長(zhǎng)步長(zhǎng)
*/
private static final double PULSE_CHANGE_PER_STEP_MS = 0.2;
/**
* 最小脈沖周期
*/
private static final double MIN_ACTIVE_PULSE_DURATION_MS = 1;
/**
* 最大脈沖周期
*/
private static final double MAX_ACTIVE_PULSE_DURATION_MS = 2;
/**
* 工作周期是否在增長(zhǎng)
*/
private boolean mIsPulseIncreasing = true;
private static final int INTERVAL_BETWEEN_STEPS_MS = 10000;
private Runnable mChangePWMRunnable = new Runnable() {
@Override
public void run() {
//調(diào)整脈沖工作周期厌丑,但是要在限制的最大值和最小值之內(nèi)
if (mIsPulseIncreasing) {
mActivePulseDuration += PULSE_CHANGE_PER_STEP_MS;
} else {
mActivePulseDuration -= PULSE_CHANGE_PER_STEP_MS;
}
// 控制在限制范圍之內(nèi)
if (mActivePulseDuration > MAX_ACTIVE_PULSE_DURATION_MS) {
mActivePulseDuration = MAX_ACTIVE_PULSE_DURATION_MS;
mIsPulseIncreasing = !mIsPulseIncreasing;
} else if (mActivePulseDuration < MIN_ACTIVE_PULSE_DURATION_MS) {
mActivePulseDuration = MIN_ACTIVE_PULSE_DURATION_MS;
mIsPulseIncreasing = !mIsPulseIncreasing;
}
//改變pwm的工作周期,要在[0, 100]內(nèi)取值
mPwm.setPwmDutyCycle(100 * mActivePulseDuration / PULSE_PERIOD_MS);
//重復(fù)改變
mHandler.postDelayed(this, INTERVAL_BETWEEN_STEPS_MS);
}
}
完整代碼
import android.os.Build;
import com.google.android.things.pio.PeripheralManagerService;
import java.util.List;
@SuppressWarnings("WeakerAccess")
public class BoardDefaults {
private static final String DEVICE_EDISON_ARDUINO = "edison_arduino";
private static final String DEVICE_EDISON = "edison";
private static final String DEVICE_JOULE = "joule";
private static final String DEVICE_RPI3 = "rpi3";
private static final String DEVICE_IMX6UL_PICO = "imx6ul_pico";
private static final String DEVICE_IMX6UL_VVDN = "imx6ul_iopb";
private static final String DEVICE_IMX7D_PICO = "imx7d_pico";
private static String sBoardVariant = "";
/**
* Return the preferred PWM port for each board.
*/
public static String getPWMPort() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO6";
case DEVICE_EDISON:
return "GP12";
case DEVICE_JOULE:
return "PWM_0";
case DEVICE_RPI3:
return "PWM0";
case DEVICE_IMX6UL_PICO:
return "PWM7";
case DEVICE_IMX6UL_VVDN:
return "PWM3";
case DEVICE_IMX7D_PICO:
return "PWM1";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
// For the edison check the pin prefix
// to always return Edison Breakout pin name when applicable.
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List<String> gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}
}
import android.app.Activity;
import com.google.android.things.pio.PeripheralManagerService;
import com.google.android.things.pio.Pwm;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.io.IOException;
/**
* PWM是脈沖寬度調(diào)制
*
* 脈沖寬度
* pulse with
* alias is duty cycle
* 定義:脈沖寬度從學(xué)術(shù)角度講就是電流或者電壓隨時(shí)間有規(guī)律變化的時(shí)間寬度
*
*
* Sample usage of the PWM API that changes the PWM pulse width at a fixed interval defined in
* {@link #INTERVAL_BETWEEN_STEPS_MS}.
*
* https://www.zybang.com/question/adcdecbe416f753023406451c9fde1e0.html
*
*/
public class PWMActivity extends Activity {
private static final String TAG = PWMActivity.class.getSimpleName();
// Parameters of the servo PWM
/**
* 最小脈沖周期
*/
private static final double MIN_ACTIVE_PULSE_DURATION_MS = 1;
/**
* 最大脈沖周期
*/
private static final double MAX_ACTIVE_PULSE_DURATION_MS = 2;
/**
* 脈沖周期
*/
private static final double PULSE_PERIOD_MS = 20; // Frequency of 50Hz (1000/20)
// Parameters for the servo movement over time
private static final double PULSE_CHANGE_PER_STEP_MS = 0.2;
private static final int INTERVAL_BETWEEN_STEPS_MS = 10000;
private Handler mHandler = new Handler();
private Pwm mPwm;
/**
* 脈沖是否在增長(zhǎng)
*/
private boolean mIsPulseIncreasing = true;
/**
* 脈沖周期
*/
private double mActivePulseDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting PWMActivity");
PeripheralManagerService service = new PeripheralManagerService();
try {
String pinName = BoardDefaults.getPWMPort();
mActivePulseDuration = MIN_ACTIVE_PULSE_DURATION_MS;
mPwm = service.openPwm(pinName);
// Always set frequency and initial duty cycle before enabling PWM
mPwm.setPwmFrequencyHz(1000 / PULSE_PERIOD_MS);
mPwm.setPwmDutyCycle(mActivePulseDuration);
mPwm.setEnabled(true);
// Post a Runnable that continuously change PWM pulse width, effectively changing the
// servo position
Log.d(TAG, "Start changing PWM pulse");
mHandler.post(mChangePWMRunnable);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Remove pending Runnable from the handler.
mHandler.removeCallbacks(mChangePWMRunnable);
// Close the PWM port.
Log.i(TAG, "Closing port");
try {
mPwm.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
} finally {
mPwm = null;
}
}
private Runnable mChangePWMRunnable = new Runnable() {
@Override
public void run() {
// Exit Runnable if the port is already closed
if (mPwm == null) {
Log.w(TAG, "Stopping runnable since mPwm is null");
return;
}
// Change the duration of the active PWM pulse, but keep it between the minimum and
// maximum limits.
// The direction of the change depends on the mIsPulseIncreasing variable, so the pulse
// will bounce from MIN to MAX.
if (mIsPulseIncreasing) {
mActivePulseDuration += PULSE_CHANGE_PER_STEP_MS;
} else {
mActivePulseDuration -= PULSE_CHANGE_PER_STEP_MS;
}
// Bounce mActivePulseDuration back from the limits
if (mActivePulseDuration > MAX_ACTIVE_PULSE_DURATION_MS) {
mActivePulseDuration = MAX_ACTIVE_PULSE_DURATION_MS;
mIsPulseIncreasing = !mIsPulseIncreasing;
} else if (mActivePulseDuration < MIN_ACTIVE_PULSE_DURATION_MS) {
mActivePulseDuration = MIN_ACTIVE_PULSE_DURATION_MS;
mIsPulseIncreasing = !mIsPulseIncreasing;
}
Log.d(TAG, "Changing PWM active pulse duration to " + mActivePulseDuration + " ms");
try {
// Duty cycle is the percentage of active (on) pulse over the total duration of the
// PWM pulse
Log.e(TAG, String.valueOf(100 * mActivePulseDuration / PULSE_PERIOD_MS));
mPwm.setPwmDutyCycle(100 * mActivePulseDuration / PULSE_PERIOD_MS);
// Reschedule the same runnable in {@link #INTERVAL_BETWEEN_STEPS_MS} milliseconds
mHandler.postDelayed(this, INTERVAL_BETWEEN_STEPS_MS);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
};
}
該例子是google官方的例子渔呵,這里給出源碼地址https://github.com/androidthings/sample-simplepio.git
如果文章對(duì)你有幫助怒竿,給我點(diǎn)個(gè)贊吧!~.~