Hello all
Its been a while since I posted here, but I would like to share with you a little project I'm working on. After reading about Code Laboratories who managed to create a windows driver and SDK for the XBOX Kinect, I went out and bought one, I don't even own a XBOX. After downloading their driver and installed it, I wrote this demo program.
Requirements:
Computer with USB
Kinect with power supply cable (comes with it)
CL NUI Platform SDK can be downloaded here
http://codelaboratories.com/nui/
Windows (I have XP) dunno if the driver will work on vista or windows 7
.NET 3.5 download this from Microsoft (the SDK demo program requires it, but my code may not)
Dark Basic Pro
After you download and install SDK and driver. Run the demo program in the SDK to make sure your setup is good. I didn't have any luck until I installed .NET 3.5. I'm not actually sure if you need it with my code, but the demo requires it.
Make sure "CLNUIDevice.dll" is in your project directory!!
It can be found in "\Program Files\Code Laboratories\CL NUI Platform\SDK\Bin"
Caveats:
The driver is still very beta. Do the wrong thing and it will crash. Sometimes crash and reboot. I can't count how many times I've got an auto reboot while debugging my demo.
You have been warned!
Do not end your program without stopping the camera i.e. StopNUICamera and destroying the handles i.e. DestroyNUICamera, DestroyNUIMotor. Or else it may crash.
Thoughts:
This thing is packed with sensors accelerometers, four microphones, tilt sensors?, 3d imaging, and a camera. Its a bit of an overkill for simple gaming. It seems to be built for robotics in mind.
It has a motorized tilt in the base. While running my program pick up the Kinect and tilt it. The motor will try like mad to compensate for the tilt. Kinda cool.
For $150 its a bargain for whats in it.
Here's the demo code
// Initialize some constants
#constant KINECT_CLNUIDEVICE_DLL 1
#constant KINECT_COLOR_IMAGE 1
#constant KINECT_DEPTH_RAW_IMAGE 2
#constant KINECT_DEPTH_RGB_IMAGE 3
#constant KINECT_COLOR_IMAGE_MEMBLOCK 1
#constant KINECT_DEPTH_RAW_IMAGE_MEMBLOCK 2
#constant KINECT_DEPTH_RGB_IMAGE_MEMBLOCK 3
#constant KINECT_IMAGE_WIDTH 640
#constant KINECT_IMAGE_HEIGHT 480
#constant KINECT_COLOR_IMAGE_DEPTH 32
#constant KINECT_DEPTH_RAW_IMAGE_DEPTH 16
#constant KINECT_DEPTH_RGB_IMAGE_DEPTH 32
#constant KINECT_IMAGE_HEADER_OFFSET 12
#constant KINECT_LED_OFF 0
#constant KINECT_LED_SOLID_GREEN 1
#constant KINECT_LED_SOLID_RED 2
#constant KINECT_LED_SOLID_ORANGE 3
#constant KINECT_LED_BLINK_GREEN 4
#constant KINECT_LED_BLINK_RED_ORANGE 5
// adjust MATRIX_RESOLUTION to get a more precise matrix
// however the lower the number the slower it is
#constant MATRIX_RESOLUTION 8
// Setup the screen
disable escapekey // do not try to escape out
autocam off
set window layout 0,0,4
set window off
randomize timer()
position camera 32,50,-2
point camera 32,-16,32
// Initialize some variables and what not
matResX as integer
matResY as integer
matResX = KINECT_IMAGE_WIDTH / MATRIX_RESOLUTION
matResY = KINECT_IMAGE_HEIGHT / MATRIX_RESOLUTION
KinectBool as boolean
KinectMotor as dword
KinectCamera as dword
KinectSerial as string
KinectMotorPos as integer
KinectMotorPos = 0
KinectAccelerometer_x as dword
KinectAccelerometer_y as dword
KinectAccelerometer_z as dword
KinectAccelerometer_x = make memory(2)
KinectAccelerometer_y = make memory(2)
KinectAccelerometer_z = make memory(2)
KinectColorDepthDivisor as integer
KinectDepthRawDepthDivisor as integer
KinectDepthRGBDepthDivisor as integer
KinectColorDepthDivisor = (KINECT_COLOR_IMAGE_DEPTH / 8)
KinectDepthRawDepthDivisor = (KINECT_DEPTH_RAW_IMAGE_DEPTH / 8)
KinectDepthRGBDepthDivisor = (KINECT_DEPTH_RGB_IMAGE_DEPTH / 8)
KinectColorImageSize as integer
KinectDepthRawImageSize as integer
KinectDepthRGBImageSize as integer
KinectColorImageSize = (KINECT_IMAGE_WIDTH * KINECT_IMAGE_HEIGHT * KinectColorDepthDivisor) + KINECT_IMAGE_HEADER_OFFSET
KinectDepthRawImageSize = (KINECT_IMAGE_WIDTH * KINECT_IMAGE_HEIGHT * KinectDepthRawDepthDivisor) + KINECT_IMAGE_HEADER_OFFSET
KinectDepthRGBImageSize = (KINECT_IMAGE_WIDTH * KINECT_IMAGE_HEIGHT * KinectDepthRGBDepthDivisor) + KINECT_IMAGE_HEADER_OFFSET
// Make the memblocks for the images to go in
make memblock KINECT_COLOR_IMAGE_MEMBLOCK, KinectColorImageSize
write memblock dword KINECT_COLOR_IMAGE_MEMBLOCK, 0, KINECT_IMAGE_WIDTH
write memblock dword KINECT_COLOR_IMAGE_MEMBLOCK, 4, KINECT_IMAGE_HEIGHT
write memblock dword KINECT_COLOR_IMAGE_MEMBLOCK, 8, KINECT_COLOR_IMAGE_DEPTH
make memblock KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, KinectDepthRawImageSize
write memblock dword KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, 0, KINECT_IMAGE_WIDTH
write memblock dword KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, 4, KINECT_IMAGE_HEIGHT
write memblock dword KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, 8, KINECT_DEPTH_RAW_IMAGE_DEPTH
make memblock KINECT_DEPTH_RGB_IMAGE_MEMBLOCK, KinectDepthRGBImageSize
write memblock dword KINECT_DEPTH_RGB_IMAGE_MEMBLOCK, 0, KINECT_IMAGE_WIDTH
write memblock dword KINECT_DEPTH_RGB_IMAGE_MEMBLOCK, 4, KINECT_IMAGE_HEIGHT
write memblock dword KINECT_DEPTH_RGB_IMAGE_MEMBLOCK, 8, KINECT_DEPTH_RGB_IMAGE_DEPTH
// Draw some objects to put stuff on
make matrix 1, 64, 48, matResX - 1, matResY - 1
position matrix 1, 0,800,0
make object plane 1, 64, 48
position object 1, 64, -32, 64
rotate object 1, 45,0,0
set object ambient 1, 0
make object plane 2, 64, 48
position object 2, 0, -32, 64
rotate object 2, 45,0,0
set object ambient 2, 0
////////////////////////////////////////
// Load the DLL. Make sure its in your project directory!
KinectLoadDLL()
////////////////////////////////////////
// creates a handle for future motor and LED instructions
KinectMotor = CreateNUIMotor()
if KinectMotor = 0
print "Could not CreateNUIMotor...."
wait key
end
endif
////////////////////////////////////////
// Turn on the LED
KinectBool = SetNUIMotorLED(KinectMotor, KINECT_LED_SOLID_GREEN)
//////////////////////////////////////////
// creates a handle for the cameras
KinectCamera = CreateNUICamera()
if KinectCamera = 0
print "Could not CreateNUICamera...."
wait key
end
endif
//////////////////////////////////////////
// turn on the camera
KinectBool = StartNUICamera(KinectCamera)
//////////////////////////////////////////
// retrieve the Serial Number Should be
// the same number from the bottom of
// of the Kinect
KinectSerial = GetNUIMotorSerial(KinectMotor)
//////////////////////////////////////////
idx as integer
zpos as float
idxmax as integer
idxmax = (((KINECT_IMAGE_WIDTH) / MATRIX_RESOLUTION) * ((KINECT_IMAGE_HEIGHT) / MATRIX_RESOLUTION)) - 1
idxx as integer
hght as float
sync on
///////////////////////////////////////////
// Main loop
while escapekey() = 0
// Grab some frames
KinectBool = GetNUICameraColorFrameRGB32(KinectCamera,get memblock ptr(KINECT_COLOR_IMAGE_MEMBLOCK) + KINECT_IMAGE_HEADER_OFFSET,0)
KinectBool = GetNUICameraDepthFrameRAW(KinectCamera,get memblock ptr(KINECT_DEPTH_RAW_IMAGE_MEMBLOCK) + KINECT_IMAGE_HEADER_OFFSET,0)
KinectBool = GetNUICameraDepthFrameRGB32(KinectCamera,get memblock ptr(KINECT_DEPTH_RGB_IMAGE_MEMBLOCK) + KINECT_IMAGE_HEADER_OFFSET,0)
// Turn them into images
make image from memblock KINECT_COLOR_IMAGE_MEMBLOCK, KINECT_COLOR_IMAGE
make image from memblock KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, KINECT_DEPTH_RAW_IMAGE
make image from memblock KINECT_DEPTH_RGB_IMAGE_MEMBLOCK, KINECT_DEPTH_RGB_IMAGE
// put the images on the planes so you can see em
texture object 1, KINECT_COLOR_IMAGE
texture object 2, KINECT_DEPTH_RGB_IMAGE
// put the depth information on the matrix
for idx = 0 to idxmax
idxx = ((idx mod matResX) * (MATRIX_RESOLUTION * KinectDepthRawDepthDivisor)) + ((int(idx / matResX) * (MATRIX_RESOLUTION * KinectDepthRawDepthDivisor)) * KINECT_IMAGE_WIDTH)
zpos = memblock word(KINECT_DEPTH_RAW_IMAGE_MEMBLOCK, idxx + KINECT_IMAGE_HEADER_OFFSET)
hgth = 0 - zpos
set matrix height 1, (matResX - 1) - (idx mod matResX), (matResY - 1) - int(idx / matResX), hgth
next idx
update matrix 1
// read the accelerometers. Why would this thing have accelerometers if its stationary?
KinectBool = GetNUIMotorAccelerometer(KinectMotor, KinectAccelerometer_x,KinectAccelerometer_y,KinectAccelerometer_z)
// Send motor position command
KinectBool = SetNUIMotorPosition(KinectMotor, KinectMotorPos) /// -8000 to 8000
// Text BLah Blah Blah
set cursor 0,0
print "Use arrow up and down to tilt camera."
print "Press Escape to quit..."
print
print "Kinect Serial #:"; KinectSerial
print "Accelerometer X:"; 0xff && (*KinectAccelerometer_x)
print "Accelerometer Y:"; 0xff && (*KinectAccelerometer_y)
print "Accelerometer Z:"; 0xff && (*KinectAccelerometer_z)
print "Kinect Tilt:";KinectMotorPos
// move motor position
inc KinectMotorPos ,(upkey() and KinectMotorPos < 8000) * 100 - (downkey() and KinectMotorPos > -8000) * 100
sync
endwhile
//////////////////////////////////////////
// Shut every thing down
//////////////////////////////////////////
KinectBool = StopNUICamera(KinectCamera)
//////////////////////////////////////////
KinectBool = DestroyNUICamera(KinectCamera)
//////////////////////////////////////////
KinectBool = DestroyNUIMotor(KinectMotor)
///////////////////////////////////////////
DeleteAllMemblocks()
end
///////////////////////////////////////////////////////////////////////////////////////////
// Wrapper stuff
function KinectLoadDll()
load dll "CLNUIDevice.dll",KINECT_CLNUIDEVICE_DLL
if dll exist(KINECT_CLNUIDEVICE_DLL) = 0
print "Could not open CLNUIDevice.dll"
end
else
print "Loaded CLNUIDevice.dll"
endif
endfunction
function CheckCall( dll as integer, c$ as string)
ret as boolean
ret = DLL CALL EXIST(dll, c$)
if ret = 1
print c$;" call exists"
else
print c$;" does not exist"
endif
endfunction ret
function CreateNUIMotor()
ret as dword
ret = call dll (KINECT_CLNUIDEVICE_DLL, "CreateNUIMotor")
endfunction ret
function DestroyNUIMotor(CLNUIMotor as dword)
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "DestroyNUIMotor", CLNUIMotor)
endfunction ret
function GetNUIMotorSerial(CLNUIMotor as dword)
ret as string
ptr as dword
temp as integer
ptr = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUIMotorSerial", CLNUIMotor)
temp = *ptr
for i = 1 to 12
temp = *ptr
ret = ret + chr$(temp)
ptr = ptr + 1
next i
endfunction ret
function SetNUIMotorPosition(CLNUIMotor as dword, position as word )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "SetNUIMotorPosition", CLNUIMotor, position)
endfunction ret
function GetNUIMotorAccelerometer(CLNUIMotor as dword , x as dword , y as dword , z as dword )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUIMotorAccelerometer", CLNUIMotor, x, y, z)
endfunction ret
function SetNUIMotorLED(CLNUIMotor as dword , value as byte)
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "SetNUIMotorLED", CLNUIMotor, value)
endfunction ret
function CreateNUICamera()
ret as dword
ret = call dll (KINECT_CLNUIDEVICE_DLL, "CreateNUICamera")
endfunction ret
function DestroyNUICamera(CLNUICamera as dword)
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "DestroyNUICamera", CLNUICamera)
endfunction ret
function StartNUICamera(CLNUICamera as dword)
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "StartNUICamera", CLNUICamera)
endfunction ret
function StopNUICamera(CLNUICamera as dword)
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "StopNUICamera", CLNUICamera)
endfunction ret
function GetNUICameraColorFrameRAW(CLNUICamera as dword , pData as dword , waitTimeout as integer )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUICameraColorFrameRAW", CLNUICamera, pData, waitTimeout)
endfunction ret
function GetNUICameraColorFrameRGB24(CLNUICamera as dword , pData as dword , waitTimeout as integer )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUICameraColorFrameRGB24", CLNUICamera, pData, waitTimeout)
endfunction ret
function GetNUICameraColorFrameRGB32(CLNUICamera as dword , pData as dword , waitTimeout as integer )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUICameraColorFrameRGB32", CLNUICamera, pData, waitTimeout)
endfunction ret
function GetNUICameraDepthFrameRAW(CLNUICamera as dword , pData as dword , waitTimeout as integer )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUICameraDepthFrameRAW", CLNUICamera, pData, waitTimeout)
endfunction ret
function GetNUICameraDepthFrameRGB32(CLNUICamera as dword , pData as dword , waitTimeout as integer )
ret as boolean
ret = call dll (KINECT_CLNUIDEVICE_DLL, "GetNUICameraDepthFrameRGB32", CLNUICamera, pData, waitTimeout)
endfunction ret
function FindNextAvailableMemblock()
idx as integer
idx = 1
while (memblock exist(idx) and idx < 256)
inc idx
endwhile
if idx > 255
idx = -1
endif
endfunction idx
function DeleteAllMemblocks()
idx as integer
for idx = 1 to 255
if memblock exist(idx)
delete memblock idx
endif
next
endfunction
[b][/b][b]