20200821_motacon_ver4

Dependencies:   mbed FatFileSystemCpp INA226_abc BLDCmotorDriver_20200902_1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //20200825 Kikkawa モタコン基板(緑色ver.1)での動作確認済み
00002 //BLDC, INA226, CAN
00003 #include "mbed.h"
00004 #include "BLDCmotorDriver.h"
00005 #include "INA226.hpp"
00006 //#include "EthernetPowerControl.h"   //消費電力削減
00007 /*基本ポート設定*/
00008 Serial pc(USBTX, USBRX); // USBシリアルポートのインスタンス
00009 BLDCmotorDriver M(p26, p24, p22, p25, p23, p21, p9, p8, p7, LED1);
00010 DigitalOut led2(LED2);  //CAN_check
00011 DigitalOut led3(LED3);  //direction_check
00012 DigitalOut led4(LED4);  //accel_check
00013 DigitalIn accel(p15);    
00014 DigitalIn direction(p16);
00015 DigitalOut P17(p17);    //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
00016 DigitalOut P18(p18);    //also setting unused analog input pins to digital outputs reduces A/D noise a bit
00017 DigitalOut P19(p19);
00018 AnalogIn Pot(p20);
00019 
00020 /*モータ駆動関係*/
00021 float dc = 0.0;
00022 
00023 /*CAN関係*/
00024 CAN CAN1(p30, p29); //CANのピンの設定
00025 int forSend = 0;        //データ送信時に一時的に使用する変数
00026 CANMessage msgCAN;    //CAN送信用
00027 void CAN1_send();
00028 
00029 /*INA226関係*/    //GNDGND(G-G):0x80, Vs+Vs+(1-1):0x8A, SDASDA(D-D):0x94, SCLSCL(C-C):0x9E, GNDVs+(G-1):0x82
00030 I2C i2c(p28,p27);
00031 void initINA226();  //INA226の初期設定を行う関数
00032 void INA226_init(); //INA226の初期設定用関数
00033 INA226 VCmonitor(i2c); 
00034 unsigned short val;
00035 double V,C;
00036 
00037 //printf用
00038 char cnt_printf = 0;
00039 
00040 void CAN1_send() { //CANでデータ送信する関数。  //CAN1.attach(&Handler_canSlaveRecieve, CAN::RxIrq); <== この文はCAN受信割り込みの設定。使いたい時に使えるようにここに書いておく。使うときはmain関数で1回実行する。
00041     msgCAN.id = 0x10; //送信するCANデータのIDを決める
00042     msgCAN.len = 7;   //CAN送信側で送るデータのバイト数
00043     
00044     forSend = (int)(V/100);
00045     msgCAN.data[0] = forSend / 100;    //forSend / 100 = 15あまり43 となり,答えの方がデータ格納される
00046     msgCAN.data[1] = forSend % 100;    //forSend / 100 = 15あまり43 となり,あまりの方がデータ格納される
00047     
00048     forSend = (int)(C/10);  //mAで取得した値を10で割り、char2分割で送り切れるようにする。(例: 測定電流が199.99Aのとき,C=199900.00となるため,forSend=19990となり,199と90で分割することで送信できる)
00049     msgCAN.data[2] = forSend / 100;   //forSend / 100 = 5あまり54 となり,答えの方がデータ格納される
00050     msgCAN.data[3] = forSend % 100;   //forSend % 100 = 5あまり54 となり,あまりの方がデータ格納される
00051     
00052     forSend = (int)(M.speed*10);  //speed * 10 = 856.5だが,intにするため,856が代入される 
00053     msgCAN.data[4] = forSend / 100;   //forSend / 100 = 8あまり56 となり,8がデータ格納される
00054     msgCAN.data[5] = forSend % 100;   //forSend % 100 = 8あまり56 となり,56がデータ格納される
00055     
00056     forSend = (int)(M.getDutyCycle()*100);  //0~1を0~100にする
00057     msgCAN.data[6] = forSend;   //charの最大値である255以下になるため,そのままデータ格納される
00058     
00059     if(CAN1.write(msgCAN)){   //格納したデータを送信する
00060     }
00061 }
00062 
00063 void INA226_init(){ //INA226の初期設定を行う関数
00064     pc.printf("\n\r");
00065     pc.printf("VCmonitor INA226 TEST Program. (BUILD:[" __DATE__ "/" __TIME__ "])\n\r");
00066     if(!VCmonitor.isExist()){ pc.printf("VCmonitor NOT FOUND "); }
00067     val = 0;
00068     if(VCmonitor.rawRead(0x00,&val) != 0){ pc.printf("VCmonitor READ ERROR "); }    //configResisterの値を読み取れるか確認(通信できてるか確認)
00069     VCmonitor.setConfigResister();  //configurationResisterの設定
00070     pc.printf("VCmonitor Reg 0x00 : 0x%04x\n\r",val);  //configResisterの値表示
00071     pc.printf("\n\r");
00072     VCmonitor.setCurrentCalibration();  //calibrationResisterの設定
00073 }    
00074 
00075 int main() {
00076     //PHY_PowerDown();    //Ethernet停止 消費電力削減
00077     pc.baud(115200);    //pcとのシリアル通信速度指定。なるべく早くしないとpc.printfの処理時間が長くなってしまうかも
00078     //ポート初期設定
00079     direction.mode(PullUp);                //進行方向スイッチ入力ピンをプルアップに設定
00080     accel.mode(PullUp);                //アクセルスイッチ入力ピンをプルアップに設定
00081     //INA226初期設定
00082     INA226_init();  //INA226の初期設定する関数に飛ぶ
00083        
00084     while(true) {  
00085         //ゲート信号デューティ比に使用する値をAD変換により取得
00086         dc = ( Pot.read()*8 - 0.3 );  //可変抵抗の値からduty比を設定 Pot.read()を8倍することで可変抵抗をたくさん回さなくても値が上昇する -0.3して回生の処理をしやすくなるようにしておく(dcが0未満の時回生にするなど)
00087         if( dc >= 1 ){ dc = 1; }
00088         M.setDutyCycle(dc);     //duty比の設定
00089                
00090         //IPMの入力電圧、入力電流の測定
00091         VCmonitor.getVoltage(&V);   //IPM電圧(mV)測定
00092         V = V * 14.825; //計算の仕方は「ソーラーカー電装品図(パワポ資料)」に書いてある 270kと20kで分圧したとき14.825をかける
00093         V = V * 0.996; //テスタ実測結果を反映して値を調整する
00094         VCmonitor.getCurrent(&C);   //IPM電流(mA)測定
00095         
00096         //アクセルがONかOFFか判定する
00097         if( !accel.read() ){    //accel.read()が0(スイッチがOFF)の時の処理
00098             led4 = 0;
00099             M.accel = 0;
00100         }
00101         if( accel.read() ){
00102             if( !direction.read() ){    //directionスイッチが0のとき
00103                 led3 = 0;               //処理
00104             }
00105             else if( direction.read() ){     //directionスイッチが1のとき
00106                 led3 = 1;
00107             }
00108             led4 = 1;
00109             M.accel = 1;
00110         }
00111         //シリアル通信でpc(teratermなど)に表示
00112         ++cnt_printf;
00113         if( cnt_printf >= 50 ){
00114             pc.printf("Duty: %.2f, Sector: %d, %.1f km/h, rpm: %1.2f, V: %.0f, C: %.0f, limit1: %.3f, HS_cnt: %d HS_usec %.2f\n\r", M.getDutyCycle(), M.getSector(), M.speed, M.rpm, V, C, M.dc_limit1, M.HS_cnt, M.HS_usec);
00115             cnt_printf = 0;
00116         }
00117         //CANデータ送信する
00118         CAN1_send(); //送信処理する関数に飛ぶ
00119     }
00120 }