DRV8301を使用してブラシレスモーター(BLDC)をパパッと回すために作りました。DRV8301の全機能は使用できていません。会社・学校などで無理やり実験の担当を押し付けられた際にどうぞ。

Dependencies:   DRV8301CTRL mbed

Committer:
Yajirushi
Date:
Wed Aug 31 17:08:53 2016 +0000
Revision:
1:27f8a06ce16d
Parent:
0:a8f76586ab79
nothing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yajirushi 0:a8f76586ab79 1 #include "mbed.h"
Yajirushi 0:a8f76586ab79 2 #include "DRV8301CTRL.h"
Yajirushi 0:a8f76586ab79 3
Yajirushi 0:a8f76586ab79 4 //ユーザーボタン
Yajirushi 0:a8f76586ab79 5 DigitalIn btn(USER_BUTTON);
Yajirushi 0:a8f76586ab79 6
Yajirushi 0:a8f76586ab79 7 //シリアル通信
Yajirushi 0:a8f76586ab79 8 Serial pc(USBTX, USBRX);
Yajirushi 0:a8f76586ab79 9
Yajirushi 0:a8f76586ab79 10 //SPI通信
Yajirushi 0:a8f76586ab79 11 SPI spi(D11, D12, D13);
Yajirushi 0:a8f76586ab79 12
Yajirushi 0:a8f76586ab79 13 //DRV8301コントロールクラス
Yajirushi 0:a8f76586ab79 14 drv8301ctrl DRV(&pc, &spi, D10, D9);
Yajirushi 0:a8f76586ab79 15 //drv8301ctrl DRV(&spi, D10, D9);
Yajirushi 0:a8f76586ab79 16
Yajirushi 0:a8f76586ab79 17 //PWM(3線式)
Yajirushi 0:a8f76586ab79 18 PwmOut m1a(D7);
Yajirushi 0:a8f76586ab79 19 PwmOut m1b(D8);
Yajirushi 0:a8f76586ab79 20 PwmOut m1c(D2);
Yajirushi 0:a8f76586ab79 21
Yajirushi 0:a8f76586ab79 22 //アナログ読み取り
Yajirushi 0:a8f76586ab79 23 AnalogIn shunt1(A0);
Yajirushi 0:a8f76586ab79 24 AnalogIn shunt2(A1);
Yajirushi 0:a8f76586ab79 25 AnalogIn vbatt(A2);
Yajirushi 0:a8f76586ab79 26 AnalogIn rvar(A3);
Yajirushi 0:a8f76586ab79 27
Yajirushi 0:a8f76586ab79 28 //PWM疑似正弦波テーブル
Yajirushi 0:a8f76586ab79 29 unsigned char step1A = 0;
Yajirushi 0:a8f76586ab79 30 unsigned char step1B = 64;
Yajirushi 0:a8f76586ab79 31 unsigned char step1C = 128;
Yajirushi 0:a8f76586ab79 32 const unsigned int sinwave[192] = {
Yajirushi 0:a8f76586ab79 33 17, 15, 13, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 0,
Yajirushi 0:a8f76586ab79 34 0, 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13, 15,
Yajirushi 0:a8f76586ab79 35 17, 19, 22, 24, 27, 29, 32, 35, 37, 40, 44, 47, 50, 53, 57, 60,
Yajirushi 0:a8f76586ab79 36 64, 67, 71, 75, 79, 82, 86, 90, 94, 98,102,106,110,115,119,123,
Yajirushi 0:a8f76586ab79 37 127,131,135,139,144,148,152,156,160,164,168,172,175,179,183,187,
Yajirushi 0:a8f76586ab79 38 190,194,197,201,204,207,210,214,217,219,222,225,227,230,232,235,
Yajirushi 0:a8f76586ab79 39 237,239,241,242,244,246,247,248,249,250,251,252,253,253,253,254,
Yajirushi 0:a8f76586ab79 40 254,254,253,253,253,252,251,250,249,248,247,246,244,242,241,239,
Yajirushi 0:a8f76586ab79 41 237,235,232,230,227,225,222,219,217,214,210,207,204,201,197,194,
Yajirushi 0:a8f76586ab79 42 190,187,183,179,175,172,168,164,160,156,152,148,144,139,135,131,
Yajirushi 0:a8f76586ab79 43 127,123,119,115,110,106,102, 98, 94, 90, 86, 82, 79, 75, 71, 67,
Yajirushi 0:a8f76586ab79 44 64, 60, 57, 53, 50, 47, 44, 40, 37, 35, 32, 29, 27, 24, 22, 19
Yajirushi 0:a8f76586ab79 45 };
Yajirushi 0:a8f76586ab79 46
Yajirushi 0:a8f76586ab79 47 //PWM周期(4us = 16KHz)
Yajirushi 0:a8f76586ab79 48 unsigned int period_us = 4;
Yajirushi 0:a8f76586ab79 49
Yajirushi 0:a8f76586ab79 50 //PWM待ち基準(マイクロ秒)(待ち基準*rvar=PWM待ち時間)
Yajirushi 0:a8f76586ab79 51 int pwmWait_us = 600;
Yajirushi 0:a8f76586ab79 52 float pwmWait_cf = 1.0f;
Yajirushi 0:a8f76586ab79 53 float pwmWait_cf_cf = 0.000001f;
Yajirushi 0:a8f76586ab79 54
Yajirushi 0:a8f76586ab79 55 //タイマー
Yajirushi 0:a8f76586ab79 56 Ticker pc_timer;
Yajirushi 0:a8f76586ab79 57
Yajirushi 0:a8f76586ab79 58 //タイマー関数
Yajirushi 0:a8f76586ab79 59 void out_pc1(){
Yajirushi 0:a8f76586ab79 60 //pc.printf(
Yajirushi 0:a8f76586ab79 61 // "DutyA=%f,DutyB=%f,DutyC=%f,speed=%f,shunt1=%f,shunt2=%f,vbatt=%f\r\n",
Yajirushi 0:a8f76586ab79 62 // m1a.read(), m1b.read(), m1c.read(), rvar.read(), shunt1.read()*3.3f, shunt2.read()*3.3f, vbatt.read()*61.8f
Yajirushi 0:a8f76586ab79 63 //);
Yajirushi 0:a8f76586ab79 64 }
Yajirushi 0:a8f76586ab79 65 void out_pc2(){
Yajirushi 0:a8f76586ab79 66 //pc.printf(
Yajirushi 0:a8f76586ab79 67 // "DutyA=%f,DutyB=%f,DutyC=%f,pwmWait_us=%05d,shunt1=%f,shunt2=%f,vbatt=%f\r\n",
Yajirushi 0:a8f76586ab79 68 // m1a.read(), m1b.read(), m1c.read(), pwmWait_us, shunt1.read()*3.3f, shunt2.read()*3.3f, vbatt.read()*61.8f
Yajirushi 0:a8f76586ab79 69 //);
Yajirushi 0:a8f76586ab79 70 }
Yajirushi 0:a8f76586ab79 71 void motor_run1(){
Yajirushi 0:a8f76586ab79 72 step1A = (step1A+1) % 192;
Yajirushi 0:a8f76586ab79 73 step1B = (step1B+1) % 192;
Yajirushi 0:a8f76586ab79 74 step1C = (step1C+1) % 192;
Yajirushi 0:a8f76586ab79 75 m1a.write((float)(sinwave[step1A] / 255.0f));
Yajirushi 0:a8f76586ab79 76 m1b.write((float)(sinwave[step1B] / 255.0f));
Yajirushi 0:a8f76586ab79 77 m1c.write((float)(sinwave[step1C] / 255.0f));
Yajirushi 0:a8f76586ab79 78 }
Yajirushi 0:a8f76586ab79 79 void motor_run2(){
Yajirushi 0:a8f76586ab79 80 step1C = (step1C == 0) ? step1C = 191 : (step1C-1) % 192;
Yajirushi 0:a8f76586ab79 81 step1B = (step1B == 0) ? step1B = 191 : (step1B-1) % 192;
Yajirushi 0:a8f76586ab79 82 step1A = (step1A == 0) ? step1A = 191 : (step1A-1) % 192;
Yajirushi 0:a8f76586ab79 83 m1c.write((float)(sinwave[step1C] / 255.0f));
Yajirushi 0:a8f76586ab79 84 m1b.write((float)(sinwave[step1B] / 255.0f));
Yajirushi 0:a8f76586ab79 85 m1a.write((float)(sinwave[step1A] / 255.0f));
Yajirushi 0:a8f76586ab79 86 }
Yajirushi 0:a8f76586ab79 87
Yajirushi 0:a8f76586ab79 88 //メイン
Yajirushi 0:a8f76586ab79 89 int main(){
Yajirushi 0:a8f76586ab79 90
Yajirushi 0:a8f76586ab79 91 //シリアル出力
Yajirushi 0:a8f76586ab79 92 pc.printf("mbed ready...\r\n");
Yajirushi 0:a8f76586ab79 93 m1a.write(0.0);
Yajirushi 0:a8f76586ab79 94 m1b.write(0.0);
Yajirushi 0:a8f76586ab79 95 m1c.write(0.0);
Yajirushi 0:a8f76586ab79 96
Yajirushi 0:a8f76586ab79 97 //ボタンが押されるまで待ち続ける
Yajirushi 0:a8f76586ab79 98 while(btn);
Yajirushi 0:a8f76586ab79 99
Yajirushi 0:a8f76586ab79 100 //ドライバ初期化
Yajirushi 0:a8f76586ab79 101 pc.printf("--------init drv8301...\r\n");
Yajirushi 0:a8f76586ab79 102 DRV.init();
Yajirushi 0:a8f76586ab79 103
Yajirushi 0:a8f76586ab79 104 //ドライバの状態を読み取る
Yajirushi 0:a8f76586ab79 105 pc.printf("--------read status(before settings) drv8301...\r\n");
Yajirushi 0:a8f76586ab79 106 DRV.readStatus1();
Yajirushi 0:a8f76586ab79 107 DRV.readStatus2();
Yajirushi 0:a8f76586ab79 108 DRV.readCtrl1();
Yajirushi 0:a8f76586ab79 109 DRV.readCtrl2();
Yajirushi 0:a8f76586ab79 110
Yajirushi 0:a8f76586ab79 111 //シャントアンプのゲインを80V/Vに
Yajirushi 0:a8f76586ab79 112 pc.printf("--------set gain 80v/v...\r\n");
Yajirushi 0:a8f76586ab79 113 DRV.readCtrl2(); //現在の値を内部変数に読み込み
Yajirushi 0:a8f76586ab79 114 DRV.setGAIN(3); //内部変数に値をセット(0x11)
Yajirushi 0:a8f76586ab79 115 DRV.writeCtrl2(); //書き込み(GAINはコントロールレジスタ2)
Yajirushi 0:a8f76586ab79 116
Yajirushi 0:a8f76586ab79 117 //ドライバの状態を読み取る
Yajirushi 0:a8f76586ab79 118 pc.printf("--------read status(after settings) drv8301...\r\n");
Yajirushi 0:a8f76586ab79 119 DRV.readStatus1();
Yajirushi 0:a8f76586ab79 120 DRV.readStatus2();
Yajirushi 0:a8f76586ab79 121 DRV.readCtrl1();
Yajirushi 0:a8f76586ab79 122 DRV.readCtrl2();
Yajirushi 0:a8f76586ab79 123
Yajirushi 0:a8f76586ab79 124 //set PWM period
Yajirushi 0:a8f76586ab79 125 pc.printf("--------Set PWM period = %d[us](%f [Hz])...\r\n", period_us, (float)(1e6 / period_us));
Yajirushi 0:a8f76586ab79 126 m1a.period_us(period_us);
Yajirushi 0:a8f76586ab79 127 m1b.period_us(period_us);
Yajirushi 0:a8f76586ab79 128 m1c.period_us(period_us);
Yajirushi 0:a8f76586ab79 129
Yajirushi 0:a8f76586ab79 130 //ボタンが押されるまで待ち続ける
Yajirushi 0:a8f76586ab79 131 pc.printf("--------WAITING PUSH BUTTON...\r\n");
Yajirushi 0:a8f76586ab79 132 while(btn);
Yajirushi 0:a8f76586ab79 133 wait(3.0);
Yajirushi 0:a8f76586ab79 134
Yajirushi 0:a8f76586ab79 135 //PCへの送出開始
Yajirushi 0:a8f76586ab79 136 pc.printf("--------Running motor!! until push user button...\r\n");
Yajirushi 0:a8f76586ab79 137 pc_timer.attach(&out_pc1, 0.15f);
Yajirushi 0:a8f76586ab79 138
Yajirushi 0:a8f76586ab79 139 //PWM送出(ボタンが押されるまで)
Yajirushi 0:a8f76586ab79 140 do{
Yajirushi 0:a8f76586ab79 141 motor_run1();
Yajirushi 0:a8f76586ab79 142 wait_us((int)(pwmWait_us * (1.0f - rvar.read())));
Yajirushi 0:a8f76586ab79 143 }while(btn);
Yajirushi 0:a8f76586ab79 144
Yajirushi 0:a8f76586ab79 145 //PCへの送出停止
Yajirushi 0:a8f76586ab79 146 pc_timer.detach();
Yajirushi 0:a8f76586ab79 147 //PWM送出停止
Yajirushi 0:a8f76586ab79 148 pc.printf("--------STOP...\r\n");
Yajirushi 0:a8f76586ab79 149 m1a.write(0.0);
Yajirushi 0:a8f76586ab79 150 m1b.write(0.0);
Yajirushi 0:a8f76586ab79 151 m1c.write(0.0);
Yajirushi 0:a8f76586ab79 152 wait(3.0);
Yajirushi 0:a8f76586ab79 153
Yajirushi 0:a8f76586ab79 154 //ボタンが押されるまで待ち続ける
Yajirushi 0:a8f76586ab79 155 pc.printf("--------WAITING PUSH BUTTON...\r\n");
Yajirushi 0:a8f76586ab79 156 while(btn);
Yajirushi 0:a8f76586ab79 157 wait(3.0);
Yajirushi 0:a8f76586ab79 158
Yajirushi 0:a8f76586ab79 159 //PCへの送出開始
Yajirushi 0:a8f76586ab79 160 pc.printf("--------Running motor!!(reverse) until push user button...\r\n");
Yajirushi 0:a8f76586ab79 161 pc_timer.attach(&out_pc1, 0.15f);
Yajirushi 0:a8f76586ab79 162
Yajirushi 0:a8f76586ab79 163 //PWM送出(ボタンが押されるまで)
Yajirushi 0:a8f76586ab79 164 do{
Yajirushi 0:a8f76586ab79 165 motor_run2();
Yajirushi 0:a8f76586ab79 166 wait_us((int)(pwmWait_us * (1.0f - rvar.read())));
Yajirushi 0:a8f76586ab79 167 }while(btn);
Yajirushi 0:a8f76586ab79 168
Yajirushi 0:a8f76586ab79 169 //PCへの送出停止
Yajirushi 0:a8f76586ab79 170 pc_timer.detach();
Yajirushi 0:a8f76586ab79 171 //PWM送出停止
Yajirushi 0:a8f76586ab79 172 pc.printf("--------STOP...\r\n");
Yajirushi 0:a8f76586ab79 173 m1a.write(0.0);
Yajirushi 0:a8f76586ab79 174 m1b.write(0.0);
Yajirushi 0:a8f76586ab79 175 m1c.write(0.0);
Yajirushi 0:a8f76586ab79 176 wait(3.0);
Yajirushi 0:a8f76586ab79 177
Yajirushi 0:a8f76586ab79 178
Yajirushi 0:a8f76586ab79 179 //ドライバの状態を読み取る
Yajirushi 0:a8f76586ab79 180 pc.printf("--------read status drv8301...\r\n");
Yajirushi 0:a8f76586ab79 181 DRV.readStatus1();
Yajirushi 0:a8f76586ab79 182 DRV.readStatus2();
Yajirushi 0:a8f76586ab79 183 DRV.readCtrl1();
Yajirushi 0:a8f76586ab79 184 DRV.readCtrl2();
Yajirushi 0:a8f76586ab79 185
Yajirushi 0:a8f76586ab79 186 //ボタンが押されるまで待ち続ける
Yajirushi 0:a8f76586ab79 187 pc.printf("----WAITING PUSH BUTTON !!(before automatic acc/dec sequence)!! ...\r\n");
Yajirushi 0:a8f76586ab79 188 while(btn);
Yajirushi 0:a8f76586ab79 189 wait(3.0);
Yajirushi 0:a8f76586ab79 190
Yajirushi 0:a8f76586ab79 191 //PWM送出(段々加速していく)
Yajirushi 0:a8f76586ab79 192 pc.printf("--------Running motor!!(automatic acceleration/deceleration)\r\n");
Yajirushi 0:a8f76586ab79 193 //PCへの送出開始
Yajirushi 0:a8f76586ab79 194 pc_timer.attach(&out_pc2, 0.15f);
Yajirushi 0:a8f76586ab79 195 do{
Yajirushi 0:a8f76586ab79 196 motor_run1();
Yajirushi 0:a8f76586ab79 197 wait_us((int)(pwmWait_us *= (pwmWait_cf -= pwmWait_cf_cf)));
Yajirushi 0:a8f76586ab79 198 }while(pwmWait_us > 2);
Yajirushi 0:a8f76586ab79 199
Yajirushi 0:a8f76586ab79 200 pwmWait_us = (pwmWait_us < 2) ? 2 : pwmWait_us;
Yajirushi 0:a8f76586ab79 201 pwmWait_cf = (pwmWait_cf < 1.0f) ? 1.00f : pwmWait_cf;
Yajirushi 0:a8f76586ab79 202 pwmWait_cf_cf = 0.000005f;
Yajirushi 0:a8f76586ab79 203
Yajirushi 0:a8f76586ab79 204 //PWM送出(段々減速していく)
Yajirushi 0:a8f76586ab79 205 do{
Yajirushi 0:a8f76586ab79 206 motor_run1();
Yajirushi 0:a8f76586ab79 207 wait_us((int)(pwmWait_us *= (pwmWait_cf += pwmWait_cf_cf)));
Yajirushi 0:a8f76586ab79 208 }while(pwmWait_us < 300);
Yajirushi 0:a8f76586ab79 209
Yajirushi 0:a8f76586ab79 210 //PCへの送出停止
Yajirushi 0:a8f76586ab79 211 pc_timer.detach();
Yajirushi 0:a8f76586ab79 212 //減速終了
Yajirushi 0:a8f76586ab79 213 pc.printf("--------Running motor!!(end of deceleration)\r\n");
Yajirushi 0:a8f76586ab79 214 wait(2.5);
Yajirushi 0:a8f76586ab79 215
Yajirushi 0:a8f76586ab79 216 //出力停止
Yajirushi 0:a8f76586ab79 217 pc.printf("--------STOP...\r\n");
Yajirushi 0:a8f76586ab79 218 m1a.write(0.0);
Yajirushi 0:a8f76586ab79 219 m1b.write(0.0);
Yajirushi 0:a8f76586ab79 220 m1c.write(0.0);
Yajirushi 0:a8f76586ab79 221
Yajirushi 0:a8f76586ab79 222 //ゲートクローズ
Yajirushi 0:a8f76586ab79 223 DRV.gateDisable();
Yajirushi 0:a8f76586ab79 224
Yajirushi 0:a8f76586ab79 225 //PCへの送出停止
Yajirushi 0:a8f76586ab79 226 pc_timer.detach();
Yajirushi 0:a8f76586ab79 227 }