Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FatFileSystemCpp INA226_abc BLDCmotorDriver_20200821_motacon_ver4
main.cpp
00001 //20200803 Kikkawa モタコン基板(緑色ver.1)での動作確認済み 00002 //BLDC, INA226, CAN 00003 00004 #include "mbed.h" 00005 #include "BLDCmotorDriver.h" 00006 #include "INA226.hpp" 00007 00008 /*定数の設定*/ 00009 #define Vin1_LENGTH 20 //要素数(個数指定用の定数) 00010 #define Cin1_LENGTH 20 //要素数(個数指定用の定数) 00011 #define canSlaveID 0x10 //CAN通信IDを0x10に設定 00012 00013 CAN canSlave(p30, p29); 00014 Timer timer; //回転数計算用タイマ設定 00015 Ticker ticker1; //割り込み設定用 00016 Ticker ticker2; //割り込み設定用 00017 00018 Serial pc(USBTX, USBRX); 00019 BLDCmotorDriver M(p26, p24, p22, p25, p23, p21, p9, p8, p7, LED1); 00020 AnalogIn Pot(p20); 00021 DigitalIn direction(p16); 00022 DigitalIn accel(p15); 00023 00024 DigitalOut led2(LED2); //CAN_check 00025 DigitalOut led3(LED3); //direction_check 00026 DigitalOut led4(LED4); //accel_check 00027 00028 double rpm = 0.0, speed = 0.0; //モータ回転数、速度 00029 float dc = 0.0, dc_limit = 0.0; 00030 int sector = 0; 00031 00032 bool flagPrintf = 0; //main関数でのprintf処理のため 00033 00034 int forSend = 0; //データ送信時に一時的に使用する変数 00035 bool CANsendOK = 0; //CAN受信完了時,セットする(mainでのprintfのため) 00036 CANMessage msgSlave; //CAN送信用 00037 void Handler_canSlaveSend(); 00038 00039 /*INA226関係*/ //GNDGND(G-G):0x80, Vs+Vs+(1-1):0x8A, SDASDA(D-D):0x94, SCLSCL(C-C):0x9E, GNDVs+(G-1):0x82 00040 I2C i2c(p28,p27); 00041 void initINA226(); //INA226の初期設定を行う関数 00042 void INA226_init(); //INA226の初期設定用関数 00043 INA226 VCmonitor(i2c); 00044 unsigned short val; 00045 double V,C; 00046 int count = 1; //INA226動作確認用カウンタ 00047 float data_Vin1[Vin1_LENGTH] = {0.0, 0.0, 0.0, 0.0, 0.0}; //一時的データ格納配列 00048 float data_Cin1[Cin1_LENGTH] = {0.0, 0.0, 0.0, 0.0, 0.0}; //一時的データ格納配列 00049 void get_VCipm(void); //IPMの電圧電流計測関数 00050 00051 void Handler_canSlaveSend() { 00052 forSend = (int)(V/100); 00053 msgSlave.data[0] = forSend / 100; //forSend / 100 = 15あまり43 となり,答えの方がデータ格納される 00054 msgSlave.data[1] = forSend % 100; //forSend / 100 = 15あまり43 となり,あまりの方がデータ格納される 00055 00056 forSend = (int)(C/10); //mAで取得した値を10で割り、char2分割で送り切れるようにする。(例: 測定電流が199.99Aのとき,C=199900.00となるため,forSend=19990となり,199と90で分割することで送信できる) 00057 msgSlave.data[2] = forSend / 100; //forSend / 100 = 5あまり54 となり,答えの方がデータ格納される 00058 msgSlave.data[3] = forSend % 100; //forSend % 100 = 5あまり54 となり,あまりの方がデータ格納される 00059 00060 forSend = (int)(speed*10); //speed * 10 = 856.5だが,intにするため,856が代入される 00061 msgSlave.data[4] = forSend / 100; //forSend / 100 = 8あまり56 となり,8がデータ格納される 00062 msgSlave.data[5] = forSend % 100; //forSend % 100 = 8あまり56 となり,56がデータ格納される 00063 00064 forSend = (int)(dc*100); //dc * 100 = 95 00065 msgSlave.data[6] = forSend; //charの最大値である255以下になるため,そのままデータ格納される 00066 00067 if(canSlave.write(msgSlave)){ //格納したデータを送信する 00068 CANsendOK = 1; 00069 } 00070 } 00071 /* 00072 void Handler_canSlaveRecieve(){ //canMasterから送信要求が来たとき,データ送信するための関数 00073 if( canSlave.read( msgSlave ) ){ //msgに送られたデータが入る 00074 led2 = !led2; 00075 if( msgSlave.id == canSlaveID ){ //IDがcanSlaveIDであれば処理する 00076 Handler_canSlaveSend(); //送信処理を開始する 00077 } 00078 } 00079 } 00080 */ 00081 void INA226_init(){ //INA226初期設定を行う関数 00082 printf("\n\r"); 00083 printf("VCmonitor INA226 TEST Program. (BUILD:[" __DATE__ "/" __TIME__ "])\n\r"); 00084 if(!VCmonitor.isExist()){ pc.printf("VCmonitor NOT FOUND "); } 00085 val = 0; 00086 if(VCmonitor.rawRead(0x00,&val) != 0){ pc.printf("VCmonitor READ ERROR "); } //configResisterの値を読み取れるか確認(通信できてるか確認) 00087 VCmonitor.setConfigResister(); //configurationResisterの設定 00088 printf("VCmonitor Reg 0x00 : 0x%04x\n\r",val); //configResisterの値表示 00089 printf("\n\r"); 00090 VCmonitor.setCurrentCalibration(); //calibrationResisterの設定 00091 } 00092 00093 int main() { 00094 /*ポート初期設定*/ 00095 direction.mode(PullUp); //進行方向スイッチ入力ピンをプルアップに設定 00096 accel.mode(PullUp); //アクセルスイッチ入力ピンをプルアップに設定 00097 00098 /*INA226初期設定*/ 00099 INA226_init(); //INA226の初期設定する関数に飛ぶ 00100 00101 /*CAN初期設定*/ 00102 // canSlave.attach(&Handler_canSlaveRecieve, CAN::RxIrq); //CAN受信割り込みの設定 00103 msgSlave.id = canSlaveID; //CAN送信側(slave)のIDを決定 00104 msgSlave.len = 7; //CAN送信側で送るデータのバイト数 00105 00106 while(true) { 00107 dc = ( Pot.read()-0.5 )*2; //可変抵抗の値からduty比を設定 00108 // dc_limit = -0.0147 * speed * speed + 2.6842 * speed + 29.885; //加速側制限.elsxの式 00109 // dc = dc * dc_limit; 00110 00111 VCmonitor.getVoltage(&V); //IPM電圧(mV)測定 00112 VCmonitor.getCurrent(&C); //IPM電流(mA)測定 00113 00114 if( !accel.read() ){ 00115 led4 = 0; 00116 M.setDutyCycle(0); //duty比の設定 duty比を0にする 00117 } 00118 if( accel.read() ){ 00119 if( !direction.read() ){ //directionスイッチが0のとき 00120 led3 = 0; //処理 00121 } 00122 else if( direction.read() ){ //directionスイッチが1のとき 00123 led3 = 1; 00124 } 00125 led4 = 1; 00126 M.setDutyCycle(dc); //duty比の設定 00127 sector = M.getSector(); 00128 //get_VCipm(); //Vin1とCin1を取得する 00129 //pc.printf("HS_cnt: %d HS_usec %d\n", M.HS_cnt, M.HS_usec); //デバッグ用 00130 } 00131 rpm = 3750 / (M.HS_usec * 0.001); //ミリ秒に直して逆数を3750に掛けるああ //BLDCmotorDriver.cpp(.h)で使っている変数をmainで使う場合は「M.」を付ける。「M」なのはmainの上の方でそう設定してるから。usecはmicro second:マイクロ秒 00132 speed = rpm * 0.10518; //3750とか0.010518は、excelで計算している。ファイル名「」 00133 pc.printf("Duty: %.2f, Sector: %d, %.1f km/h, rpm: %1.2f, V: %.0f, C: %.0f\n\r",dc, sector, speed, rpm, V, C); 00134 Handler_canSlaveSend(); //送信処理を開始する 00135 } 00136 }
Generated on Sun Jul 17 2022 17:00:08 by
1.7.2