動作確認済み
GMD.cpp@2:858781a5bbd1, 2016-07-03 (annotated)
- Committer:
- inst
- Date:
- Sun Jul 03 01:20:22 2016 +0000
- Revision:
- 2:858781a5bbd1
- Parent:
- 1:9d066dbe1893
set?duty???????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 1:9d066dbe1893 | 1 | #include <algorithm> |
inst | 0:010c6f6ac7c0 | 2 | #include "GMD.hpp" |
inst | 0:010c6f6ac7c0 | 3 | |
inst | 0:010c6f6ac7c0 | 4 | const float GMD::_frequency_to_tick_coeff = 3300.0f; |
inst | 0:010c6f6ac7c0 | 5 | const uint32_t GMD::_default_frequency_kHz = 50; |
inst | 0:010c6f6ac7c0 | 6 | const float GMD::_duty_lower_limit = 0.0f; |
inst | 0:010c6f6ac7c0 | 7 | const float GMD::_duty_upper_limit = 1.0f; |
inst | 0:010c6f6ac7c0 | 8 | |
inst | 0:010c6f6ac7c0 | 9 | GMD::GMD(PinName p0, PinName p1, PinName shut_down) : _shut_down(shut_down, 1) { |
inst | 0:010c6f6ac7c0 | 10 | _pwm[0] = new FastPWM(p0); |
inst | 0:010c6f6ac7c0 | 11 | _pwm[1] = new FastPWM(p1); |
inst | 0:010c6f6ac7c0 | 12 | |
inst | 0:010c6f6ac7c0 | 13 | set_frequency_kHz(_default_frequency_kHz); |
inst | 0:010c6f6ac7c0 | 14 | } |
inst | 0:010c6f6ac7c0 | 15 | |
inst | 0:010c6f6ac7c0 | 16 | void GMD::set_frequency_kHz(float f_kHz) { |
inst | 0:010c6f6ac7c0 | 17 | _pwm[0]->period_ticks(_frequency_to_tick_coeff / f_kHz); |
inst | 0:010c6f6ac7c0 | 18 | _pwm[1]->period_ticks(_frequency_to_tick_coeff / f_kHz); |
inst | 0:010c6f6ac7c0 | 19 | } |
inst | 0:010c6f6ac7c0 | 20 | |
inst | 2:858781a5bbd1 | 21 | float sign(float n) { |
inst | 2:858781a5bbd1 | 22 | if (n < 0.0f) { |
inst | 2:858781a5bbd1 | 23 | return -1.0f; |
inst | 2:858781a5bbd1 | 24 | } |
inst | 2:858781a5bbd1 | 25 | return 1.0f; |
inst | 2:858781a5bbd1 | 26 | } |
inst | 2:858781a5bbd1 | 27 | |
inst | 0:010c6f6ac7c0 | 28 | float GMD::set(float p) { |
inst | 2:858781a5bbd1 | 29 | p = std::max(-1.0f, std::min(p, 1.0f)); |
inst | 2:858781a5bbd1 | 30 | p = (abs(p) * (_duty_upper_limit - _duty_lower_limit) + _duty_lower_limit) * sign(p); |
inst | 1:9d066dbe1893 | 31 | |
inst | 0:010c6f6ac7c0 | 32 | if (p < 0.0f) { |
inst | 2:858781a5bbd1 | 33 | _pwm[0]->write(-p); |
inst | 0:010c6f6ac7c0 | 34 | _pwm[1]->write(0.0f); |
inst | 0:010c6f6ac7c0 | 35 | } else { |
inst | 0:010c6f6ac7c0 | 36 | _pwm[0]->write(0.0f); |
inst | 0:010c6f6ac7c0 | 37 | _pwm[1]->write(p); |
inst | 0:010c6f6ac7c0 | 38 | } |
inst | 0:010c6f6ac7c0 | 39 | _shut_down = 1; |
inst | 0:010c6f6ac7c0 | 40 | return p; |
inst | 0:010c6f6ac7c0 | 41 | } |
inst | 0:010c6f6ac7c0 | 42 | |
inst | 0:010c6f6ac7c0 | 43 | void GMD::release() { |
inst | 0:010c6f6ac7c0 | 44 | _shut_down = 0; |
inst | 0:010c6f6ac7c0 | 45 | } |
inst | 0:010c6f6ac7c0 | 46 | |
inst | 0:010c6f6ac7c0 | 47 | float GMD::operator=(float p) { |
inst | 0:010c6f6ac7c0 | 48 | return set(p); |
inst | 0:010c6f6ac7c0 | 49 | } |
inst | 0:010c6f6ac7c0 | 50 |