Easy way to control Kinect motor through OpenNI

8,370 views
Skip to first unread message

BitGriff

unread,
Dec 12, 2011, 12:50:25 PM12/12/11
to OpenNI
Hello all,

Here is easy way to work with Kinect's motor, through OpenNI. No other
drivers or libraries is required.

The code is following:

// OpenNI includes
#include <XnUSB.h>

// Standard includes
#include <stdio.h>
#include <time.h>

/**
* Class to control Kinect's motor.
*/
class KinectMotor
{
public:
KinectMotor();
virtual ~KinectMotor();

/**
* Open device.
* @return true if succeeded, false - overwise
*/
bool Open();

/**
* Close device.
*/
void Close();

/**
* Move motor up or down to specified angle value.
* @param angle angle value
* @return true if succeeded, false - overwise
*/
bool Move(int angle);

private:
XN_USB_DEV_HANDLE m_dev;
bool m_isOpen;
};

KinectMotor::KinectMotor()
{
m_isOpen = false;
}

KinectMotor::~KinectMotor()
{
Close();
}

bool KinectMotor::Open()
{
const XnUSBConnectionString *paths;
XnUInt32 count;
XnStatus res;

// Init OpenNI USB
res = xnUSBInit();
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBInit failed");
return false;
}

// Open "Kinect motor" USB device
res = xnUSBEnumerateDevices(0x045E /* VendorID */, 0x02B0 /*ProductID
*/, &paths, &count);
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBEnumerateDevices failed");
return false;
}

// Open first found device
res = xnUSBOpenDeviceByPath(paths[0], &m_dev);
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBOpenDeviceByPath failed");
return false;
}

XnUChar buf[1]; // output buffer

// Init motor
res = xnUSBSendControl(m_dev, (XnUSBControlType) 0xc0, 0x10, 0x00,
0x00, buf, sizeof(buf), 0);
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBSendControl failed");
Close();
return false;
}

res = xnUSBSendControl(m_dev,
XnUSBControlType::XN_USB_CONTROL_TYPE_VENDOR, 0x06, 0x01, 0x00, NULL,
0, 0);
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBSendControl failed");
Close();
return false;
}
return true;
}

void KinectMotor::Close()
{
if (m_isOpen) {
xnUSBCloseDevice(m_dev);
m_isOpen = false;
}
}

bool KinectMotor::Move(int angle)
{
XnStatus res;

// Send move control request
res = xnUSBSendControl(m_dev, XN_USB_CONTROL_TYPE_VENDOR, 0x31,
angle, 0x00, NULL, 0, 0);
if (res != XN_STATUS_OK) {
xnPrintError(res, "xnUSBSendControl failed");
return false;
}
return true;
}

int main(int argc, char *argv[])
{
KinectMotor motor;

if (!motor.Open()) // Open motor device
return 1;

motor.Move(31); // move it up to 31 degree
Sleep(1000);

motor.Move(-31); // move it down to 31 degree
Sleep(1000);

motor.Move(0);
return 0;
}

flymanbox

unread,
Dec 14, 2011, 3:58:29 AM12/14/11
to OpenNI
Great , Thanks !!!

AND, Is there any way to control the led State??

> }- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Nicolas Burrus

unread,
Dec 14, 2011, 4:02:44 AM12/14/11
to openn...@googlegroups.com
Thanks for sharing this!

A stupid question, have you tried it while grabbing images? No USB
resources conflicts? Since the motor is a different usb device I
expect it to work, but if you can already confirm it would be great!

Cheers,
Nicolas

> --
> You received this message because you are subscribed to the Google Groups "OpenNI" group.
> To post to this group, send email to openn...@googlegroups.com.
> To unsubscribe from this group, send email to openni-dev+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/openni-dev?hl=en.
>

flymanbox

unread,
Dec 14, 2011, 4:18:53 AM12/14/11
to OpenNI
It's works fine.

> > For more options, visit this group athttp://groups.google.com/group/openni-dev?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Nicolas Tisserand

unread,
Dec 14, 2011, 6:49:42 AM12/14/11
to openn...@googlegroups.com
Hi,

I've made a few changes to your code, to make it compile on macosx, and move the motors of all connected kinects simultaneously.

The modified file is here:

And I had to do patch openni this way to get it working:

Works fine, thanks a bunch!

BitGriff

unread,
Dec 15, 2011, 5:00:30 AM12/15/11
to OpenNI

On 14 дек, 14:02, Nicolas Burrus <nicolas.bur...@gmail.com> wrote:
> Thanks for sharing this!
>
> A stupid question, have you tried it while grabbing images? No USB
> resources conflicts? Since the motor is a different usb device I
> expect it to work, but if you can already confirm it would be great!
>

