Skip to content

Commit c2bfcae

Browse files
author
Josh Grunzweig
committed
Signed-off-by: Josh Grunzweig <[email protected]>
1 parent bdd9219 commit c2bfcae

20 files changed

+1270
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
== 0.1.1 / 2010-12-22
2+
* Initial release version
3+
4+
== 0.1.0 / 2010-12-13
5+
* Initial packaged version
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The MIT License
2+
3+
Copyright (c) 2010 Josh Grunzweig & Eric Monti
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
= ffi-libfreenect
2+
3+
FFI-based Ruby wrapper for the OpenKinect libfreenect library.
4+
5+
== Requirements
6+
* ffi >= 0.5.0
7+
8+
== Installation
9+
10+
=== via Gem
11+
12+
(sudo)? gem install ffi-libfreenect
13+
14+
=== via Rake
15+
16+
git clone http://github.com/jgrunzweig/ffi-libfreenect.git
17+
cd ffi-libfreenect
18+
(sudo)? gem install jeweler
19+
rake install
20+
21+
=== Synopsis
22+
23+
require 'freenect'
24+
25+
ctx = Freenect.init()
26+
devs = ctx.num_devices
27+
28+
STDERR.puts "Number of Kinects detected: #{devs}"
29+
unless devs > 0
30+
STDERR.puts "Error: no kinect detected"
31+
exit 1
32+
end
33+
34+
STDERR.puts "Selecting device 0"
35+
dev = ctx.open_device(0)
36+
37+
dev.set_led(:red) # play with the led
38+
dev.set_tilt_degrees(30) # tilt up to max
39+
sleep 1
40+
dev.set_tilt_degrees(-30) # tilt down to max
41+
sleep 1
42+
dev.set_tilt_degrees(0.0) # tilt back to center
43+
sleep 1
44+
dev.set_led(:green) # play with the led
45+
46+
# Actual video and depth capture work similarly to eachother.
47+
# But they're still both pretty un-sugary.
48+
#
49+
# Future versions will probably abstract this and try to make it more
50+
# ruby-ish.
51+
#
52+
# The example below shows how to write a single video frame to a PPM file.
53+
54+
dev.set_depth_format(Freenect::DEPTH_11BIT)
55+
dev.set_video_format(Freenect::VIDEO_RGB)
56+
dev.start_depth()
57+
dev.start_video()
58+
59+
$snapshot_finished = nil
60+
61+
STDERR.puts "Taking snapshot"
62+
dev.set_video_callback do |device, video, timestamp|
63+
if not $snapshot_finished
64+
fname = "%u.ppm" % timestamp
65+
STDERR.puts "Writing #{fname}"
66+
File.open(fname, "w") do |f|
67+
f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
68+
f.write(video.read_string_length(Freenect::RGB_SIZE))
69+
end
70+
$snapshot_finished = true
71+
end
72+
end
73+
74+
until $snapshot_finished
75+
break if (ctx.process_events < 0)
76+
end
77+
78+
dev.set_led(:off)
79+
dev.stop_depth
80+
dev.stop_video
81+
dev.close
82+
ctx.close
83+
84+
== Copyright
85+
86+
Copyright (c) 2010 Josh Grunzweig & Eric Monti. See LICENSE.txt for details.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'rubygems'
2+
require 'rake'
3+
4+
begin
5+
require 'jeweler'
6+
Jeweler::Tasks.new do |gem|
7+
gem.name = "ffi-libfreenect"
8+
gem.summary = gem.description = %Q{FFI bindings for the libfreenect OpenKinect library}
9+
gem.homepage = "http://github.com/jgrunzweig/ffi-libfreenect"
10+
gem.authors = ["Josh Grunzweig", "Eric Monti"]
11+
12+
gem.rdoc_options += ["--title", "FFI Freenect", "--main", "README.rdoc", "--line-numbers"]
13+
gem.add_dependency("ffi", ">= 0.5.0")
14+
15+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16+
end
17+
Jeweler::GemcutterTasks.new
18+
rescue LoadError
19+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20+
end
21+
22+
require 'spec/rake/spectask'
23+
Spec::Rake::SpecTask.new(:spec) do |spec|
24+
spec.libs << 'lib' << 'spec'
25+
spec.spec_files = FileList['spec/**/*_spec.rb']
26+
end
27+
28+
Spec::Rake::SpecTask.new(:rcov) do |spec|
29+
spec.libs << 'lib' << 'spec'
30+
spec.pattern = 'spec/**/*_spec.rb'
31+
spec.rcov = true
32+
end
33+
34+
task :spec => :check_dependencies
35+
36+
task :default => :spec
37+
38+
begin
39+
require 'yard'
40+
YARD::Rake::YardocTask.new
41+
rescue LoadError
42+
task :yardoc do
43+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
44+
end
45+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.1
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env ruby
2+
# This is a more or less a straight ruby port of the "record" utility
3+
# included in the libfreenect fakenect directory using the
4+
# ffi-libfreenect ruby class wrappers.
5+
#
6+
# This was really implemented just to see if ffi-libfreenect was working.
7+
# However, the output should be completely compatible the C version fakenect.
8+
#
9+
# usage: record.rb output_dir
10+
#
11+
12+
begin
13+
require 'rubygems'
14+
rescue LoadError
15+
end
16+
17+
$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
18+
require 'freenect'
19+
20+
$last_timestamp = 0
21+
$record_running = true
22+
23+
def open_dump(type, timestamp, extension)
24+
$last_timestamp = timestamp
25+
filename = "%s-%f-%u.%s" % [ type, Time.now.to_f, timestamp, extension]
26+
STDERR.puts "Writing: #{File.join($out_dir, filename)}"
27+
File.open(File.join($out_dir,"INDEX.txt"), "a"){|f| f.puts(filename) }
28+
File.open(File.join($out_dir, filename), "wb") {|f| yield f}
29+
end
30+
31+
orig_dir = Dir.pwd
32+
unless $out_dir = ARGV.shift
33+
STDERR.puts "usage: #{File.basename $0} output_dir"
34+
exit 1
35+
end
36+
Dir.mkdir($out_dir) unless File.directory?($out_dir)
37+
38+
trap('INT') do
39+
STDERR.puts "Caught INT signal cleaning up"
40+
$record_running = false
41+
end
42+
43+
ctx = Freenect.init()
44+
dev = ctx.open_device(0)
45+
46+
dev.set_depth_format(Freenect::DEPTH_11BIT)
47+
dev.set_video_format(Freenect::VIDEO_RGB)
48+
dev.start_depth()
49+
dev.start_video()
50+
51+
dev.set_depth_callback do |device, depth, timestamp|
52+
open_dump('d', timestamp, "pgm") do |f|
53+
f.puts("P5 %d %d 65535\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
54+
f.write(depth.read_string_length(Freenect::DEPTH_11BIT_SIZE))
55+
end
56+
end
57+
58+
dev.set_video_callback do |device, video, timestamp|
59+
open_dump('r', timestamp, 'ppm') do |f|
60+
f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
61+
f.write(video.read_string_length(Freenect::RGB_SIZE))
62+
end
63+
end
64+
65+
while $record_running and (ctx.process_events >= 0)
66+
open_dump('a', $last_timestamp, "dump") do |f|
67+
state = dev.get_tilt_state
68+
f.write(state.to_ptr.read_string_length(state.size))
69+
end
70+
end
71+
72+
Dir.chdir(orig_dir)
73+
dev.stop_depth
74+
dev.stop_video
75+
dev.close
76+
ctx.close
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class TILT_LED
2+
3+
def initialize
4+
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
5+
require 'freenect'
6+
check_args
7+
initialize_device
8+
tilt_led_set
9+
cleanup
10+
end
11+
12+
#
13+
# Define usage
14+
#
15+
def script_usage
16+
puts "Usage: ruby tilt_led.rb -l (0-6) -t (-30-30)"
17+
puts "LED Options:"
18+
puts " 0 - Off"
19+
puts " 1 - Green"
20+
puts " 2 - Red"
21+
puts " 3 - Yellow"
22+
puts " 4 - Blink Yellow"
23+
puts " 5 - Blink Green"
24+
puts " 6 - Blink Red/Yellow"
25+
puts "Tilt Options:"
26+
puts " Value must be between -30 and 30 degrees."
27+
exit 1
28+
end
29+
30+
#
31+
# Check arguments. I'm sure there's a better way to do this. What can I saw, I'm a noob.
32+
#
33+
def check_args
34+
if( ARGV.include?("-l") || ARGV.include?("-t") )
35+
if (ARGV.include?("-l"))
36+
@led_option = ARGV[(ARGV.index("-l") + 1 )]
37+
ARGV.delete("-l")
38+
ARGV.delete(@led_option)
39+
if @led_option.to_i < 0 || @led_option.to_i > 6
40+
script_usage
41+
end
42+
end
43+
if (ARGV.include?("-t"))
44+
@tilt_option = ARGV[(ARGV.index("-t") + 1 )]
45+
ARGV.delete("-t")
46+
ARGV.delete(@tilt_option)
47+
if @tilt_option.to_i < -30 || @tilt_option.to_i > 30
48+
script_usage
49+
end
50+
end
51+
else
52+
script_usage
53+
end
54+
end
55+
56+
def initialize_device
57+
@ctx = FFI::MemoryPointer.new(:pointer)
58+
@dev = FFI::MemoryPointer.new(:pointer)
59+
#
60+
# initialize context / @device
61+
#
62+
if (FFI::Freenect.freenect_init(@ctx,nil) != 0)
63+
puts "Error: can't initialize context"
64+
exit 1
65+
end
66+
@ctx = @ctx.read_pointer
67+
68+
if (FFI::Freenect.freenect_open_device(@ctx, @dev, 0) != 0)
69+
puts "Error: can't initialize device"
70+
exit 1
71+
end
72+
@dev = @dev.read_pointer
73+
end
74+
75+
def tilt_led_set
76+
#
77+
# Set tilt and/or led options on the @device
78+
#
79+
puts "Number of devices: #{FFI::Freenect.freenect_num_devices(@ctx)}"
80+
if (not @tilt_option.nil?)
81+
puts "Tilt set to: #{@tilt_option}"
82+
FFI::Freenect.freenect_set_tilt_degs(@dev, @tilt_option.to_f)
83+
end
84+
if (not @led_option.nil?)
85+
puts "LED set to: #{@led_option}"
86+
FFI::Freenect.freenect_set_led(@dev, @led_option.to_i)
87+
end
88+
end
89+
90+
def cleanup
91+
#
92+
# Close the context and @device (for cleanup purposes)
93+
#
94+
FFI::Freenect.freenect_close_device(@dev)
95+
if FFI::Freenect.freenect_shutdown(@ctx) != 0
96+
puts "Error shutting down context"
97+
exit 1
98+
else
99+
puts "Successfully shut down context"
100+
exit 0
101+
end
102+
end
103+
end
104+
105+
run = TILT_LED.new
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env ruby
2+
#
3+
4+
$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
5+
6+
require 'freenect'
7+
8+
ctx = Freenect.init
9+
unless ctx.num_devices() > 0
10+
STDERR.puts "No kinect device detected"
11+
exit 1
12+
end
13+
14+
dev = ctx[0]
15+
dev.set_led(:blink_red_yellow)
16+
17+
3.times do
18+
dev.set_tilt_degrees(15)
19+
sleep 2
20+
dev.set_tilt_degrees(-15)
21+
sleep 2
22+
end
23+
dev.set_tilt_degrees(0.0)
24+
25+
dev.set_led(:off)
26+
dev.close
27+
ctx.shutdown
28+

0 commit comments

Comments
 (0)