object_oriented_beaglebone_black 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/examples/arduino/arduino_sketches/DcDcConverterManagement/DcDcConverterManagement.ino +100 -0
  4. data/examples/arduino/arduino_sketches/VoltageMeasurement/VoltageMeasurement.ino +78 -0
  5. data/examples/object_oriented_beaglebone_black_examples/current_measurement/current_input.rb +30 -0
  6. data/examples/object_oriented_beaglebone_black_examples/current_measurement.rb +7 -0
  7. data/examples/object_oriented_beaglebone_black_examples/dc_dc_converter_control_example_1.rb +54 -0
  8. data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output.rb +30 -0
  9. data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input.rb +73 -0
  10. data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino.rb +8 -0
  11. data/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation.rb +49 -0
  12. data/examples/object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input.rb +26 -0
  13. data/examples/object_oriented_beaglebone_black_examples/voltage_measurement.rb +7 -0
  14. data/examples/object_oriented_beaglebone_black_examples.rb +10 -0
  15. data/lib/object_oriented_beaglebone_black/analog_input.rb +32 -4
  16. data/lib/object_oriented_beaglebone_black/pin_mappings.rb +3 -3
  17. data/lib/object_oriented_beaglebone_black/pwm.rb +68 -10
  18. data/lib/object_oriented_beaglebone_black/uart_connection.rb +95 -0
  19. data/lib/object_oriented_beaglebone_black/uart_mappings.rb +29 -0
  20. data/lib/object_oriented_beaglebone_black/version.rb +1 -1
  21. data/lib/object_oriented_beaglebone_black.rb +2 -0
  22. data/object_orieted_beaglebone_black.gemspec +3 -0
  23. data/spec/examples/{current_measurement → object_oriented_beaglebone_black_examples/current_measurement}/current_input_spec.rb +3 -2
  24. data/spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output_spec.rb +41 -0
  25. data/spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input_spec.rb +55 -0
  26. data/spec/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation_spec.rb +126 -0
  27. data/spec/examples/{voltage_measurement → object_oriented_beaglebone_black_examples/voltage_measurement}/voltage_input_spec.rb +4 -2
  28. data/spec/object_oriented_beaglebone_black/uart_connection_spec.rb +47 -0
  29. data/spec/object_oriented_beaglebone_black/uart_mappings_spec.rb +22 -0
  30. data/spec/spec_helper.rb +1 -0
  31. metadata +44 -12
  32. data/examples/current_measurement/current_input.rb +0 -16
  33. data/examples/current_measurement/current_input_with_point04_shunt.rb +0 -12
  34. data/examples/current_measurement.rb +0 -8
  35. data/examples/voltage_measurement/voltage_input.rb +0 -16
  36. data/examples/voltage_measurement/voltage_input_with_100k_and_20k_voltage_divider.rb +0 -12
  37. data/examples/voltage_measurement.rb +0 -8
@@ -0,0 +1,26 @@
1
+ require 'bigdecimal'
2
+
3
+ module ObjectOrientedBeagleboneBlackExamples
4
+ module VoltageMeasurement
5
+ class VoltageInput
6
+
7
+ def initialize(pin_key, higher_side_resistor_resistance: nil, lower_side_resistor_resistance: nil)
8
+ @analog_input = ObjectOrientedBeagleboneBlack::AnalogInput.new(pin_key)
9
+
10
+ @voltage_current_calculation = VoltageCurrentCalculation.new
11
+ @higher_side_resistor_resistance = higher_side_resistor_resistance
12
+ @lower_side_resistor_resistance = lower_side_resistor_resistance
13
+ end
14
+
15
+ def value
16
+ raw_analog_reading = BigDecimal(@analog_input.raw_value.to_s)
17
+ voltage_in_bigdecimal = @voltage_current_calculation.voltage(raw_analog_reading,
18
+ higher_side_resistor_resistance: @higher_side_resistor_resistance,
19
+ lower_side_resistor_resistance: @lower_side_resistor_resistance)
20
+
21
+ voltage_in_bigdecimal.to_f
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ require 'object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input'
2
+
3
+ module ObjectOrientedBeagleboneBlackExamples
4
+ module VoltageMeasurement
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'object_oriented_beaglebone_black'
2
+
3
+ require 'object_oriented_beaglebone_black_examples/voltage_current_calculation'
4
+ require 'object_oriented_beaglebone_black_examples/current_measurement'
5
+ require 'object_oriented_beaglebone_black_examples/voltage_measurement'
6
+ require 'object_oriented_beaglebone_black_examples/uart_connection_to_arduino'
7
+
8
+ module ObjectOrientedBeagleboneBlackExamples
9
+ # Your code goes here...
10
+ end
@@ -4,6 +4,8 @@ module ObjectOrientedBeagleboneBlack
4
4
  class AnalogInput