I tried it, when grabbing image - it works well. :-)
Not tried with depth, and skeleton tracking, but i think, it will work
fine.

Crash

unread,
Dec 16, 2011, 9:32:56 AM12/16/11
to OpenNI
Isn't it just faster to do this with openfreenect motor drivers...it's
much easier and you have motor and led support and you can still use
openni and nite! And it works for most systems and support C, C+
+ ,C# ... and maybe and some other languages.


Best regards ,Crash.

BitGriff

unread,
Dec 16, 2011, 3:06:17 PM12/16/11
to OpenNI

This method is example of using features, already implemented in
OpenNI.
And it doesn't need openfreenect or any other software.

led controlled the same way - sending USB control request through
OpenNI USB driver.
I need just a few lines of code.

Best regards.

rumppat

unread,
Dec 18, 2011, 8:07:06 AM12/18/11
to OpenNI
Your code doesn't work for me.

My configuration:
OpenNI-Bin-Dev-Linux-x64-v1.4.0.2
Sensor-Bin-Linux-x64-v5.0.5.1_avin2
nite-bin-linux-x64-v1.5.0.2

What I get, when I start the test Program:
"xnUSBSendControl failed: Wrong USB control type requested!"

Can someone please tell my, what is wrong here?

BitGriff

unread,
Dec 18, 2011, 8:17:06 AM12/18/11
to OpenNI

I use OpenNI 1.3.2.3 on Windows 7.

It can be caused by changes in ps3drv USB driver, in your version of
OpenNI.

Best regards,

rumppat

unread,
Dec 18, 2011, 1:00:00 PM12/18/11
to OpenNI
Thank you for this information.

I've chosen a combination of freenect and OpenNI/NITE/Avin2-driver to
be able to control the motor and still being able to create programs
using OpenNI & NITE.
Since I only need the motor control to calibrate my kinect before
demos or presentations of my application, the freenect-glview sample
is just fine for me. :)

KR
rumppat

cherry980711

unread,
Dec 30, 2011, 10:05:52 PM12/30/11
to OpenNI
Nice!!! Thank you!

Andrew Davison

unread,
Dec 31, 2011, 4:09:29 AM12/31/11
to OpenNI

Some time back, I uploaded a chapter and code on doing this in Java.
You can find it at:
http://fivedots.coe.psu.ac.th/~ad/jg/nui16/

- Andrew

陳顥文

unread,
Feb 8, 2012, 12:22:12 PM2/8/12
to openn...@googlegroups.com
hi!

i got some error when i execute
but everythings is right when compile

1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnUSBSendControl 在函式 "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) 中被參考
1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnUSBOpenDeviceByPath 在函式 "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) 中被參考
1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnUSBEnumerateDevices 在函式 "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) 中被參考
1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnPrintError 在函式 "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) 中被參考
1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnUSBInit 在函式 "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) 中被參考
1>isocpp.obj : error LNK2019: 無法解析的外部符號 __imp__xnUSBCloseDevice 在函式 "public: void __thiscall KinectMotor::Close(void)" (?Close@KinectMotor@@QAEXXZ) 中被參考
1>E:\Users\Nathaniel.Think\Documents\Visual Studio 2010\Projects\isocpp\Debug\isocpp.exe : fatal error LNK1120: 6 個無法解析的外部符號

somebody can help me?

Niranjan s

unread,
Feb 9, 2012, 1:58:10 AM2/9/12
to openn...@googlegroups.com
can you give more details about the error?? are u compiling your project in a x86 or 64 mode??

2012/2/8 陳顥文 <ar801...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "OpenNI" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openni-dev/-/m_Fuk7VusvwJ.

mini

unread,
Feb 9, 2012, 2:53:30 AM2/9/12
to OpenNI
Hi,

do you have any ideas on how to do this in C#?
I didn't find any equivalent to "XnUSB.h" in the C# wrapper.

