多线程4加速度计 - 数据不稳定

时间:2015-09-08 04:56:04

标签: c++ multithreading accelerometer

我今年刚开始学习C ++。目前,我使用四个加速度计来计算我学校项目的人体运动。我尝试使用多线程,但数据输出不稳定,因为当我多次尝试时,一些数据会改变。它有时会提供正确的输出数据。同时在屏幕上打印时,输出将保存到Microsoft Excel。但是,当我单独尝试加速度计时,它似乎工作得很好。只有当我将它们多线程连接时,输出才会改变。我似乎无法找到导致输出数据定期更改的原因。我感谢任何帮助。请原谅我的语法错误不是我的母语。谢谢= D

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <process.h>
#include <Windows.h>
#include "erslib.h"

struct SENSOR_DATA {
    int ID_1, ID_2, ID_3,ID_4;
    int y_1, y_2, y_3, y_4;
    int z_1, z_2, z_3, z_4;
    int x_1, x_2, x_3, x_4;
};

void Thread_0(void);
void Thread_1(void);
void Thread_2(void);
void Thread_3(void);

FILE *fp[4];
errno_t err[4];
int key = 0;

int MainLoopFlag = true;
SENSOR_DATA acc;

int main()
{
 DWORD ThreadID_0;
 DWORD ThreadID_1;
 DWORD ThreadID_2;
 DWORD ThreadID_3;

HANDLE Handle_0 = NULL;
HANDLE Handle_1 = NULL; 
HANDLE Handle_2 = NULL; 
HANDLE Handle_3 = NULL; 

Handle_0 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_0, 0, 0, &ThreadID_0);
Sleep(1000);
Handle_1 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_1, 0, 0, &ThreadID_1);
Sleep(1000);
Handle_2 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_2, 0, 0, &ThreadID_2);
Sleep(1000);
Handle_3 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_3, 0, 0, &ThreadID_3);
Sleep(1000);

WaitForSingleObject(Thread_0, INFINITE);
WaitForSingleObject(Thread_1, INFINITE);
WaitForSingleObject(Thread_2, INFINITE);
WaitForSingleObject(Thread_3, INFINITE);

//////Print and Data Output//////    
while(MainLoopFlag){
    if(_kbhit()){
        key = _getch();  //a key to start data saving//
        if(key == 'a' && Save_flag==0){
            printf("Preparing for saving...\n");
            Sleep(5000);
            printf("Saving start\n");

            char * fname_0 = ("C:\\Users\\Desktop\\adam\\ID 1\\test02.csv");
            err[0] = fopen_s(&fp[0], fname_0,"w+");
            if (fp[0]==NULL) {
                printf("%s File cannot be opened。\n",fname_0);
            return -1;
            }
            char * fname_1 = ("C:\\Users\\Desktop\\adam\\ID 2\\test02.csv");
            err[1] = fopen_s(&fp[1], fname_1,"w+");
            if (fp[1]==NULL) {
                printf("%s File cannot be opened。\n",fname_1);
                return -1;
            }
            char * fname_2 = ("C:\\Users\\Desktop\\adam\\ID 3\\test02.csv");
            err[2] = fopen_s(&fp[2], fname_2,"w+");
            if (fp[2]==NULL) {
                printf("%s File cannot be opened。\n",fname_2);
                return -1;
            }
            char * fname_3 = ("C:\\Users\\Desktop\\adam\\ID 4\\test02.csv");
            err[3] = fopen_s(&fp[3], fname_3,"w+");
            if (fp[3]==NULL) {
                printf("%s File cannot be opened。\n",fname_3);
                return -1;
            }
            Save_flag=1;
            while(MainLoopFlag && Save_flag==1){ 
                printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);
                fprintf(fp[0],"%d, %d, %d, %d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);

                printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);
                fprintf(fp[1],"%d, %d, %d, %d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);

                printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);
                fprintf(fp[2],"%d, %d, %d, %d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);

                printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);
                fprintf(fp[3],"%d, %d, %d, %d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);


                if(_kbhit()){
                    key = _getch();
                    //s key to stop data saving
                     if(key == 's' && Save_flag==1){
                        printf("End Data saving\n");

                        Save_flag=0;
                        fclose(fp[0]);
                        fclose(fp[1]);
                        fclose(fp[2]);
                        fclose(fp[3]);
                        MainLoopFlag = false;}
                }

            }

        }
    }
}



