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.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:macro:lego:lab:csharp [2018/01/15 21:51] – [System Architecture] sebadab796 | en: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: | ||
| + | |||
| + | |||
| + | < | ||
| + | _brick = new Brick(new BluetoothCommunication(" | ||
| + | _brick.ConnectAsync(); | ||
| + | _brick.DirectCommand.PlayToneAsync(10, | ||
| + | </ | ||
| + | |||
| + | |||
| + | 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 " | ||
| + | |||
| + | < | ||
| + | this.DrawHandAsync(body.HandLeftState, | ||
| + | this.DrawHandAsync(body.HandRightState, | ||
| + | </ | ||
| + | |||
| + | |||
| + | In the above method, hand and shoulder variables (both right) should be defined: | ||
| + | < | ||
| + | Joint rShoulder = joints[JointType.ShoulderRight]; | ||
| + | Joint rHand = joints[JointType.HandRight]; | ||
| + | </ | ||
| + | |||
| + | 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. | ||
| + | |||
| + | < | ||
| + | drawingContext.DrawEllipse(this.handClosedBrush, | ||
| + | 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(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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. | ||
| + | |||
| + | < | ||
| + | private async void goForward() | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | private async void turnRight() | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | </ | ||
| Line 11: | Line 77: | ||
| There are basically four classes that can be treated as the data model for the Leap Motion: | There are basically four classes that can be treated as the data model for the Leap Motion: | ||
| + | |||
| === Hand === | === Hand === | ||
| - | | + | |
| - | | + | |
| - | - More information: | + | |
| + | <figure RefPic1> | ||
| + | {{ : | ||
| + | < | ||
| + | hand. [[https:// | ||
| + | </ | ||
| - | Figure 1, The Hand PalmNormal and Direction define the orientation of the | ||
| - | hand. Source: https:// | ||
| === Arm === | === Arm === | ||
| - | | + | |
| - | | + | |
| - | - More information: | + | |
| === Finger === | === Finger === | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | - More information: | + | |
| - | PAGE 2Figure | + | <figure RefPic2> |
| - | finger tip and the general direction in which a finger is pointing. | + | {{ : |
| - | https:// | + | < |
| + | finger tip and the general direction in which a finger is pointing. | ||
| + | </ | ||
| === Bone === | === Bone === | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | - More information: | + | |
| ==== Coordinates System ==== | ==== Coordinates System ==== | ||
| - | The Leap Motion system employs a right-handed Cartesian coordinate | + | 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. |
| - | 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 | + | <figure RefPic3> |
| - | 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 | + | |
| - | https:// | + | |
| ==== System Architecture ==== | ==== System Architecture ==== | ||
| Line 64: | 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 | + | <figure RefPic4> |
| - | Source: | + | {{ : |
| - | https:// | + | < |
| + | </ | ||
| === WebSocket Interface === | === WebSocket Interface === | ||
| Line 73: | 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: | ||
| ==== Setting Up a Project ==== | ==== Setting Up a Project ==== | ||
| - | NOTE: The Motion Leap libraries (file LeapCSharp.NETx.x.dll located | + | |
| - | in the lib folder of the Leap SDK package) are expected to be loaded | + | The Motion Leap libraries (file LeapCSharp.NETx.x.dll located in the lib folder of the Leap SDK package) are expected to be loaded from the same directory as the executable of the application, |
| - | from the same directory as the executable of the application, | + | === Hello World C# === |
| - | required that SDK is downloaded. The linking step will be covered | + | |
| - | later. | + | |
| - | PAGE 5Hello | + | |
| - | 1. Create new project of the WPF or Windows Forms type. | + | |
| - | 2. Add a reference to one of the pre-built libraries: | + | |
| - | 2.1. | + | |
| - | Select Project > Add Reference. | + | |
| - | 2.2. | + | |
| - | In the References dialog, select Browse. | + | |
| - | PAGE 62.3. | + | |
| - | Browse to Leap Motion SDK folder. | + | |
| - | 2.4. | + | |
| - | Choose the appropriate library for your project | + | |
| - | (depending on the version of .NET being used) | + | |
| - | PAGE 73. Set the target platform to x86 or x64 | + | <note important> |
| - | 3.1. | + | |
| - | Select Project > Properties. | + | |
| - | 3.2. | + | |
| - | 3.3. | + | |
| - | 3.4. | + | |
| - | Select Build page. | + | |
| - | Set the Configuration to All Configurations. | + | |
| - | Under General, set Platform target to x86 or x64. | + | |
| - | PAGE 84. Edit the Post Build Event to copy the native libraries to the | + | |
| - | project’s target executable directory. | + | |
| - | 4.1. | + | |
| - | Important: | + | |
| - | the libraries in the lib/x64 directory, not the 32-bit libraries in | + | |
| - | the lib/x86 directory. | + | |
| - | 4.2. | + | |
| - | Open the project properties page. | + | |
| - | 4.3. | + | |
| - | Select the Build Events page. | + | |
| - | 4.4. | + | |
| - | Click Edit Post-build. | + | |
| - | 4.5. | + | |
| - | Edit the event field to copy the libraries: | + | |
| - | 4.5.1. | + | |
| - | xcopy /yr " | + | |
| - | 4.5.2. | + | |
| - | xcopy /yr " | + | |
| - | 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: | ||
| {{ : | {{ : | ||
| - | <note important> | + | <note important> |
| ==== Citations ==== | ==== Citations ==== | ||
| 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:// | + | * [[https:// |
| - | https:// | + | * [[https:// |
| - | https:// | + | * [[https:// |
| - | https:// | + | * [[https:// |
en/edu/zmitac/macro/lego/lab/csharp.1516049488.txt.gz · Last modified: by sebadab796