5
5
  include ObjectOrientedBeagleboneBlack::PinMappings
6
6
 
7
+ ANALOG_INPUT_DEVICE_TREE_OVERLAY_PARAMETER = "cape-bone-iio"
8
+
7
9
  def initialize(pin_key)
8
10
  @pin_key = pin_key
9
11
  @slots_file_path = File.join(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG["slots_directory"], "slots"))
@@ -14,11 +16,37 @@ module ObjectOrientedBeagleboneBlack
14
16
  def activate_device_tree_overlays
15
17
  # Note: Since slots file acts as an interface to activate Device Tree Overlay, simply writing to it does what needs to be done.
16
18
  # I'm using appending here so that testing in a local environment becomes straightfoward.
17
- # Note: Closing this file caused an error in BeagleBone Black:
19
+
20
+ puts "This can take a few seconds for necessary setups..."
21
+
22
+ slots_file = File.open(@slots_file_path, "a+")
23
+ slots_file_content = slots_file.read
24
+
25
+ unless slots_file_content.include? ANALOG_INPUT_DEVICE_TREE_OVERLAY_PARAMETER
26
+ # until slots_file_content.include? ANALOG_INPUT_DEVICE_TREE_OVERLAY_PARAMETER
27
+
28
+ # Just in case where the previous read operation is not fully terminated.
29
+ sleep 1
30
+
31
+ slots_file.write(ANALOG_INPUT_DEVICE_TREE_OVERLAY_PARAMETER)
32
+
33
+ # Just in case where it takes time to load device tree overlay.
34
+ sleep 1
35
+
36
+ # Note: Sometime, the file pointer seems to be at the end of the file and doesn't read the file content before that.
37
+ # slots_file_content = slots_file.read
38
+ end
39
+
40
+ # Note: Closing this file sometimes causes an error in BeagleBone Black:
18
41
  # Errno::EEXIST: File exists @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
19
- # So modified the code not to close the file.
20
- # File.open(@slots_file_path, "a") { |file| file.write("cape-bone-iio") }
21
- File.open(@slots_file_path, "a").write("cape-bone-iio")
42
+ begin
43
+ slots_file.close if File.exist?(@slots_file_path) && !slots_file.closed?
44
+ rescue SystemCallError => system_call_error_message
45
+ puts "Error happened while closing #{@slots_file_path} with the message: #{system_call_error_message}"
46
+ end
47
+
48
+ puts "Setups done."
49
+
22
50
  end
23
51
 
24
52
  def raw_value
@@ -5,7 +5,7 @@ module ObjectOrientedBeagleboneBlack
5
5
  module PinMappings
6
6
 
7
7
  def load_pin_index_array
8
- # In order not to use activesupport (since Beaglebone Black that is used currently doesn't have Internet access to download it), Hash#deep_symbolize_keys! is not used.
8
+ # In order not to use activesupport (since Beaglebone Black didn't have Internet access to download it previously), Hash#deep_symbolize_keys! is not used.
9
9
  # ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY ||= JSON.load(File.read(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_ROOT, "config", "pin_index.json")))["pinIndex"].each { |property_hash| property_hash.deep_symbolize_keys! }
10
10
  ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY ||= JSON.load(File.read(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_ROOT, "config", "pin_index.json")))["pinIndex"]
11
11
  end
