Increasing the Data Read Frequency of MGS1600GY

1 year 7 months ago - 1 year 7 months ago #29535613 by EVK
Hi all,

I have changed MGS1600GY's ROS Driver a little, but kept the sensor_connect() and init_port() functions mostly the same(check the code blocks below). I am able to read position data from the sensor at 10 Hz. Yet no matter, I increase the frequency of the rate object (ros::Rate(10)), I cannot read position data from the sensor more than 10 Hz. Is there any way I can increase this frequency?

Code blocks:
void MagSens::init_port() {
    if(!is_connected())
        return;

    // Get the existing Comm Port Attributes in cwrget
    int BAUDRATE = B115200;
    struct termios newtio;
    tcgetattr(port_handler, &newtio);

    // Set the Tx and Rx Baud Rate to 115200
    cfsetospeed(&newtio, (speed_t)BAUDRATE);
    cfsetispeed(&newtio, (speed_t)BAUDRATE);

    // Enable the Receiver and Set local Mode
    newtio.c_iflag = IGNBRK; // Ignore Break Condition & no processing under input options
    newtio.c_lflag = 0; // Select the RAW Input Mode through Local options
    newtio.c_oflag = 0; // Select the RAW Output Mode through Local options
    newtio.c_cflag |= (CLOCAL | CREAD); // Select the Local Mode & Enable Receiver through Control options
   
    // Make RAW Mode more explicit by turning Canonical Mode off, Echo off, Echo Erase off and Signals off
    newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    // Disable Software Flow Control
    newtio.c_iflag &= ~(IXON | IXOFF | IXANY);

    // Set Data format to 8N1
    newtio.c_cflag &= ~CSIZE; // Mask the Character Size Bits through Control options
    newtio.c_cflag |= CS8; // Select Character Size to 8-Bits through Control options
    newtio.c_cflag &= ~PARENB; // Select Parity Disable through Control options
    newtio.c_cflag &= ~PARODD; // Select the Even Parity (Disabled) through Control options
    newtio.c_cflag &= ~CSTOPB; // Set number of Stop Bits to 1

    // Timeout Parameters. Set to 0 characters (VMIN) and 10 second (VTIME) timeout. This was done to prevent the read call from     blocking indefinitely.*/
    newtio.c_cc[VMIN] = 0;
    newtio.c_cc[VTIME] = 100;

    // Flush the Input buffer and set the attribute NOW without waiting for Data to Complete
    tcflush(port_handler, TCIFLUSH);
    tcsetattr(port_handler, TCSANOW, &newtio);
}

int MagSens::sensor_connect(std::string port) {
    if(is_connected()) {
        std::cout << "Device is connected, attempting to disconnect." << std::endl;
        sensor_disconnect();
    }

    port_handler = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
    if(port_handler == RQ_INVALID_HANDLE) {
        std::cout << "failed." << std::endl;
        throw PortOpenFailed{ };
    }
    std::cout << "Opening port: " << port << " succeeded." << std::endl;
    fcntl(port_handler, F_SETFL, O_APPEND | O_NONBLOCK & ~FNDELAY);
   
    std::cout << "Initializing port...";
    init_port();
    std::cout << "...done." << std::endl;

    return RQ_SUCCESS;
}

ros::Rate{ 10 }; // Frequency of reading from the sensor does not increase no matter I change this "10" here to 15 or 20
while(ros::ok()) {
    ...
    ...
    rate.sleep();
}
 

Please Log in or Create an account to join the conversation.

Moderators: tonysantoni
Time to create page: 0.051 seconds