realtime process control with RTOS

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
ykuroda
Date:
Tue Oct 02 03:04:57 2018 +0000
Revision:
20:13fcc82f4cab
Parent:
19:9381d775bfc5
Child:
21:967504024346
v2.0 the first edition for Kikaikougaku Jikken 1

Who changed what in which revision?

UserRevisionLine numberNew 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 19:9381d775bfc5 6 //
Jonathan Austin 0:2757d7abb7d9 7 #include "mbed.h"
ykuroda 20:13fcc82f4cab 8
ykuroda 20:13fcc82f4cab 9 const int DELTA_T = 1; // mainスレッドの時間間隔の設定[ms](デフォルトは1ms)
ykuroda 20:13fcc82f4cab 10 int delta_t = DELTA_T;
ykuroda 20:13fcc82f4cab 11 int qbits = 6; // 量子化シフト値
ykuroda 20:13fcc82f4cab 12 const int SIG_MYT = 0x1; // signal番号
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 20:13fcc82f4cab 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 20:13fcc82f4cab 41 void sampling_thread(const void* arg) {
ykuroda 19:9381d775bfc5 42 while(true){
ykuroda 19:9381d775bfc5 43 Thread::signal_wait(SIG_MYT); // シグナルを待つ
ykuroda 20:13fcc82f4cab 44 unsigned short a_data = ain.read_u16(); // AD入力(ADは12ビット)
ykuroda 20:13fcc82f4cab 45 aout.write_u16((a_data>>qbits)<<qbits);// DA出力(量子化粒度はピンにより設定)
Jonathan Austin 0:2757d7abb7d9 46 }
Jonathan Austin 0:2757d7abb7d9 47 }
Jonathan Austin 1:846c97078558 48
ykuroda 20:13fcc82f4cab 49 // 量子化粒度設定ハンドラ
ykuroda 20:13fcc82f4cab 50 void qsize_handler(void) {
ykuroda 20:13fcc82f4cab 51 // 量子化設定ピン(p13,14)の設定を読んで量子化粒度を決定する
ykuroda 20:13fcc82f4cab 52 // 設定ピンの状態を読み取り,2ビット値に変換
ykuroda 20:13fcc82f4cab 53 int quantization = (quantization_MSB<<1)&0x2|quantization_LSB&0x1;
ykuroda 20:13fcc82f4cab 54 qbits = 6+quantization*4; // 量子化サイズの決定
ykuroda 20:13fcc82f4cab 55 // (デジタル出力時の量子化サイズは 0:1024, 1:256, 2:64, 3:16 steps)
ykuroda 20:13fcc82f4cab 56 }
ykuroda 20:13fcc82f4cab 57
ykuroda 20:13fcc82f4cab 58 // サンプリングタイム設定割り込みハンドラ...時間設定ピンの状態が変化した時に呼び出される
ykuroda 20:13fcc82f4cab 59 void pinstate_handler(void) {
ykuroda 20:13fcc82f4cab 60 // ピンの状態を読み取り,2ビット値に変換
ykuroda 20:13fcc82f4cab 61 int samplingtime = (samplingtime_MSB<<1)&0x2|samplingtime_LSB&0x1;
ykuroda 20:13fcc82f4cab 62 // 制御周期を10^nとする(n=0:1s, n=1:0.1s, n=2:0.01s, n=3:0.001s)
ykuroda 20:13fcc82f4cab 63 delta_t = (int)pow(10.0,(double)samplingtime);
ykuroda 20:13fcc82f4cab 64 squarewave.period_ms(delta_t); // 方形波の周期の設定
ykuroda 20:13fcc82f4cab 65 pulsewave.period_ms(delta_t); // パルス波の周期の設定
ykuroda 20:13fcc82f4cab 66 squarewave.write(0.5F); // duty比
ykuroda 20:13fcc82f4cab 67 pulsewave.write(0.1F); // duty比
ykuroda 20:13fcc82f4cab 68 }
ykuroda 20:13fcc82f4cab 69
ykuroda 20:13fcc82f4cab 70 // デジタル入力割り込みハンドラ...デジタル入力信号の状態が変化した時に呼び出される
ykuroda 20:13fcc82f4cab 71 void din_handler(void) {
ykuroda 20:13fcc82f4cab 72 led1 = din1; // ピンの状態をそのままLEDの点灯状態にする
ykuroda 20:13fcc82f4cab 73 led2 = din2;
ykuroda 20:13fcc82f4cab 74 }
ykuroda 19:9381d775bfc5 75
ykuroda 19:9381d775bfc5 76 int main() {
ykuroda 20:13fcc82f4cab 77 squarewave.period_ms(delta_t); // 初期周期の設定
ykuroda 20:13fcc82f4cab 78 pulsewave.period_ms(delta_t); // 初期周期の設定
ykuroda 20:13fcc82f4cab 79 squarewave.write(0.5F); // 初期duty比(方形波=50%)
ykuroda 20:13fcc82f4cab 80 pulsewave.write(0.1F); // 初期duty比(パルス波=10%)
ykuroda 20:13fcc82f4cab 81
ykuroda 20:13fcc82f4cab 82 samplingtime_MSB.rise(pinstate_handler);// 周期設定ハンドラの設定.
ykuroda 20:13fcc82f4cab 83 samplingtime_MSB.fall(pinstate_handler);// 設定ピンの状態変化で
ykuroda 20:13fcc82f4cab 84 samplingtime_LSB.rise(pinstate_handler);// ハンドラが呼び出される
ykuroda 20:13fcc82f4cab 85 samplingtime_LSB.fall(pinstate_handler);// ようにする
ykuroda 20:13fcc82f4cab 86 quantization_MSB.rise(qsize_handler); // 量子化粒度設定ハンドラの設定
ykuroda 20:13fcc82f4cab 87 quantization_MSB.fall(qsize_handler);
ykuroda 20:13fcc82f4cab 88 quantization_LSB.rise(qsize_handler);
ykuroda 20:13fcc82f4cab 89 quantization_LSB.fall(qsize_handler);
ykuroda 20:13fcc82f4cab 90 din1.rise(&din_handler); // デジタル入力ハンドラの設定
ykuroda 20:13fcc82f4cab 91 din1.fall(&din_handler); // din1,2の状態変化でハンドラ
ykuroda 20:13fcc82f4cab 92 din2.rise(&din_handler); // が呼ばれる
ykuroda 20:13fcc82f4cab 93 din2.fall(&din_handler);
ykuroda 20:13fcc82f4cab 94
ykuroda 20:13fcc82f4cab 95 Thread thread_dt(sampling_thread); // サンプリング処理スレッドを起動
ykuroda 19:9381d775bfc5 96
ykuroda 19:9381d775bfc5 97 while(true){
ykuroda 20:13fcc82f4cab 98 Thread::wait(delta_t); // mainスレッドの時間間隔の設定[ms]
ykuroda 20:13fcc82f4cab 99 thread_dt.signal_set(SIG_MYT); // スレッドへ再開シグナルを送る
ykuroda 20:13fcc82f4cab 100 led4 = !led4; // 処理の先頭でLEDをを点滅
ykuroda 19:9381d775bfc5 101 }
ykuroda 20:13fcc82f4cab 102 }