realtime process control with RTOS
Fork of mbed-os-example-mbed5-blinky by
main.cpp@22:f0164a2a09b0, 2018-10-04 (annotated)
- Committer:
- ykuroda
- Date:
- Thu Oct 04 03:23:22 2018 +0000
- Revision:
- 22:f0164a2a09b0
- Parent:
- 21:967504024346
- Child:
- 23:bc5ad77d56bb
EventQueue;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 20:13fcc82f4cab | 1 | // mbed-os-io-control |
ykuroda | 20:13fcc82f4cab | 2 | // I/O制御プログラム(機械工学実験1) |
ykuroda | 19:9381d775bfc5 | 3 | // |
ykuroda | 20:13fcc82f4cab | 4 | // 20161027 ... v1.0 ... originally written by Y.Kuroda for Mechatronics |
ykuroda | 20:13fcc82f4cab | 5 | // 20180917 ... v2.0 ... customised for Jikken1(mbed LPC1768専用) |
ykuroda | 22:f0164a2a09b0 | 6 | // 20181004 ... v3.0 ... uses EventQueue |
ykuroda | 19:9381d775bfc5 | 7 | // |
Jonathan Austin |
0:2757d7abb7d9 | 8 | #include "mbed.h" |
ykuroda | 20:13fcc82f4cab | 9 | |
ykuroda | 22:f0164a2a09b0 | 10 | const int DELTA_T = 1000; // mainスレッドの時間間隔の設定[ms](デフォルトは1ms) |
ykuroda | 20:13fcc82f4cab | 11 | int delta_t = DELTA_T; |
ykuroda | 20:13fcc82f4cab | 12 | int qbits = 6; // 量子化シフト値 |
ykuroda | 20:13fcc82f4cab | 13 | DigitalOut led1(LED1); |
ykuroda | 20:13fcc82f4cab | 14 | DigitalOut led2(LED2); |
ykuroda | 20:13fcc82f4cab | 15 | DigitalOut led3(LED3); |
ykuroda | 20:13fcc82f4cab | 16 | DigitalOut led4(LED4); |
ykuroda | 20:13fcc82f4cab | 17 | |
ykuroda | 20:13fcc82f4cab | 18 | // p11,12 サンプリング周期設定ピン(デフォルトは0) |
ykuroda | 22:f0164a2a09b0 | 19 | // p11上位ビット,p12下位ビット.0:1kHz, 1: 100Hz, 2: 10Hz, 3: 1Hz |
ykuroda | 20:13fcc82f4cab | 20 | InterruptIn samplingtime_MSB(p11); |
ykuroda | 20:13fcc82f4cab | 21 | InterruptIn samplingtime_LSB(p12); |
Jonathan Austin |
0:2757d7abb7d9 | 22 | |
ykuroda | 20:13fcc82f4cab | 23 | // p13,14 量子化粒度設定ピン(デフォルトは0) |
ykuroda | 20:13fcc82f4cab | 24 | // p13上位ビット,p14下位ビット.0:10bit, 1:8bit, 2:6bit, 3:4bit |
ykuroda | 20:13fcc82f4cab | 25 | InterruptIn quantization_MSB(p13); |
ykuroda | 20:13fcc82f4cab | 26 | InterruptIn quantization_LSB(p14); |
ykuroda | 20:13fcc82f4cab | 27 | |
ykuroda | 20:13fcc82f4cab | 28 | // アナログ入出力ピン(入出力共に0 - 3.3V) |
ykuroda | 20:13fcc82f4cab | 29 | AnalogOut aout(p18); |
ykuroda | 20:13fcc82f4cab | 30 | AnalogIn ain(p20); |
Jonathan Austin |
0:2757d7abb7d9 | 31 | |
ykuroda | 20:13fcc82f4cab | 32 | // デジタル入力ピン(割り込みにより実現) |
ykuroda | 20:13fcc82f4cab | 33 | InterruptIn din1(p21); |
ykuroda | 20:13fcc82f4cab | 34 | InterruptIn din2(p22); |
ykuroda | 20:13fcc82f4cab | 35 | |
ykuroda | 20:13fcc82f4cab | 36 | // 方形波,パルス波出力ピン |
ykuroda | 20:13fcc82f4cab | 37 | PwmOut squarewave(p23); |
ykuroda | 20:13fcc82f4cab | 38 | PwmOut pulsewave(p24); |
ykuroda | 20:13fcc82f4cab | 39 | |
ykuroda | 20:13fcc82f4cab | 40 | // サンプリング処理をするスレッド(時間管理された処理) |
ykuroda | 22:f0164a2a09b0 | 41 | void sampling_job(const void* arg) { |
ykuroda | 22:f0164a2a09b0 | 42 | unsigned short a_data = ain.read_u16(); // AD入力(ADは12ビット) |
ykuroda | 22:f0164a2a09b0 | 43 | aout.write_u16((a_data>>qbits)<<qbits);// DA出力(量子化粒度はピンにより設定) |
ykuroda | 22:f0164a2a09b0 | 44 | led4=!led4; |
Jonathan Austin |
0:2757d7abb7d9 | 45 | } |
Jonathan Austin |
1:846c97078558 | 46 | |
ykuroda | 20:13fcc82f4cab | 47 | // 量子化粒度設定ハンドラ |
ykuroda | 20:13fcc82f4cab | 48 | void qsize_handler(void) { |
ykuroda | 20:13fcc82f4cab | 49 | // 量子化設定ピン(p13,14)の設定を読んで量子化粒度を決定する |
ykuroda | 20:13fcc82f4cab | 50 | // 設定ピンの状態を読み取り,2ビット値に変換 |
ykuroda | 20:13fcc82f4cab | 51 | int quantization = (quantization_MSB<<1)&0x2|quantization_LSB&0x1; |
ykuroda | 20:13fcc82f4cab | 52 | qbits = 6+quantization*4; // 量子化サイズの決定 |
ykuroda | 20:13fcc82f4cab | 53 | // (デジタル出力時の量子化サイズは 0:1024, 1:256, 2:64, 3:16 steps) |
ykuroda | 22:f0164a2a09b0 | 54 | } |
ykuroda | 20:13fcc82f4cab | 55 | |
ykuroda | 20:13fcc82f4cab | 56 | // サンプリングタイム設定割り込みハンドラ...時間設定ピンの状態が変化した時に呼び出される |
ykuroda | 20:13fcc82f4cab | 57 | void pinstate_handler(void) { |
ykuroda | 20:13fcc82f4cab | 58 | // ピンの状態を読み取り,2ビット値に変換 |
ykuroda | 20:13fcc82f4cab | 59 | int samplingtime = (samplingtime_MSB<<1)&0x2|samplingtime_LSB&0x1; |
ykuroda | 22:f0164a2a09b0 | 60 | // 制御周期を10^nとする(n=0:1s, n=1:0.1s, n=2:0.01s, n=3:0.001s) |
ykuroda | 20:13fcc82f4cab | 61 | delta_t = (int)pow(10.0,(double)samplingtime); |
ykuroda | 20:13fcc82f4cab | 62 | squarewave.period_ms(delta_t); // 方形波の周期の設定 |
ykuroda | 20:13fcc82f4cab | 63 | pulsewave.period_ms(delta_t); // パルス波の周期の設定 |
ykuroda | 20:13fcc82f4cab | 64 | squarewave.write(0.5F); // duty比 |
ykuroda | 20:13fcc82f4cab | 65 | pulsewave.write(0.1F); // duty比 |
ykuroda | 20:13fcc82f4cab | 66 | } |
ykuroda | 20:13fcc82f4cab | 67 | |
ykuroda | 20:13fcc82f4cab | 68 | // デジタル入力割り込みハンドラ...デジタル入力信号の状態が変化した時に呼び出される |
ykuroda | 20:13fcc82f4cab | 69 | void din_handler(void) { |
ykuroda | 20:13fcc82f4cab | 70 | led1 = din1; // ピンの状態をそのままLEDの点灯状態にする |
ykuroda | 20:13fcc82f4cab | 71 | led2 = din2; |
ykuroda | 21:967504024346 | 72 | } |
ykuroda | 21:967504024346 | 73 | |
ykuroda | 19:9381d775bfc5 | 74 | int main() { |
ykuroda | 20:13fcc82f4cab | 75 | squarewave.period_ms(delta_t); // 初期周期の設定 |
ykuroda | 20:13fcc82f4cab | 76 | pulsewave.period_ms(delta_t); // 初期周期の設定 |
ykuroda | 20:13fcc82f4cab | 77 | squarewave.write(0.5F); // 初期duty比(方形波=50%) |
ykuroda | 20:13fcc82f4cab | 78 | pulsewave.write(0.1F); // 初期duty比(パルス波=10%) |
ykuroda | 20:13fcc82f4cab | 79 | |
ykuroda | 22:f0164a2a09b0 | 80 | samplingtime_MSB.disable_irq(); |
ykuroda | 22:f0164a2a09b0 | 81 | samplingtime_MSB.mode(PullDown); |
ykuroda | 20:13fcc82f4cab | 82 | samplingtime_MSB.rise(pinstate_handler);// 周期設定ハンドラの設定. |
ykuroda | 20:13fcc82f4cab | 83 | samplingtime_MSB.fall(pinstate_handler);// 設定ピンの状態変化で |
ykuroda | 22:f0164a2a09b0 | 84 | samplingtime_MSB.enable_irq(); |
ykuroda | 22:f0164a2a09b0 | 85 | |
ykuroda | 22:f0164a2a09b0 | 86 | samplingtime_LSB.disable_irq(); |
ykuroda | 22:f0164a2a09b0 | 87 | samplingtime_LSB.mode(PullDown); |
ykuroda | 20:13fcc82f4cab | 88 | samplingtime_LSB.rise(pinstate_handler);// ハンドラが呼び出される |
ykuroda | 20:13fcc82f4cab | 89 | samplingtime_LSB.fall(pinstate_handler);// ようにする |
ykuroda | 22:f0164a2a09b0 | 90 | samplingtime_LSB.enable_irq(); |
ykuroda | 22:f0164a2a09b0 | 91 | |
ykuroda | 20:13fcc82f4cab | 92 | quantization_MSB.rise(qsize_handler); // 量子化粒度設定ハンドラの設定 |
ykuroda | 20:13fcc82f4cab | 93 | quantization_MSB.fall(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 94 | quantization_LSB.rise(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 95 | quantization_LSB.fall(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 96 | din1.rise(&din_handler); // デジタル入力ハンドラの設定 |
ykuroda | 20:13fcc82f4cab | 97 | din1.fall(&din_handler); // din1,2の状態変化でハンドラ |
ykuroda | 20:13fcc82f4cab | 98 | din2.rise(&din_handler); // が呼ばれる |
ykuroda | 20:13fcc82f4cab | 99 | din2.fall(&din_handler); |
ykuroda | 20:13fcc82f4cab | 100 | |
ykuroda | 22:f0164a2a09b0 | 101 | EventQueue queue; |
ykuroda | 22:f0164a2a09b0 | 102 | queue.call_every(DELTA_T, sampling_job, (void*)0); |
ykuroda | 22:f0164a2a09b0 | 103 | queue.dispatch(); |
ykuroda | 20:13fcc82f4cab | 104 | } |