Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Python
Tip/Trick

NAO.NET, Python Programs to .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
19 Jun 2015CPOL3 min read 40.4K   774   11   17
Workaround to run and exchange data between .NET and other platforms

Image 1

Introduction

Youtube article video

This article mainly shows how to:

  1. Execute any process or file like Python programs using .NET
  2. Exchange data between Python and .NET using XML

Background

I have a robot named NAO V5. This robot version does not support .NET, but it uses Python to operate the robot. If you want to let .NET run command and exchange data with Python for your application, this will be useful for you. If you have NAO Robot and you want to use .NET framework like my case, you can use the DLL attached.

In this example, I use NAO robot v5 only as an example because it does not support .NET and previous versions support .NET.

Files Include

The attached files include:

  • .NET Windows application as interface
  • .NET DLL To interact with Python programs
  • Python example file
  • INI configuration file (you can change its values to your environment)

Using the Code

To use this code, you need:

  • .NET with some knowledge for C#
  • Some knowledge about Python

This code shows you the following tricks:

  • Execute Python files from .NET environment using C#
  • Getting the result data from executing files
  • Write and read XML files
  • Read INI files
  • Exchange data between Python and .NET through XML files
  • Use NAO Robot as example

I'll divide the code into three parts:

  1. Run python code from .NET Environment
  2. Exchange data between Python and .NET. using XML
  3. Use the complete example for NAO Robot

1. Run Python Code from .NET Environment

If you want to Execute python programs from .NET C# program, then you use this script:

C#
myProcess.StartInfo.FileName = fileName; //This is python File Name
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();

This code in the MSDN also.

2. Exchange Data between Python and .NET using XML

Usually, the previous code does not allow you to get much information you need from the process result or python program, to add more values. You can use many ways, I choose XML files.

Here is how to:

  1. Create and write XML In Python
  2. Read XML in C#

A. Write XML in Python

C#
E = lxml.builder.ElementMaker()
ROOT = E.root
DOC = E.doc
XMLHeadYaw = E.HeadYaw
XMLHeadPitch = E.HeadPitch
XMLLHand = E.LHand
XMLRHand = E.RHand

the_doc = ROOT(
        DOC(
            XMLHeadYaw('', value=str(HeadYaw)),
            XMLHeadPitch('', value=str(HeadPitch)),
            XMLLHand('', value=str(LHand)),
            XMLRHand('', value=str(RHand)),
            )   
        )

B. Read XML in C#

C#
public static string GetNAOValue(string ItemName,string ItemField)
     {
                 XmlDocument doc = new XmlDocument();
                 doc.Load(clsDefinitions.xmlFile);
                 XmlNode ItemNode = doc.GetElementsByTagName(ItemName).Item(0);
                 return ItemNode.Attributes[ItemField].Value;
 }

3. Use the Complete Example for NAO Robot

In my case: I had to adjust the Python files to start with the following statements using the arguments which I will pass from my .NET C# Program.

C#
if __name__ == "__main__":
   robotIP = clsStatics.clsStatics.RobotIP
   PORT = clsStatics.clsStatics.RobotPort
   Hand = "RHand"
   Angle = 1
   if len(sys.argv) > 1: 
        robotIP = sys.argv[1]
        clsFile.clsFile.logInFile(str(robotIP))
        Hand =  sys.argv[2]
        clsFile.clsFile.logInFile(str(Hand))
        Angle= float(sys.argv[3])
        clsFile.clsFile.logInFile(str(Angle))

HandMove(robotIP,PORT,Hand,Angle)

Then, adjust the .NET to execute these Python files like I discussed above, then add INI file to config the Robot information, then add XML to pass more data and results from robot and Python environment to the .NET environment (optional , you can cancel this step as well).

Points of Interest

I found this is a good workaround to interact with other platforms like Python using .NET C#. By wrap C# code to get and pass data while running both programs at the same time. *Note: You can use this code for any other program or execute any process from your .NET program.

History

  • Added more NAO Robot Events (current Have Hand, Head, Also Take picture)
  • Needed to add more Actions to the DLL
  • Caught more events and memory values from Robot
  • There is also OpenCV module for .NET started to develop it (in Progress..), but I may present it in another article

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

 
Questionlatency of calling Python scripts each 1sec Pin
Member 1180515917-Feb-16 6:04
Member 1180515917-Feb-16 6:04 
AnswerRe: latency of calling Python scripts each 1sec Pin
Tareq_Gamal6-Jun-16 13:01
Tareq_Gamal6-Jun-16 13:01 
Questionrun NAO Python code from C# WPF project Pin
Member 118051599-Nov-15 21:59
Member 118051599-Nov-15 21:59 
AnswerRe: run NAO Python code from C# WPF project Pin
Tareq Mohammed10-Nov-15 20:44
Tareq Mohammed10-Nov-15 20:44 
GeneralRe: run NAO Python code from C# WPF project Pin
Member 1180515913-Nov-15 1:02
Member 1180515913-Nov-15 1:02 
GeneralRe: run NAO Python code from C# WPF project Pin
Tareq_Gamal13-Nov-15 7:15
Tareq_Gamal13-Nov-15 7:15 
GeneralRe: run NAO Python code from C# WPF project Pin
Member 1180515917-Nov-15 21:25
Member 1180515917-Nov-15 21:25 
GeneralRe: run NAO Python code from C# WPF project Pin
Tareq_Gamal19-Nov-15 1:48
Tareq_Gamal19-Nov-15 1:48 
GeneralRe: run NAO Python code from C# WPF project Pin
Member 1180515920-Nov-15 21:57
Member 1180515920-Nov-15 21:57 
GeneralRe: run NAO Python code from C# WPF project Pin
Member 1180515920-Nov-15 23:43
Member 1180515920-Nov-15 23:43 
GeneralRe: run NAO Python code from C# WPF project Pin
Tareq_Gamal20-Nov-15 22:44
Tareq_Gamal20-Nov-15 22:44 
GeneralRe: run NAO Python code from C# WPF project Pin
Member 1180515923-Nov-15 1:21
Member 1180515923-Nov-15 1:21 
GeneralRe: run NAO Python code from C# WPF project Pin
Tareq_Gamal15-Apr-16 4:11
Tareq_Gamal15-Apr-16 4:11 
QuestionGood idea Pin
mhassan08325-Jun-15 22:29
mhassan08325-Jun-15 22:29 
AnswerRe: Good idea Pin
Tareq_Gamal15-Apr-16 4:15
Tareq_Gamal15-Apr-16 4:15 
GeneralMy Vote of 5 Pin
aarif moh shaikh21-Jun-15 18:57
professionalaarif moh shaikh21-Jun-15 18:57 
GeneralRe: My Vote of 5 Pin
Tareq Mohammed21-Jun-15 21:29
Tareq Mohammed21-Jun-15 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.