Class: Rotor::Stepper

Inherits:
Object
  • Object
show all
Defined in:
lib/rotor/stepper.rb

Instance Method Summary collapse

Constructor Details

#initialize(coil_A_1_pin, coil_A_2_pin, coil_B_1_pin, coil_B_2_pin, enable_pin = nil, homing_switch, homing_normally, steps_per_mm) ⇒ Stepper

Returns a new instance of Stepper.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rotor/stepper.rb', line 5

def initialize(coil_A_1_pin, coil_A_2_pin, coil_B_1_pin, coil_B_2_pin, enable_pin=nil, homing_switch, homing_normally,steps_per_mm)
  @io = WiringPi::GPIO.new(WPI_MODE_GPIO)
  @coil_A_1_pin = coil_A_1_pin
  @coil_A_2_pin = coil_A_2_pin
  @coil_B_1_pin = coil_B_1_pin
  @coil_B_2_pin = coil_B_2_pin
  @enable_pin = enable_pin
  @steps_per_mm = steps_per_mm
  @homing_switch = homing_switch
  @homing_normally = homing_normally

  @step = 0
  @ps = [[1,0,1,0],[0,1,1,0],[0,1,0,1],[1,0,0,1]]

  [@coil_A_1_pin, @coil_A_2_pin, @coil_B_1_pin, @coil_B_2_pin].each do |pin|
    `echo #{pin} > /sys/class/gpio/unexport`
    @io.mode(pin,OUTPUT)
    @io.write(pin,LOW)
  end

  unless @enable_pin == nil
    `echo #{@enable_pin} > /sys/class/gpio/unexport`
    @io.mode(@enable_pin,OUTPUT)
    @io.write(@enable_pin,LOW)
    @io.write(@enable_pin,HIGH)
  end

  unless @homing_switch == nil
    `echo #{@homing_switch} > /sys/class/gpio/unexport`
    @io.mode(@homing_switch,INPUT)
  end
end

Instance Method Details

#at_home?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/rotor/stepper.rb', line 70

def at_home?
  if @io.read(@homing_switch) == @homing_normally
    return true
  else
    return false
  end
end

#at_safe_area?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/rotor/stepper.rb', line 78

def at_safe_area?
  if @io.read(@homing_switch) == @homing_normally
    return false
  else
    return true
  end
end

#backwards(delay = 5, steps = 100) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/rotor/stepper.rb', line 49

def backwards(delay=5,steps=100)
  delay_time = delay/1000.0
  (0..(steps * @steps_per_mm)).each do |i|
    set_step(@ps[@step][0],@ps[@step][1],@ps[@step][2],@ps[@step][3])
    @step -= 1
    @step = 3 if @step == -1
    sleep delay_time
  end
  
end

#forward(delay = 5, steps = 100) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rotor/stepper.rb', line 38

def forward(delay=5,steps=100)
  delay_time = delay/1000.0
  (0..(steps * @steps_per_mm)).each do |i|
    set_step(@ps[@step][0],@ps[@step][1],@ps[@step][2],@ps[@step][3])
    @step += 1
    @step = 0 if @step == 4
    sleep delay_time
  end
end

#power_downObject



86
87
88
# File 'lib/rotor/stepper.rb', line 86

def power_down
  set_step(0, 0, 0, 0)
end

#set_home(direction) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rotor/stepper.rb', line 60

def set_home(direction)
  puts "Setting #{direction} with Homing on GPIO #{@homing_switch}"
  @move = true
  while @move == true
    backwards(1,1) if direction == :backwards && @io.read(@homing_switch) == @homing_normally
    forward(1,1) if direction == :forward && @io.read(@homing_switch) == @homing_normally
    @move = false unless @io.read(@homing_switch) == @homing_normally
  end
end