Click here to Skip to main content
15,889,335 members
Articles / Internet of Things / Arduino
Tip/Trick

Control Arduino Servo By KinectV2 Using C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
14 Dec 2015CPOL2 min read 16.1K   866   4  
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)


Written By
Team Leader ArabicRobotics.com
Egypt Egypt
Tareq Gamal El-din Mohammed,
---------
Website:
www.ArabicRobotics.com

---------

Graduated from Modern Academy for Computer science and Information Technology. Egypt,
Then flow Microsoft development track Certificates:
MCAD.NET (Microsoft Certified Application Developer)
MCSD.NET (Microsoft Certified Solution Developer)
Microsoft SharePoint Administration, Configuration and Development.

Robotics fields was a Hobby since 2002,
started to develop some applications for "Robosapien", "RoboSapienV2", "RS Media", RoboMe and WowWee Rovio. from WowWee company,

Started working with robots as a professional way at 2014
By using "NAOqi" Robotics from Aldebaran.

By developing some applications and libraries like :
NAO.NET.
https://www.youtube.com/watch?v=oOyy-2XyT-c

OpenCV with NAO Robot:

- NAORobot Vision using OpenCV -TotaRobot P1
https://www.youtube.com/watch?v=MUcj8463x08

- NAO Robot Vision using OpenCV - P2
https://www.youtube.com/watch?v=93k1usaS-QM

NAO Alarm Clock :
https://www.youtube.com/watch?v=djLlMeGLqOU
-----------------------------

also Robotic Arm Project:


Other Projects Developed by Tareq Gamal El-din Mohammed :

Developed and posted some applications in Code Project web site like :

- Control your Distributed Application using Windows and Web Service
http://www.codeproject.com/Articles/101895/Control-your-Distributed-Application-using-Windows


- Quick and dirty directory copy
http://www.codeproject.com/Articles/12745/Quick-and-dirty-directory-copy

- Program Execute Timer(From the Web)
http://www.codeproject.com/Articles/12743/Program-Executer-Timer

Comments and Discussions

 
-- There are no messages in this forum --