Module: Beaglebone::PWM

Defined in:
lib/beaglebone/pwm.rb

Overview

PWM

procedural methods for PWM control

Summary

#start is called to enable a PWM pin

Constant Summary collapse

POLARITIES =

Polarity hash

{ :NORMAL => 0, :INVERTED => 1 }

Class Method Summary collapse

Class Method Details

.cleanupObject

reset all PWM pins we’ve used to IN and unexport them



286
287
288
# File 'lib/beaglebone/pwm.rb', line 286

def cleanup
  get_pwm_pins.each { |x| disable_pwm_pin(x) }
end

.disable_pwm_pin(pin) ⇒ Object

Disable a PWM pin

Parameters:

  • pin

    should be a symbol representing the header pin



303
304
305
306
# File 'lib/beaglebone/pwm.rb', line 303

def disable_pwm_pin(pin)
  Beaglebone::check_valid_pin(pin, :pwm)
  Beaglebone::delete_pin_status(pin) if Beaglebone::device_tree_unload("#{TREES[:PWM][:pin]}#{pin}")
end

.enabled?(pin) ⇒ Boolean

Returns true if specified pin is enabled in PWM mode, else false

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/beaglebone/pwm.rb', line 73

def enabled?(pin)
  return true if Beaglebone::get_pin_status(pin, :type) == :pwm

  return false unless valid?(pin)
  if Dir.exists?(pwm_directory(pin))

    start(pin, nil, nil, nil, false)
    return true
  end
  false
end

.get_pwm_pinsArray<Symbol>

Return an array of PWM pins in use

Examples:

PWM.get_pwm_pins => [:P9_13, :P9_14]

Returns:

  • (Array<Symbol>)


296
297
298
# File 'lib/beaglebone/pwm.rb', line 296

def get_pwm_pins
  Beaglebone.pinstatus.clone.select { |x,y| x if y[:type] == :pwm}.keys
end

.run(pin) ⇒ Object

Start PWM output on specified pin. Pin must have been previously started

Parameters:

  • pin

    should be a symbol representing the header pin

Raises:

  • (StandardError)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/beaglebone/pwm.rb', line 109

def run(pin)
  Beaglebone::check_valid_pin(pin, :pwm)

  return false unless enabled?(pin)

  raise StandardError, "Pin is not PWM enabled: #{pin}" unless Beaglebone::get_pin_status(pin, :type) == :pwm

  run_fd = Beaglebone::get_pin_status(pin, :fd_run)

  raise StandardError, "Pin is not PWM enabled: #{pin}" unless run_fd

  run_fd.write('1')
  run_fd.flush

  raise StandardError, "Could not start PWM: #{pin}" unless read_run_value(pin) == 1
  true

end

.set_duty_cycle(pin, duty, newperiod = nil) ⇒ Object

Set duty cycle of specified pin in percentage

Examples:

PWM.set_duty_cycle(:P9_14, 25)

Parameters:

  • pin

    should be a symbol representing the header pin

  • duty

    should specify the duty cycle in percentage

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/beaglebone/pwm.rb', line 154

def set_duty_cycle(pin, duty, newperiod=nil)

  raise ArgumentError, "Duty cycle must be >= 0 and <= 100, #{duty} invalid" if duty < 0 || duty > 100
  check_pwm_enabled(pin)


  fd = Beaglebone::get_pin_status(pin, :fd_duty)
  raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd

  period = newperiod || Beaglebone::get_pin_status(pin, :period)

  value = ((duty * period) / 100.0).round

  fd.write(value.to_s)
  fd.flush

  raise StandardError, "Could not set duty cycle: #{pin} (#{value})" unless read_duty_value(pin) == value

  Beaglebone::set_pin_status(pin, :duty_pct, duty)
  value

end

.set_duty_cycle_ns(pin, duty) ⇒ Object

Set duty cycle of specified pin in nanoseconds

Examples:

PWM.set_duty_cycle_ns(:P9_14, 2500000)

Parameters:

  • pin

    should be a symbol representing the header pin

  • duty

    should specify the duty cycle in nanoseconds

Raises:

  • (StandardError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/beaglebone/pwm.rb', line 184

def set_duty_cycle_ns(pin, duty)

  check_pwm_enabled(pin)

  fd = Beaglebone::get_pin_status(pin, :fd_duty)
  raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd

  period = Beaglebone::get_pin_status(pin, :period)

  duty = duty.to_i

  if duty < 0 || duty > period
    raise ArgumentError, "Duty cycle ns must be >= 0 and <= #{period} (current period), #{duty} invalid"
  end

  value = duty

  fd.write(value.to_s)
  fd.flush

  #since we're setting the duty_ns, we want to update the duty_pct value as well here.
  raise StandardError, "Could not set duty cycle: #{pin} (#{value})" unless read_duty_value(pin, true) == value

  value
end

.set_frequency(pin, frequency) ⇒ Object

Set frequency of specified pin in cycles per second

Examples:

PWM.set_frequency(:P9_14, 100)

Parameters:

  • pin

    should be a symbol representing the header pin

  • frequency

    should specify the frequency in cycles per second

Raises:

  • (ArgumentError)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/beaglebone/pwm.rb', line 216

def set_frequency(pin, frequency)
  frequency = frequency.to_i
  raise ArgumentError, "Frequency must be > 0 and <= 1000000000, #{frequency} invalid" if frequency < 1 || frequency > 1000000000
  check_pwm_enabled(pin)

  fd = Beaglebone::get_pin_status(pin, :fd_period)
  raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd

  duty_ns = Beaglebone::get_pin_status(pin, :duty)
  duty_pct = Beaglebone::get_pin_status(pin, :duty_pct)

  value = (1000000000 / frequency).round

  #we can't set the frequency lower than the previous duty cycle
  #adjust if necessary
  if duty_ns > value
    set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value)
  end

  fd.write(value.to_s)
  fd.flush

  raise StandardError, "Could not set frequency: #{pin} (#{value})" unless read_period_value(pin) == value

  #adjust the duty cycle if we haven't already
  if duty_ns <= value
    set_duty_cycle(pin, duty_pct, value)
  end

  value
