動作確認済み

Dependents:   NewMD2 NewMD3

Committer:
inst
Date:
Thu Aug 25 00:01:01 2016 +0000
Revision:
9:0d8dbe2671cd
Parent:
5:b3c1c5db8489
Child:
10:2fa9eeafd727
????????????????????

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 5:b3c1c5db8489 8 const uint32_t GMD<MIN, MAX>::_default_frequency_kHz = 20;
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 4:aa4eca6acb60 15 _pwm[0]->write(0.0f);
inst 4:aa4eca6acb60 16 _pwm[1]->write(0.0f);
inst 4:aa4eca6acb60 17
inst 1:b85088467ae0 18 set_frequency_kHz(_default_frequency_kHz);
inst 1:b85088467ae0 19 }
inst 1:b85088467ae0 20
inst 1:b85088467ae0 21 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 22 void GMD<MIN, MAX>::set_frequency_kHz(float f_kHz) {
inst 1:b85088467ae0 23 _pwm[0]->period_ticks(_frequency_to_tick_coeff / f_kHz);
inst 1:b85088467ae0 24 _pwm[1]->period_ticks(_frequency_to_tick_coeff / f_kHz);
inst 1:b85088467ae0 25 }
inst 1:b85088467ae0 26
inst 2:3d70888e91c6 27 float sign(float n) {
inst 2:3d70888e91c6 28 if (n < 0.0f) {
inst 1:b85088467ae0 29 return -1.0f;
inst 1:b85088467ae0 30 }
inst 1:b85088467ae0 31 return 1.0f;
inst 1:b85088467ae0 32 }
inst 1:b85088467ae0 33
inst 1:b85088467ae0 34 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 35 float GMD<MIN, MAX>::set(float p) {
inst 1:b85088467ae0 36 p = std::max(-1.0f, std::min(p, 1.0f));
inst 2:3d70888e91c6 37
inst 2:3d70888e91c6 38 p = (abs(p) * (MAX - MIN) + MIN) * sign(p);
inst 1:b85088467ae0 39 p *= 0.01f;
inst 1:b85088467ae0 40
inst 1:b85088467ae0 41 if (p < 0.0f) {
inst 2:3d70888e91c6 42 _pwm[0]->write(-p);
inst 1:b85088467ae0 43 _pwm[1]->write(0.0f);
inst 1:b85088467ae0 44 } else {
inst 1:b85088467ae0 45 _pwm[0]->write(0.0f);
inst 1:b85088467ae0 46 _pwm[1]->write(p);
inst 1:b85088467ae0 47 }
inst 1:b85088467ae0 48 _shut_down = 1;
inst 1:b85088467ae0 49 return p;
inst 1:b85088467ae0 50 }
inst 1:b85088467ae0 51
inst 1:b85088467ae0 52 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 53 void GMD<MIN, MAX>::release() {
inst 1:b85088467ae0 54 _shut_down = 0;
inst 1:b85088467ae0 55 }
inst 1:b85088467ae0 56
inst 1:b85088467ae0 57 template <size_t MIN, size_t MAX>
inst 1:b85088467ae0 58 float GMD<MIN, MAX>::operator=(float p) {
inst 1:b85088467ae0 59 return set(p);
inst 1:b85088467ae0 60 }
inst 1:b85088467ae0 61