@@ -14,9 +14,9 @@ module ObjectOrientedBeagleboneBlack
14
14
  load_pin_index_array
15
15
 
16
16
  if !name.nil?
17
- ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY.select { |property_hash| property_hash["name"] == name }.first
17
+ ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY.find { |property_hash| property_hash["name"] == name }
18
18
  elsif !key.nil?
19
- ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY.select { |property_hash| property_hash["key"] == key }.first
19
+ ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_PIN_INDEX_ARRAY.find { |property_hash| property_hash["key"] == key }
20
20
  end
21
21
 
22
22
  end
@@ -3,6 +3,8 @@ require 'bigdecimal'
3
3
  module ObjectOrientedBeagleboneBlack
4
4
  class Pwm
5
5
 
6
+ PWM_DEVICE_TREE_OVERLAY_PARAMETER = "am33xx_pwm"
7
+
6
8
  module Polarity
7
9
  DIRECT = 0
8
10
  INVERSE = 1
@@ -18,16 +20,72 @@ module ObjectOrientedBeagleboneBlack
18
20
  def activate_device_tree_overlays
19
21
  # Note: Since slots file acts as an interface to activate Device Tree Overlay, simply writing to it does what needs to be done.
20
22
  # I'm using appending here so that testing in a local environment becomes straightfoward.
21
- # Note: Closing this file caused an error in BeagleBone Black:
22
- # Errno::ENOENT:
23
- # No such file or directory @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
24
- # So modified the code not to close the file.
25
- # File.open(@slots_file_path, "a") do |file|
26
- # file.write("am33xx_pwm")
27
- # file.write("bone_pwm_#{@pin_key}")
28
- # end
29
- File.open(@slots_file_path, "a").write("am33xx_pwm")
30
- File.open(@slots_file_path, "a").write("bone_pwm_#{@pin_key}")
23
+
24
+ puts "This can take several seconds for necessary setups..."
25
+
26
+ slots_file = File.open(@slots_file_path, "a+")
27
+ slots_file_content = slots_file.read
28
+
29
+ unless slots_file_content.include? PWM_DEVICE_TREE_OVERLAY_PARAMETER
30
+ # until slots_file_content.include? PWM_DEVICE_TREE_OVERLAY_PARAMETER
31
+
32
+ # Just in case where the previous read session is not fully completed.
33
+ sleep 1
34
+
35
+ slots_file.write(PWM_DEVICE_TREE_OVERLAY_PARAMETER)
36
+
37
+ # It seems to take a time to load the device tree overlay. Wait for two seconds.
38
+ sleep 2
39
+
40
+ # Note: Sometime, the file pointer seems to be at the end of the file and doesn't read the file content before that.
41
+ # slots_file_content = slots_file.read
42
+
43
+ end
44
+
45
+ # Note: Closing this file sometimes causes an error in BeagleBone Black:
46
+ # Errno::ENOENT: No such file or directory @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
47
+ begin
48
+ slots_file.close if File.exist?(@slots_file_path) && !slots_file.closed?
49
+ rescue SystemCallError => system_call_error_message
50
+ puts "Error happened while closing #{@slots_file_path} with the message: #{system_call_error_message}"
51
+ end
52
+
53
+ # Just in case
54
+ sleep 1
55
+
56
+ puts "First step of setups done."
57
+
58
+ pwm_pin_device_tree_overlay_parameter = "bone_pwm_#{@pin_key}"
59
+
60
+ slots_file = File.open(@slots_file_path, "a+")
61
+ slots_file_content = slots_file.read
62
+
63
+ unless slots_file_content.include? pwm_pin_device_tree_overlay_parameter
64
+ # until slots_file_content.include? pwm_pin_device_tree_overlay_parameter
65
+
66
+ # Just in case where the previous read session is not fully completed.
67
+ sleep 1
68
+
69
+ slots_file.write(pwm_pin_device_tree_overlay_parameter)
70
+
71
+ # It seems to take a time to load the device tree overlay. Wait for one second.
72
+ sleep 2
73
+
74
+ # Note: Sometime, the file pointer seems to be at the end of the file and doesn't read the file content before that.
75
+ # slots_file_content = slots_file.read
76
+
77
+ end
78
+
79
+ # Note: Closing this file sometimes causes an error in BeagleBone Black:
80
+ # Errno::ENOENT: No such file or directory @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
81
+ begin
82
+ slots_file.close if File.exist?(@slots_file_path) && !slots_file.closed?
83
+ rescue SystemCallError => system_call_error_message
84
+ puts "Error happened while closing #{@slots_file_path} with the message: #{system_call_error_message}"
85
+ end
86
+
87
+ puts "Setups done."
88
+
31
89
  end
