farmbot-serial 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07780e1523d04161c781211ba25173ffa6e51db0
4
- data.tar.gz: f2677ab821c15c5664f099d7afd7e51805b2f2b2
3
+ metadata.gz: 873981048eab4aa88fcf4cacae9f3c88de7a4073
4
+ data.tar.gz: c56dc431685db09f7238cf2ae7285acc1cc05f9c
5
5
  SHA512:
6
- metadata.gz: 34dffc021c50794f21635582f35c102f6199c9ee349bb53f496ec9b15655c702ba4f01725c713366b1cd4611b588fa47212bd7346418ad08aa85b25cdbd9cc09
7
- data.tar.gz: 6f5bc299b55e95d3083c255d0f30d13f7b7db18b3a36740a61cc4d923f6f4e14f4f73fb367d1b6198293ebcc1660b845ffd96225e0480099240fa83a6ded11c6
6
+ metadata.gz: 31f5d25c1b2fcd205b240b4d8f8bfcad3adde8be560f288db9f8d14c0a31cc800420c0fe9b5b92d2f7dd1f2a345082362ad83bb62f0a9e88077ec121eb33b772
7
+ data.tar.gz: 138d3fc9b44b824a3081656fbcfd994aa0cd3e4bcbd6d4400f5bb25bb3f67169bad4eade7f9d123c00438c1cb3d66e262a9beab7ead0db4d4a3fe54a6fbb4d6e
data/.gitignore CHANGED
@@ -1,4 +1,3 @@
1
- *.gem
2
1
  *.rbc
3
2
  *.swp
