動作確認済み

Dependents:   NewMD2 NewMD3

Committer:
inst
Date:
Sat Jul 02 06:16:41 2016 +0000
Revision:
1:b85088467ae0
Child:
2:3d70888e91c6
template????????duty???????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
inst 1:b85088467ae0 1 #include <algorithm>
inst 1:b85088467ae0 2 #include "GMD.hpp"
inst 1:b85088467ae0 3
inst 1:b85088467ae0 4 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 5 const float GMD<MIN, MAX>::_frequency_to_tick_coeff = 3300.0f;
inst 1:b85088467ae0 6
inst 1:b85088467ae0 7 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 8 const uint32_t GMD<MIN, MAX>::_default_frequency_kHz = 50;
inst 1:b85088467ae0 9
inst 1:b85088467ae0 10 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 11 GMD<MIN, MAX>::GMD(PinName p0, PinName p1, PinName shut_down) : _shut_down(shut_down, 1) {
inst 1:b85088467ae0 12 _pwm[0] = new FastPWM(p0);
inst 1:b85088467ae0 13 _pwm[1] = new FastPWM(p1);
inst 1:b85088467ae0 14
inst 1:b85088467ae0 15 set_frequency_kHz(_default_frequency_kHz);
inst 1:b85088467ae0 16 }
inst 1:b85088467ae0 17
inst 1:b85088467ae0 18 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 19 void GMD<MIN, MAX>::set_frequency_kHz(float f_kHz) {
inst 1:b85088467ae0 20 _pwm[0]->period_ticks(_frequency_to_tick_coeff / f_kHz);
inst 1:b85088467ae0 21 _pwm[1]->period_ticks(_frequency_to_tick_coeff / f_kHz);
inst 1:b85088467ae0 22 }
inst 1:b85088467ae0 23
inst 1:b85088467ae0 24 float sign(float v) {
inst 1:b85088467ae0 25 if (v < 0.0f) {
inst 1:b85088467ae0 26 return -1.0f;
inst 1:b85088467ae0 27 }
inst 1:b85088467ae0 28 return 1.0f;
inst 1:b85088467ae0 29 }
inst 1:b85088467ae0 30
inst 1:b85088467ae0 31 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 32 float GMD<MIN, MAX>::set(float p) {
inst 1:b85088467ae0 33 p = std::max(-1.0f, std::min(p, 1.0f));
inst 1:b85088467ae0 34 p = p * (MAX - MIN) + MIN;
inst 1:b85088467ae0 35 p *= 0.01f;
inst 1:b85088467ae0 36
inst 1:b85088467ae0 37 if (p < 0.0f) {
inst 1:b85088467ae0 38 _pwm[0]->write(p);
inst 1:b85088467ae0 39 _pwm[1]->write(0.0f);
inst 1:b85088467ae0 40 } else {
inst 1:b85088467ae0 41 _pwm[0]->write(0.0f);
inst 1:b85088467ae0 42 _pwm[1]->write(p);
inst 1:b85088467ae0 43 }
inst 1:b85088467ae0 44 _shut_down = 1;
inst 1:b85088467ae0 45 return p;
inst 1:b85088467ae0 46 }
inst 1:b85088467ae0 47
inst 1:b85088467ae0 48 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 49 void GMD<MIN, MAX>::release() {
inst 1:b85088467ae0 50 _shut_down = 0;
inst 1:b85088467ae0 51 }
inst 1:b85088467ae0 52
inst 1:b85088467ae0 53 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 54 float GMD<MIN, MAX>::operator=(float p) {
inst 1:b85088467ae0 55 return set(p);
inst 1:b85088467ae0 56 }
inst 1:b85088467ae0 57