Class: FB::OutgoingHandler

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

Overview

Writes GCode to the serial line. Creates messages the travel from the PI to the ARDUINO.

Defined Under Namespace

Classes: BadBooleanValue, BadNumericValue, InvalidAxisEntry

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
  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

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

#read_parameter(num) ⇒ Object

Parameters are settings, which is not to be confused with status.



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

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

#read_pin(pin, mode = :digital) ⇒ Object



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

def read_pin(pin, mode = :digital)
  unless [:analog, :digital].include?(mode)
    raise "Mode must be :analog or :digital"
  end
  write { "F42 P#{pin} M#{(mode == :digital) ? 0 : 1}" }
end

#read_status(num) ⇒ Object



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

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

#set_acceleration(axis, value) ⇒ Object



85
86
87
# File 'lib/arduino/outgoing_handler.rb', line 85

def set_acceleration(axis, value)
  set_paramater_value(axis, value, 41, 42, 43)
end

#set_end_inversion(axis, value) ⇒ Object



98
99
100
# File 'lib/arduino/outgoing_handler.rb', line 98

def set_end_inversion(axis, value)
  set_paramater_value(axis, bool_to_int(value), 21, 22, 23)
end

#set_max_speed(axis, value) ⇒ Object



81
82
83
# File 'lib/arduino/outgoing_handler.rb', line 81

def set_max_speed(axis, value)
  set_paramater_value(axis, value, 71, 72, 73)
end

#set_motor_inversion(axis, value) ⇒ Object



102
103
104
# File 'lib/arduino/outgoing_handler.rb', line 102

def set_motor_inversion(axis, value)
  set_paramater_value(axis, bool_to_int(value), 31, 32, 33)
end

#set_negative_coordinates(axis, value) ⇒ Object



106
107
108
# File 'lib/arduino/outgoing_handler.rb', line 106

def set_negative_coordinates(axis, value)
  raise "Not yet implemented. TODO: This method."
end

#set_steps_per_mm(axis, value) ⇒ Object



93
94
95
96
# File 'lib/arduino/outgoing_handler.rb', line 93

def set_steps_per_mm(axis, value)
  raise "The Farmbot Arduino does not currently store a value for steps "\
        "per mm. Keep track of this information at the application level"
end

#set_timeout(axis, value) ⇒ Object



89
90
91
# File 'lib/arduino/outgoing_handler.rb', line 89

def set_timeout(axis, value)
  set_paramater_value(axis, value, 11, 12, 13)
end

#wait(ms) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/arduino/outgoing_handler.rb', line 110

def wait(ms)
  if (ms.is_a?(Integer))
    write { "F44 P13 T#{ ms } M0 v1" }
  else
    raise BadNumericValue, "Expected sleep command to receive an integer."
  end
end

#write_parameter(num, val) ⇒ Object



70
71
72
73
74
# File 'lib/arduino/outgoing_handler.rb', line 70

def write_parameter(num, val)
  write { "F22 P#{num} V#{val}" }
  key = Gcode::PARAMETER_DICTIONARY.fetch(num, "UNKNOWN_PARAMETER_#{num}")
  bot.status.transaction { |i| i[key] = val }
end

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



76
77
78
79
# File 'lib/arduino/outgoing_handler.rb', line 76

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