User Tools

Site Tools


No ACL setup yet! Denying access to everyone.
en:edu:zmitac:macro:lego:lab:csharp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:macro:lego:lab:csharp [2018/01/15 21:59] – [Sample source code] sebadab796en:edu:zmitac:macro:lego:lab:csharp [2018/01/28 05:59] (current) – patrkaz065
Line 1: Line 1:
-===== Laboratory on LEGO Mindstorms - Programming NXT in C# =====+===== Laboratory on LEGO Mindstorms - EV3 C# Programming Theory & Practice =====
  
 +==== Introduction ====
 +
 +This tutorial shows the basics of programming theory and practice of LEGO EV3 robot in C# language. The physical behaviour and parameters of the robot are represented by set of methods and fields assigned to appropriate classes. 
 +In order to control the behaviour of the robot, appropriate classes have to be instantiated and initialized and appropriate methods have to be called. For example, the EV3 brick is represented in code by the class Brick.
 +
 +
 +==== C# Example - Controlling EV3 movement with hand gestures ====
 +The example is program which navigates the EV3 depending on the hand movement detected by kinect device.
 +The EV3 robot brick is represented in the C# code by class brick. The following code creates a new brick instance and initializes it with the new blutooth connection assigned to given port (COM4 in this case). Then, asynchronous bluetooth connection is etablished and confirmation sound is played on the robot side:
 +
 +
 +<code>
 +_brick = new Brick(new BluetoothCommunication("COM4"));
 +_brick.ConnectAsync();
 +_brick.DirectCommand.PlayToneAsync(10, 1000, 300);
 +</code>
 +
 +
 +The idea is such: asynchronously detect the movement of hands and take into account joints placement - basing on its coordinates the relative position of hand with respect to shoulders will be recognized. All neccessary data is given in argument "joints"
 +
 +<code>
 +this.DrawHandAsync(body.HandLeftState, jointPoints[JointType.HandLeft], dc, joints);
 +this.DrawHandAsync(body.HandRightState, jointPoints[JointType.HandRight], dc, joints);
 +</code>
 +
 +
 +In the above method, hand and shoulder variables (both right) should be defined:
 +<code>
 +Joint rShoulder = joints[JointType.ShoulderRight];
 +Joint rHand = joints[JointType.HandRight];
 +</code>
 +
 +Basing on the relative placement of the hand with respect to the shoulder, the appropriate action is performed (movement method is called). The mathematical comparisons are simply horizontal and vertical displacements between shoulders and hands.
 +
 +<code>
 +drawingContext.DrawEllipse(this.handClosedBrush, null, handPosition, HandSize, HandSize);
 +if (Math.Abs(rShoulder.Position.X - rHand.Position.X) < Math.Abs(rShoulder.Position.Y-rHand.Position.Y))
 +{
 +   if (rHand.Position.Y > rShoulder.Position.Y)
 +      goForward();
 +   else
 +      goBackward();
 +} else
 +{
 +   if (rHand.Position.X > rShoulder.Position.X)
 +      turnRight();
 +   else
 +      turnLeft();
 +}
 +</code>
 +
 +The movement functions are self explanatory and designed in the following way, calling the direct brick commands with appropriate parameters to move the robot as expected.
 +
 +<code>
 +private async void goForward()
 +{
 +   _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B|OutputPort.D,20);
 +}
 +
 +
 +private async void turnRight()
 +{
 +   _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, 20);
 +   _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.D, 0);
 +}
 +</code>
  
  
Line 16: Line 82:
   * Reports the physical characteristics of a detected hand.   * Reports the physical characteristics of a detected hand.
   * It’s tracking data includes a palm position and velocity, vectors for the palm normal and direction to the fingers and lists of the attached fingers.   * It’s tracking data includes a palm position and velocity, vectors for the palm normal and direction to the fingers and lists of the attached fingers.
