65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 和 KinectV2 控制 Arduino 舵机

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4投票s)

2015 年 12 月 14 日

CPOL

2分钟阅读

viewsIcon

16988

downloadIcon

907

使用 C# 和 Kinect V2 控制 Arduino 端口和舵机

引言

源代码包含 4 个项目

  • Arduino 项目(包括 Arduino 处理命令和舵机、LED 控制的方式)
  • dllArduinoConnection:这是一个库,可用于通过 C# 控制 Arduino
  • MotorController:这是一个 DLL。您可以单独使用它来通过 Windows Forms 应用程序控制您的 KinectV2。
  • WinKinectControl:这个 Windows Forms 应用程序展示了所有功能协同工作。

还有更多项目,以确保一切正常工作

  • Microsoft KinectV2 演示示例(来自 KinectV2 SDK)

这个简单的程序允许您

  • 使用 Kinectv2 .NET 环境控制您的 Arduino 设备。
  • 它对于使用 C# 控制 Arduino 非常有用。
  • 使用 Microsoft Kinect 设备 V2 控制 Arduino。

背景

我看到很多关于 Arduino 的示例以及它的工作原理和编程方式。对于 .NET 环境,我制作了一个 DLL,使用串口(通过 Arduino 自带的 USB 电缆)从 C# 控制 Arduino。我还将此应用于一个使用 Kinect v2 和舵机的简单示例项目。

Using the Code

该项目分为 4 个部分

  1. Arduino 代码
  2. Arduino-C# 接口
  3. 舵机 C# 控制
  4. Kinect 控制舵机

第一部分:Arduino 程序

对于 Arduino 部分程序,它通过串口发送消息作为参数,Arduino 设备只是等待包含命令的串口数据。最重要的行是这一行

val = Serial.read();         // read it and store it in 'val'

然后,参数的切换读取如下代码

//// param 1,2,3,4 is the chars read from serial port.
void SwitchParam1()
{
	switch (serialData.param1)
	{
	/// case 'm' means that command is to move a motor.
	case 'm':		
	// ConvertToInt is a function which just convert char to Integer.
	serialData.intParam3 = serialData.convertToInt(serialData.param3);
	serialData.intParam4 = serialData.convertToInt(serialData.param4);		
		Motor(serialData.param2);
			break;	
			// case 'l' means we want to on/off a port			
	case 'l':
		Light(serialData.param2);// this is example to on/off leds 1,2,3,...
		break;
	}
}

第二部分:Arduino-C# 接口

代码中的这部分使用 C# 编写,用于

System.IO.Ports.SerialPort

发送数据(参数)到 Arduino。这是一个简单的示例

/// <summary>
/// send data with return the duino feedback then choose close/ let port open 
/// </summary>
/// <param name="data"></param>
/// <param name="close">false = the port will still open 
/// after sending the command to Arduino.</param>
/// <param name="waitForReplayMS">This Parameter for wait 
/// until return Value from Arduino with Millisacands</param>
public string Send(string data,bool close,int waitForReplayMS=2000)
{
    string strReturnData;
    try
    {
        Send(data,false);
        System.Threading.Thread.Sleep(waitForReplayMS);
        strReturnData =  Read();
        System.Threading.Thread.Sleep(waitForReplayMS);
        strReturnData = currentRead;
        
        if (close)
            BluetoothPort.Close();
            
        return strReturnData;                
    }
    catch (Exception ex)
    {
        
        throw ex;
    }                
}

第三部分:舵机 C# 控制

这是从 C# 控制连接到 Arduino 的舵机的代码。

它类似于 Arduino 处理中的 LED。您可以在 Arduino 代码中看到处理方式,因为它将角度、命令转换为舵机等。

/// <summary>
/// Actual Move motor to a degree or more/less and return moving result
/// </summary>
/// <param name="motor">motor name to move </param>
/// <param nameq="action">MoveTo/More/less</param>
/// <param name="degree">degree by 10-20,30,... untill 180 </param>
/// <param name="waitForReplay"> time to wait for return value and for moving </param>
/// <returns> moving return value from serial port </returns>
public string MoveMotor(Motor motor,MotorAction action, int degree, int waitForReplay=2000)

   string Command ="";
   switch (action)
   {                
           /// Get Command as string
       case MotorAction.MoveTo:
           Command =  MoveTo(motor, degree);
           break;
       case MotorAction.More:
           Command = More(motor, degree);
           break;
       case MotorAction.Less:
           Command = Less(motor,degree);
           break;
           /// End get
   }
   try
   {
       /// Send Parameters and get the FeedBack
       
       string duinoReturn = blueTooth.Send(Command ,true, waitForReplay);
       // return the Duino feed Back after doing this Action
       return duinoReturn;
   }
   catch (Exception ex)
   {               
       throw ex;
   }

第四部分:Kinect 控制舵机

要一起使用,这里有一个示例(包含在源代码中),使用左手控制舵机。

///this code get the current position for hand and move servo to left side.
if (hand.GetHandPosition(MotorController.utilities.Side.Left).x >=0)
{                
////this method to move servo and return the result from Arduino 
	MoveResult =  moveMotor(dllArduinoConnection.clsMotor.Motor.leftMotor,
	dllArduinoConnection.clsMotor.MotorAction.MoveTo,100);
}

关注点

现在,您可以使用 dllArduinoConnection DLL 通过 Windows 应用程序控制您的 Arduino 舵机和端口。您还可以使用 MotorController DLL 在 Windows Forms 应用程序中使用 Kinect 设备。

注释

  • 在使用 dllArduinoConnection DLL 之前,您必须将附加的 Arduino 源代码上传到您的 Arduino。
  • 您只需在程序中添加对 MotorController DLL 的引用,即可在 Windows Forms 应用程序中使用 KinectV2。

历史

我计划使用蓝牙代替仅使用 USB 串口控制 Arduino,并添加更多命令到 Arduino。

© . All rights reserved.