Click here to load reader
Upload
satoshi-fujimoto
View
145
Download
2
Embed Size (px)
Citation preview
2014/11/18レゴマインドストーム EV3×Kinect for Windows v1
Kinectで操作するレゴの実験指導書(仮)
Kinect で得られる骨格情報を用いて、レゴを動かします。
Kinect でレゴを動かすサンプルプログラムは下からダウンロード。
http://www.afrel.co.jp/kinectdownload/SampleKinectNXT.zip
とりあえず、レゴなしで Kinect で骨格情報を取得、モータのパワーを出力してみましょう。
【C#によるKinectアプリケーションの作成方法】Visual Studio 2013 を用います。次の 3 つが作成する際の重要なポイントなので覚えてください。
1.起動して、新規プロジェクトの作成→Visual C#の Windows から WPF Application を選択し、プロジェクト
名(SampleKinectNXT)をつける。
2.ソリューションエクスプローラーのプロジェクトの Reference を右クリックし、Micosoft.Kinect(.dll)を参照す
る。(Kinect SDK のバージョンは v1.8 です)
3.MainWindow.xaml.cs を編集します。
ここでダウンロードしたソリューション(VS2010 で書かれている)を開き、MainWindow.xaml.cs をコピーして貼
り付けてください。
【レゴなしでKinectの骨格情報を取得、モータのパワーを出力するプログラムを実行する】1.とりあえずレゴには接続しないので、レゴに関係する部分をコメントアウトします。using System;using System.Collections.Generic;using System.IO.Ports;using System.Windows;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using Microsoft.Kinect;//using NxtNet;
public MainWindow() {
・・・ // Nxt接続ポートの初期化
//InitializeNxt();
・・・
private void Window_Closed(object sender, EventArgs e) {
・・・ // NXTの停止・切断
//DisconnectNxt();・・・
次の関数もコメントアウトします。private void SetIsLinked(bool value)
以下の#region NXTの処理
で囲まれた部分をすべてコメントアウトします。private void InitializeNxt()private void connectButton_Click (object sender, RoutedEventArgs e)private void ConnectNxt()private void disconnectButton_Click (object sender, RoutedEventArgs e)private void DisconnectNxt()private void SetPower(int left, int right)private void StopNxt()
DriveNxt 関数内のコメントアウトされた参照関数をコメントアウトします。private void DriveNxt (Dictionary<JointType, Joint> trackedJoints) {・・・
// 首位置、各手首位置が水平状態になった場合、
// NXTと Kinectの連携を開始する。 … (9-4) //if (isLinked == false) //{ // if ( isConnected // && -5 <= leftPower && leftPower <= 5 // && -5 <= rightPower && rightPower <= 5) // { // SetIsLinked(true); // } //}・・・
// 5フレーム続けてトラッキングできないときは
//NXTを停止する
if (5 <= notTrackedCount) { //StopNxt();・・・
2.次に実行するアプリケーションの表示(UI)部分(MainWindow.xaml)を編集します。
MainWindow.xaml.cs では、rgbImage.Source にエラーが出ていたと思います。(赤の波線が付いている)
private void StopKinect(KinectSensor kinect) {
・・・ rgbImage.Source = null;
・・・ private void kinect_ColorFrameReady (Object sender, ColorImageFrameReadyEventArgs e) { // ピクセルデータを
//ビットマップに変換し画面に表示 … (4-3) rgbImage.Source = BitmapSource.Create (frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixel, frame.Width * frame.BytesPerPixel);
・・・
これは MainWindow.xaml ファイルを編集し、Kinect の RGB 画像を表示する場所<Image>を作ることで解決
します。
関節(ジョイント)の描画も同じく、エラーが出ます。private Dictionary<JointType, Joint> DrawSkeletonFrame(KinectSensor kinect, SkeletonFrame frame) {
・・・ // ジョイントの描画をすべて削除する … (6-2) skeletonCanvas.Children.Clear();
これも xaml ファイルを編集し、Canvas を表示する場所<Canvas>を作ることで解決します。
private void DriveNxt (Dictionary<JointType, Joint> trackedJoints) {
・・・ // 画面に出力値を表示 … (9-6) leftPowerText.Text = leftPower + "%"; rightPowerText.Text = rightPower + "%";
これも同様に、xaml ファイルを編集し、モータのパワーを出力するテキストの場所<TextBlock>を作ります。
最終的な MainWindow.xaml の内容を以下に示します。<Window x:Class="SampleKinectNXT.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="480" Width="800"> <Grid> <Image x:Name="rgbImage"/> <Canvas x:Name="skeletonCanvas" Height="480" Width="640"/> <TextBlock x:Name="leftPowerText" FontSize="24" HorizontalAlignment="Left"/> <TextBlock x:Name="rightPowerText" FontSize="24" HorizontalAlignment="Right"/> </Grid></Window>
3.では、実行してみてください。
左右にモータのパワー出力を表示
しています。腕を上げ下げして出力
が変わることを確かめてください。
次は NXT に接続して、実際に動
かしてみましょう。