-  * More information: [[https://developer.leapmotion.com/documentation/cpp/api/gen-cpp/class_leap_1_1_hand.html#details]] 
  
- +<figure RefPic1> 
-Figure 1, The Hand PalmNormal and Direction define the orientation of the +{{ :en:edu:zmitac:macro:lego:lab:1.png |}} 
-hand. Source: https://developer.leapmotion.com/documentation/+<caption>The Hand PalmNormal and Direction define the orientation of the 
 +hand. [[https://developer.leapmotion.com/documentation/python/devguide/Leap_Overview.html|Source]]</caption> 
 +</figure>
  
 === Arm === === Arm ===
Line 26: Line 93:
   * Bone-like class that represents the forearm.   * Bone-like class that represents the forearm.
   * Provides the orientation, length width and end points of an arm.   * Provides the orientation, length width and end points of an arm.
-  * More information: Arm class details. 
  
 === Finger === === Finger ===
Line 34: Line 100:
   * Information is provided for each finger on the tracked hand (characteristics or partially/not visible fingers are estimated)   * Information is provided for each finger on the tracked hand (characteristics or partially/not visible fingers are estimated)
   * Fingers are identified by type name, i.e. thumb, index.   * Fingers are identified by type name, i.e. thumb, index.
-  * More information: Finger class details. 
  
-PAGE 2Figure 2.Finger TipPosition and Direction vectors provide the position of a +<figure RefPic2> 
-finger tip and the general direction in which a finger is pointing. Source: +{{ :en:edu:zmitac:macro:lego:lab:2.png |}} 
-https://developer.leapmotion.com/documentation/+<caption>Finger TipPosition and Direction vectors provide the position of a 
 +finger tip and the general direction in which a finger is pointing. [[https://developer.leapmotion.com/documentation/csharp/devguide/Sample_Tutorial.html|Source]]</caption> 
 +</figure>
  
 === Bone === === Bone ===
Line 45: Line 112:
   * Provided by a Finger object.   * Provided by a Finger object.
   * Bones are ordered from base to tip, indexed from 0 to 3 (the thumb does not have a base metacarpal bone and therefore contains a valid, zero length at that location).   * Bones are ordered from base to tip, indexed from 0 to 3 (the thumb does not have a base metacarpal bone and therefore contains a valid, zero length at that location).
-  * More information: Details on Bone class. 
 ==== Coordinates System ==== ==== Coordinates System ====
  
 The Leap Motion system employs a right-handed Cartesian coordinate system. The origin is centered at the top of the Leap Motion Controller. The x- and z-axes lie in the horizontal plane, with the x-axis running parallel to the long edge of the device. The y-axis is vertical, with positive values increasing upwards (in contrast to the downward orientation of most computer graphics coordinate systems). The z-axis has positive values increasing toward the user.  The Leap Motion system employs a right-handed Cartesian coordinate system. The origin is centered at the top of the Leap Motion Controller. The x- and z-axes lie in the horizontal plane, with the x-axis running parallel to the long edge of the device. The y-axis is vertical, with positive values increasing upwards (in contrast to the downward orientation of most computer graphics coordinate systems). The z-axis has positive values increasing toward the user. 
  
-PAGE 3Figure 3. The Leap Motion right-handed coordinate system. Source: +<figure RefPic3> 
-https://developer.leapmotion.com/documentation/+{{ :en:edu:zmitac:macro:lego:lab:3.png |}} 
 +<caption>The Leap Motion right-handed coordinate system. [[https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Coordinate_Mapping.html|Source]]</caption> 
 +</figure> 
 ==== System Architecture ==== ==== System Architecture ====
  
Line 61: Line 130:
   * In case of C# the library is not linked directly, but with use of the special wrapper for binding   * In case of C# the library is not linked directly, but with use of the special wrapper for binding
  
-PAGE 4Figure 4.Leap-enabled application.  +<figure RefPic4> 
-Source: +{{ :en:edu:zmitac:macro:lego:lab:4.png |}} 
-https://developer.leapmotion.com/documentation/+<caption>Leap-enabled application. [[https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Architecture.html|Source]]</caption> 
 +</figure>
  
 === WebSocket Interface === === WebSocket Interface ===
Line 70: Line 140:
   * Tracking data in the form of JSON messages   * Tracking data in the form of JSON messages
   * JavaScript client is available for convenience   * JavaScript client is available for convenience
-  * More information: Details 
  
 ==== Setting Up a Project ==== ==== Setting Up a Project ====
Line 90: Line 159:
   * Edit the Post Build Event to copy the native libraries to the project’s target executable directory.   * Edit the Post Build Event to copy the native libraries to the project’s target executable directory.
  
-Important: If you are creating a 64-bit application, use +<note important>If you are creating a 64-bit application, use the libraries in the lib/x64 directory, not the 32-bit libraries in the lib/x86 directory.</note> 
-the libraries in the lib/x64 directory, not the 32-bit libraries in + 
-the lib/x86 directory. +  * Open the project properties page. 
-4.2. +  * Select the Build Events page. 
-Open the project properties page. +  * Click Edit Post-build. 
-4.3. +  * Edit the event field to copy the libraries: 
-Select the Build Events page. +  * For 32-bit projects add: <code c>xcopy /yr "$(LEAP_SDK)\lib\x86\LeapC.dll" "$(TargetDir)"</code> 
-4.4. +  * For 64-bit projects add: <code c>xcopy /yr "$(LEAP_SDK)\lib\x64\LeapC.dll" "$(TargetDir)"</code> 
-Click Edit Post-build. +  * Add your source code!
-4.5. +
-Edit the event field to copy the libraries: +
-4.5.1. For 32-bit projects add: +
-xcopy /yr "$(LEAP_SDK)\lib\x86\LeapC.dll" "$(TargetDir)" +
-4.5.2. For 64-bit projects add: +
-xcopy /yr "$(LEAP_SDK)\lib\x64\LeapC.dll" "$(TargetDir)" +
-5. Add your source code!+
 ==== Sample source code ==== ==== Sample source code ====
 Sample source code can be found here: Sample source code can be found here:
Line 115: Line 177:
 All citations, used images and more information about Leap Motion All citations, used images and more information about Leap Motion
 can be found here: can be found here:
-https://developer.leapmotion.com/documentation/csharp/devguide/Sample_Tutorial.html +  * [[https://developer.leapmotion.com/documentation/csharp/devguide/Sample_Tutorial.html|Sample_Tutorial]] 
-https://developer.leapmotion.com/documentation/python/devguide/Leap_Overview.html +  * [[https://developer.leapmotion.com/documentation/python/devguide/Leap_Overview.html|Leap_Overview]] 
-https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Architecture.html +  * [[https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Architecture.html|Leap_Architecture]] 
-https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Coordinate_Mapping.html+  * [[https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Coordinate_Mapping.html|Leap_Coordinate_Mapping]]
en/edu/zmitac/macro/lego/lab/csharp.1516049993.txt.gz · Last modified: by sebadab796