첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestApplication.zip
다운로드

▶ 부품 내역

구분 모델 수량 비고
CONTROLLER PWM/Servo Driver PCA9685 12-bit 1  
MOTOR Tower Pro Micro Servo 9g - SG90 1  

▶ 회로 구성도

▶ MainPage.xaml

<Page x:Class="TestApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApplication"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <TextBlock
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Margin="10"
                Text="Survo Motor Angle" />
            <Slider x:Name="slider"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Margin="10"
                Width="200"
                Minimum="-90"
                Maximum="60" />
            <TextBlock
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Margin="10"
                Text="{Binding ElementName=slider, Path=Value, Mode=OneWay}" />
        </StackPanel>
    </Grid>
</Page>

 

▶ MainPage.xaml.cs

using System;
using Windows.Devices.Pwm;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

using PwmPCA9685;

namespace TestApplication
{
    /// <summary>
    /// 메인 페이지
    /// </summary>
    public sealed partial class MainPage : Page
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// PWM 컨트롤러
        /// </summary>
        private PwmController pwmController;

        /// <summary>
        /// PWM 핀
        /// </summary>
        private PwmPin pwmPin;

        /// <summary>
        /// 주파수
        /// </summary>
        private const double FREQUENCY = 1000d / 15d;

        /// <summary>
        /// 중심 펄스 너비
        /// </summary>
        private const double CENTERING_PULSE_WIDTH = 0.0015d; // 1.5 * 0.001

        /// <summary>
        /// 주파수 * 중심 펄스 너비
        /// </summary>
        private const double FREQUENCY_MULTIPLY_BY_CENTERING_PULSE_WIDTH = FREQUENCY * CENTERING_PULSE_WIDTH;

        /// <summary>
        /// 각도 너비
        /// </summary>
        private const double ANGLE_WIDTH = 90d;

        /// <summary>
        /// 배율
        /// </summary>
        private const double MAGNIFICATION = 0.5d;

        /// <summary>
        /// 배율 / 각도 너비
        /// </summary>
        private const double MAGNIFICATION_DIVIDE_BY_ANGLE_WIDTH = MAGNIFICATION / ANGLE_WIDTH;

        /// <summary>
        /// 핀 번호
        /// </summary>
        private const int PIN_NUMBER = 3;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainPage()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            InitializePCA9685();

            this.slider.ValueChanged += slider_ValueChanged;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 슬라이더 값 변경시 처리하기 - slider_ValueChanged(sender, e)

        /// <summary>
        /// 슬라이더 값 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            double angle = this.slider.Value;

            double dutyCyclePercentage = GetDutyCyclePercentage(angle);

            this.pwmPin.SetActiveDutyCyclePercentage(dutyCyclePercentage);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region PCA9685 초기화하기 - InitializePCA9685()

        /// <summary>
        /// PCA9685 초기화하기
        /// </summary>
        private async void InitializePCA9685()
        {
            this.pwmController = (await PwmController.GetControllersAsync(PwmProviderPCA9685.GetPwmProvider()))[0];

            this.pwmController.SetDesiredFrequency(FREQUENCY);

            this.pwmPin = this.pwmController.OpenPin(PIN_NUMBER);

            this.pwmPin.SetActiveDutyCyclePercentage(0);

            this.pwmPin.Start();
        }

        #endregion
        #region 가동 주기 백분율 구하기 - GetDutyCyclePercentage(angle)

        /// <summary>
        /// 가동 주기 백분율 구하기
        /// </summary>
        /// <param name="angle">각도</param>
        /// <returns>가동 주기 백분율</returns>
        private double GetDutyCyclePercentage(double angle)
        {
            double dutyCyclePercentage = FREQUENCY_MULTIPLY_BY_CENTERING_PULSE_WIDTH * (1 + MAGNIFICATION_DIVIDE_BY_ANGLE_WIDTH * angle);

            return dutyCyclePercentage;
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요