I'm working with Win7 64bit and what I've tried so far is installing
the CL NUI drivers (version 1121) in order to control the kinect's
motor.
What I have managed is to compile and run a simple demo (C#) on x86
but whenever I changed to x64 I get an exception. However I need x64
for Openni.

Thank you very much for your help,
best regards,
Thomas

Roee Shenberg

unread,
Feb 9, 2012, 3:00:03 AM2/9/12
to openn...@googlegroups.com
From what I've seen, OpenNI.net doesn't wrap XnUSB, so you have to do the P/Invokes yourself. All the OpenNI P/Invokes are done at inside OpenNIImporter.cs (https://github.com/OpenNI/OpenNI/blob/master/Wrappers/OpenNI.net/OpenNIImporter.cs), so you can look at that, and create the correct P/Invokes according to XnUSB.h, and then translate the C code to C#.

Hope that helps,
Roee

--
You received this message because you are subscribed to the Google Groups "OpenNI" group.

Kunal shah

unread,
Feb 9, 2012, 4:14:34 AM2/9/12
to openn...@googlegroups.com
just try by changing target framework to x64 by right clicking to solution select debug tab and change the platform by activex64

i think it will do.

--
You received this message because you are subscribed to the Google Groups "OpenNI" group.
To post to this group, send email to openn...@googlegroups.com.
To unsubscribe from this group, send email to openni-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/openni-dev?hl=en.




--
" Monitor alone uses more than half of computer's total power. I choose to switch off the monitor every time I take a break."

Please consider your environmental responsibility before Printing.

Save paper, save water
Say no to Plastic

Longman

unread,
Feb 9, 2012, 5:11:29 AM2/9/12
to openn...@googlegroups.com
Can you post/publish how you did it on x86 in C#? Would be great :)

mini

unread,
Feb 9, 2012, 8:32:35 AM2/9/12
to OpenNI
Thank you for all your inputs.

Unfortunately I have never done P/Invokes myself but maybe I will give
it a try ;-)

I tried changing the solution and target platform to AnyCPU or x64 =>
nothing but errors.

Here's a (very very) small example of what I try to do (just the CLNUI
part to control the motor).
___________________________________________________________________
Code:
added CLNUIDevice.cs and CLNUIDevice.dll to Project

// just to see whether I can reach the code in debug mode
IntPtr motor = IntPtr.Zero;
motor = CLNUIDevice.CreateMotor();

Configuration:
Solution Platform: x86, Target Platform: x86,
=> motor = CLNUIDevice.CreateMotor(); can be executed

Any other configuration (Solution Platform: x86 or AnyCPU or x64;
Target Platform x64 or AnyCPU) => error
____________________________________________________________________________________


I found a similar discussion here <http://nkinect.codeplex.com/
discussions/245113> according to which it should work but I wasn't
successful.

If anyone is successful please let me know ;-)

best regards,

Thomas

mini

unread,
Feb 14, 2012, 3:17:46 AM2/14/12
to OpenNI
Hi all,

unfortunately I still had no success in integrating the CL NUI motor
drivers to use them together with the opennni drivers on my 64 bit
system.

Meanwhile I found a discussion ( <http://groups.google.com/group/
openni-dev/browse_thread/thread/9500415cda4d2bdd/8c8e8e14204c5f68?
lnk=gst&q=libfreenect+motor#8c8e8e14204c5f68> )
on using libfreenect drivers for motor control and sticking to the
openni drivers for camera control.

Does anyone know how to install the libfreenect drivers for motor
control over the openni drivers? I didn't find an installation guide
anywhere.

Thanks again for your help,

best regards,

Thomas
Message has been deleted

Nathaniel

unread,
Feb 21, 2012, 4:53:15 AM2/21/12
to openn...@googlegroups.com
i tried Debug in Win32 also x64 in administration mode


My OS is Windows 7 x64

the kinect has been install and can be execute other code(OpenNI)

sorry Post Chinese language edition Visual Studio error message 
the error message translate to English as:

1>kinectmoto.obj : error LNK2019: unresolved-external-symbol __imp__xnUSBSendControl  in function   "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) Is referenced
1>kinectmoto.obj : error LNK2019: unresolved-external-symbol __imp__xnUSBOpenDeviceByPath  in function   "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) Is referenced
1>kinectmoto.obj : error LNK2019: unresolved-external-symbol __imp__xnUSBEnumerateDevices  in function   "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) Is referenced
1>kinectmoto.obj : error LNK2019: unresolved-external-symbol __imp__xnPrintError  in function   "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) Is referenced
1>kinectmoto.obj : error LNK2019:unresolved-external-symbol __imp__xnUSBInit  in function   "public: bool __thiscall KinectMotor::Open(void)" (?Open@KinectMotor@@QAE_NXZ) Is referenced
1>kinectmoto.obj : error LNK2019: unresolved-external-symbol__imp__xnUSBCloseDevice  in function   "public: void __thiscall KinectMotor::Close(void)" (?Close@KinectMotor@@QAEXXZ) Is referenced
1>e:\users\nathaniel.think\documents\visual studio 2010\Projects\kinect moto cpp\Debug\kinect moto cpp.exe : fatal error LNK1120: 6 unresolved-external-symbol


thank your help:)

