RPM counter Rev.0.1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #define DBGFLAG  1          //デバッグ出力フラグ 0:なし 1:結果のみ 2:入力含む 3〜:中間結果含む
00003 #define BUFSIZE_BIT 8       //バッファサイズ(ビット数定義)
00004 #define BUFSIZE (1<<BUFSIZE_BIT)    //バッファサイズ
00005 #define BUFSIZE_MASK (BUFSIZE-1)    //バッファ演算マスク
00006 
00007 #define IGNPULSE_WIDTH 3000         //出力イグニッションパルス幅
00008 
00009 // ECU Simple1 alfa1  for mbed NXP LPC1768   2019/2/xx
00010 //2019/2/27 周期ベースに書き換え。リングバッファのサイズ可変。
00011 //          前のパルスのベクトル値を割り込み中に保存するようにして、関係各所で算出していたのを省略。
00012 
00013 InterruptIn clankPulse(p14);
00014 AnalogIn adc(p16);
00015 Serial pc(USBTX, USBRX);
00016 Timer t;                            //点火パルス発生タイミング
00017 Timeout RearIgn_DelayTimer;         // 点火遅延タイマ、リア用
00018 Timeout RearIgn_PulseWidthTimer;    // 点火パルス幅タイマ、リア用
00019  
00020  
00021 DigitalOut IgnWait(LED2);           //点火パルス発生 遅延中
00022 DigitalOut IgnPulse(LED3);          //点火パルス
00023 DigitalOut ErrBIndicater(LED4);     //エラー(負論理)
00024 
00025 DigitalOut PGTrigger(LED1);         //クランク回転パルス読み取り発生フラグ
00026 //int PGTrigger;                    //クランク回転パルス読み取り発生フラグ排他利用
00027 
00028 //パルスタイミング用バッファ定義
00029 int clankPulseTime[BUFSIZE];        //クランク回転パルス リングバッファ(本体)
00030 int clankPulseTimeBufSize = BUFSIZE;        //クランク回転パルス リングバッファのサイズ(本体の定義と一致させること)
00031 int clankPulseTimeVector = 0;           //クランク回転パルス リングバッファの位置
00032 int clankPulseTimeVectorBefore = BUFSIZE_MASK;  //クランク回転パルス リングバッファの位置(一つ前)
00033 
00034 //初期化
00035 void init() {
00036     pc.baud(115200);                //シリアル出力ボーレート(19200|38400|57600|115200)
00037     ErrBIndicater = 1 ;
00038 
00039     if ( DBGFLAG > 0 ) { 
00040         pc.printf("\n---- Nucleo LPC1768 ECU Simple1 test\r\n");
00041     }
00042     t.start();                  //タイマースタート
00043 }
00044 
00045 
00046 // ワンショットタイマ 割込みハンドラ(パルス幅設定)
00047 void isrDlyOff(void) {
00048     IgnPulse = 0;                       // 点火パルス終了
00049 }
00050 
00051 // ワンショットタイマ 割込みハンドラ(点火開始まち)
00052 void isrDlyOn(void) {
00053     IgnWait = 0;               // 点灯
00054     IgnPulse = 1;               // 点火パルス開始
00055     // 点火パルス幅 割込み設定、タイマスタート
00056     RearIgn_PulseWidthTimer.attach_us(isrDlyOff, IGNPULSE_WIDTH );   // 3ms
00057 }
00058 
00059 //点火処理(リア)
00060 void RearIgn(int RearIgn_Delay) {
00061     // 点火パルス遅延 割込み設定、タイマスタート
00062     RearIgn_DelayTimer.attach_us(isrDlyOn, RearIgn_Delay);       // 2.3s
00063 }
00064 
00065 //クランク回転パルス 割込みハンドラ
00066 void flip_rise(){             //risng edge interrupt
00067     PGTrigger = 0;
00068 }
00069 
00070 void flip_fall(){           //falling edge interrupt
00071     //リングバッファのベクトルをずらす。
00072     clankPulseTimeVectorBefore = clankPulseTimeVector;
00073     clankPulseTimeVector = (clankPulseTimeVector +1 ) & BUFSIZE_MASK;
00074     
00075     //キャプチャー時間列にシフト代入して、フラグ立てる。
00076     clankPulseTime[ clankPulseTimeVector ] = t.read_us() ;
00077 
00078     PGTrigger = 1;
00079             
00080     if ( DBGFLAG > 1 ) {
00081         pc.printf("\r\n" );
00082         pc.printf("flip_fall: clankPulseTime: %d [us] (scale X%d) ", clankPulseTime[ clankPulseTimeVector ] );
00083         pc.printf("address@%d", clankPulseTimeVector  );
00084         pc.printf("\r\n" );
00085     }
00086 
00087 }
00088 
00089 
00090 //クランク回転周期[us]読み取り
00091 int readCrankCycle(){
00092     int timeCycle;  //周期[us]
00093 
00094 //エンジン回転周期計算
00095     timeCycle = ( clankPulseTime[ clankPulseTimeVector ] - clankPulseTime[ clankPulseTimeVectorBefore ]);
00096 
00097     if ( timeCycle <= 0 ){
00098         timeCycle = -1;
00099     }
00100 
00101     if ( DBGFLAG > 2 ) {
00102         float timeCycle_sec = (float) timeCycle /1000000;
00103         float freqency = (float) 1 / timeCycle * 1000000;
00104         int engineRpm = (int) (  (float) 60 / timeCycle * 1000000 );
00105 
00106         pc.printf("readCrankCycle: ");
00107         pc.printf(" timeCycle %d[us] ", timeCycle , freqency );
00108         pc.printf("= after: %d ", clankPulseTime[ clankPulseTimeVector ] );
00109         pc.printf("- before: %d ", clankPulseTime[ clankPulseTimeVectorBefore ] );
00110         pc.printf("\r\n" );
00111         pc.printf("readCrankCycle:");
00112         pc.printf(" cycle: %f[sec] / engineRpm: %d", timeCycle_sec , engineRpm );
00113         pc.printf("\r\n" );
00114         pc.printf(" freqency: %f[Hz] ", freqency );
00115         pc.printf("\r\n" );
00116     }
00117     return timeCycle;
00118 
00119 }
00120 
00121 int ADC()
00122 {
00123     float meas_r;
00124     int meas_int;
00125     
00126     meas_r = adc.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
00127 
00128     meas_int = (int)( (float)(meas_r * 100 ) + (float)0.49999 );
00129 
00130     // Display values
00131     if ( DBGFLAG > 1 ) {
00132         float meas_v;
00133         meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range
00134         pc.printf("ADC: measure = %f = %.0f mV", meas_r, meas_v );
00135         pc.printf(" Return value = %d ", meas_int );
00136         pc.printf("\r\n" );
00137     }
00138 
00139     return meas_int;
00140 }
00141 
00142 
00143 //PCへログ出力 デバッグ用
00144 void log2pc( int throttleOpenPercent ,int timeCycle ,int RearIgn_Delay) {
00145 //エンジン回転数計算
00146     int engineRpm = (int) (  (float) 60 / timeCycle * 1000000 );
00147 
00148     pc.printf("Accell:%3d[%%]", throttleOpenPercent );
00149     pc.printf(" %5drpm " ,engineRpm );
00150     pc.printf(" %6.3f[ms] " ,(float)0.001 * timeCycle );
00151     pc.printf(" @vector%3d ", clankPulseTimeVector );
00152     pc.printf(" delay:%3d[ms] ", (float)0.001 * RearIgn_Delay );
00153 
00154     pc.printf("\r\n" );
00155 
00156 //  ログ出力完了時間の記録
00157     int logTime = t.read_us() ;
00158 
00159 //  ログ出力完了前にエンジンパルスが来たらエラー
00160     if ( logTime > clankPulseTime[clankPulseTimeVector]  ){
00161         //正常
00162     } else {
00163         ErrBIndicater = 0;
00164         pc.printf("\r\n" );
00165         pc.printf(" Log exe. time OVER %f" ,(float)( logTime - clankPulseTime[clankPulseTimeVector] )/1000000 );
00166         pc.printf(" = engine:%f - log:%f[s] ", (float)logTime/1000000, (float)(clankPulseTime[clankPulseTimeVector])/1000000 );
00167         pc.printf("\r\n" );
00168     }
00169 // ログが歯抜けになったらエラー
00170     
00171 }
00172 
00173 int main() {
00174     int throttleOpenPercent[8]; //スロットル開度列
00175     int engineCycle;                //エンジン回転数/秒
00176     
00177     //clankPulse.rise(&flip_rise);          //回転パルス割り込み設定
00178     clankPulse.fall(&flip_fall);        //回転パルス割り込み設定
00179     init();                         //初期化
00180 
00181     while(1){
00182     //クランク回転パルス入力の立ち上がりでPGTriggerが降り、立ち下がりでフラグが1に立つので、
00183     //処理を開始する。
00184         if ( PGTrigger == 1 ) {
00185             engineCycle = readCrankCycle();
00186             
00187             //アクセル読み込み
00188             throttleOpenPercent[0] = ADC();
00189 
00190             //マップ読み込み
00191             int RearIgn_Delay = (float) engineCycle/200 * (101-throttleOpenPercent[0]) ;
00192             //点火出力
00193             IgnWait = 1;               // 点灯
00194             RearIgn( RearIgn_Delay);
00195 
00196             //デバッグ出力
00197             if ( DBGFLAG > 0 ) { 
00198                 log2pc(throttleOpenPercent[0],engineCycle,RearIgn_Delay); 
00199             }
00200             PGTrigger = 0;          
00201         }
00202 
00203 //  wait(0.01);
00204     }
00205 }