32
90
 
33
91
  # duty_cycle (value between 0 and 1)
@@ -0,0 +1,95 @@
1
+ require 'serialport'
2
+
3
+ module ObjectOrientedBeagleboneBlack
4
+ class UartConnection
5
+ include ObjectOrientedBeagleboneBlack::UartMappings
6
+
7
+ PARITY = SerialPort::NONE
8
+
9
+ def initialize(uart_id)
10
+ @uart_id = uart_id
11
+ @slots_file_path = File.join(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG["slots_directory"], "slots"))
12
+
13
+ activate_device_tree_overlays
14
+ end
15
+
16
+ def activate_device_tree_overlays
17
+ # Note: Since slots file acts as an interface to activate Device Tree Overlay, simply writing to it does what needs to be done.
18
+ # I'm using appending here so that testing in a local environment becomes straightfoward.
19
+
20
+ puts "This can take a few seconds for necessary setups..."
21
+
22
+ slots_file = File.open(@slots_file_path, "a+")
23
+ slots_file_content = slots_file.read
24
+
25
+ unless slots_file_content.include? device_tree_overlay_parameter
26
+
27
+ # Just in case where the previous read operation is not fully terminated.
28
+ sleep 1
29
+
30
+ slots_file.write(device_tree_overlay_parameter)
31
+
32
+ # Just in case where it takes time to load device tree overlay.
33
+ sleep 1
34
+
35
+ end
36
+
37
+ # Note: Closing this file sometimes causes an error in BeagleBone Black:
38
+ # Errno::EEXIST: File exists @ fptr_finalize - /sys/devices/bone_capemgr.9/slots
39
+ begin
40
+ slots_file.close if File.exist?(@slots_file_path) && !slots_file.closed?
41
+ rescue SystemCallError => system_call_error_message
42
+ puts "Error happened while closing #{@slots_file_path} with the message: #{system_call_error_message}"
43
+ end
44
+
45
+ puts "Setups done."
46
+
47
+ end
48
+
49
+ def read(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's')
50
+
51
+ data = nil
52
+
53
+ SerialPort.open(teletype_device_path,
54
+ serial_baud_rate,
55
+ serial_data_bits,
56
+ serial_stop_bits,
57
+ PARITY) do |serial_port|
58
+ serial_port.sync = true
59
+
60
+ serial_port.putc(communication_command)
61
+
62
+ data = serial_port.gets
63
+ end
64
+
65
+ data.strip
66
+
67
+ end
68
+
69
+ def write(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's')
70
+
71
+ data = nil
72
+
73
+ SerialPort.open(teletype_device_path,
74
+ serial_baud_rate,
75
+ serial_data_bits,
76
+ serial_stop_bits,
77
+ PARITY) do |serial_port|
78
+ serial_port.sync = true
79
+
80
+ serial_port.puts(communication_command)
81
+ end
82
+
83
+ end
84
+
85
+ private
86
+ def device_tree_overlay_parameter
87
+ @device_tree_overlay_parameter ||= property_hash(id: @uart_id)["devicetree"]
88
+ end
89
+
90
+ def teletype_device_path
91
+ @teletype_device_path ||= property_hash(id: @uart_id)["teletype_device_path"]
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ # require 'active_support/core_ext/hash/keys'
3
+
4
+ module ObjectOrientedBeagleboneBlack
5
+ module UartMappings
6
+
7
+ ID_TELETYPE_DEVICE_PATH_MAPPING = { "UART0" => "/dev/ttyO0", "UART1" => "/dev/ttyO1", "UART2" => "/dev/ttyO2", "UART3" => "/dev/ttyO3", "UART4" => "/dev/ttyO4", "UART5" => "/dev/ttyO5" }
8
+
9
+ def load_uarts_array
10
+ ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_UARTS_HASH ||= JSON.load(File.read(File.join(OBJECT_ORIENTED_BEAGLEBONE_BLACK_ROOT, "config", "uarts.json")))["uarts"]
11
+ end
12
+
13
+ def property_hash(id: nil)
14
+ load_uarts_array
15
+
16
+ uart_hash = {}
17
+
18
+ if !id.nil?
19
+ teletype_device_path = ID_TELETYPE_DEVICE_PATH_MAPPING[id]
20
+ uart_hash = ::OBJECT_ORIENTED_BEAGLEBONE_BLACK_UARTS_HASH[teletype_device_path]
21
+ uart_hash["teletype_device_path"] = teletype_device_path
22
+ end
23
+
24
+ uart_hash
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module ObjectOrientedBeagleboneBlack
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -14,6 +14,7 @@ OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG = YAML.load(ERB.new(File.read(File.join(
14
14
  # OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG.deep_symbolize_keys!
15
15
 
16
16
  require 'object_oriented_beaglebone_black/pin_mappings'
17
+ require 'object_oriented_beaglebone_black/uart_mappings'
17
18
  require 'object_oriented_beaglebone_black/io'
18
19
  require 'object_oriented_beaglebone_black/io/direction'
19
20
  require 'object_oriented_beaglebone_black/io/value'
@@ -21,6 +22,7 @@ require 'object_oriented_beaglebone_black/gpio'
21
22
  require 'object_oriented_beaglebone_black/led'
22
23
  require 'object_oriented_beaglebone_black/pwm'
23
24
  require 'object_oriented_beaglebone_black/analog_input'
25
+ require 'object_oriented_beaglebone_black/uart_connection'
24
26
 
25
27
  module ObjectOrientedBeagleboneBlack
26
28
  # Your code goes here...
@@ -1,6 +1,8 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ examples = File.expand_path('../examples', __FILE__)
5
+ $LOAD_PATH.unshift(examples) unless $LOAD_PATH.include?(examples)
4
6
  require 'object_oriented_beaglebone_black/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
@@ -20,6 +22,7 @@ Gem::Specification.new do |spec|
20
22
  spec.require_paths = ["lib", "examples"]
21
23
 
22
24
  # spec.add_dependency "activesupport"
25
+ spec.add_dependency "serialport"
23
26
 
24
27
  spec.add_development_dependency "bundler", "~> 1.6"
25
28
  spec.add_development_dependency "rake"
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'current_measurement'
3
2
  require 'bigdecimal'
4
3
 
5
4
  describe "Current measurement by ACS712 Breakout", analog_input: true do
@@ -28,10 +27,12 @@ describe "Current measurement by ACS712 Breakout", analog_input: true do
28
27
  it "should calculated the measured current" do
29
28
 
30
29
  pin_key = "P9_40"
30
+ voltage_current_ratio_for_current_measurement = BigDecimal("0.185") # 185 [mV/A] for ACS712
31
+
31
32
  expected_current = BigDecimal("10").to_f
32
33
  expected_raw_value = (BigDecimal(expected_current.to_s) * BigDecimal("0.185")).to_f
33
34
 
34
- current_input = CurrentMeasurement::CurrentInput.new(pin_key)
35
+ current_input = ObjectOrientedBeagleboneBlackExamples::CurrentMeasurement::CurrentInput.new(pin_key, voltage_current_ratio_for_current_measurement: voltage_current_ratio_for_current_measurement)
35
36
 
36
37
  # Since the real "slots" file creates a directory structure when a device tree overlay is written to it,
37
38
  # in the "test" environment with a regular file, it is mimiced here.
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'bigdecimal'
4
+
5
+ describe "Convert duty cycle and sent to Arduino Due through UART connection" do
6
+
7
+ before(:each) do
8
+
9
+ @slots_directory = OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG["slots_directory"]
10
+
11
+ # In order to run this spec example in the real environment, the directories and files that already exist there are not created in that case.
12
+ if ENV["OBJECT_ORIENTED_BEAGLEBONE_BLACK_ENV"] == 'test'
13
+ FileUtils.mkdir_p(@slots_directory, mode: 0700) unless Dir.exists?(@slots_directory)
14
+ FileUtils.touch(File.join(@slots_directory, "slots"))
15
+ File.chmod(0700, File.join(@slots_directory, "slots"))
16
+ end
17
+ end
18
+
19
+ after(:each) do
20
+ if ENV["OBJECT_ORIENTED_BEAGLEBONE_BLACK_ENV"] == 'test'
21
+ File.open(File.join(@slots_directory, "slots"), 'w') {|file| file.truncate(0) }
22
+ end
23
+ end
24
+
25
+ it "should sent the value through the specified UART connection" do
26
+
27
+ uart_id = "UART4"
28
+ # The parameters for Arduino Due:
29
+ pwm_steps = BigDecimal("255")
30
+
31
+ uart_connection_double = double(uart_id)
32
+
33
+ pwm_output = ObjectOrientedBeagleboneBlackExamples::UartConnectionToArduino::PwmOutput.new(uart_connection_double, pwm_steps)
34
+
35
+ expect(uart_connection_double).to receive(:write).with(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 'p127')
36
+
37
+ pwm_output.send(0.5)
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'bigdecimal'
4
+
5
+ describe "Extract voltage and current from the measured values by Arduino Due through UART connection" do
6
+
7
+ before(:each) do
8
+
9
+ @slots_directory = OBJECT_ORIENTED_BEAGLEBONE_BLACK_CONFIG["slots_directory"]
10
+
11
+ # In order to run this spec example in the real environment, the directories and files that already exist there are not created in that case.
12
+ if ENV["OBJECT_ORIENTED_BEAGLEBONE_BLACK_ENV"] == 'test'
13
+ FileUtils.mkdir_p(@slots_directory, mode: 0700) unless Dir.exists?(@slots_directory)
14
+ FileUtils.touch(File.join(@slots_directory, "slots"))
15
+ File.chmod(0700, File.join(@slots_directory, "slots"))
16
+ end
17
+ end
18
+
19
+ after(:each) do
20
+ if ENV["OBJECT_ORIENTED_BEAGLEBONE_BLACK_ENV"] == 'test'
21
+ File.open(File.join(@slots_directory, "slots"), 'w') {|file| file.truncate(0) }
22
+ end
23
+ end
24
+
25
+ it "should read the value through the specified UART connection" do
26
+
27
+ uart_id = "UART4"
28
+ # The parameters for Arduino Due:
29
+ analog_steps = BigDecimal("4095")
30
+ reference_voltage = BigDecimal("3.3")
31
+
32
+ uart_connection_double = double(uart_id)
33
+
34
+ voltage_current_input = ObjectOrientedBeagleboneBlackExamples::UartConnectionToArduino::VoltageCurrentInput.new(uart_connection_double, analog_steps, reference_voltage)
35
+
36
+ expect(uart_connection_double).to receive(:read).with(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 'm') do
37
+ "{\"raw_reading_for_input_voltage\": 534, \"raw_reading_for_input_current\": 5, \"raw_reading_for_output_voltage\": 281}"
38
+ end
39
+
40
+ voltage_current_input.measure
41
+
42
+ # higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
43
+ input_voltage = voltage_current_input.input_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
44
+ # shunt resistance: 0.04[Ω]
45
+ input_current = voltage_current_input.input_current(resistance_for_current_measurement: BigDecimal("0.04"))
46
+ # higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
47
+ output_voltage = voltage_current_input.output_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
48
+
49
+ expect(input_voltage).to be_within(0.1).of(BigDecimal("5.7"))
50
+ expect(input_current).to be_within(0.1).of(BigDecimal("0.1"))
51
+ expect(output_voltage).to be_within(0.1).of(BigDecimal("3.0"))
52
+
53
+ end
54
+
55
+ end