Weather casting with Machine Learning (SVM and SRNN).
Dependencies: EthernetInterface GraphicHandler NTPClient SRNN SVM SensorModule mbed-rtos mbed
Revision 1:8538381cae81, committed 2015-02-16
- Comitter:
- yukari_hinata
- Date:
- Mon Feb 16 07:53:45 2015 +0000
- Parent:
- 0:f6cdb984f638
- Child:
- 2:20ecfe6edd71
- Commit message:
- changed.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon Feb 16 07:53:45 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#2f204a58a350
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient.lib Mon Feb 16 07:53:45 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/NTPClient/#881559865a93
--- a/SRNN.lib Sun Feb 15 09:28:23 2015 +0000 +++ b/SRNN.lib Mon Feb 16 07:53:45 2015 +0000 @@ -1,1 +1,1 @@ -SRNN#d623e7ef4dca +SRNN#89a285dbc78e
--- a/SVM.lib Sun Feb 15 09:28:23 2015 +0000 +++ b/SVM.lib Mon Feb 16 07:53:45 2015 +0000 @@ -1,1 +1,1 @@ -SVM#a8938dc2c422 +SVM#01a20b89db32
--- a/SensorModule.lib Sun Feb 15 09:28:23 2015 +0000 +++ b/SensorModule.lib Mon Feb 16 07:53:45 2015 +0000 @@ -1,1 +1,1 @@ -SensorModule#3d6d2ff7ff8a +SensorModule#d1f55b26ccac
--- a/debug/debug.cpp Sun Feb 15 09:28:23 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#include "debug.hpp" - -void print_mat_func(float* mat, int row, int col) { - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - if (j == 0) { printf("|"); } - float val = MATRIX_AT(mat,col,i,j); - if (val > 0) { printf(" "); } - printf("%3.3f ",val); - if (j == col-1) { printf("| \r\n"); }; - } - } -} - -void print_vec_func(float* vec, int dim) { - for (int i = 0; i < dim; i++) { - printf("%f ", vec[i]); - } - printf("\r\n"); -} - -void no_memory(void) { - fprintf( stderr, "panic: can't allocate to memory! \r\n"); - exit(1); -}
--- a/debug/debug.hpp Sun Feb 15 09:28:23 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -#ifndef DEBUG_H_INCLUDED -#define DEBUG_H_INCLUDED - -#include "mbed.h" -#include <new> - -#include "../util/util.hpp" - -// Debug Macros -#define DBG(...) printf("" __VA_ARGS__) -#define PRINT_INT(x) (printf(#x " : %d \r\n", x)) -#define PRINT_FLT(x) (printf(#x " : %f \r\n", x)) - -#define PRINT_VEC(vec,dim) (printf("FILE[%s] LINE %d, %s: \r\n", __FILE__, __LINE__, (#vec)) \ - (print_vec_func((vec),(dim))) -#define PRINT_MAT(mat,row,col) (printf("FILE[%s] LINE %d, %s: \r\n", __FILE__, __LINE__, (#mat)) \ - (print_vec_func((vec),(row),(col))) - -// Debug Modules - -void print_vec_func(float* vec, int dim); - -void print_mat_func(float* mat, int row, int col); - -void write_log(char* name, ...); - -void no_memory(void); // error handler for memory allocate failing - -#endif /* DEBUG_H_INCLUDED */ -
--- a/main.cpp Sun Feb 15 09:28:23 2015 +0000 +++ b/main.cpp Mon Feb 16 07:53:45 2015 +0000 @@ -1,15 +1,19 @@ #include "main.hpp" -LocalFileSystem local("local"); // マウントポイントを定義(ディレクトリパスになる) +LocalFileSystem *local_fs; // マウントポイントを定義(ディレクトリパスになる) // 時間(global) -time_t global_time; // 現在時刻 -Mutex time_mutex; // 時間のMutex +time_t global_time; // 現在時刻 +Mutex time_mutex; // 時間のMutex // Pointer to Class instance (global) SRNN *srnn; MCSVM *mcsvm; -SensorModule *sm; -GraphicHandler *gh; +SensorModule *sensor_module; +GraphicHandler *graphic_handler; +// ネットワーク関係(global) +EthernetInterface eth_if; +TCPSocketServer http_server; +NTPClient ntp_client; // 系列データ float* new_seqence_data; // 現在の(一番新しい)系列データ @@ -50,7 +54,7 @@ int main(void) { set_new_handler(no_memory); - + local_fs = new LocalFileSystem("local"); setup(); printf("EXIT SUCESS!! \r\n"); return 0;
--- a/main.hpp Sun Feb 15 09:28:23 2015 +0000 +++ b/main.hpp Mon Feb 16 07:53:45 2015 +0000 @@ -4,22 +4,18 @@ #include "mbed.h" #include "rtos.h" -#include "share.hpp" -#include "setup.hpp" +#include "EthernetInterface.h" +#include "NTPClient.h" #include "MCSVM.hpp" #include "SRNN.hpp" #include "SensorModule.hpp" #include "GraphicHandler.hpp" -#include "./debug/debug.hpp" +#include "share.hpp" +#include "setup.hpp" -#define PREDICT_LENGTH (3) // 予測系列長 -#define PREDICT_INTERVAL_TIME (1 * 60 * 60) // 予測間隔 : 1h -#define LEN_DATA_SEQUENCE (100) // 観測データの履歴長 -#define NUM_WEATHERS (4) // 気候の種類 -#define DIM_SIGNAL (3) // 信号の次元(=センサの数) -#define MCSVM_NUM_SAMPLES (200) // MCSVMのサンプル数 +#include "./debug/debug.hpp" // // 時間(global) extern time_t global_time; // 現在時刻 @@ -27,7 +23,13 @@ // Pointer to Class instance (global) extern SRNN *srnn; extern MCSVM *mcsvm; -extern SensorModule *sm; -extern GraphicHandler *gh; +extern SensorModule *sensor_module; +extern GraphicHandler *graphic_handler; +// ネットワーク関係(global) +extern EthernetInterface eth_if; +extern TCPSocketServer http_server; +extern NTPClient ntp_client; +// ファイルシステム(global) +extern LocalFileSystem *local_fs; #endif /* MAIN_H_INCLUDED */ \ No newline at end of file
--- a/setup.cpp Sun Feb 15 09:28:23 2015 +0000 +++ b/setup.cpp Mon Feb 16 07:53:45 2015 +0000 @@ -9,7 +9,7 @@ float buf_data[DIM_SIGNAL]; float* tmp_sample = new float[MCSVM_NUM_SAMPLES * DIM_SIGNAL]; - int* tmp_sample_label = new int[MCSVM_NUM_SAMPLES]; + int* tmp_sample_label = new int[MCSVM_NUM_SAMPLES]; float* tmp_mc_alpha = new float[MCSVM_NUM_SAMPLES * NUM_WEATHERS * (NUM_WEATHERS - 1) / 2]; svm_setup_fp = fopen( "/local/svm_samp.csv" , "r" ); @@ -20,7 +20,7 @@ line = 0; while( ( ret = fscanf( svm_setup_fp, " %[^\n,],%f,%f,%f", buf_str, &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { - + if ( !strcmp(buf_str,"shiny") ) { tmp_sample_label[line] = SHINY; } else if ( !strcmp(buf_str,"cloudy") ) { @@ -36,13 +36,13 @@ tmp_sample[line * 3] = buf_data[0]; tmp_sample[line * 3 + 1] = buf_data[1]; tmp_sample[line * 3 + 2] = buf_data[2]; - line++; + // printf("svm sample loading.... %d \r\n", line); } - mcsvm = new MCSVM(NUM_WEATHERS, DIM_SIGNAL, MCSVM_NUM_SAMPLES, tmp_sample, tmp_sample_label); + // fclose( svm_setup_fp ); - fclose( svm_setup_fp ); + mcsvm = new MCSVM(NUM_WEATHERS, DIM_SIGNAL, MCSVM_NUM_SAMPLES, tmp_sample, tmp_sample_label); svm_setup_fp = fopen("/local/alpha.csv", "r"); if ( svm_setup_fp == NULL ) { @@ -52,16 +52,19 @@ // 一列のデータではfscanfフォーマットがだるいので, fgetsを使用 line = 0; - while( fgets( buf_str, 20, svm_setup_fp) != NULL ){ + while( fgets( buf_str, 20, svm_setup_fp) != NULL ) { tmp_mc_alpha[line] = atof(buf_str); // printf("%d %f \r\n", line, tmp_mc_alpha[line]); line++; } - + mcsvm->set_alpha(tmp_mc_alpha, MCSVM_NUM_SAMPLES, NUM_WEATHERS); - - delete [] tmp_sample; delete [] tmp_sample_label; delete [] buf_data; - delete [] tmp_mc_alpha; delete [] buf_str; + + delete [] tmp_sample; + delete [] tmp_sample_label; + delete [] buf_data; + delete [] tmp_mc_alpha; + delete [] buf_str; fclose( svm_setup_fp ); free( svm_setup_fp ); // mbed BUG - we must free file pointer. @@ -70,56 +73,110 @@ // SRNNのセットアップ. 初期データのセット. static void srnn_setup(void) { - FILE* srnn_setup_fp; - int ret; - float buf_data[DIM_SIGNAL]; - float* sample = new float[LEN_DATA_SEQUENCE * DIM_SIGNAL]; - float* sample_maxmin = new float[DIM_SIGNAL * 2]; + FILE* srnn_setup_fp; + int ret; + float buf_data[DIM_SIGNAL]; + float* sample = new float[LEN_DATA_SEQUENCE * DIM_SIGNAL]; + float* sample_maxmin = new float[DIM_SIGNAL * 2]; - // 信号の正規化のために, 信号の最大値と最小値を決めてやる必要がある. - sample_maxmin[0] = 50; sample_maxmin[1] = -20; // 気温の最大/最小値(想定値) - sample_maxmin[2] = 1030; sample_maxmin[3] = 960; // 気圧 - sample_maxmin[4] = 100; sample_maxmin[5] = 0; // 湿度 + // 信号の正規化のために, 信号の最大値と最小値を決めてやる必要がある. + sample_maxmin[0] = 50; + sample_maxmin[1] = -20; // 気温の最大/最小値(想定値) + sample_maxmin[2] = 1030; + sample_maxmin[3] = 960; // 気圧 + sample_maxmin[4] = 100; + sample_maxmin[5] = 0; // 湿度 - srnn_setup_fp = fopen( "/local/srnninit.csv" , "r" ); - if( srnn_setup_fp == NULL ){ - fprintf( stderr, "Error in SRNN setup. init sample file cannot open. \r\n"); - exit(1); - } + srnn_setup_fp = fopen( "/local/srnninit.csv" , "r" ); + if( srnn_setup_fp == NULL ) { + fprintf( stderr, "Error in SRNN setup. init sample file cannot open. \r\n"); + exit(1); + } - int line = 0; - while( ( ret = fscanf( srnn_setup_fp, "%f,%f,%f", &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ){ - memcpy(&(sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); - // printf("sample %d : %f %f %f \r\n", line, MATRIX_AT(sample,DIM_SIGNAL,line,0), MATRIX_AT(sample,DIM_SIGNAL,line,1), MATRIX_AT(sample,DIM_SIGNAL,line,2)); - line++; - } + int line = 0; + while( ( ret = fscanf( srnn_setup_fp, "%f,%f,%f", &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { + memcpy(&(sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); + // printf("sample %d : %f %f %f \r\n", line, MATRIX_AT(sample,DIM_SIGNAL,line,0), MATRIX_AT(sample,DIM_SIGNAL,line,1), MATRIX_AT(sample,DIM_SIGNAL,line,2)); + line++; + } - /* アドバイス:RNNにおいては,ダイナミクス(中間層のニューロン数)は多いほど良い */ - srnn = new SRNN(DIM_SIGNAL, 20, LEN_DATA_SEQUENCE, PREDICT_LENGTH, sample, sample_maxmin); - - delete [] sample; delete [] sample_maxmin; - fclose( srnn_setup_fp ); - free( srnn_setup_fp ); + /* アドバイス:RNNにおいては,ダイナミクス(中間層のニューロン数)は多いほど良い */ + srnn = new SRNN(DIM_SIGNAL, 20, LEN_DATA_SEQUENCE, PREDICT_LENGTH, sample, sample_maxmin); + + delete [] sample; + delete [] sample_maxmin; + fclose( srnn_setup_fp ); + free( srnn_setup_fp ); } // センサーのセットアップ. static void sensor_setup(void) { - sm = new SensorModule(5); - sm->read_all_sensor(); + sensor_module = new SensorModule(5); } // ネットワークのセットアップ static void network_setup(void) { // セットアップ, 最初の時間取得 + const char* prefix_net_str = "[Network Status]"; + + //setup ethernet interface + printf("%s Ethernet initializing....", prefix_net_str); + if ( eth_if.init() < 0 ) {// Use DHCP + fprintf( stderr, "%s Ethernet init failed. \r\n", prefix_net_str); + exit(1); + } + + if ( eth_if.connect() < 0 ) { + fprintf( stderr, "%s Ethernet connect failed. \r\n", prefix_net_str); + exit(1); + } + + // init time + printf("%s Trying to update time...\r\n", prefix_net_str); + + // Please specify near ntp server. ex) Japan -> ntp.nict.jp:123 + if (ntp_client.setTime("ntp.nict.jp") == 0) { + time_mutex.lock(); + global_time = time(NULL); + time_mutex.unlock(); + printf("%s Set time successfully! \n Time is set to (UTC): %s\r\n", prefix_net_str, ctime(&global_time)); + } else { + fprintf( stderr, "%s Error in setup time \r\n", prefix_net_str); + } + + //setup tcp socket + if(http_server.bind(80) < 0) { + fprintf( stderr, "%s HTTP server bind port 80 failed.\n\r", prefix_net_str); + exit(1); + } else { + printf("%s HTTP server bind successed.\n\r", prefix_net_str); + } + + if(http_server.listen(1) < 0) { + fprintf( stderr, "%s HTTP server listen failed.\n\r", prefix_net_str); + exit(1); + } else { + printf("%s HTTP server is listening...\n\r", prefix_net_str); + } + + printf("%s IP Address : %s \r\n", prefix_net_str, eth_if.getIPAddress()); + + printf("%s Network setup finished! \r\n", prefix_net_str); } // セットアップ. void setup(void) { + printf("SETUP START. \r\n"); + srnn_setup(); + printf("SRNN OK. \r\n"); mcsvm_setup(); - srnn_setup(); + printf("SVM OK. \r\n"); sensor_setup(); + printf("SENSOR OK. \r\n"); network_setup(); + printf("NETWORK OK. \r\n"); + }
--- a/setup.hpp Sun Feb 15 09:28:23 2015 +0000 +++ b/setup.hpp Mon Feb 16 07:53:45 2015 +0000 @@ -4,6 +4,6 @@ #include "main.hpp" // セットアップを一任. -void setup(void); +extern void setup(void); #endif /* SETUP_H_INCLUDED */ \ No newline at end of file
--- a/share.hpp Sun Feb 15 09:28:23 2015 +0000 +++ b/share.hpp Mon Feb 16 07:53:45 2015 +0000 @@ -1,6 +1,14 @@ #ifndef SHARE_H_INCLUDED #define SHARE_H_INCLUDED +#define PREDICT_LENGTH (3) // 予測系列長 +#define PREDICT_INTERVAL_TIME (1 * 60 * 60) // 予測間隔 : 1h +#define LEN_DATA_SEQUENCE (100) // 観測データの履歴長 +#define NUM_WEATHERS (4) // 気候の種類 +#define DIM_SIGNAL (3) // 信号の次元(=センサの数) +#define MCSVM_NUM_SAMPLES (200) // MCSVMのサンプル数 + + // 天候を表す列挙型 typedef enum { SHINY = 0, // 晴れ