// Close Handle
CloseHandle(Handle_0);
CloseHandle(Handle_1);
CloseHandle(Handle_2);
CloseHandle(Handle_3);

return 0;
}

//--------------------------------------------------------------------------
// Name: Thread_1(void)
//--------------------------------------------------------------------------
void Thread_1(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 20;
int flag=0;
int temp_char_1[7];


ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);

//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);


// 1となる位置を調べる
flag=0;

while(MainLoopFlag){
    n=ERS_CheckRecv(ComPort);
    ERS_Recv(ComPort,buf,n);
    for(i=0;i<n;i++){
        if((buf[i] & 0x80) == 0x80){
            flag=1; 
            break;
        }
    }
    if(flag==1)break;
    Sleep(1);
}

// 1 となる位置から後ろをbuf_dataに入れる

n_cou = n - i;
for(j=0;j<n_cou;j++) 
    buf_data[j]=buf[i+j];

while(MainLoopFlag){
    while(MainLoopFlag && n_cou<5){
        n=ERS_CheckRecv(ComPort);
        ERS_Recv(ComPort,buf,n);
        for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
        n_cou=n_cou+n;
    }


    for(i=0;i<5;i++)bin_data[i]=buf_data[i];

    temp_char_1[0] = (bin_data[0] & 0x38) >> 3; //ID
    acc.ID_2 = (int)temp_char_1[0];

    temp_char_1[1] = (bin_data[0] & 0x07) << 7; // x = 3bit

    temp_char_1[2] = (bin_data[1] & 0x38) << 4; // y = 3bit

    temp_char_1[3] = (bin_data[1] & 0x07) << 7; // z = 3bit

    temp_char_1[4] = (bin_data[2] & 0x7F) | temp_char_1[1]; 
    acc.x_2 = (int)temp_char_1[4];

    temp_char_1[5] = (bin_data[3] & 0x7F) | temp_char_1[2];
    acc.y_2 = (int)temp_char_1[5];

    temp_char_1[6] = (bin_data[4] & 0x7F) | temp_char_1[3];
    acc.z_2 = (int)temp_char_1[6];

    for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];  
    n_cou=n_cou-5;
}

    ERS_CloseAll();
}

//--------------------------------------------------------------------------
// Name: Thread_2(void)
//--------------------------------------------------------------------------
void Thread_2(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 15;
int flag=0;
int temp_char_2[7];


ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);

//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);


// 1となる位置を調べる
flag=0;

while(MainLoopFlag){
    n=ERS_CheckRecv(ComPort);
    ERS_Recv(ComPort,buf,n);
    for(i=0;i<n;i++){
        if((buf[i] & 0x80) == 0x80){
            flag=1; 
            break;
        }
    }
    if(flag==1)break;
    Sleep(1);
}

// 1 となる位置から後ろをbuf_dataに入れる

n_cou = n - i;
for(j=0;j<n_cou;j++) 
    buf_data[j]=buf[i+j];

while(MainLoopFlag){
    while(MainLoopFlag && n_cou<5){
        n=ERS_CheckRecv(ComPort);
        ERS_Recv(ComPort,buf,n);
        for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
        n_cou=n_cou+n;
    }


    for(i=0;i<5;i++)bin_data[i]=buf_data[i];
    temp_char_2[0] = (bin_data[0] & 0x38) >> 3; //ID
    acc.ID_3 = (int)temp_char_2[0];

    temp_char_2[1] = (bin_data[0] & 0x07) << 7; // x = 3bit

    temp_char_2[2] = (bin_data[1] & 0x38) << 4; // y = 3bit

    temp_char_2[3] = (bin_data[1] & 0x07) << 7; // z = 3bit

    temp_char_2[4] = (bin_data[2] & 0x7F) | temp_char_2[1]; 
    acc.x_3 = (int)temp_char_2[4];

    temp_char_2[5] = (bin_data[3] & 0x7F) | temp_char_2[2];
    acc.y_3 = (int)temp_char_2[5];

    temp_char_2[6] = (bin_data[4] & 0x7F) | temp_char_2[3];
    acc.z_3 = (int)temp_char_2[6];

    for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];  
    n_cou=n_cou-5;
}

    ERS_CloseAll();
}

