![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
2017年度の製作を開始します
Fork of Control_Main_Full_20160608 by
InputHandler/InputHandler.cpp@52:a9d23be503c6, 2017-06-10 (annotated)
- Committer:
- YusukeWakuta
- Date:
- Sat Jun 10 07:43:58 2017 +0000
- Branch:
- ?????
- Revision:
- 52:a9d23be503c6
- Parent:
- 51:8a579a19a4ff
- Child:
- 53:947eff37d01d
?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YusukeWakuta | 49:87af91607b46 | 1 | #include "InputHandler.h" |
YusukeWakuta | 49:87af91607b46 | 2 | #include "mbed.h" |
YusukeWakuta | 49:87af91607b46 | 3 | |
YusukeWakuta | 49:87af91607b46 | 4 | InputCalc::InputCalc() |
YusukeWakuta | 49:87af91607b46 | 5 | { |
YusukeWakuta | 49:87af91607b46 | 6 | rawNeu = 0.0; |
YusukeWakuta | 49:87af91607b46 | 7 | rawMax = NORMMIN; |
YusukeWakuta | 49:87af91607b46 | 8 | rawMin = NORMMAX; |
YusukeWakuta | 49:87af91607b46 | 9 | |
YusukeWakuta | 49:87af91607b46 | 10 | shiftedMax = 0.0; |
YusukeWakuta | 49:87af91607b46 | 11 | shiftedMin = 0.0; |
YusukeWakuta | 49:87af91607b46 | 12 | |
YusukeWakuta | 49:87af91607b46 | 13 | NeushiftWidth = 0.0; |
YusukeWakuta | 49:87af91607b46 | 14 | upperShiftRate = 0.0; |
YusukeWakuta | 49:87af91607b46 | 15 | lowerShiftRate = 0.0; |
YusukeWakuta | 49:87af91607b46 | 16 | } |
YusukeWakuta | 49:87af91607b46 | 17 | |
YusukeWakuta | 49:87af91607b46 | 18 | void InputCalc::setNeutral(float input) |
YusukeWakuta | 49:87af91607b46 | 19 | { |
YusukeWakuta | 49:87af91607b46 | 20 | float sum = 0.0; |
YusukeWakuta | 49:87af91607b46 | 21 | for (int i = 0; i < NEUTRAL_SUM_NUM ; i++) { |
YusukeWakuta | 49:87af91607b46 | 22 | sum += input; |
YusukeWakuta | 49:87af91607b46 | 23 | } |
YusukeWakuta | 49:87af91607b46 | 24 | rawNeu = sum / NEUTRAL_SUM_NUM; |
YusukeWakuta | 49:87af91607b46 | 25 | } |
YusukeWakuta | 49:87af91607b46 | 26 | |
YusukeWakuta | 49:87af91607b46 | 27 | void InputCalc::setMaxAndMin(double input) |
YusukeWakuta | 49:87af91607b46 | 28 | { |
YusukeWakuta | 49:87af91607b46 | 29 | if (input > rawMax) |
YusukeWakuta | 49:87af91607b46 | 30 | rawMax = input; |
YusukeWakuta | 49:87af91607b46 | 31 | else if (input < rawMin) |
YusukeWakuta | 49:87af91607b46 | 32 | rawMin = input; |
YusukeWakuta | 49:87af91607b46 | 33 | } |
YusukeWakuta | 49:87af91607b46 | 34 | |
YusukeWakuta | 49:87af91607b46 | 35 | //入力値の中央値を0とし、その分最大値と最小値をシフトします |
YusukeWakuta | 49:87af91607b46 | 36 | void InputCalc::shift2ZeroNeu() |
YusukeWakuta | 49:87af91607b46 | 37 | { |
YusukeWakuta | 51:8a579a19a4ff | 38 | shiftedMax = rawMax - rawNeu; //初期値を0へとシフトするので、最大値と最小値から中央値を引けばシフト各値もシフトできる |
YusukeWakuta | 49:87af91607b46 | 39 | shiftedMin = rawMin - rawNeu; |
YusukeWakuta | 49:87af91607b46 | 40 | } |
YusukeWakuta | 49:87af91607b46 | 41 | |
YusukeWakuta | 52:a9d23be503c6 | 42 | float InputCalc::GetUpperAndLowerRate() |
YusukeWakuta | 52:a9d23be503c6 | 43 | { |
YusukeWakuta | 52:a9d23be503c6 | 44 | return shiftedMax / (-shiftedMin); |
YusukeWakuta | 52:a9d23be503c6 | 45 | } |
YusukeWakuta | 52:a9d23be503c6 | 46 | |
YusukeWakuta | 49:87af91607b46 | 47 | //シフトした後の値を-1.0~1.0へと正規化するための比率を取得します |
YusukeWakuta | 49:87af91607b46 | 48 | void InputCalc::GetNormRate() |
YusukeWakuta | 49:87af91607b46 | 49 | { |
YusukeWakuta | 49:87af91607b46 | 50 | upperShiftRate = 1.0 / shiftedMax; |
YusukeWakuta | 49:87af91607b46 | 51 | lowerShiftRate = -1.0 / shiftedMin; |
YusukeWakuta | 49:87af91607b46 | 52 | } |
YusukeWakuta | 49:87af91607b46 | 53 | |
YusukeWakuta | 49:87af91607b46 | 54 | //値を-1.0~1.0へと正規化します |
YusukeWakuta | 52:a9d23be503c6 | 55 | void InputCalc::NormValue(float *input) |
YusukeWakuta | 49:87af91607b46 | 56 | { |
YusukeWakuta | 52:a9d23be503c6 | 57 | if(*input< 0.0) |
YusukeWakuta | 52:a9d23be503c6 | 58 | *input *= GetUpperAndLowerRate(); |
YusukeWakuta | 49:87af91607b46 | 59 | *input -= rawNeu; |
YusukeWakuta | 49:87af91607b46 | 60 | if (*input > 0.0) |
YusukeWakuta | 49:87af91607b46 | 61 | *input *= upperShiftRate; |
YusukeWakuta | 49:87af91607b46 | 62 | else if (*input < 0.0) |
YusukeWakuta | 49:87af91607b46 | 63 | *input *= lowerShiftRate; |
YusukeWakuta | 49:87af91607b46 | 64 | } |
YusukeWakuta | 49:87af91607b46 | 65 | |
YusukeWakuta | 49:87af91607b46 | 66 | //初期値の分、入力値を0に寄せて、拡大します。 |
YusukeWakuta | 52:a9d23be503c6 | 67 | void InputCalc::WidenNeutral(float *input) |
YusukeWakuta | 49:87af91607b46 | 68 | { |
YusukeWakuta | 49:87af91607b46 | 69 | if (-NEUWIDTH < *input&& *input < NEUWIDTH) |
YusukeWakuta | 49:87af91607b46 | 70 | *input = 0; |
YusukeWakuta | 49:87af91607b46 | 71 | else if (*input < -NEUWIDTH) { |
YusukeWakuta | 49:87af91607b46 | 72 | *input += NEUWIDTH; |
YusukeWakuta | 49:87af91607b46 | 73 | *input *= WIDENRATE; |
YusukeWakuta | 49:87af91607b46 | 74 | } else if (NEUWIDTH < *input) { |
YusukeWakuta | 49:87af91607b46 | 75 | *input -= NEUWIDTH; |
YusukeWakuta | 49:87af91607b46 | 76 | *input *= WIDENRATE; |
YusukeWakuta | 49:87af91607b46 | 77 | } |
YusukeWakuta | 49:87af91607b46 | 78 | } |
YusukeWakuta | 49:87af91607b46 | 79 | |
YusukeWakuta | 51:8a579a19a4ff | 80 | //範囲外の値を下限値と上限値に置き換える |
YusukeWakuta | 52:a9d23be503c6 | 81 | float InputCalc::CutInvalidInput(float input) |
YusukeWakuta | 49:87af91607b46 | 82 | { |
YusukeWakuta | 49:87af91607b46 | 83 | if(input > NORMMAX) |
YusukeWakuta | 49:87af91607b46 | 84 | input = NORMMAX; |
YusukeWakuta | 49:87af91607b46 | 85 | else if(input < NORMMIN) |
YusukeWakuta | 49:87af91607b46 | 86 | input = NORMMIN; |
YusukeWakuta | 52:a9d23be503c6 | 87 | return input; |
YusukeWakuta | 49:87af91607b46 | 88 | } |
YusukeWakuta | 49:87af91607b46 | 89 | |
YusukeWakuta | 52:a9d23be503c6 | 90 | float InputCalc::Processing(float input) |
YusukeWakuta | 49:87af91607b46 | 91 | { |
YusukeWakuta | 49:87af91607b46 | 92 | setMaxAndMin(input); |
YusukeWakuta | 49:87af91607b46 | 93 | shift2ZeroNeu(); |
YusukeWakuta | 49:87af91607b46 | 94 | GetNormRate(); |
YusukeWakuta | 49:87af91607b46 | 95 | NormValue(&input); |
YusukeWakuta | 49:87af91607b46 | 96 | WidenNeutral(&input); |
YusukeWakuta | 49:87af91607b46 | 97 | return CutInvalidInput(input); |
YusukeWakuta | 49:87af91607b46 | 98 | } |