Class: FB::OutgoingHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/arduino/outgoing_handler.rb

Overview

Responsible for writing to the serial line. Sends Gcode from the pi to the arduino. (Pi -> Arduino)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ OutgoingHandler

Returns a new instance of OutgoingHandler.



7
8
9
# File 'lib/arduino/outgoing_handler.rb', line 7

def initialize(bot)
  @bot = bot
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



5
6
7
# File 'lib/arduino/outgoing_handler.rb', line 5

def bot
  @bot
end

Instance Method Details

#emergency_stopObject



11
12
13
14
15
16
# File 'lib/arduino/outgoing_handler.rb', line 11

def emergency_stop(*)
  # This message is special- it is the only method that bypasses the queue.
  bot.outbound_queue = []  # Dump pending commands.
  bot.serial_port.puts "E" # Don't queue this one- write to serial line.
  bot.status[:last] = :emergency_stop
end

#home_allObject



50
51
52
# File 'lib/arduino/outgoing_handler.rb', line 50

def home_all
  write { "G28" }
end

#home_xObject



38
39
40
# File 'lib/arduino/outgoing_handler.rb', line 38

def home_x
  write { "F11" }
end

#home_yObject



42
43
44
# File 'lib/arduino/outgoing_handler.rb', line 42

def home_y
  write { "F12" }
end

#home_zObject



46
47
48
# File 'lib/arduino/outgoing_handler.rb', line 46

def home_z
  write { "F13" }
end

#move_absolute(x: 0, y: 0, z: 0, s: 100) ⇒ Object



31
32
33
34
35
36
# File 'lib/arduino/outgoing_handler.rb', line 31

def move_absolute(x: 0, y: 0, z: 0, s: 100)
  x = [x.to_i, 0].max
  y = [y.to_i, 0].max
  z = [z.to_i, 0].max
  write { "G00 X#{x} Y#{y} Z#{z}" }
end

#move_relative(x: 0, y: 0, z: 0, s: 100) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/arduino/outgoing_handler.rb', line 18

def move_relative(x: 0, y: 0, z: 0, s: 100)
  write do
    # REMEBER: YOU NEVER WANT TO MUTATE VARIABLES HERE. If you mutate vars
    # in a block, it will result in the return value getting incremented
    # every time the value is read. For this reason, we use ||= and not =.
    x1 ||= [(bot.current_position.x + (x || 0)), 0].max
    y1 ||= [(bot.current_position.y + (y || 0)), 0].max
    z1 ||= [(bot.current_position.z + (z || 0)), 0].max

    "G00 X#{x1} Y#{y1} Z#{z1}"
  end
end

#pin_write(pin:, value:, mode:) ⇒ Object



66
67
68
69
# File 'lib/arduino/outgoing_handler.rb', line 66

def pin_write(pin:, value:, mode:)
  write { "F41 P#{pin} V#{value} M#{mode}" }
  bot.status.set_pin(pin, value)
end

#read_parameter(num) ⇒ Object



54
55
56
# File 'lib/arduino/outgoing_handler.rb', line 54

def read_parameter(num)
  write { "F21 P#{num}" }
end

#read_status(pin) ⇒ Object



62
63
64
# File 'lib/arduino/outgoing_handler.rb', line 62

def read_status(pin)
  write { "F31 P#{pin}" }
end

#write_parameter(num, val) ⇒ Object



58
59
60
# File 'lib/arduino/outgoing_handler.rb', line 58

def write_parameter(num, val)
  write { "F22 P#{num} V#{val}" }
end