モーターを回す程度のライブラリ
motor_driver.hpp@0:eb65057bbb64, 2017-02-18 (annotated)
- Committer:
- Gaku0606
- Date:
- Sat Feb 18 12:26:54 2017 +0000
- Revision:
- 0:eb65057bbb64
- Child:
- 1:dd7259ae444a
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gaku0606 | 0:eb65057bbb64 | 1 | #ifndef _MOTOR_DRIVER_HPP_ |
Gaku0606 | 0:eb65057bbb64 | 2 | #define _MOTOR_DRIVER_HPP_ |
Gaku0606 | 0:eb65057bbb64 | 3 | |
Gaku0606 | 0:eb65057bbb64 | 4 | #include "mbed.h" |
Gaku0606 | 0:eb65057bbb64 | 5 | |
Gaku0606 | 0:eb65057bbb64 | 6 | class motor_driver{ |
Gaku0606 | 0:eb65057bbb64 | 7 | |
Gaku0606 | 0:eb65057bbb64 | 8 | public: |
Gaku0606 | 0:eb65057bbb64 | 9 | motor_driver(PinName r_f, PinName r_b, PinName l_f, PinName l_b, PinName r_st, PinName l_st); |
Gaku0606 | 0:eb65057bbb64 | 10 | |
Gaku0606 | 0:eb65057bbb64 | 11 | private: |
Gaku0606 | 0:eb65057bbb64 | 12 | |
Gaku0606 | 0:eb65057bbb64 | 13 | PwmOut RF; |
Gaku0606 | 0:eb65057bbb64 | 14 | PwmOut RB; |
Gaku0606 | 0:eb65057bbb64 | 15 | PwmOut LF; |
Gaku0606 | 0:eb65057bbb64 | 16 | PwmOut LB; |
Gaku0606 | 0:eb65057bbb64 | 17 | DigitalOut Rst; |
Gaku0606 | 0:eb65057bbb64 | 18 | DigitalOut Lst; |
Gaku0606 | 0:eb65057bbb64 | 19 | |
Gaku0606 | 0:eb65057bbb64 | 20 | public: |
Gaku0606 | 0:eb65057bbb64 | 21 | /** |
Gaku0606 | 0:eb65057bbb64 | 22 | * @bref 引数のデューティー比で前進する. |
Gaku0606 | 0:eb65057bbb64 | 23 | * @param duty デューティー比 |
Gaku0606 | 0:eb65057bbb64 | 24 | * @note 引数なしで全力前進 |
Gaku0606 | 0:eb65057bbb64 | 25 | */ |
Gaku0606 | 0:eb65057bbb64 | 26 | void forward(float duty = 1.0f); |
Gaku0606 | 0:eb65057bbb64 | 27 | |
Gaku0606 | 0:eb65057bbb64 | 28 | /** |
Gaku0606 | 0:eb65057bbb64 | 29 | * @bref 引数のデューティー比で後進する. |
Gaku0606 | 0:eb65057bbb64 | 30 | * @param duty デューティー比 |
Gaku0606 | 0:eb65057bbb64 | 31 | * @note 引数無しで全力後進 |
Gaku0606 | 0:eb65057bbb64 | 32 | */ |
Gaku0606 | 0:eb65057bbb64 | 33 | void back(float duty = 1.0f); |
Gaku0606 | 0:eb65057bbb64 | 34 | |
Gaku0606 | 0:eb65057bbb64 | 35 | /** |
Gaku0606 | 0:eb65057bbb64 | 36 | * @bref 指定した比率で左右のタイヤを回し,右折します. |
Gaku0606 | 0:eb65057bbb64 | 37 | * @param 左右のタイヤへのpwm比 |
Gaku0606 | 0:eb65057bbb64 | 38 | */ |
Gaku0606 | 0:eb65057bbb64 | 39 | void right(float percentage = 0.5f); |
Gaku0606 | 0:eb65057bbb64 | 40 | |
Gaku0606 | 0:eb65057bbb64 | 41 | /** |
Gaku0606 | 0:eb65057bbb64 | 42 | * @bref 指定した比率で左右のタイヤを回し,左折します. |
Gaku0606 | 0:eb65057bbb64 | 43 | * @param 左右のタイヤへのpwm比 |
Gaku0606 | 0:eb65057bbb64 | 44 | */ |
Gaku0606 | 0:eb65057bbb64 | 45 | void left(float percentage = 0.5f); |
Gaku0606 | 0:eb65057bbb64 | 46 | |
Gaku0606 | 0:eb65057bbb64 | 47 | /** |
Gaku0606 | 0:eb65057bbb64 | 48 | * @bref 指定したデューティー比で右に超信地旋回する. |
Gaku0606 | 0:eb65057bbb64 | 49 | * @param duty デューティー比 |
Gaku0606 | 0:eb65057bbb64 | 50 | */ |
Gaku0606 | 0:eb65057bbb64 | 51 | void pivot_right(float duty = 1.0f); |
Gaku0606 | 0:eb65057bbb64 | 52 | |
Gaku0606 | 0:eb65057bbb64 | 53 | /** |
Gaku0606 | 0:eb65057bbb64 | 54 | * @bref 指定したデューティー比で左に超信地旋回する. |
Gaku0606 | 0:eb65057bbb64 | 55 | * @param duty デューティー比 |
Gaku0606 | 0:eb65057bbb64 | 56 | */ |
Gaku0606 | 0:eb65057bbb64 | 57 | void pivot_left(float duty = 1.0f); |
Gaku0606 | 0:eb65057bbb64 | 58 | |
Gaku0606 | 0:eb65057bbb64 | 59 | /** |
Gaku0606 | 0:eb65057bbb64 | 60 | * @bref 左右のモーターを停止させる. |
Gaku0606 | 0:eb65057bbb64 | 61 | * @note ブレーキではない |
Gaku0606 | 0:eb65057bbb64 | 62 | */ |
Gaku0606 | 0:eb65057bbb64 | 63 | void stop(); |
Gaku0606 | 0:eb65057bbb64 | 64 | |
Gaku0606 | 0:eb65057bbb64 | 65 | /** |
Gaku0606 | 0:eb65057bbb64 | 66 | * @bref モータードライバーを省電力待機モードにする. |
Gaku0606 | 0:eb65057bbb64 | 67 | * @note 基本的に走っていないときはこれを実行したほうがよいかと |
Gaku0606 | 0:eb65057bbb64 | 68 | */ |
Gaku0606 | 0:eb65057bbb64 | 69 | void standBy(); |
Gaku0606 | 0:eb65057bbb64 | 70 | |
Gaku0606 | 0:eb65057bbb64 | 71 | /** |
Gaku0606 | 0:eb65057bbb64 | 72 | * @bref ブレーキをかける |
Gaku0606 | 0:eb65057bbb64 | 73 | * @note モーター的にはあまりよくないようです |
Gaku0606 | 0:eb65057bbb64 | 74 | */ |
Gaku0606 | 0:eb65057bbb64 | 75 | void brake(); |
Gaku0606 | 0:eb65057bbb64 | 76 | }; |
Gaku0606 | 0:eb65057bbb64 | 77 | |
Gaku0606 | 0:eb65057bbb64 | 78 | motor_driver::motor_driver(PinName r_f, PinName r_b, PinName l_f, PinName l_b, PinName r_st, PinName l_st) : RF(r_f), RB(r_b), LF(l_f), LB(l_b), Rst(r_st), Lst(l_st){ |
Gaku0606 | 0:eb65057bbb64 | 79 | |
Gaku0606 | 0:eb65057bbb64 | 80 | RF = 0; |
Gaku0606 | 0:eb65057bbb64 | 81 | RB = 0; |
Gaku0606 | 0:eb65057bbb64 | 82 | LF = 0; |
Gaku0606 | 0:eb65057bbb64 | 83 | LB = 0; |
Gaku0606 | 0:eb65057bbb64 | 84 | Rst = 0; |
Gaku0606 | 0:eb65057bbb64 | 85 | Lst = 0; |
Gaku0606 | 0:eb65057bbb64 | 86 | } |
Gaku0606 | 0:eb65057bbb64 | 87 | |
Gaku0606 | 0:eb65057bbb64 | 88 | inline void motor_driver::forward(float duty){ |
Gaku0606 | 0:eb65057bbb64 | 89 | RF = duty; |
Gaku0606 | 0:eb65057bbb64 | 90 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 91 | LF = duty; |
Gaku0606 | 0:eb65057bbb64 | 92 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 93 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 94 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 95 | return; |
Gaku0606 | 0:eb65057bbb64 | 96 | } |
Gaku0606 | 0:eb65057bbb64 | 97 | |
Gaku0606 | 0:eb65057bbb64 | 98 | inline void motor_driver::back(float duty){ |
Gaku0606 | 0:eb65057bbb64 | 99 | RF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 100 | RB = duty; |
Gaku0606 | 0:eb65057bbb64 | 101 | LF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 102 | LB = duty; |
Gaku0606 | 0:eb65057bbb64 | 103 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 104 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 105 | return; |
Gaku0606 | 0:eb65057bbb64 | 106 | } |
Gaku0606 | 0:eb65057bbb64 | 107 | |
Gaku0606 | 0:eb65057bbb64 | 108 | inline void motor_driver::right(float percentage){ |
Gaku0606 | 0:eb65057bbb64 | 109 | //右を弱める |
Gaku0606 | 0:eb65057bbb64 | 110 | RF = percentage; |
Gaku0606 | 0:eb65057bbb64 | 111 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 112 | LF = 1.0f; |
Gaku0606 | 0:eb65057bbb64 | 113 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 114 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 115 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 116 | return; |
Gaku0606 | 0:eb65057bbb64 | 117 | } |
Gaku0606 | 0:eb65057bbb64 | 118 | |
Gaku0606 | 0:eb65057bbb64 | 119 | inline void motor_driver::left(float percentage){ |
Gaku0606 | 0:eb65057bbb64 | 120 | //左を弱める |
Gaku0606 | 0:eb65057bbb64 | 121 | RF = 1.0f; |
Gaku0606 | 0:eb65057bbb64 | 122 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 123 | LF = percentage; |
Gaku0606 | 0:eb65057bbb64 | 124 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 125 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 126 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 127 | return; |
Gaku0606 | 0:eb65057bbb64 | 128 | } |
Gaku0606 | 0:eb65057bbb64 | 129 | |
Gaku0606 | 0:eb65057bbb64 | 130 | inline void motor_driver::pivot_right(float duty){ |
Gaku0606 | 0:eb65057bbb64 | 131 | RF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 132 | RB = duty; |
Gaku0606 | 0:eb65057bbb64 | 133 | LF = duty; |
Gaku0606 | 0:eb65057bbb64 | 134 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 135 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 136 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 137 | return; |
Gaku0606 | 0:eb65057bbb64 | 138 | } |
Gaku0606 | 0:eb65057bbb64 | 139 | |
Gaku0606 | 0:eb65057bbb64 | 140 | inline void motor_driver::pivot_left(float duty){ |
Gaku0606 | 0:eb65057bbb64 | 141 | RF = duty; |
Gaku0606 | 0:eb65057bbb64 | 142 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 143 | LF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 144 | LB = duty; |
Gaku0606 | 0:eb65057bbb64 | 145 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 146 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 147 | return; |
Gaku0606 | 0:eb65057bbb64 | 148 | } |
Gaku0606 | 0:eb65057bbb64 | 149 | |
Gaku0606 | 0:eb65057bbb64 | 150 | inline void motor_driver::stop(){ |
Gaku0606 | 0:eb65057bbb64 | 151 | RF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 152 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 153 | LF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 154 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 155 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 156 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 157 | return; |
Gaku0606 | 0:eb65057bbb64 | 158 | } |
Gaku0606 | 0:eb65057bbb64 | 159 | |
Gaku0606 | 0:eb65057bbb64 | 160 | inline void motor_driver::standBy(){ |
Gaku0606 | 0:eb65057bbb64 | 161 | Rst = 0; |
Gaku0606 | 0:eb65057bbb64 | 162 | Lst = 0; |
Gaku0606 | 0:eb65057bbb64 | 163 | RF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 164 | RB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 165 | LF = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 166 | LB = 0.0f; |
Gaku0606 | 0:eb65057bbb64 | 167 | return; |
Gaku0606 | 0:eb65057bbb64 | 168 | } |
Gaku0606 | 0:eb65057bbb64 | 169 | |
Gaku0606 | 0:eb65057bbb64 | 170 | inline void motor_driver::brake(){ |
Gaku0606 | 0:eb65057bbb64 | 171 | RF = 1; |
Gaku0606 | 0:eb65057bbb64 | 172 | RB = 1; |
Gaku0606 | 0:eb65057bbb64 | 173 | LF = 1; |
Gaku0606 | 0:eb65057bbb64 | 174 | LB = 1; |
Gaku0606 | 0:eb65057bbb64 | 175 | Rst = 1; |
Gaku0606 | 0:eb65057bbb64 | 176 | Lst = 1; |
Gaku0606 | 0:eb65057bbb64 | 177 | } |
Gaku0606 | 0:eb65057bbb64 | 178 | #endif |
Gaku0606 | 0:eb65057bbb64 | 179 |