Showing posts with label motion detecting. Show all posts
Showing posts with label motion detecting. Show all posts

Sunday, June 7, 2009

Motion Detecting Lego NXT mindstorm arm written in ruby




The building instructions for this arm can be found here:
http://www.nxtprograms.com/robot_arm/steps.html

I added the Ultra sensor to it, and you will notice some of the funtions in the code aren't used at all, because I plan on doing more with this.

If you liked this you might also like
My NXT tagged posts.
My Arduino tagged posts.
or
How to make a contact MIC.


require "rubygems"
require "serialport"
require "nxt_comm"

$DEBUG = false

@nxt = NXTComm.new('/dev/tty.NXT-DevB-1')
def sleeper
sleep(0.5)
end

@nxt = NXTComm.new("/dev/tty.NXT-DevB-1")


def do_motor_a
a = Commands::Motor.new(@nxt)
a.port = :a
a.duration = {:rotations => 0.25}
a.control_power = true
a.start
puts "Run State: #{a.run_state}"
end

def do_motor_b
b = Commands::Motor.new(@nxt)
b.port = :b
b.duration = {:rotations => 0.25}
b.control_power = true
b.start
end

def do_motor_c
c = Commands::Motor.new(@nxt)
c.port = :c
c.duration = {:rotations => 0.25}
c.control_power = true
c.start
puts "Run State: #{c.run_state}"
end

def do_motorb_a
a = Commands::Motor.new(@nxt)
a.port = :a
a.duration = {:rotations => 0.25}
a.control_power = true
a.direction = :backward
a.start
puts "Run State: #{a.run_state}"
end

def do_motorb_b
b = Commands::Motor.new(@nxt)
b.port = :b
b.direction = :backward
b.duration = {:rotations => 0.25}
b.control_power = true
b.start
end

def do_motorb_c
c = Commands::Motor.new(@nxt)
c.port = :c
c.direction = :backward
c.duration = {:rotations => 0.25}
c.control_power = true
c.start
puts "Run State: #{c.run_state}"
end

def inputs_2
s = @nxt.get_input_values(NXTComm::SENSOR_2)
puts s[:value_raw]
if s[:value_raw] > 1005
@nxt.play_tone(659,500)
do_motorb_a
do_motorb_b
sleep 1
do_motor_a
do_motor_b
sleep 1
end
end

def inputs_1
sm = @nxt.get_input_values(NXTComm::SENSOR_1)
puts sm.inspect
sleep 1
end

loop {
inputs_2
}