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,126 @@
1
+ require 'spec_helper'
2
+ require 'bigdecimal'
3
+
4
+ describe ObjectOrientedBeagleboneBlackExamples::VoltageCurrentCalculation do
5
+
6
+ context "External analog digital converter (Arduino)" do
7
+
8
+ before(:each) do
9
+ @analog_steps = BigDecimal("1023")
10
+ reference_voltage = BigDecimal("5")
11
+ @voltage_current_calculation = ObjectOrientedBeagleboneBlackExamples::VoltageCurrentCalculation.new(@analog_steps, reference_voltage)
12
+ end
13
+
14
+ context "voltage" do
15
+
16
+ it "should calculate voltage based on the raw analog data" do
17
+
18
+ raw_analog_reading = BigDecimal("1023")
19
+ expect(@voltage_current_calculation.voltage(raw_analog_reading)).to be_within(BigDecimal("0.1")).of(BigDecimal("5.0"))
20
+
21
+ raw_analog_reading = BigDecimal("511")
22
+ expect(@voltage_current_calculation.voltage(raw_analog_reading)).to be_within(BigDecimal("0.1")).of(BigDecimal("2.5"))
23
+
24
+ end
25
+
26
+ it "should calculate voltage based on the raw analog data with voltage divisor" do
27
+
28
+ raw_analog_reading = BigDecimal("1023")
29
+ expect(@voltage_current_calculation.voltage(raw_analog_reading,
30
+ higher_side_resistor_resistance: BigDecimal("1000"),
31
+ lower_side_resistor_resistance: BigDecimal("1000"))).to be_within(BigDecimal("0.1")).of(BigDecimal("10.0"))
32
+
33
+ raw_analog_reading = BigDecimal("511")
34
+ expect(@voltage_current_calculation.voltage(raw_analog_reading,
35
+ higher_side_resistor_resistance: BigDecimal("1000"),
36
+ lower_side_resistor_resistance: BigDecimal("1000"))).to be_within(BigDecimal("0.1")).of(BigDecimal("5.0"))
37
+
38
+ raw_analog_reading = BigDecimal("1023")
39
+ expect(@voltage_current_calculation.voltage(raw_analog_reading,
40
+ higher_side_resistor_resistance: BigDecimal("10"),
41
+ lower_side_resistor_resistance: BigDecimal("2.2"))).to be_within(BigDecimal("0.1")).of(BigDecimal("27.7"))
42
+
43
+ end
44
+
45
+ context "voltage divider" do
46
+
47
+ it "should calculate resistor factor" do
48
+
49
+ higher_side_resistor_resistance = BigDecimal("1000")
50
+ lower_side_resistor_resistance = BigDecimal("1000")
51
+
52
+ expect(@voltage_current_calculation.resistor_factor(higher_side_resistor_resistance,
53
+ lower_side_resistor_resistance))
54
+ .to eq(@analog_steps * BigDecimal("0.5"))
55
+
56
+ higher_side_resistor_resistance = BigDecimal("10")
57
+ lower_side_resistor_resistance = BigDecimal("2.2")
58
+
59
+ expect(@voltage_current_calculation.resistor_factor(higher_side_resistor_resistance,
60
+ lower_side_resistor_resistance))
61
+ .to be_within(BigDecimal("1"))
62
+ .of(@analog_steps * BigDecimal("0.18"))
63
+
64
+ end
65
+
66
+ it "should get analog steps as resistor factor if the resistances are nil" do
67
+
68
+ higher_side_resistor_resistance = nil
69
+ lower_side_resistor_resistance = nil
70
+
71
+ expect(@voltage_current_calculation.resistor_factor(higher_side_resistor_resistance,
72
+ lower_side_resistor_resistance))
73
+ .to eq(@analog_steps * BigDecimal("1"))
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ context "current" do
82
+
83
+ it "should calculate current based on the raw analog voltage accross shunt resistance" do
84
+
85
+ resistance_for_current_measurement = BigDecimal("0.005")
86
+ voltage_gain_for_current_measurement = BigDecimal("100")
87
+
88
+ raw_analog_reading = BigDecimal("1023")
89
+ expect(@voltage_current_calculation.current(raw_analog_reading,
90
+ resistance_for_current_measurement: resistance_for_current_measurement,
91
+ voltage_gain_for_current_measurement: voltage_gain_for_current_measurement)).to be_within(BigDecimal("0.1")).of(BigDecimal("10.0"))
92
+
93
+ raw_analog_reading = BigDecimal("511")
94
+ expect(@voltage_current_calculation.current(raw_analog_reading,
95
+ resistance_for_current_measurement: resistance_for_current_measurement,
96
+ voltage_gain_for_current_measurement: voltage_gain_for_current_measurement)).to be_within(BigDecimal("0.1")).of(BigDecimal("5.0"))
97
+
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+
104
+ context "Internal analog digital converter" do
105
+
106
+ before(:each) do
107
+ @voltage_current_calculation = ObjectOrientedBeagleboneBlackExamples::VoltageCurrentCalculation.new
108
+ end
109
+
110
+ context "current" do
111
+
112
+ it "should calculate current based on the voltage measured by current sensor" do
113
+
114
+ voltage_current_ratio_for_current_measurement = BigDecimal("0.185") # 185 [mV/A] for ACS712
115
+
116
+ raw_analog_reading = BigDecimal("0.5")
117
+ expect(@voltage_current_calculation.current(raw_analog_reading,
118
+ voltage_current_ratio_for_current_measurement: voltage_current_ratio_for_current_measurement)).to be_within(BigDecimal("0.1")).of(BigDecimal("2.7"))
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
126
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'voltage_measurement'
3
2
  require 'bigdecimal'