Nathaniel

unread,
Feb 21, 2012, 4:56:52 AM2/21/12
to openn...@googlegroups.com
the Xnusb.h have been placed in c:\program files (x86)\microsoft visual studio 10.0\vc\include\xnusb.h

Tony

unread,
Mar 15, 2012, 1:37:35 AM3/15/12
to OpenNI
You need to setup your project to include the openNi.lib object
Project properties-> linker->dependencies

エルマン 「Herman」

unread,
Mar 15, 2012, 1:38:59 AM3/15/12
to openn...@googlegroups.com
Hi there,

Is there any way to control the motor in linux?

Thanks, really appreciate your help

> --
> You received this message because you are subscribed to the Google Groups "OpenNI" group.

Anthony Smart

unread,
Mar 29, 2012, 3:09:45 PM3/29/12
to openn...@googlegroups.com
Follow the instructions I wrote for installing my program here:

http://csmartonline.com/blog/2012/03/29/compile-openni-for-kinect-motor-control-on-linux/

That should allow you to successfully use the original script posted above. This is based off of the following patch I found on GitHub:

https://github.com/manctl/openni/commit/5da82cf6d717267e57b97f9fbafc90b71b56572d

However, I have already wrote a program to control the Kinect motor, both in Linux and Windows, which can be found here:

Linux version: http://csmartonline.com/blog/2012/03/29/kinect-motor-control-linux/
Windows version: http://csmartonline.com/blog/2012/03/27/kinect-motor-control/

The program is provided "AS IS" and I do not have any plans for maintaining it.

エルマン

unread,
Mar 29, 2012, 3:28:54 PM3/29/12
to openn...@googlegroups.com
Thank you very much Anthony , I'm trying right now on ubuntu 10.10 32 bits.

I got:
libusb couldn't open USB device /dev/bus/usb/001/004: Permision denied.
libusb requires write access to usb device nodes.
Kinect not found! Use --help for help.

And if I run under sudo
sudo ./a.out
I got:
Kinect not found! Use --help for help.

I'm going to check the github patch

I really appreciate your help and your time!

Thanks!

Anthony Smart

unread,
Mar 30, 2012, 3:50:28 AM3/30/12
to openn...@googlegroups.com
I'm sorry you are having problems. If it was to do with the GitHub patch it would say something to the effect of 'wrong control type' being sent so I doubt it has to do with that if you edited the file my instructions say to edit. Also, by running the program as root, it should have worked as that overcomes the permission problem you described with libusb. I actually know how to fix that and had to fix it for myself initially but, I just forgot to add that into my instructions. To fix it, install SensorKinect by avin as described in the linked article at the bottom of the page or, if you know how, create your own udev rules to allow USB permissions for your user. This is a minor bug with OpenNI though and has nothing to do with your problem. It should have worked perfectly fine when run as root.

