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.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/examples/arduino/arduino_sketches/DcDcConverterManagement/DcDcConverterManagement.ino +100 -0
- data/examples/arduino/arduino_sketches/VoltageMeasurement/VoltageMeasurement.ino +78 -0
- data/examples/object_oriented_beaglebone_black_examples/current_measurement/current_input.rb +30 -0
- data/examples/object_oriented_beaglebone_black_examples/current_measurement.rb +7 -0
- data/examples/object_oriented_beaglebone_black_examples/dc_dc_converter_control_example_1.rb +54 -0
- data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output.rb +30 -0
- data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input.rb +73 -0
- data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino.rb +8 -0
- data/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation.rb +49 -0
- data/examples/object_oriented_beaglebone_black_examples/voltage_measurement/voltage_input.rb +26 -0
- data/examples/object_oriented_beaglebone_black_examples/voltage_measurement.rb +7 -0
- data/examples/object_oriented_beaglebone_black_examples.rb +10 -0
- data/lib/object_oriented_beaglebone_black/analog_input.rb +32 -4
- data/lib/object_oriented_beaglebone_black/pin_mappings.rb +3 -3
- data/lib/object_oriented_beaglebone_black/pwm.rb +68 -10
- data/lib/object_oriented_beaglebone_black/uart_connection.rb +95 -0
- data/lib/object_oriented_beaglebone_black/uart_mappings.rb +29 -0
- data/lib/object_oriented_beaglebone_black/version.rb +1 -1
- data/lib/object_oriented_beaglebone_black.rb +2 -0
- data/object_orieted_beaglebone_black.gemspec +3 -0
- data/spec/examples/{current_measurement → object_oriented_beaglebone_black_examples/current_measurement}/current_input_spec.rb +3 -2
- data/spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output_spec.rb +41 -0
- data/spec/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input_spec.rb +55 -0
- data/spec/examples/object_oriented_beaglebone_black_examples/voltage_current_calculation_spec.rb +126 -0
- data/spec/examples/{voltage_measurement → object_oriented_beaglebone_black_examples/voltage_measurement}/voltage_input_spec.rb +4 -2
- data/spec/object_oriented_beaglebone_black/uart_connection_spec.rb +47 -0
- data/spec/object_oriented_beaglebone_black/uart_mappings_spec.rb +22 -0
- data/spec/spec_helper.rb +1 -0
- metadata +44 -12
- data/examples/current_measurement/current_input.rb +0 -16
- data/examples/current_measurement/current_input_with_point04_shunt.rb +0 -12
- data/examples/current_measurement.rb +0 -8
- data/examples/voltage_measurement/voltage_input.rb +0 -16
- data/examples/voltage_measurement/voltage_input_with_100k_and_20k_voltage_divider.rb +0 -12
- data/examples/voltage_measurement.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23dd492f02ed9599e354f09bc32122056d4b570d
|
4
|
+
data.tar.gz: 52af68d2197f4b880dc83a2b541531546b38d24b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e19440fd68f62438dbcaca492524cedc1f5ca10dc3f018ceb22d66b9310e3668b47b4e8b4e01288afa0c87a902b48dd8e5c181eb07dca6c8bace62a578955d6
|
7
|
+
data.tar.gz: cc30fabdeb896be78abe143067145e90901258b0f766ed5d3053e799681251df8e575790b4512539916e11a52cb7388454be34e8b8270aa0e95643e5582dedd4
|
data/README.md
CHANGED
@@ -86,6 +86,7 @@ Or install it yourself as:
|
|
86
86
|
|
87
87
|
pin_key = "P9_40"
|
88
88
|
|
89
|
+
# This can take a few seconds:
|
89
90
|
analog_input = ObjectOrientedBeagleboneBlack::AnalogInput.new(pin_key)
|
90
91
|
|
91
92
|
analog_input.raw_value # Read the raw voltage value in the range of 0 and 1.8[V].
|
@@ -102,6 +103,7 @@ Or install it yourself as:
|
|
102
103
|
|
103
104
|
pwm_pin_key = "P9_14"
|
104
105
|
|
106
|
+
# This can take several seconds:
|
105
107
|
pwm = ObjectOrientedBeagleboneBlack::Pwm.new(pwm_pin_key)
|
106
108
|
|
107
109
|
pwm.period = 1000 # Unit is [ns] (nano second)
|
@@ -0,0 +1,100 @@
|
|
1
|
+
/*
|
2
|
+
Sends the raw voltage and current to the serial port and receives raw PWM value from the serial port.
|
3
|
+
|
4
|
+
Created by Tadatoshi Takahashi
|
5
|
+
*/
|
6
|
+
|
7
|
+
const int inputVoltageAnalogInputPin = 0;
|
8
|
+
const int inputCurrentAnalogInputPin = 1;
|
9
|
+
const int outputVoltageAnalogInputPin = 2;
|
10
|
+
const int outputCurrentAnalogInputPin = 3;
|
11
|
+
const int pwmDigitalPin = 8; // Just in case, selected a pin number different from the ones used for input pins.
|
12
|
+
|
13
|
+
void setup() {
|
14
|
+
pinMode(pwmDigitalPin, OUTPUT);
|
15
|
+
Serial.begin(9600, SERIAL_8N1); // send and receive at 9600 baud
|
16
|
+
}
|
17
|
+
|
18
|
+
void loop() {
|
19
|
+
|
20
|
+
// Serial.println("In loop");
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
// delay(1000);
|
25
|
+
|
26
|
+
}
|
27
|
+
|
28
|
+
void serialEvent() {
|
29
|
+
|
30
|
+
// Serial.println("Testing serialEvent method.");
|
31
|
+
|
32
|
+
while (Serial.available()) {
|
33
|
+
|
34
|
+
// Serial.println("In Serial.available()");
|
35
|
+
|
36
|
+
char command = Serial.read();
|
37
|
+
|
38
|
+
// Serial.print("command: ");
|
39
|
+
// Serial.println(command);
|
40
|
+
|
41
|
+
// 'm' for measure:
|
42
|
+
if (command == 'm') {
|
43
|
+
|
44
|
+
// Serial.println("if for m");
|
45
|
+
|
46
|
+
measureRawVoltageAndCurrent();
|
47
|
+
|
48
|
+
// 'p' for pwm (e.g. "p127": 127 out of between 0 and 255 gives 50% duty cycle):
|
49
|
+
} else if (command == 'p') {
|
50
|
+
|
51
|
+
// Serial.println("else if for p");
|
52
|
+
|
53
|
+
int dutyCycle = Serial.parseInt();
|
54
|
+
// Serial.print("duty cycle: ");
|
55
|
+
// Serial.println(dutyCycle);
|
56
|
+
|
57
|
+
setDutyCycle(pwmDigitalPin, dutyCycle);
|
58
|
+
// Serial.println("duty_cycle_set");
|
59
|
+
|
60
|
+
// 'o' for b'o'ost (set duty cycle for the second switch with the first one clsed = 255):
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
// delay(1000);
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
void measureRawVoltageAndCurrent() {
|
70
|
+
|
71
|
+
// Serial.println("in measureRawVoltageAndCurrent");
|
72
|
+
|
73
|
+
// In order to have analog steps between 0 and 4095:
|
74
|
+
analogReadResolution(12);
|
75
|
+
|
76
|
+
int rawReadingForInputVoltage = analogRead(inputVoltageAnalogInputPin);
|
77
|
+
int rawReadingForInputCurrent = analogRead(inputCurrentAnalogInputPin);
|
78
|
+
int rawReadingForOutputVoltage = analogRead(outputVoltageAnalogInputPin);
|
79
|
+
// int rawReadingForOutputCurrent = analogRead(outputCurrentAnalogInputPin);
|
80
|
+
|
81
|
+
Serial.print("{\"raw_reading_for_input_voltage\": ");
|
82
|
+
Serial.print(rawReadingForInputVoltage);
|
83
|
+
Serial.print(", \"raw_reading_for_input_current\": ");
|
84
|
+
Serial.print(rawReadingForInputCurrent);
|
85
|
+
Serial.print(", \"raw_reading_for_output_voltage\": ");
|
86
|
+
Serial.print(rawReadingForOutputVoltage);
|
87
|
+
// Serial.print(", \"raw_reading_for_output_current\": ");
|
88
|
+
// Serial.print(rawReadingForOutputCurrent);
|
89
|
+
Serial.println("}");
|
90
|
+
|
91
|
+
// delay(1000);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
void setDutyCycle(int pwmPin, int dutyCycle) {
|
96
|
+
analogWrite(pwmDigitalPin, dutyCycle);
|
97
|
+
|
98
|
+
// delay(1000);
|
99
|
+
}
|
100
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
/*
|
2
|
+
Sends the raw voltage and current to the serial port.
|
3
|
+
|
4
|
+
Created by Tadatoshi Takahashi
|
5
|
+
*/
|
6
|
+
|
7
|
+
const int inputVoltagePin = 0;
|
8
|
+
const int inputCurrentPin = 1;
|
9
|
+
const int outputVoltagePin = 2;
|
10
|
+
const int outputCurrentPin = 3;
|
11
|
+
|
12
|
+
void setup() {
|
13
|
+
Serial.begin(9600, SERIAL_8N1); // send and receive at 9600 baud
|
14
|
+
}
|
15
|
+
|
16
|
+
void loop() {
|
17
|
+
|
18
|
+
// Serial.println("In loop");
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
// delay(1000);
|
23
|
+
|
24
|
+
}
|
25
|
+
|
26
|
+
void serialEvent() {
|
27
|
+
|
28
|
+
// Serial.println("Testing serialEvent method.");
|
29
|
+
|
30
|
+
while (Serial.available()) {
|
31
|
+
|
32
|
+
// Serial.println("In Serial.available()");
|
33
|
+
|
34
|
+
char command = Serial.read();
|
35
|
+
|
36
|
+
// Serial.print("command: ");
|
37
|
+
// Serial.println(command);
|
38
|
+
|
39
|
+
// 'm' for measure:
|
40
|
+
if (command == 'm') {
|
41
|
+
|
42
|
+
// Serial.println("if for m");
|
43
|
+
|
44
|
+
measureRawVoltageAndCurrent();
|
45
|
+
|
46
|
+
}
|
47
|
+
|
48
|
+
}
|
49
|
+
|
50
|
+
// delay(1000);
|
51
|
+
|
52
|
+
}
|
53
|
+
|
54
|
+
void measureRawVoltageAndCurrent() {
|
55
|
+
|
56
|
+
// Serial.println("in measureRawVoltageAndCurrent");
|
57
|
+
|
58
|
+
// In order to have analog steps between 0 and 4095:
|
59
|
+
analogReadResolution(12);
|
60
|
+
|
61
|
+
int rawReadingForInputVoltage = analogRead(inputVoltagePin);
|
62
|
+
int rawReadingForInputCurrent = analogRead(inputCurrentPin);
|
63
|
+
int rawReadingForOutputVoltage = analogRead(outputVoltagePin);
|
64
|
+
// int rawReadingForOutputCurrent = analogRead(outputCurrentPin);
|
65
|
+
|
66
|
+
Serial.print("{\"raw_reading_for_input_voltage\": ");
|
67
|
+
Serial.print(rawReadingForInputVoltage);
|
68
|
+
Serial.print(", \"raw_reading_for_input_current\": ");
|
69
|
+
Serial.print(rawReadingForInputCurrent);
|
70
|
+
Serial.print(", \"raw_reading_for_output_voltage\": ");
|
71
|
+
Serial.print(rawReadingForOutputVoltage);
|
72
|
+
// Serial.print(", \"raw_reading_for_output_current\": ");
|
73
|
+
// Serial.print(rawReadingForOutputCurrent);
|
74
|
+
Serial.println("}");
|
75
|
+
|
76
|
+
// delay(1000);
|
77
|
+
|
78
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module ObjectOrientedBeagleboneBlackExamples
|
4
|
+
module CurrentMeasurement
|
5
|
+
class CurrentInput
|
6
|
+
|
7
|
+
def initialize(pin_key, resistance_for_current_measurement: nil, voltage_gain_for_current_measurement: BigDecimal("1.0"), voltage_current_ratio_for_current_measurement: nil)
|
8
|
+
@analog_input = ObjectOrientedBeagleboneBlack::AnalogInput.new(pin_key)
|
9
|
+
|
10
|
+
@voltage_current_calculation = VoltageCurrentCalculation.new
|
11
|
+
|
12
|
+
@resistance_for_current_measurement = resistance_for_current_measurement
|
13
|
+
@voltage_gain_for_current_measurement = voltage_gain_for_current_measurement
|
14
|
+
@voltage_current_ratio_for_current_measurement = voltage_current_ratio_for_current_measurement
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
raw_analog_reading = BigDecimal(@analog_input.raw_value.to_s)
|
19
|
+
|
20
|
+
current_in_bigdecimal = @voltage_current_calculation.current(raw_analog_reading,
|
21
|
+
resistance_for_current_measurement: @resistance_for_current_measurement,
|
22
|
+
voltage_gain_for_current_measurement: @voltage_gain_for_current_measurement,
|
23
|
+
voltage_current_ratio_for_current_measurement: @voltage_current_ratio_for_current_measurement)
|
24
|
+
|
25
|
+
current_in_bigdecimal.to_f
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'object_oriented_beaglebone_black_examples'
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
uart_id = "UART4"
|
5
|
+
|
6
|
+
uart_connection = ObjectOrientedBeagleboneBlack::UartConnection.new(uart_id)
|
7
|
+
|
8
|
+
# The parameters for Arduino Due:
|
9
|
+
analog_steps = BigDecimal("4095")
|
10
|
+
reference_voltage = BigDecimal("3.3")
|
11
|
+
|
12
|
+
voltage_current_input = ObjectOrientedBeagleboneBlackExamples::UartConnectionToArduino::VoltageCurrentInput.new(uart_connection, analog_steps, reference_voltage)
|
13
|
+
|
14
|
+
voltage_current_input.measure
|
15
|
+
|
16
|
+
# higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
|
17
|
+
input_voltage = voltage_current_input.input_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
|
18
|
+
# shunt resistance: 0.04[Ω]
|
19
|
+
input_current = voltage_current_input.input_current(resistance_for_current_measurement: BigDecimal("0.04"))
|
20
|
+
# higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
|
21
|
+
output_voltage = voltage_current_input.output_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
|
22
|
+
|
23
|
+
puts "----- Before setting PWM -----"
|
24
|
+
|
25
|
+
puts "input_voltage: #{input_voltage}"
|
26
|
+
puts "input_current: #{input_current}"
|
27
|
+
puts "output_voltage: #{output_voltage}"
|
28
|
+
|
29
|
+
puts "output_voltage / input_voltage = #{(BigDecimal(output_voltage.to_s) / BigDecimal(input_voltage.to_s)).to_f}"
|
30
|
+
|
31
|
+
# The parameters for Arduino Due:
|
32
|
+
pwm_steps = BigDecimal("255")
|
33
|
+
|
34
|
+
pwm_output = ObjectOrientedBeagleboneBlackExamples::UartConnectionToArduino::PwmOutput.new(uart_connection, pwm_steps)
|
35
|
+
|
36
|
+
pwm_output.send(0.5)
|
37
|
+
|
38
|
+
voltage_current_input.measure
|
39
|
+
|
40
|
+
# higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
|
41
|
+
input_voltage = voltage_current_input.input_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
|
42
|
+
# shunt resistance: 0.04[Ω]
|
43
|
+
input_current = voltage_current_input.input_current(resistance_for_current_measurement: BigDecimal("0.04"))
|
44
|
+
# higher_side_resistor_resistance: 100[kΩ], lower_side_resistor_resistance: 8.2[kΩ]
|
45
|
+
output_voltage = voltage_current_input.output_voltage(higher_side_resistor_resistance: BigDecimal("100000"), lower_side_resistor_resistance: BigDecimal("8200"))
|
46
|
+
|
47
|
+
puts "----- After setting PWM -----"
|
48
|
+
|
49
|
+
puts "input_voltage: #{input_voltage}"
|
50
|
+
puts "input_current: #{input_current}"
|
51
|
+
puts "output_voltage: #{output_voltage}"
|
52
|
+
|
53
|
+
puts "output_voltage / input_voltage = #{(BigDecimal(output_voltage.to_s) / BigDecimal(input_voltage.to_s)).to_f}"
|
54
|
+
|
data/examples/object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module ObjectOrientedBeagleboneBlackExamples
|
4
|
+
module UartConnectionToArduino
|
5
|
+
class PwmOutput
|
6
|
+
|
7
|
+
def initialize(uart_connection, pwm_steps)
|
8
|
+
@uart_connection = uart_connection
|
9
|
+
|
10
|
+
@pwm_steps = BigDecimal(pwm_steps.to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def send(duty_cycle)
|
14
|
+
|
15
|
+
duty_cycle_in_bigdecimal = BigDecimal(duty_cycle.to_s)
|
16
|
+
raw_value = convert_duty_cycle(duty_cycle_in_bigdecimal)
|
17
|
+
communication_command = "p#{raw_value}"
|
18
|
+
|
19
|
+
@uart_connection.write(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: communication_command)
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def convert_duty_cycle(duty_cycle_in_bigdecimal)
|
25
|
+
(duty_cycle_in_bigdecimal * @pwm_steps).to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module ObjectOrientedBeagleboneBlackExamples
|
5
|
+
module UartConnectionToArduino
|
6
|
+
class VoltageCurrentInput
|
7
|
+
|
8
|
+
def initialize(uart_connection, analog_steps, reference_voltage)
|
9
|
+
@uart_connection = uart_connection
|
10
|
+
@voltage_current_calculation = VoltageCurrentCalculation.new(analog_steps, reference_voltage)
|
11
|
+
end
|
12
|
+
|
13
|
+
def measure
|
14
|
+
|
15
|
+
raw_data = @uart_connection.read(serial_baud_rate: 9600, serial_data_bits: 8, serial_stop_bits: 1, communication_command: 'm')
|
16
|
+
|
17
|
+
@raw_data_hash = JSON.parse(raw_data)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def input_voltage(higher_side_resistor_resistance: nil, lower_side_resistor_resistance: nil)
|
22
|
+
|
23
|
+
if @raw_data_hash.has_key?("raw_reading_for_input_voltage")
|
24
|
+
raw_reading_in_bigdecimal = BigDecimal(@raw_data_hash["raw_reading_for_input_voltage"].to_s)
|
25
|
+
voltage_in_bigdecimal = @voltage_current_calculation.voltage(raw_reading_in_bigdecimal,
|
26
|
+
higher_side_resistor_resistance: higher_side_resistor_resistance,
|
27
|
+
lower_side_resistor_resistance: lower_side_resistor_resistance)
|
28
|
+
voltage_in_bigdecimal.to_f
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def input_current(resistance_for_current_measurement: nil, voltage_gain_for_current_measurement: BigDecimal("1.0"), voltage_current_ratio_for_current_measurement: nil)
|
34
|
+
|
35
|
+
if @raw_data_hash.has_key?("raw_reading_for_input_current")
|
36
|
+
raw_reading_in_bigdecimal = BigDecimal(@raw_data_hash["raw_reading_for_input_current"].to_s)
|
37
|
+
current_in_bigdecimal = @voltage_current_calculation.current(raw_reading_in_bigdecimal,
|
38
|
+
resistance_for_current_measurement: resistance_for_current_measurement,
|
39
|
+
voltage_gain_for_current_measurement: voltage_gain_for_current_measurement,
|
40
|
+
voltage_current_ratio_for_current_measurement: voltage_current_ratio_for_current_measurement)
|
41
|
+
current_in_bigdecimal.to_f
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def output_voltage(higher_side_resistor_resistance: nil, lower_side_resistor_resistance: nil)
|
47
|
+
|
48
|
+
if @raw_data_hash.has_key?("raw_reading_for_output_voltage")
|
49
|
+
raw_reading_in_bigdecimal = BigDecimal(@raw_data_hash["raw_reading_for_output_voltage"].to_s)
|
50
|
+
voltage_in_bigdecimal = @voltage_current_calculation.voltage(raw_reading_in_bigdecimal,
|
51
|
+
higher_side_resistor_resistance: higher_side_resistor_resistance,
|
52
|
+
lower_side_resistor_resistance: lower_side_resistor_resistance)
|
53
|
+
voltage_in_bigdecimal.to_f
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def output_current(resistance_for_current_measurement: nil, voltage_gain_for_current_measurement: BigDecimal("1.0"), voltage_current_ratio_for_current_measurement: nil)
|
59
|
+
|
60
|
+
if @raw_data_hash.has_key?("raw_reading_for_output_current")
|
61
|
+
raw_reading_in_bigdecimal = BigDecimal(@raw_data_hash["raw_reading_for_output_current"].to_s)
|
62
|
+
current_in_bigdecimal = @voltage_current_calculation.current(raw_reading_in_bigdecimal,
|
63
|
+
resistance_for_current_measurement: resistance_for_current_measurement,
|
64
|
+
voltage_gain_for_current_measurement: voltage_gain_for_current_measurement,
|
65
|
+
voltage_current_ratio_for_current_measurement: voltage_current_ratio_for_current_measurement)
|
66
|
+
current_in_bigdecimal.to_f
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'object_oriented_beaglebone_black_examples/uart_connection_to_arduino/voltage_current_input'
|
2
|
+
require 'object_oriented_beaglebone_black_examples/uart_connection_to_arduino/pwm_output'
|
3
|
+
|
4
|
+
module ObjectOrientedBeagleboneBlackExamples
|
5
|
+
module UartConnectionToArduino
|
6
|
+
# Your code goes here...
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "bigdecimal"
|
2
|
+
|
3
|
+
module ObjectOrientedBeagleboneBlackExamples
|
4
|
+
class VoltageCurrentCalculation
|
5
|
+
|
6
|
+
# The default value is for Analog Input pin of BeagleBone Black.
|
7
|
+
# Since Analog Input pin reading doesn't work when it's used with PWM,
|
8
|
+
# this class is created to accommodate the case where external analog digital converter is used.
|
9
|
+
# e.g. Analog input pin of Arduino.
|
10
|
+
def initialize(analog_steps = BigDecimal("1.8"), reference_voltage = BigDecimal("1.8"))
|
11
|
+
@analog_steps = analog_steps
|
12
|
+
@reference_voltage = reference_voltage
|
13
|
+
end
|
14
|
+
|
15
|
+
def voltage(raw_analog_reading, higher_side_resistor_resistance: nil, lower_side_resistor_resistance: nil)
|
16
|
+
|
17
|
+
(raw_analog_reading / resistor_factor(higher_side_resistor_resistance, lower_side_resistor_resistance)) * @reference_voltage
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def current(raw_analog_reading, resistance_for_current_measurement: nil, voltage_gain_for_current_measurement: BigDecimal("1.0"), voltage_current_ratio_for_current_measurement: nil)
|
22
|
+
|
23
|
+
current = nil
|
24
|
+
|
25
|
+
voltage = voltage(raw_analog_reading)
|
26
|
+
|
27
|
+
if !resistance_for_current_measurement.nil? && voltage_current_ratio_for_current_measurement.nil? # The case where voltage accross shunt resistance is directly measured
|
28
|
+
voltage = voltage / voltage_gain_for_current_measurement
|
29
|
+
current = voltage / resistance_for_current_measurement
|
30
|
+
elsif resistance_for_current_measurement.nil? && !voltage_current_ratio_for_current_measurement.nil? # The case where current sensor circuit is used
|
31
|
+
current = voltage / voltage_current_ratio_for_current_measurement
|
32
|
+
end
|
33
|
+
|
34
|
+
current
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def resistor_factor(higher_side_resistor_resistance, lower_side_resistor_resistance)
|
39
|
+
|
40
|
+
if higher_side_resistor_resistance.nil? && lower_side_resistor_resistance.nil?
|
41
|
+
@analog_steps
|
42
|
+
else
|
43
|
+
@analog_steps * (lower_side_resistor_resistance / (higher_side_resistor_resistance + lower_side_resistor_resistance))
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|