v1

Dependencies:   mbed IM920

main.cpp

Committer:
imadaemi
Date:
2021-07-29
Revision:
1:69472741bd36
Parent:
0:f4fb5a0f6981
Child:
2:41238982b498

File content as of revision 1:69472741bd36:

#include "mbed.h"
#include "IM920.h"

// ***************************************************
// コンストラクタ
// ***************************************************
Serial pc(USBTX, USBRX, 115200);
Serial im920_serial(D1, D0, 115200);

IM920 im920(im920_serial, pc, 115200);

// ***************************************************
// 関数の宣言
// ***************************************************
void ReadPC();
void ReadSensorSetup();
void ReadData();
void Read0xA5();
void Help();

// ***************************************************
// 変数の宣言
// ***************************************************
char nich_status;

bool header_set = false;
char im_buf[16];//16なのって意味ある?
int im_buf_n = 0;

float lat, lon, height;
float press, temp, altitude;

float imu[6], mag[3];
float mag_geo[3];

float voltage_main, voltage_sep;
float current_main, current_sep;

// ***************************************************
// 無線のヘッダまとめ
// ***************************************************
const char HEADER_SETUP = 0x01;
// 0xA1

const char HEADER_DATA = 0xA2;
// 0xA2,lat,lon,height,press,temp,altitude
//   1 

const char HEADER_ECHO = 0xA5;
// 0xA5,コマンド
//   1     1

// ***************************************************
// メイン関数
// ***************************************************
int main() {
    wait(0.5f);
    pc.printf("Hello Ground!\r\n");
    pc.attach(&ReadPC, Serial::RxIrq);
    im920.attach(&Read0xA5, HEADER_ECHO);
    im920.attach(&ReadSensorSetup, HEADER_SETUP);
    im920.attach(&ReadData, HEADER_DATA);
}

void ReadPC(){
    char c = pc.getc();
    if(!header_set){
        im920.header((char)0xF0);
        header_set = true;
    }
    
    switch(c){
        case '0':
        im920.write((char)0x00);
        im_buf[im_buf_n] = '0';
        im_buf_n ++;
        pc.printf("INPUT : 0\r\n");
        break;
        
        case '1':
        im920.write((char)0x01);
        im_buf[im_buf_n] = '1';
        im_buf_n ++;
        pc.printf("INPUT : 1\r\n");
        break;
        
        case '?':
        Help();
        break;
        
        case '\n':
        if(header_set){
            im920.send();
            pc.printf("Send : %s\r\n", im_buf);
            header_set = false;
            for(int i = 0; i < im_buf_n; i ++){
                im_buf[i] = '\0';
            }
            im_buf_n = 0;
        }
        break;
    }
}

void ReadSensorSetup(){
    pc.printf("\r\n");
    pc.printf("****Sensor Setup****\r\n");
    
    if(im920.data[1] == 1){
        pc.printf("GPS         : OK\r\n");   
    }else{
        pc.printf("GPS         : NG\r\n");
    }
    if(im920.data[2] == 1){
        pc.printf("LPS22HB     : OK\r\n");
    }else{
        pc.printf("LPS22HB     : NG\r\n");  
    }
    if(im920.data[3] == 1){
        pc.printf("MPU9250     : OK\r\n");
    }else{
        pc.printf("MPU9250     : NG\r\n");
    }
    if(im920.data[4] == 1){
        pc.printf("MPU9250_MAG : OK\r\n");
    }else{
        pc.printf("MPU9250_MAG : NG\r\n");
    }
    if(im920.data[5] == 1){
        pc.printf("INA226_MAIN : OK\r\n");
    }else{
        pc.printf("INA226_MAIN : NG\r\n");
    }
    if(im920.data[6] == 1){
        pc.printf("INA226_SEP  : OK\r\n");
    }else{
        pc.printf("INA226_SEP  : NG\r\n");
    }
    
    for(int i = 0; i < 20; i++){
     pc.printf("*");   
    }
    pc.printf("\r\n");
}

void ReadData(){
    //pc.printf("***Acquired Data****\r\n");
    pc.printf("***Recieved Data****\r\n");
    int bit = 1;
    nich_status = im920.data[bit];
    bit += 1;
    pc.printf("Nichrome Status : %c\r\n",nich_status);
    lat = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Latitude        : %.7f\r\n",lat);
    lon = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Longitude       : %.7f\r\n",lon);
    height = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Height          : %.7f\r\n",height);
    
    press = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Pressure        : %.4f[hPa]\r\n", press);
    temp = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Temperarure     : %.2f[℃]\r\n", temp);
    altitude = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Altitude        : %.2f[m]\r\n", altitude);
    
    voltage_main = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Voltage Main    : %.2f\r\n",voltage_main);
    current_main = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Crrent Main     : %.2f\r\n",current_main);
    voltage_sep = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Voltage Sep     : %.2f\r\n",voltage_sep);
    current_sep = (float)im920.toFloat(bit);
    bit += 4;
    pc.printf("Current Sep     : %.2f\r\n",current_sep);
    
    
    
    for(int i = 0; i < 20; i++){
        pc.printf("*");
    }
    pc.printf("\r\n\r\n");
}

void Read0xA5(){
    for(int i = 0; i < 20; i++){
        pc.printf("*");
    }
    pc.printf("\r\nIM920 ECHO : ");
    
    switch(im920.data[1]){
        case 0x01:
        pc.printf("SEPARATE\r\n");
        break;
        
        case 0x00:
        pc.printf("STOP SEPARATE\r\n");
        break;
    }
    
    for(int i = 0; i < 20; i++){
        pc.printf("*");
    }
    pc.printf("\r\n");
}

void Help(){
    pc.printf("\r\n");
    pc.printf("**I can help you!***\r\n");
    pc.printf("0 : Stop Separate\r\n");
    pc.printf("1 : Separate\r\n");
    pc.printf("? : Help\r\n");
    pc.printf("********************\r\n\r\n");    
}