I just got finished setting up a virtual machine and following my instructions to make sure they actually work for Ubuntu 10.10. Although OpenNI could not compile the samples it comes with (I didn't spend much time on this as they were not my main focus), it was still able to compile it's driver and install successfully. My already compiled version of the program worked as is. Just for kicks, I compiled my program from source to see if it made any difference and that too worked. So now I am at a loss of what the problem could be for you.

If OpenNI is installed and working properly, you should be able to run the samples included with it, (if you were successful in compiling them). If they do not run successfully, then something is wrong with your installation.

Also, the original script posted above will work with very little modification (just need to change the sleep commands to be lowercase and to be '1' instead of '1000' and then use -std=gnu++0x as a g++ option on the command line). Not sure if you know how to do that or not but, I thought I'd throw it in as a option for you to try. :-D

Hope this helps!

paolofuse

unread,
Mar 30, 2012, 3:51:22 AM3/30/12
to openn...@googlegroups.com
Is there any way to include this into the Java wrapper?
Thanks.

Romuuu

unread,
Mar 30, 2012, 5:28:37 AM3/30/12
to openn...@googlegroups.com
Hello BitGriff,

Thanks a lot for your code, it works fine for me. But I have a question : where can we find the VendorID and the ProductID? Because my project works for now with Kinect, but we want to support Asus XTion too (and future others if any).

Thank you for your help!

Romu


On Monday, December 12, 2011 6:50:25 PM UTC+1, BitGriff wrote:
Hello all,

Here is easy way to work with Kinect's motor, through OpenNI. No other
drivers or libraries is required.

The code is following:

// OpenNI includes
#include <XnUSB.h>

// Standard includes
#include <stdio.h>
#include <time.h>

/**
 * Class to control Kinect's motor.
 */
class KinectMotor
{
public:
        KinectMotor();
        virtual ~KinectMotor();

        /**
         * Open device.
         * @return true if succeeded, false - overwise
         */
        bool Open();

        /**
         * Close device.
         */
        void Close();

        /**
         * Move motor up or down to specified angle value.
         * @param angle angle value
         * @return true if succeeded, false - overwise
         */
        bool Move(int angle);

private:
        XN_USB_DEV_HANDLE m_dev;
        bool m_isOpen;
};

KinectMotor::KinectMotor()
{
        m_isOpen = false;
}

KinectMotor::~KinectMotor()
{
        Close();
}

bool KinectMotor::Open()
{
        const XnUSBConnectionString *paths;
        XnUInt32 count;
        XnStatus res;

        // Init OpenNI USB
        res = xnUSBInit();
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBInit failed");
                return false;
        }

        // Open "Kinect motor" USB device
        res = xnUSBEnumerateDevices(0x045E /* VendorID */, 0x02B0 /*ProductID
*/, &paths, &count);
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBEnumerateDevices failed");
                return false;
        }

        // Open first found device
        res = xnUSBOpenDeviceByPath(paths[0], &m_dev);
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBOpenDeviceByPath failed");
                return false;
        }

        XnUChar buf[1]; // output buffer

        // Init motor
        res = xnUSBSendControl(m_dev, (XnUSBControlType) 0xc0, 0x10, 0x00,
0x00, buf, sizeof(buf), 0);
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBSendControl failed");
                Close();
                return false;
        }

        res = xnUSBSendControl(m_dev,
XnUSBControlType::XN_USB_CONTROL_TYPE_VENDOR, 0x06, 0x01, 0x00, NULL,
0, 0);
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBSendControl failed");
                Close();
                return false;
        }
        return true;
}

void KinectMotor::Close()
{
        if (m_isOpen) {
                xnUSBCloseDevice(m_dev);
                m_isOpen = false;
        }
}

bool KinectMotor::Move(int angle)
{
        XnStatus res;

        // Send move control request
        res = xnUSBSendControl(m_dev, XN_USB_CONTROL_TYPE_VENDOR, 0x31,
angle, 0x00, NULL, 0, 0);
        if (res != XN_STATUS_OK) {
                xnPrintError(res, "xnUSBSendControl failed");
                return false;
        }
        return true;
}

int main(int argc, char *argv[])
{
        KinectMotor motor;

        if (!motor.Open()) // Open motor device
                return 1;

        motor.Move(31); // move it up to 31 degree
        Sleep(1000);

        motor.Move(-31); // move it down to 31 degree
        Sleep(1000);

        motor.Move(0);
        return 0;
}

Romuuu

unread,
Mar 30, 2012, 8:48:15 AM3/30/12
to openn...@googlegroups.com
Ok sorry for the question, there is no motor on the Asus XTion... ;)

Longman

unread,
Apr 2, 2012, 4:11:31 AM4/2/12
to openn...@googlegroups.com
Thanks Anthony! Works like a charm for me!

Anthony Smart

unread,
Apr 3, 2012, 11:33:12 PM4/3/12
to openn...@googlegroups.com
Yeah! I'm glad it worked for you!!

I was beginning to wonder if it would work for anyone else but me...

Romuuu

unread,
Apr 4, 2012, 9:12:21 AM4/4/12
to openn...@googlegroups.com
Hi,

I have another question : is it normal that the LED is red when I use this code? Even after the program close.

Thank You!

Romu

Bartłomiej Hołota

unread,
Apr 19, 2012, 6:11:42 PM4/19/12
to openn...@googlegroups.com
Works for me on Ubuntu 12.04 x64 (beta :P). No problems so far.

jmendeth

unread,
Sep 29, 2012, 1:36:38 PM9/29/12
to openn...@googlegroups.com
Hey, wouldn't it be nice to add Motor and LED control to the OpenNI API?
I propose to add them as capabilities of the Device class.

What do you think?

El divendres 20 d’abril de 2012 0:11:42 UTC+2, Bartłomiej Hołota va escriure:

ilyes issaoui

unread,
Dec 4, 2012, 9:03:44 PM12/4/12
to openn...@googlegroups.com
Hello every body,

I'm new in kinect, I tested your code with VS2010 but I have this error
LINK : fatal error LNK1123: échec lors de la conversion en fichier COFF : fichier non valide ou endommagé

I installed OpenNI 1.5.4, and I linked it with visual
I have windows 7 32bit
Can you help me please?
thank you :)
Reply all
Reply to author
Forward
0 new messages