■ 서보 모터를 사용하는 방법을 보여준다.
▶ 부품 내역
1 2 3 4 5 6 7 8 |
──────────────────────────── 구분 모델 수량 비고 ───── ──────────────── ── ── CONTROLLER PWM/Servo Driver PCA9685 12-bit 1 MOTOR Tower Pro Micro Servo 9g - SG90 1 ──────────────────────────── |
[회로 구성도]
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
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 } } |