4
3
 
5
4
  describe "Voltage measurement with voltage divider of 11:1, e.g. 51[kΩ] and 5.1[kΩ]", analog_input: true do
@@ -28,10 +27,13 @@ describe "Voltage measurement with voltage divider of 11:1, e.g. 51[kΩ] and 5.1
28
27
  it "should calculate the measuring voltage" do
29
28
 
30
29
  pin_key = "P9_40"
30
+ higher_side_resistor_resistance = BigDecimal("51000") # [Ω]
31
+ lower_side_resistor_resistance = BigDecimal("5100") # [Ω]
32
+
31
33
  expected_voltage = BigDecimal("20").to_f
32
34
  expected_raw_value = (BigDecimal(expected_voltage.to_s) / BigDecimal("11")).to_f
33
35
 
34
- voltage_input = VoltageMeasurement::VoltageInput.new(pin_key)
36
+ voltage_input = ObjectOrientedBeagleboneBlackExamples::VoltageMeasurement::VoltageInput.new(pin_key, higher_side_resistor_resistance: higher_side_resistor_resistance, lower_side_resistor_resistance: lower_side_resistor_resistance)
35
37
 
36
38
  # Since the real "slots" file creates a directory structure when a device tree overlay is written to it,
37
39
  # in the "test" environment with a regular file, it is mimiced here.
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'bigdecimal'
4
+
5
+ describe "ObjectOrientedBeagleboneBlack::UartConnection" 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
+
29
+ uart_connection = ObjectOrientedBeagleboneBlack::UartConnection.new(uart_id)
30
+
31
+ # TODO: The following requires serial connection. Find a way to test it.
32
+ # uart_connection.read(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 's')
33
+
34
+ end
35
+
36
+ it "should write the value through the specified UART connection" do
37
+
38
+ uart_id = "UART4"
39
+
40
+ uart_connection = ObjectOrientedBeagleboneBlack::UartConnection.new(uart_id)
41
+
42
+ # TODO: The following requires serial connection. Find a way to test it.
43
+ # uart_connection.write(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 'p127')
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ObjectOrientedBeagleboneBlack::UartMappings do
4
+
5
+ class TestUartMappingsClass
6
+ include ObjectOrientedBeagleboneBlack::UartMappings
7
+ end
8
+
9
+ before(:each) do
10
+ @test_uart_mappings = TestUartMappingsClass.new
11
+ end
12
+
13
+ it "should get Hash for UART4" do
14
+
15
+ expect(@test_uart_mappings.property_hash(id: "UART4")).to eq({"teletype_device_path" => "/dev/ttyO4",
16
+ "devicetree" => "BB-UART4",
17
+ "rx" => "P9_11",
18
+ "tx" => "P9_13"})
19
+
20
+ end
21
+
22
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  ENV["OBJECT_ORIENTED_BEAGLEBONE_BLACK_ENV"] ||= 'test'
2
2
 
