Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

Control Arduino Servo By KinectV2 Using C#

5.00/5 (4 votes)
14 Dec 2015CPOL2 min read 16.6K   866  
Control Arduino ports and servo using C# and Kinect V2

Image 1

Introduction

Source code contains 4 projects:

  • Arduino project (includes the way Arduino handles the commands and servo, led control)
  • dllArduinoConnection: This is a library you can use to control Arduino by C#
  • MotorController: This is a DLL. You can use it alone for controlling your KinectV2 by Windows Forms application.
  • WinKinectControl: This program Windows Forms application to see all works together.

and more projects for making sure that everything is working fine:

  • Microsoft demo for kinectV2 as example (from KinectV2 SDK)

This simple programs allow you to:

  • Control your Arduino device using Kinectv2 .NET environment.
  • It is useful for controlling Arduino using C#.
  • Control Arduino using Microsoft Kinect Device V2.

Background

I see many examples for the Arduino and how it works and is programmed. For .NET Environment, I made a DLL to control the Arduino from C# using Serial Port using (USB cable ) comes with and Arduinoono. Also, I applied this in a project using Kinect v2 and Servo as a simple example.

Using the Code

The project is divided into 4 parts:

  1. Arduino Code
  2. Arduino-C# Interface
  3. Servo C# control
  4. Kinect Control Servo

Part 1: Arduino Program

For Arduino part program send messages as parameters over serial port, Arduino device is just waiting for serial data which contains the command. The most important line is this one:

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

Then switch parameters read like this code:

C#
//// 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;
	}
}

Part 2: Arduino-C# Interface

This part in the code is made by C# using:

C#
System.IO.Ports.SerialPort

to send data (Parameters) to the Arduino. Here is a simple example:

C#
/// <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;
    }                
}

Part 3: Servo C# Control

This is the code for control Servo connected to Arduino from C#.

It's similar to leds in C# for Arduino handle. You can see the handle in Arduino code because it converts the degree, Command to be Servo, etc.

C#
/// <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;
   }

Part 4: Kinect Control Servo

To use all together, here is an example (included in the source code) Controls Servo using the left hand.

C#
///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);
}

Points of Interest

Now, you can run control your Arduino Servos, Ports from your Windows Application using dllArduinoConnection DLL. You can also use your Kinect Device in a Windows Forms Application using MotorController DLL.

Notes

  • You have to upload the Arduino source attached in your Arduino before using the dllArduinoConnection DLL.
  • You can just add a reference to MotorController DLL in your program to use KinectV2 in your Windows Forms Application.

History

I plan to control Arduino using Bluetooth nested of just serial port using USB, also add more commands to Arduino.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)