//--------------------------------------------------------------------------
// Name: Thread_3(void)
//--------------------------------------------------------------------------
void Thread_3(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 3;
int flag=0;
int temp_char_3[7];


ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);

//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);


// 1となる位置を調べる
flag=0;

while(MainLoopFlag){
    n=ERS_CheckRecv(ComPort);
    ERS_Recv(ComPort,buf,n);
    for(i=0;i<n;i++){
        if((buf[i] & 0x80) == 0x80){
            flag=1; 
            break;
        }
    }
    if(flag==1)break;
    Sleep(1);
}

// 1 となる位置から後ろをbuf_dataに入れる

n_cou = n - i;
for(j=0;j<n_cou;j++) 
    buf_data[j]=buf[i+j];

//for(i=0;i<n_cou;i++)printf("%x ",buf_data[i]);
//printf("\n");

while(MainLoopFlag){
    while(MainLoopFlag && n_cou<5){
        n=ERS_CheckRecv(ComPort);
        ERS_Recv(ComPort,buf,n);
        for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
        n_cou=n_cou+n;
    }


    for(i=0;i<5;i++)bin_data[i]=buf_data[i];
    //for(i=0;i<5;i++)printf("%x ",bin_data[i]);
    //printf("\n");

    temp_char_3[0] = (bin_data[0] & 0x38) >> 3; //ID
    acc.ID_4 = (int)temp_char_3[0];

    temp_char_3[1] = (bin_data[0] & 0x07) << 7; // x = 3bit

    temp_char_3[2] = (bin_data[1] & 0x38) << 4; // y = 3bit

    temp_char_3[3] = (bin_data[1] & 0x07) << 7; // z = 3bit

    temp_char_3[4] = (bin_data[2] & 0x7F) | temp_char_3[1]; 
    acc.x_4 = (int)temp_char_3[4];

    temp_char_3[5] = (bin_data[3] & 0x7F) | temp_char_3[2];
    acc.y_4 = (int)temp_char_3[5];

    temp_char_3[6] = (bin_data[4] & 0x7F) | temp_char_3[3];
    acc.z_4 = (int)temp_char_3[6];

    for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];  
    n_cou=n_cou-5;
}

    ERS_CloseAll();
}

正确的数据: (实施例)

ID-> 1,x-> 47,y-> 147,z-> 298

ID-> 2,x-> 298,y-> 25,z-> 147

ID-> 3,x-> 47,y-> 147,z-> 298

ID-> 4,x-> 213,y-> 123,z-> 43

ID-> 1,x-> 49,y-> 152,z-> 222

ID-> 2,x-> 256,y-> 30,z-> 155

ID-> 3,x-> 47,y-> 147,z-> 298

ID-> 4,x-> 221,y-> 132,z-> 54

数据不正确:

ID-> 1,x-> 905,y-> 179,z-> 20

ID-> 6,x-> 47,y-> 147,z-> 298

ID-> 0,x-> 0,y-> 0,z-> 0

ID-> 4,x-> 1010,y-> 56,z-> 23

ID-> 1,x-> 905,y-> 179,z-> 20

ID-> 6,x-> 47,y-> 147,z-> 298

ID-> 0,x-> 0,y-> 0,z-> 0

ID-> 4,x-> 1010,y-> 56,z-> 23

基本上,当您移动加速度计时,ID不会发生变化,而x,y和z会发生变化。

0 个答案:

没有答案