3
3
  require 'object_oriented_beaglebone_black'
4
+ require 'object_oriented_beaglebone_black_examples'
4
5
 
5
6
  # Requires supporting ruby files with custom matchers and macros, etc,
6
7
  # in spec/support/ and its subdirectories.
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_oriented_beaglebone_black
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadatoshi Takahashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-15 00:00:00.000000000 Z
11
+ date: 2016-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: serialport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -74,12 +88,18 @@ files:
74
88
  - config/i2c.json
75
89
  - config/pin_index.json
76
90
  - config/uarts.json
77
- - examples/current_measurement.rb
78
- - examples/current_measurement/current_input.rb
79
- - examples/current_measurement/current_input_with_point04_shunt.rb
80
- - examples/voltage_measurement.rb
81
- - examples/voltage_measurement/voltage_input.rb
82
- - examples/voltage_measurement/voltage_input_with_100k_and_20k_voltage_divider.rb
91
+ - examples/arduino/arduino_sketches/DcDcConverterManagement/DcDcConverterManagement.ino
92
+ - examples/arduino/arduino_sketches/VoltageMeasurement/VoltageMeasurement.ino
93
+ - examples/object_oriented_beaglebone_black_examples.rb
94
+ - examples/object_oriented_beaglebone_black_examples/current_measurement.rb
95
+ - examples/object_oriented_beaglebone_black_examples/current_measurement/current_input.rb
96
+ - examples/object_oriented_beaglebone_black_examples/dc_dc_converter_control_example_1.rb
97
+ - examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino.rb
98
+ - examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output.rb
99
+ - examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input.rb
100
+ - examples/object_oriented_beaglebone_black_examples/voltage_current_calculation.rb
101
+ - examples/object_oriented_beaglebone_black_examples/voltage_measurement.rb
102
+ - examples/object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input.rb
83
103
  - lib/object_oriented_beaglebone_black.rb
84
104
  - lib/object_oriented_beaglebone_black/analog_input.rb
85
105
  - lib/object_oriented_beaglebone_black/gpio.rb
@@ -89,15 +109,22 @@ files:
89
109
  - lib/object_oriented_beaglebone_black/led.rb
90
110
  - lib/object_oriented_beaglebone_black/pin_mappings.rb
91
111
  - lib/object_oriented_beaglebone_black/pwm.rb
112
+ - lib/object_oriented_beaglebone_black/uart_connection.rb
113
+ - lib/object_oriented_beaglebone_black/uart_mappings.rb
92
114
  - lib/object_oriented_beaglebone_black/version.rb
93
115
  - object_orieted_beaglebone_black.gemspec
94
- - spec/examples/current_measurement/current_input_spec.rb
95
- - spec/examples/voltage_measurement/voltage_input_spec.rb
116
+ - spec/examples/object_oriented_beaglebone_black_examples/current_measurement/current_input_spec.rb
117
+ - spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output_spec.rb
118
+ - spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input_spec.rb
119
+ - spec/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation_spec.rb
120
+ - spec/examples/object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input_spec.rb
96
121
  - spec/object_oriented_beaglebone_black/analog_input_spec.rb
97
122
  - spec/object_oriented_beaglebone_black/gpio_spec.rb
98
123
  - spec/object_oriented_beaglebone_black/led_spec.rb
99
124
  - spec/object_oriented_beaglebone_black/pin_mappings_spec.rb
100
125
  - spec/object_oriented_beaglebone_black/pwm_spec.rb
126
+ - spec/object_oriented_beaglebone_black/uart_connection_spec.rb
127
+ - spec/object_oriented_beaglebone_black/uart_mappings_spec.rb
101
128
  - spec/spec_helper.rb
102
129
  - spec/support/analog_input_file_behavoir_helper.rb
103
130
  - spec/support/gpio_file_behavior_helper.rb
