7. Using Visual Studio in combination with C # To control the tilt angle of Kinect,

Source: Internet
Author: User

7. Using Visual Studio in combination with C # To control the tilt angle of Kinect,

I always feel that I have written a lot in front of me, but I have never used Visual Studio, a heavy weapon used to develop the kinect.

So this time we will use Visual Studio to write a C # program, connect to the Kinect and call the standard function library of the Kinect SDK to change the pitch angle of the Kinect.

First, open VS to create a project. Select Windows under Visual C #, select the console application, name it Kinect_Controller, and select your own save path.

After the creation, go to solution Resource Manager. Find the reference, right-click it, and choose add reference. In the pop-up reference manager, search for Kinect in the upper right corner, select Microsoft Kinect 1.8.0.0, and click OK.

If the component reference settings are normal, we can see the components of the Kinect following the reference.

Next is our main program. Paste all the code here and then explain it.

Using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Microsoft. kinect; using System. threading; namespace Kinect_Controller {class Program {static void Main (string [] args) {KinectSensor sensor = KinectSensor. kinectSensors [0]; if (sensor = null) {Console. writeLine ("no available Kinect device found, program exited"); return;} sensor. start (); // obtain the first sensor connected to the PC and Start the sensor. eleva TionAngle = 0; // The pitch angle is restored to the zero-degree Console. writeLine ("Kinect started, Space key exited"); Console. writeLine ("current angle" + 0); ConsoleKey press; // Add 5 degrees to the up arrow, 5 degrees to the down arrow, and built-in maximum and minimum angle detection while (press = Console. readKey (). key )! = ConsoleKey. spacebar) {if (press = ConsoleKey. downArrow) {if (sensor. elevationAngle-5 <sensor. minElevationAngle) sensor. elevationAngle = sensor. minElevationAngle; else sensor. elevationAngle = sensor. elevationAngle-5;} else if (press = ConsoleKey. upArrow) {if (sensor. elevationAngle + 5> sensor. maxElevationAngle) sensor. elevationAngle = sensor. maxElevationAngle; else sensor. elevationAngle = sensor. elevationAngle + 5;} Thread. sleep (1, 1000); Console. writeLine ("current angle" + sensor. elevationAngle);} sensor. stop ();}}}

Next we will explain the code in detail from the top down.
(1) We have added two namespaces at the top.
Using Microsoft. Kinect; // notify the compiler to use the namespace. This is required for all the Kinect programs written in C.
Using System. Threading; // This namespace must be introduced because the Thread method is used in the program.

(2) sensor acquisition
KinectSensor sensor = KinectSensor. KinectSensors [0];
Until sensor. Start (); this code segment is the first Kinect sensor to be connected to the pc. If it is empty, the output "no available Kinect device is found and the program exits". If it is not empty, start the Kinect.

(3) reset Angle
The tilt angle of the Kinect system may not be set to zero in the initial state due to human or program reasons. Therefore, after starting the Kinect system, we must first reset the angle of the Kinect system. Use sensor. ElevationAngle = 0 and output two sentences to describe our program.

(4) adjust the angle of the upstream and downstream keys based on the boundary Mechanism
Because the angle and pitch range in the Kinect is-27 ~ 27. Therefore, we need to add a detection mechanism.
This is not difficult. The principle is that the read button is safe if it is the up arrow key (the down arrow key) and the angle is not greater than the maximum angle after 5 degrees (minus 5 degrees, if the value exceeds, the angle is equal to the maximum angle.

Sensor. MinElevationAngle and sensor. MaxElevationAngle are-27 and + 27 respectively.

After adjusting the angle, output the current angle to remind the operator

(5) Exit
While (press = Console. ReadKey (). Key )! = ConsoleKey. Spacebar)
This is the while condition, so when we press the space, it will jump out and directly execute sensor. Stop () to exit the program.

(6) Precautions and Problems
As mentioned in the official documents of Kinect for Windows 1.0, the angle can be changed at most once in one second. After 15 consecutive changes, the system will force a rest of 20 seconds to prevent the motor from overheating, this is also the reason why we introduce Thread (1000), which meets this requirement and gives the Kinect a sufficient rest and cooling.

Because of this, our program will inevitably have some problems. If you press the up and down arrow keys more frequently than one second, the keyboard input will accumulate in the buffer zone for processing, because our setting is to wait a second and process it once. This will cause a great deal of latency and lag. We can make some optimization as needed, that is, the keyboard input is invalid within 1 second of sleep. This time we will not discuss it in depth, because when writing a blog post, my C # is also a little miserable.

The last one is the. NET Component version. By default, VS considers that your program needs to be bound to a specific version of the. NET component. In this way, after the component is upgraded, VS cannot find the component of the original version and cannot compile the component. In Solution Explorer, right-click Microsoft. Kinect and choose Properties. Set the version to False.

Run the Kinect program on your own. The running result is as follows.

NOTE: For the code in the blog post, refer to and modify it from "getting started with the design of the Kinect somatosensory program".


By Mr. Losers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.