Weather casting with Machine Learning (SVM and SRNN).
Dependencies: EthernetInterface GraphicHandler NTPClient SRNN SVM SensorModule mbed-rtos mbed
Diff: main.cpp
- Revision:
- 4:00da8e8c7e2a
- Parent:
- 3:5add3759e08a
- Child:
- 5:b61f3f5b0fc8
--- a/main.cpp Thu Feb 19 08:20:46 2015 +0000 +++ b/main.cpp Thu Feb 19 13:53:12 2015 +0000 @@ -36,29 +36,20 @@ // データ更新 - // __disable_irq(); // 割り込み禁止 sensor_module->read_all_sensor(); new_seqence_data[TEMPERATURE] = sensor_module->get_temperture(); new_seqence_data[AIR_PRESSURE] = sensor_module->get_pressure(); new_seqence_data[HUMIDITY] = sensor_module->get_humidity(); printf("T:%f P:%f H:%f \r\n", new_seqence_data[TEMPERATURE], new_seqence_data[AIR_PRESSURE], new_seqence_data[HUMIDITY]); graphic_handler->set_now_data(new_seqence_data); - // __enable_irq(); // 割り込み許可 - - /* - sprintf( str_buf, "%d/%d/%d %d:%d:%d,%.2f,%.2f,%.2f\n", - (local_time_p->tm_year + 1900), (local_time_p->tm_mon + 1), local_time_p->tm_mday, local_time_p->tm_hour, local_time_p->tm_min, local_time_p->tm_sec, - new_seqence_data[TEMPERATURE], new_seqence_data[AIR_PRESSURE], new_seqence_data[HUMIDITY]); - */ seqence_data_fp = fopen( SEQUENCE_DATA_NAME, "a"); - // check_file_open( seqence_data_fp, SEQUENCE_DATA_NAME); + check_file_open( seqence_data_fp, SEQUENCE_DATA_NAME); // 形式に沿った文字列を書き出す : y/m/d h:m,<temperature>,<air_pressure>,<humidity> fprintf( seqence_data_fp, "%d/%d/%d %d:%d:%d,%f,%f,%f\n", (local_time_p->tm_year + 1900), (local_time_p->tm_mon + 1), local_time_p->tm_mday, local_time_p->tm_hour, local_time_p->tm_min, local_time_p->tm_sec, new_seqence_data[TEMPERATURE], new_seqence_data[AIR_PRESSURE], new_seqence_data[HUMIDITY]); - // fputs( str_buf, seqence_data_fp ); fclose( seqence_data_fp ); } @@ -67,7 +58,7 @@ void ml_task(void const *arg) { // ローカル変数 - int line = 0, ret; + int line = 0, diff_line; //float* srnn_sample = new float[LEN_DATA_SEQUENCE * DIM_SIGNAL]; // SRNNのサンプル float srnn_sample[LEN_DATA_SEQUENCE * DIM_SIGNAL]; // 読み込みバッファ @@ -81,36 +72,73 @@ seqence_data_fp = fopen( SEQUENCE_DATA_NAME, "r"); check_file_open( seqence_data_fp, SEQUENCE_DATA_NAME); // まず、行数を数える - while( fgets( str_buf, seqence_data_fp) != NULL ) { - } + line = 0; + while( fgets( str_buf, BUF_SIZE, seqence_data_fp) != NULL ) { + line++; + } + printf(" line : %d \r\n", line); + // 差分行の計算 + diff_line = (line - LEN_DATA_SEQUENCE); + rewind( seqence_data_fp ); + printf(" go to line : %d \r\n", diff_line); line = 0; + // 差分行までfpを移動 + fflush( seqence_data_fp ); + while( fgets( str_buf, BUF_SIZE, seqence_data_fp) != NULL ) { + line++; + if ( line == diff_line ) break; + } + // 最新LEN_DATA_SEQUENCE個のデータを読み込む. + line = 0; + /* while( ( ret = fscanf( seqence_data_fp, " %[^\n,],%f,%f,%f", str_buf, &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) != EOF ) { if (line == LEN_DATA_SEQUENCE) break; memcpy(&(srnn_sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); - printf("sample %d : %s %f %f %f \r\n", line, str_buf, MATRIX_AT(srnn_sample,DIM_SIGNAL,line,0), MATRIX_AT(srnn_sample,DIM_SIGNAL,line,1), MATRIX_AT(srnn_sample,DIM_SIGNAL,line,2)); + printf("sample %d : %s %f %f %f \r\n", diff_line + line, str_buf, MATRIX_AT(srnn_sample,DIM_SIGNAL,line,0), MATRIX_AT(srnn_sample,DIM_SIGNAL,line,1), MATRIX_AT(srnn_sample,DIM_SIGNAL,line,2)); line++; } + */ - /* while( fgets( str_buf, BUF_SIZE, seqence_data_fp) != NULL ) { if (line == LEN_DATA_SEQUENCE) break; - printf("%s \r", str_buf); + if (open_count > 20) + printf("%d %s \r", line, str_buf); + //sscanf( str_buf, " %[^\n,],%f,%f,%f", str_buf, &(buf_data[0]), &(buf_data[1]), &(buf_data[2])) ) sscanf( str_buf, "%[^,],%[^,],%[^,],%[^,]", str_dummy , str_dum1 , str_dum2 , str_dum3); - srnn_sample[line * DIM_SIGNAL] = float(atof(str_dum1)); - srnn_sample[line * DIM_SIGNAL + 1] = float(atof(str_dum2)); - srnn_sample[line * DIM_SIGNAL + 2] = float(atof(str_dum3)); - printf(" str : %s , f0 : %f , f1 : %f , f2 : %f \r\n", str_dummy - , srnn_sample[line * DIM_SIGNAL] - , srnn_sample[line * DIM_SIGNAL + 1] - , srnn_sample[line * DIM_SIGNAL + 2]); + + // エラーハンドル... 仕様か過失かわからないが, 19回目以降に開いたファイルはおかしい. + buf_data[0] = float(atof(str_dum1)); + buf_data[1] = float(atof(str_dum2)); + buf_data[2] = float(atof(str_dum3)); + // 直前の結果+乱数 + if ( line >= 1 ) { + if (fabsf(buf_data[TEMPERATURE] - srnn_sample[(line-1) * DIM_SIGNAL]) > 30) + buf_data[TEMPERATURE] = (srnn_sample[(line-1) * DIM_SIGNAL + TEMPERATURE] + uniform_rand(1)); + if (fabsf(buf_data[AIR_PRESSURE] - srnn_sample[(line-1) * DIM_SIGNAL + AIR_PRESSURE]) > 30) + buf_data[AIR_PRESSURE] = (srnn_sample[(line-1) * DIM_SIGNAL + 1] + uniform_rand(1)); + if (fabsf(buf_data[HUMIDITY] - srnn_sample[(line-1) * DIM_SIGNAL + 2]) > 30) + buf_data[HUMIDITY] = (srnn_sample[(line-1) * DIM_SIGNAL + HUMIDITY] + uniform_rand(1)); + } + + memcpy(&(srnn_sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); + /* + srnn_sample[line * DIM_SIGNAL] = buf_data[0]; + srnn_sample[line * DIM_SIGNAL + 1] = buf_data[1]; + srnn_sample[line * DIM_SIGNAL + 2] = buf_data[2]; + */ + + if (open_count > 20) + printf(" %d str : %s , f0 : %f , f1 : %f , f2 : %f \r\n", line, str_dummy + , srnn_sample[line * DIM_SIGNAL] + , srnn_sample[line * DIM_SIGNAL + 1] + , srnn_sample[line * DIM_SIGNAL + 2]); // memcpy(&(srnn_sample[line * DIM_SIGNAL]), buf_data, sizeof(float) * DIM_SIGNAL); line++; } - */ fclose( seqence_data_fp ); srnn->set_sample(srnn_sample); @@ -119,7 +147,6 @@ printf("Learning... %02d:%02d \r\n", local_time_p->tm_hour, local_time_p->tm_min); srnn->learning(); srnn->predict(new_seqence_data); - // 金曜日ここから memcpy(new_predict_data, srnn->predict_signal, sizeof(float) * DIM_SIGNAL * PREDICT_LENGTH); // MCSVMによる天候識別 for (int i_predict = 0; i_predict < PREDICT_LENGTH; i_predict++) { @@ -180,17 +207,18 @@ while (true) { if (ml_flag) { - Thread::signal_wait(0x3, osWaitForever); + Thread::signal_wait(0x2, osWaitForever); } - // 1. 描画更新 <- 学習中は止めたい... - printf("draw thread start. \r\n"); + + // printf("draw thread start. \r\n"); if (time(NULL) % 60 == 0) { // 一分毎に表示時間を更新 graphic_handler->update_time(); } graphic_handler->update_image(); + // 画面更新 graphic_handler->update_draw(); - printf("draw thread finish. \r\n"); + // printf("draw thread finish. \r\n"); Thread::wait(1 * 1000); } @@ -231,30 +259,28 @@ { set_new_handler(no_memory); local_fs = new LocalFileSystem("local"); + setup(); + // graphic_handler->draw_log("setup finish"); ml_flag = 1; Thread draw_thread(draw_task, NULL, osPriorityNormal, 2000); Thread liveled_thread(liveled_task, NULL, osPriorityLow, 200); - - // Thread read_thread(read_task, NULL, osPriorityNormal); - + osThreadSetPriority(Thread::gettid() ,osPriorityHigh); while (true) { - Thread::wait(4 * 1000); - now_time = get_JST(); + ml_flag = 1; + now_time = time(NULL); local_time_p = localtime(&now_time); read_task(NULL); - // truncate_data_file(); ml_task(NULL); ml_flag = 0; liveled_thread.signal_set(0x1); - draw_thread.signal_set(0x3); + draw_thread.signal_set(0x2); + Thread::wait(2 * 1000); } - - printf("finished."); - - return 1; + + error("unreachable here"); }