@@ -128,13 +155,18 @@ signing_key:
128
155
  specification_version: 4
129
156
  summary: For using BeagleBone Black in Object-Oriented way through Ruby.
130
157
  test_files:
131
- - spec/examples/current_measurement/current_input_spec.rb
132
- - spec/examples/voltage_measurement/voltage_input_spec.rb
158
+ - spec/examples/object_oriented_beaglebone_black_examples/current_measurement/current_input_spec.rb
159
+ - spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output_spec.rb
160
+ - spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input_spec.rb
161
+ - spec/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation_spec.rb
162
+ - spec/examples/object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input_spec.rb
133
163
  - spec/object_oriented_beaglebone_black/analog_input_spec.rb
134
164
  - spec/object_oriented_beaglebone_black/gpio_spec.rb
135
165
  - spec/object_oriented_beaglebone_black/led_spec.rb
136
166
  - spec/object_oriented_beaglebone_black/pin_mappings_spec.rb
137
167
  - spec/object_oriented_beaglebone_black/pwm_spec.rb
168
+ - spec/object_oriented_beaglebone_black/uart_connection_spec.rb
169
+ - spec/object_oriented_beaglebone_black/uart_mappings_spec.rb
138
170
  - spec/spec_helper.rb
139
171
  - spec/support/analog_input_file_behavoir_helper.rb
140
172
  - spec/support/gpio_file_behavior_helper.rb
@@ -1,16 +0,0 @@
1
- require 'bigdecimal'
2
-
3
- module CurrentMeasurement
4
- class CurrentInput
5
-
6
- def initialize(pin_key)
7
- @analog_input = ObjectOrientedBeagleboneBlack::AnalogInput.new(pin_key)
8
- end
9
-
10
- def value
11
- # Conversion for ACS712 Breakout. If a different current measurement device is used, extract this code to subclass and make this class parent class.
12
- (@analog_input.raw_value / BigDecimal("0.185")).to_f
13
- end
14
-
15
- end
16
- end
@@ -1,12 +0,0 @@
1
- require 'bigdecimal'
2
-
3
- module CurrentMeasurement
4
- class CurrentInputWithPoint04Shunt < CurrentInput
5
-
6
- def value
7
- # Conversion for 0.04 Ohm shunt resistance Breakout.
8
- (@analog_input.raw_value / BigDecimal("0.04")).to_f
9
- end
10
-
11
- end
12
- end
@@ -1,8 +0,0 @@
1
- require 'object_oriented_beaglebone_black'
2
-
3
- require 'current_measurement/current_input'
4
- require 'current_measurement/current_input_with_point04_shunt'
5
-
6
- module CurrentMeasurement
7
- # Your code goes here...
8
- end
@@ -1,16 +0,0 @@
1
- require 'bigdecimal'
2
-
3
- module VoltageMeasurement
4
- class VoltageInput
5
-
6
- def initialize(pin_key)
7
- @analog_input = ObjectOrientedBeagleboneBlack::AnalogInput.new(pin_key)
8
- end
9
-
10
- def value
11
- # For voltage divider 11:1, e.g. 51[kΩ] and 5.1[kΩ]. If another voltage divider is added, extract this code to subclass and make this class parent class.
12
- (@analog_input.raw_value * BigDecimal("11")).to_f
13
- end
14
-
15
- end
16
- end
@@ -1,12 +0,0 @@
1
- require 'bigdecimal'
2
-
3
- module VoltageMeasurement
4
- class VoltageInputWith100kAnd20kVoltageDivider < VoltageInput
5
-
6
- def value
7
- # For voltage divider 6:1, e.g. 100[kΩ] and 20[kΩ].
8
- (@analog_input.raw_value * BigDecimal("6")).to_f
9
- end
10
-
11
- end
12
- end
@@ -1,8 +0,0 @@
1
- require 'object_oriented_beaglebone_black'
2
-
3
- require 'voltage_measurement/voltage_input'
4
- require 'voltage_measurement/voltage_input_with_100k_and_20k_voltage_divider'
5
-
6
- module VoltageMeasurement
7
- # Your code goes here...
8
- end