end

.set_period_ns(pin, period) ⇒ Object

Set frequency of specified pin based on period duration

Examples:

PWM.set_frequency_ns(:P9_14, 100000000)

Parameters:

  • pin

    should be a symbol representing the header pin

  • period

    should specify the length of a cycle in nanoseconds

Raises:

  • (ArgumentError)


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/beaglebone/pwm.rb', line 255

def set_period_ns(pin, period)
  period = period.to_i
  raise ArgumentError, "period must be > 0 and <= 1000000000, #{period} invalid" if period < 1 || period > 1000000000
  check_pwm_enabled(pin)

  fd = Beaglebone::get_pin_status(pin, :fd_period)
  raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd

  duty_ns = Beaglebone::get_pin_status(pin, :duty)
  value = period.to_i

  #we can't set the frequency lower than the previous duty cycle
  #adjust if necessary
  if duty_ns > value
    set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value)
  end

  fd.write(value.to_s)
  fd.flush

  raise StandardError, "Could not set period: #{pin} (#{value})" unless read_period_value(pin) == value

  #adjust the duty cycle if we haven't already
  if duty_ns <= value
    set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value)
  end

  value
end

.set_polarity(pin, polarity) ⇒ Object

Set polarity on specified pin

Examples:

PWM.set_polarity(:P9_14, :INVERTED)

Parameters:

  • pin

    should be a symbol representing the header pin

  • polarity

    should specify the polarity, :NORMAL or :INVERTED

Raises:

  • (StandardError)


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/beaglebone/pwm.rb', line 134

def set_polarity(pin, polarity)
  check_valid_polarity(polarity)
  check_pwm_enabled(pin)

  polarity_fd = Beaglebone::get_pin_status(pin, :fd_polarity)
  raise StandardError, "Pin is not PWM enabled: #{pin}" unless polarity_fd

  polarity_fd.write(POLARITIES[polarity.to_sym].to_s)
  polarity_fd.flush

  raise StandardError, "Could not set polarity: #{pin}" unless read_polarity_value(pin) == POLARITIES[polarity.to_sym]

end

.start(pin, duty = nil, frequency = nil, polarity = nil, run = true) ⇒ Object

Initialize a PWM pin

Examples:

PWM.start(:P9_14, 90, 10, :NORMAL)

Parameters:

  • pin

    should be a symbol representing the header pin

  • duty (defaults to: nil)

    should specify the duty cycle

  • frequency (defaults to: nil)

    should specify cycles per second

  • polarity (defaults to: nil)

    optional, should specify the polarity, :NORMAL or :INVERTED

  • run (defaults to: true)

    optional, if false, pin will be configured but will not run

Raises:

  • (StandardError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/beaglebone/pwm.rb', line 25

def start(pin, duty=nil, frequency=nil, polarity=nil, run=true)
  #make sure the pwm controller dtb is loaded
  Beaglebone::device_tree_load(TREES[:PWM][:global])

  Beaglebone::check_valid_pin(pin, :pwm)

  #if pin is enabled for something else, disable it
  if Beaglebone::get_pin_status(pin) && Beaglebone::get_pin_status(pin, :type) != :pwm
    Beaglebone::disable_pin(pin)
  end

  #load device tree for pin if not already loaded
  unless Beaglebone::get_pin_status(pin, :type) == :pwm
    Beaglebone::device_tree_load("#{TREES[:PWM][:pin]}#{pin}", 0.5)
    Beaglebone::set_pin_status(pin, :type, :pwm)
  end

  duty_fd = File.open("#{pwm_directory(pin)}/duty", 'r+')
  period_fd = File.open("#{pwm_directory(pin)}/period", 'r+')
  polarity_fd = File.open("#{pwm_directory(pin)}/polarity", 'r+')
  run_fd = File.open("#{pwm_directory(pin)}/run", 'r+')

  Beaglebone::set_pin_status(pin, :fd_duty, duty_fd)
  Beaglebone::set_pin_status(pin, :fd_period, period_fd)
  Beaglebone::set_pin_status(pin, :fd_polarity, polarity_fd)
  Beaglebone::set_pin_status(pin, :fd_run, run_fd)

  read_period_value(pin)
  read_duty_value(pin)
  read_polarity_value(pin)

  run_fd.write('0')
  run_fd.flush

  set_polarity(pin, polarity) if polarity
  set_frequency(pin, frequency) if frequency
  set_duty_cycle(pin, duty) if duty

  if run
    run_fd.write('1')
    run_fd.flush
  end

  raise StandardError, "Could not start PWM: #{pin}" unless read_run_value(pin) == 1
  true
end

.stop(pin) ⇒ Object

Stop PWM output on specified pin

Parameters:

  • pin

    should be a symbol representing the header pin

Raises:

  • (StandardError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/beaglebone/pwm.rb', line 88

def stop(pin)
  Beaglebone::check_valid_pin(pin, :pwm)

  return false unless enabled?(pin)

  raise StandardError, "Pin is not PWM enabled: #{pin}" unless Beaglebone::get_pin_status(pin, :type) == :pwm

  run_fd = Beaglebone::get_pin_status(pin, :fd_run)

  raise StandardError, "Pin is not PWM enabled: #{pin}" unless run_fd

  run_fd.write('0')
  run_fd.flush

  raise StandardError, "Could not stop PWM: #{pin}" unless read_run_value(pin) == 0
  true
end