Android Things

Android Things之脉冲宽度调制(PWM)

2017-09-10  本文已影响29人  Yanqilong

上一篇文章讲了Android Things之闪烁的LED灯,不明白的请查看http://www.jianshu.com/p/09a6a66a339c

本节内容讲PWM如何进行控制,在讲之前先了解一些概念

电路连接图

电路连接图

获取开发板名称

请看这里http://www.jianshu.com/p/09a6a66a339c

获取PWM引脚名称,为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引脚并初始化配置

//声明设备管理器对象
PeripheralManagerService service = new PeripheralManagerService();

//获取PWM引脚名称
String pinName = BoardDefaults.getPWMPort();
//连接PWM引脚
Pwm pwm = service.openPwm(pinName);
//初始化属性,包括频率和工作周期
pwm.setPwmFrequencyHz(50);
pwm.setPwmDutyCycle(1);
//可以获取pwm
mPwm.setEnabled(true);

动态改变PWM

    /**
     * 脉冲宽度增长步长
     */
    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;

    /**
     * 工作周期是否在增长
     */
    private boolean mIsPulseIncreasing = true;
    private static final int INTERVAL_BETWEEN_STEPS_MS = 10000;
private Runnable mChangePWMRunnable = new Runnable() {
        @Override
        public void run() {
            //调整脉冲工作周期,但是要在限制的最大值和最小值之内
            if (mIsPulseIncreasing) {
                mActivePulseDuration += PULSE_CHANGE_PER_STEP_MS;
            } else {
                mActivePulseDuration -= PULSE_CHANGE_PER_STEP_MS;
            }
            // 控制在限制范围之内
            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]内取值
                mPwm.setPwmDutyCycle(100 * mActivePulseDuration / PULSE_PERIOD_MS);
                //重复改变
                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是脉冲宽度调制
 *
 * 脉冲宽度
 * pulse with
 * alias is duty cycle
 * 定义:脉冲宽度从学术角度讲就是电流或者电压随时间有规律变化的时间宽度
 *
 *
 * 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;

    /**
     * 脉冲是否在增长
     */
    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

如果文章对你有帮助,给我点个赞吧!~.~

上一篇下一篇

猜你喜欢

热点阅读