Taiyo Mineo / Mbed 2 deprecated WeatherPredictor

Dependencies:   EthernetInterface GraphicHandler NTPClient SRNN SVM SensorModule mbed-rtos mbed

setup.cpp

Committer:
yukari_hinata
Date:
2015-02-16
Revision:
1:8538381cae81
Parent:
0:f6cdb984f638
Child:
2:20ecfe6edd71

File content as of revision 1:8538381cae81:

#include "setup.hpp"

// mcsvmのセットアップ : サンプル/係数のセット
static void mcsvm_setup(void)
{
    FILE* svm_setup_fp;
    char buf_str[20];
    int ret, line;
    float buf_data[DIM_SIGNAL];

    float* tmp_sample = new float[MCSVM_NUM_SAMPLES * DIM_SIGNAL];
    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" );
    if( svm_setup_fp == NULL ) {
        fprintf( stderr, "Error in svm setup : sample file cannot open. \r \n" );
        exit(1);
    }

    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") ) {
            tmp_sample_label[line] = CLOUDY;
        } else if ( !strcmp(buf_str,"rainy") ) {
            tmp_sample_label[line] = RAINY;
        } else if ( !strcmp(buf_str,"snowy") ) {
            tmp_sample_label[line] = SNOWY;
        } else {
            continue;
        }

        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);
    }

    // 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 ) {
        fprintf( stderr, "Error in open learned alpha data. \r\n");
        exit(1);
    }

    // 一列のデータではfscanfフォーマットがだるいので, fgetsを使用
    line = 0;
    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;
    fclose( svm_setup_fp );
    free( svm_setup_fp );   // mbed BUG - we must free file pointer.

}

// 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];

    // 信号の正規化のために, 信号の最大値と最小値を決めてやる必要がある.
    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);
    }

    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 );
}

// センサーのセットアップ.
static void sensor_setup(void)
{
    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();
    printf("SVM OK. \r\n");
    sensor_setup();
    printf("SENSOR OK. \r\n");
    network_setup();
    printf("NETWORK OK. \r\n");

}