1. Hardware:
ASUS X450J (Windows 10 Preview)
Lego EV3 (PortA : Left wheel, PortB: Right wheel
2. MainWindow.xaml
<Window x:Class="WPFLego1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
<Grid Margin="0,0,340,237">
<Button Name="前進" MouseEnter="前進_MouseEnter" MouseLeave="前進_MouseLeave" Content="前進" Margin="172,10,-172,-10" />
<Button x:Name="後退" MouseEnter="後退_MouseEnter" MouseLeave="後退_MouseLeave" Content="後退" Margin="172,158,-172,-158" />
<Button x:Name="左轉" MouseEnter="左轉_MouseEnter" MouseLeave="左轉_MouseLeave" Content="左轉" Margin="-10,85,10,-85" />
<Button x:Name="右轉" MouseEnter="右轉_MouseEnter" MouseLeave="右轉_MouseLeave" Content="右轉" Margin="354,85,-354,-85" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Lego.Ev3.Core;
using Lego.Ev3.Desktop;
using System.Threading.Tasks;
namespace WPFLego1
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Settings
EV3B = new Brick(new BluetoothCommunication("COM7"));
EV3B.BrickChanged += EV3B_BrickChanged;
EV3B.ConnectAsync();
}
void EV3B_BrickChanged(object sender, BrickChangedEventArgs e)
{
for (int i = 0; i < e.Ports.Count; i++)
{
//this.Title += e.Ports[InputPort.A].SIValue.ToString();
}
}
Brick EV3B;
private void 前進_MouseEnter(object sender, MouseEventArgs e)
{
this.Title = "前進";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, 50);
}
private void 前進_MouseLeave(object sender, MouseEventArgs e)
{
this.Title = "停止前進";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, 0);
}
private void 後退_MouseEnter(object sender, MouseEventArgs e)
{
this.Title = "後退";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, -50);
}
private void 後退_MouseLeave(object sender, MouseEventArgs e)
{
this.Title = "停止後退";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, 0);
}
private void 左轉_MouseEnter(object sender, MouseEventArgs e)
{
this.Title = "左轉";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50);
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, -50);
}
private void 左轉_MouseLeave(object sender, MouseEventArgs e)
{
this.Title = "停止左轉";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, 0);
}
private void 右轉_MouseEnter(object sender, MouseEventArgs e)
{
this.Title = "右轉";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, -50);
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, 50);
}
private void 右轉_MouseLeave(object sender, MouseEventArgs e)
{
this.Title = "停止右轉";
EV3B.DirectCommand.TurnMotorAtPowerAsync(OutputPort.All, 0);
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
EV3B.DirectCommand.StopMotorAsync(OutputPort.All, true);
EV3B.Disconnect();
}
}
}
沒有留言:
張貼留言