Skip to content

MATLAB data receiver script can't handle "partial chunks" #23

@vladysor

Description

@vladysor

#10 must be solved first


To maximize the USB efficiency, data transmission is arranged in chunks. Typical transmitter sketch looks like this:

// chunk_size in bytes, for OTG_FS "64" is passed
void transfer_serial_data(uint16_t* buf, const uint16_t buf_size, const uint16_t chunk_size) {
    for (uint16_t i = 0; i < (buf_size*2); i += chunk_size) {
        uint16_t cur_chunk_size = ((buf_size*2) - i < chunk_size) ? (buf_size*2 - i) : chunk_size;
        Serial.write((const uint8_t *) buf + i, cur_chunk_size);
    }
}

If data is not exactly the multiple of chunk_size, the last chunk is recalculated as (buf_size*2 - i), which allows to have a buf of any size.

However in MATLAB receiver script reads full chunk_size, regardless of data_length, typically like this:

function data = read_16bit_data(arduino, data_length)
    chunk_size = 32; % in bytes
    data_length_byte = data_length*2;

    raw_data_8bit = zeros(data_length_byte/chunk_size, chunk_size);
    raw_data_16bit = zeros(data_length_byte/chunk_size, chunk_size/2);
    
    % readings
    for i = 1:(data_length_byte/chunk_size)
        raw_data_8bit(i, :) = read(arduino, chunk_size, 'uint8');
        raw_data_16bit(i, :) = typecast_uint8(raw_data_8bit(i, :));
    end
    
    % rearrange by channel
    data = reshape(raw_data_16bit', 1, []);
end

function casted_data = typecast_uint8(data)
    reshaped_data = reshape(data, 2, []);
    casted_data = bitshift(uint16(reshaped_data(2, :)), 8) + uint16(reshaped_data(1, :));
end

Improve the receiver script with better performance and ability to handle partial chunks. Implement this in /libraries/SensEdu/examples/Basic_UltraSound/matlab/Basic_UltraSound_ReadData.m, and test for different mic_data_size that are not multiple of 32bytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requestgood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions