Schkedy
Schkedy

Reputation: 21

Freezing idle function in GLUT

I have an Arduino that sends data from the accelerometer to the computer. When I tried to read data exactly in the idle() function it doesn't work. The program just draws a window with a cube, and that window is freezing (to close the window I have to kill the process).

#include <cmath>
#include <iostream>
#include <GL/glut.h>
#include <termios.h>
#include <cstring>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

float robotX = 0.0f;
float robotY = 0.0f;
float robotZ = 0.0f;
int ASCIInum = 48;

//Reading data from Arduino
float* charArrayToFloatArray(unsigned char *a, int size) {
    auto *floatArray = new float[7];
    bool flagPoint = 1;
    int count = 0;
    int indexOfPoint = 0;
    bool flagSign = 0;
    for (int i = 0;i<size;i++) {
        if (a[i] == '\t') {
            if (flagSign)
                floatArray[count]*=-1;
            cout<<floatArray[count] << " ";
            flagSign = 0;
            count++;
            flagPoint = 1;
            if (count == 7)
                i = size+1;
        }
        else if (a[i]== '.') {
            flagPoint = 0;
            indexOfPoint = i;
        }
        else if(a[i] == '-')
            flagSign = 1;
        else if (flagPoint)
            floatArray[count] = floatArray[count]*10+(a[i]-ASCIInum);
        else if (!flagPoint)
            floatArray[count]+= (a[i]-ASCIInum)*pow(10,indexOfPoint - i);
    }
    return floatArray;
}



void initOpenGL() {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 1.0, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void updateRobotPosition(float x, float y, float z) {
    robotX = x;
    robotY = y;
    robotZ = z;
    glutPostRedisplay();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(0.0, 0.0, 5.0,
              0.0, 0.0, 0.0, 
              0.0, 1.0, 0.0);

    glTranslatef(robotX, robotY, robotZ);
    glColor3f(1.0, 0.0, 0.0);
    glutSolidCube(0.5); 
    glutSwapBuffers();
}

void idle() {

    unsigned char read_buf[64];
    int serial_port = open("/dev/ttyUSB0", O_RDWR); //Problem is happened after that function
    struct termios tty;
    if(tcgetattr(serial_port, &tty) != 0) {
        std::cerr << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
        exit(1);
    }
    cfsetispeed(&tty, B115200);
    cfsetospeed(&tty, B115200);

    tty.c_cflag &= ~PARENB; 
    tty.c_cflag &= ~CSTOPB; 
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;
    tty.c_cflag &= ~CRTSCTS;
    tty.c_cflag |= CREAD | CLOCAL;

    if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
        std::cerr << "Error " << errno << " from tcsetattr: " << strerror(errno) << std::endl;
        exit(1);

    int num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
    close(serial_port);

    float *a = charArrayToFloatArray(read_buf, num_bytes);
    static float x = 0, y = 0, z = 0;
    x += a[0]*0.1*0.1;
    y += a[1]*0.1*0.1;
    z += a[2]*0.1*0.1;
    
    close(serial_port);
    updateRobotPosition(x, y, z);
}

int main(int argc, char **argv) { 

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Robot Simulation");
    initOpenGL();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutMainLoop();

    return 0;
}

When I only read data without the Glut "environment" (just in the main function), the program reads all the data from the flow how I want it.

Upvotes: 2

Views: 41

Answers (0)

Related Questions