4
3
  .bundle/*
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "farmbot-serial"
7
- spec.version = '0.0.1' # TODO: Fb::Serial::VERSION
7
+ spec.version = '0.0.2' # TODO: Fb::VERSION
8
8
  spec.authors = ["Tim Evers", "Rick Carlino"]
9
9
  spec.email = ["rick.carlino@gmail.com"]
10
10
  spec.description = "Serial library for Farmbot"
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "pry"#, "~> 0.10"
24
24
  spec.add_development_dependency "simplecov"#, "~> 0.9"
25
25
 
26
- spec.add_runtime_dependency "serialport"#, "~> 1.3"
26
+ spec.add_runtime_dependency "serialport"#, "~> 1.3"
27
27
  end
@@ -7,22 +7,28 @@
7
7
  module Fb
8
8
  class HardwareInterfaceArduino
9
9
 
10
- attr_accessor :ramps_param, :ramps_main
11
- #attr_accessor :test_serial_read, :test_serial_write
12
10
  attr_accessor :serial_port
13
- attr_accessor :external_info
11
+
12
+ attr_reader :is_busy
13
+ attr_reader :is_error
14
+ attr_reader :is_done
15
+ attr_reader :return_values
14
16
 
15
17
  # initialize the interface
16
18
  #
17
19
  def initialize(test_mode)
18
- @status_debug_msg = $debug_msg
19
-
20
- @test_mode = test_mode
20
+ @status_debug_msg = $debug_msg
21
+ @test_mode = test_mode
21
22
 
22
23
  # connect to arduino
23
24
  connect_board()
24
25
 
25
- @external_info = ""
26
+ @external_info = ""
27
+ @return_values = Queue.new
28
+
29
+ @is_busy = false
30
+ @is_error = false
31
+ @is_done = false
26
32
 
27
33
  end
28
34
 
@@ -50,32 +56,37 @@ module Fb
50
56
 
51
57
  # write a command to the robot
52
58
  #
53
- def execute_command(text, log, onscreen)
59
+ def write_command(text, log, onscreen)
54
60
  begin
55
61
 
56
62
  write_status = create_write_status(text, log, onscreen)
63
+ write_to_serial(write_status)
64
+
65
+ @parameters = []
66
+ @is_busy = true
67
+ @is_error = false
57
68
 
58
- #puts @serial_port.test_serial_read
59
- prepare_serial_port(write_status)
60
- #puts @serial_port.test_serial_read
61
-
62
- # wait until the arduino responds
63
- while(write_status.is_busy())
69
+ rescue => e
70
+ handle_execution_exception(e)
71
+ end
72
+ end
64
73
 
74
+ # check the responses from the robot
75
+ #
76
+ def check_command_execution
77
+ begin
78
+ if Time.now - write_status.start < write_status.timeout and @is_done == 0
79
+ @is_error = true
80
+ else
65
81
  check_emergency_stop
66
82
  process_feedback(write_status)
67
-
68
83
  end
69
-
70
- log_result_of_execution(write_status)
71
-
72
84
  rescue => e
73
- #puts 'exception received'
74
- #puts e
75
85
  handle_execution_exception(e)
76
86
  end
77
87
  end
78
88
 
89
+
79
90
  def create_write_status(text, log, onscreen)
80
91
  write_status = Fb::HardwareInterfaceArduinoWriteStatus.new
81
92
  write_status.text = text
@@ -89,18 +100,16 @@ module Fb
89
100
  puts("ST: serial error\n#{e.message}\n#{e.backtrace.inspect}")
90
101
  @serial_port.rts = 1
91
102
  connect_board
92
- sleep 5 if @test_mode == false
103
+ @is_error = true
93
104
  end
94
105
 
95
- def log_result_of_execution(write_status)
96
-
97
- # log things if needed
98
- if write_status.done == 1
99
- puts 'ST: done' if write_status.onscreen
100
- else
101
- puts 'ST: timeout'
102
- sleep 5 if @test_mode == false
103
- end
106
+ # set the serial port ready to send and write the text
107
+ #
108
+ def write_to_serial(write_status)
109
+ puts "WR: #{write_status.text}" if write_status.onscreen
110
+ @serial_port.read_timeout = 2
111
+ clean_serial_buffer() if @test_mode == false
112
+ serial_port_write( "#{write_status.text}\n" )
104
113
  end
105
114
 
106
115
  # receive all characters coming from the serial port
@@ -108,31 +117,24 @@ module Fb
108
117
  def process_feedback(write_status)
109
118
  i = @serial_port.read(1)
110
119
 
111
- #print "-#{i}"
112
- #print "#{i}"
113
-
114
120
  if i != nil
115
121
  i.each_char do |c|
116
-
117
- add_and_process_characters(write_status, c)
118
-
122
+ process_characters(write_status, c)
119
123
  end
120
124
  else
121
125
  sleep 0.001 if @test_mode == false
122
- #write_status.done = 1 if @test_mode == true
123
126
  end
124
127
  end
125
128
 
126
129
  # keep incoming characters in a buffer until it is a complete string
127
130
  #
128
- def add_and_process_characters(write_status, c)
131
+ def process_characters(write_status, c)
129
132
 
130
133
  if c == "\r" or c == "\n"
131
134
  if write_status.received.length >= 3
132
135
  log_incoming_text(write_status)
133
136
  write_status.split_received
134
137
  process_code_and_params(write_status)
135
-
136
138
  end
137
139
  else
138
140
  write_status.received = write_status.received + c
@@ -153,11 +155,11 @@ module Fb
153
155
  # command is finished
154
156
  when 'R02'
155
157
  write_status.done = 1
156
-
158
+ @is_done = true
157
159
  # command is finished with errors
158
160
  when 'R03'
159
161
  write_status.done = 1
160
-
162
+ @is_done = true
161
163
  # command is still ongoing
162
164
  when 'R04'
163
165
  write_status.start = Time.now
@@ -172,15 +174,6 @@ module Fb
172
174
 
173
175
  end
174
176
 
175
- # set the serial port ready to send
176
- #
177
- def prepare_serial_port(write_status)
178
- puts "WR: #{write_status.text}" if write_status.onscreen
179
- @serial_port.read_timeout = 2
180
- clean_serial_buffer() if @test_mode == false
181
- serial_port_write( "#{write_status.text}\n" )
182
- end
183
-
184
177
  # empty the input buffer so no old data is processed
185
178
  #
186
179
  def clean_serial_buffer
@@ -195,10 +188,8 @@ module Fb
195
188
 
196
189
  # if there is an emergency stop, immediately write it to the arduino
197
190
  #
198
- def check_emergency_stop
199
- if (Fb::HardwareInterface.current.status.emergency_stop)
200
- serial_port_write( "E\n" )
201
- end
191
+ def emergency_stop
192
+ serial_port_write( "E\n" )
202
193
  end
203
194
 
204
195
  # write to log
@@ -211,136 +202,44 @@ module Fb
211
202
  #
212
203
  def process_value(code,text)
213
204
 
214
- params = Fb::HardwareInterfaceArduinoValuesReceived.new
215
-
216
- process_value_split(code, params, text)
205
+ # get all parameters in the current text
206
+ return_value = process_value_split(code, text)
217
207
 
218
- # depending on the report code, process the values
219
- # this is done by reading parameter names and their values
220
- # and respong on it as needed
221
-
222
- process_value_process_param_list(params,code)
223
- process_value_process_named_params(params,code)
224
- process_value_process_text(code,text)
208
+ # save the list for the client
209
+ @return_values << return_value
225
210
 
226
211
  end
227
212
 
228
- def process_value_split(code, params, text)
213
+ def process_value_split(code, text)
214
+
215
+ params = Fb::HardwareInterfaceArduinoValuesReceived.new
216
+ params.code = code
217
+ params.text = text
229
218
 
230
219
  # get all separate parameters from the text
231
220
  text.split(' ').each do |param|
232
221
 
233
- if code == "R81"
234
- # this is the only code that uses two letter parameters
235
- par_code = param[0..1].to_s
222
+ case code
223
+ when "R81"
224
+ # this is the only code that uses two letter parameters
225
+ par_name = param[0..1].to_s
236
226
  par_value = param[2..-1].to_i
237
227
  else
238
- par_code = param[0..0].to_s
228
+ par_name = param[0..0].to_s
239
229
  par_value = param[1..-1].to_i
240
230
  end
241
231
 
242
- params.load_parameter(par_code, par_value)
243
-
244
- end
245
-
246
- end
247
-
248
- def process_value_process_param_list(params,code)
249
- #if params.p != 0
250
- process_value_R21(params,code)
251
- process_value_R23(params,code)
252
- process_value_R41(params,code)
253
- #end
254
- end
255
-
256
-
257
- # Process report parameter value
258
- #
259
- def process_value_R21(params,code)
260
- if code == 'R21'
261
- param = @ramps_param.get_param_by_id(params.p)
262
- if param != nil
263
- param['value_ar'] = params.v
264
- end
265
- end
266
- end
267
-
268
- # Process report parameter value and save to database
269
- #
270
- def process_value_R23(params,code)
271
- if code == 'R23'
272
- param = @ramps_param.get_param_by_id(params.p)
273
- if param != nil
274
- @ramps_param.save_param_value(params.p, :by_id, :from_db, params.v)
275
- end
276
- end
277
- end
278
-
279
- # Process report pin values
280
- #
281
- def process_value_R41(params,code)
282
- raise 'Not implemented.'
283
- end
284
-
285
- def process_value_process_named_params(params,code)
286
- process_value_R81(params,code)
287
- process_value_R82(params,code)
288
- end
289
-
290
- # Process report end stops
291
- #
292
- def process_value_R81(params,code)
293
- if code == 'R81'
294
- Fb::HardwareInterface.current.status.info_end_stop_x_a = (params.xa == 1)
295
- Fb::HardwareInterface.current.status.info_end_stop_x_b = (params.xb == 1)
296
- Fb::HardwareInterface.current.status.info_end_stop_y_a = (params.ya == 1)
297
- Fb::HardwareInterface.current.status.info_end_stop_y_b = (params.yb == 1)
298
- Fb::HardwareInterface.current.status.info_end_stop_z_a = (params.za == 1)
299
- Fb::HardwareInterface.current.status.info_end_stop_z_b = (params.zb == 1)
300
- end
301
- end
302
-
303
- # Process report position
304
- def process_value_R82(params,code)
305
- if code == 'R82'
306
-
307
- Fb::HardwareInterface.current.status.info_current_x_steps = params.x
308
- Fb::HardwareInterface.current.status.info_current_x = params.x / @ramps_param.axis_x_steps_per_unit
309
-
310
- Fb::HardwareInterface.current.status.info_current_y_steps = params.y
311
- Fb::HardwareInterface.current.status.info_current_y = params.y / @ramps_param.axis_y_steps_per_unit
232
+ params.load_parameter(par_name, par_value)
312
233
 
313
- Fb::HardwareInterface.current.status.info_current_z_steps = params.z
314
- Fb::HardwareInterface.current.status.info_current_z = params.z / @ramps_param.axis_z_steps_per_unit
315
-
316
- end
317
- end
318
-
319
- def process_value_process_text(code,text)
320
- process_value_process_R83(code,text)
321
- process_value_process_R99(code,text)
322
- end
323
-
324
- # Process report software version
325
- #
326
- def process_value_process_R83(code,text)
327
- if code == 'R83'
328
- Fb::HardwareInterface.current.status.device_version = text
329
234
  end
330
- end
331
235
 
332
- # Process report of a debug comment
333
- #
334
- def process_value_process_R99(code,text)
335
- if code == 'R99'
336
- puts ">#{text}<"
337
- end
236
+ return params
338
237
  end
339
238
 
340
239
  def puts(*)
341
240
  # Too many messages in the test buffer right now. Will delete this later.
342
241
  # just disabling it for now.
343
- STDOUT.print('')
242
+ STDOUT.print('x')
344
243
  end
345
244
  end
346
245
  end
@@ -1,6 +1,12 @@
1
1
  module Fb
2
2
  class HardwareInterfaceArduinoValuesReceived
3
3
 
4
+ attr_accessor :code
5
+ attr_accessor :text
6
+ attr_accessor :external_info
7
+
8
+ # value holders with the name used in the serial
9
+ # communucation as they are received from arduino
4
10
  attr_accessor :p , :v
5
11
  attr_accessor :x , :y , :z
6
12
  attr_accessor :xa, :xb
@@ -21,7 +27,8 @@ module Fb
21
27
  @yb = 0
22
28
  @za = 0
23
29
  @zb = 0
24
-
30
+ @text = ''
31
+ @code = 0
25
32
  end
26
33
 
27
34
  def load_parameter(name, value)
@@ -0,0 +1,35 @@
1
+ class HardwareInterfaceArduinoWriteStatus
2
+
3
+ attr_accessor :done
4
+ attr_accessor :code
5
+ attr_accessor :received
6
+ attr_accessor :start
7
+ attr_accessor :log
8
+ attr_accessor :onscreen
9
+ attr_accessor :text
10
+ attr_accessor :params
11
+ attr_accessor :timeout
12
+
13
+ def initialize
14
+ @done = 0
15
+ @code = ''
16
+ @received = ''
17
+ @text = ''
18
+ @params = ''
19
+ @log = false
20
+ @onscreen = false
21
+ @start = Time.now
22
+ @timeout = 5
23
+ end
24
+
25
+ # def is_busy
26
+ # Time.now - @start < @timeout and @done == 0
27
+ # end
28
+
29
+ def split_received
30
+ # get the parameter and data part
31
+ @code = received[0..2].upcase
32
+ @params = received[3..-1].to_s.upcase.strip
33
+ end
34
+
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: farmbot-serial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Evers
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-20 00:00:00.000000000 Z
12
+ date: 2015-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -107,20 +107,13 @@ files:
107
107
  - README.md
108
108
  - Rakefile
109
109
  - farmbot-serial.gemspec
110
- - lib/arduino_default_params.rb
111
110
  - lib/farmbot-serial.rb
112
- - lib/hardware_interface.rb
113
111
  - lib/hardware_interface_arduino.rb
114
112
  - lib/hardware_interface_arduino_values_received.rb
115
- - lib/hardware_interface_arduino_write_status.rb
116
- - lib/hardware_interface_param.rb
113
+ - lib/hardware_interface_write_status.rb
117
114
  - lib/serial_port_sim.rb
118
- - lib/status.rb
119
115
  - spec/lib/ramps_arduino_spec.rb
120
116
  - spec/lib/ramps_arduino_values_received_spec.rb
121
- - spec/lib/ramps_arduino_write_status_spec.rb
122
- - spec/lib/ramps_param_spec.rb
123
- - spec/lib/ramps_spec.rb
124
117
  - spec/spec_helper.rb
125
118
  - testcommands.csv
126
119
  homepage: http://github.com/farmbot/farmbot-serial
@@ -150,7 +143,4 @@ summary: Serial library for Farmbot
150
143
  test_files:
151
144
  - spec/lib/ramps_arduino_spec.rb
152
145
  - spec/lib/ramps_arduino_values_received_spec.rb
153
- - spec/lib/ramps_arduino_write_status_spec.rb
154
- - spec/lib/ramps_param_spec.rb
155
- - spec/lib/ramps_spec.rb
156
146
  - spec/spec_helper.rb