function example_promove_csv (filename)
% Example script for reading and plotting ProMove-mini CSV data exported from Inertia Studio
% @ Copyright 2016 Inertia Technology
% 
% Usage: example_promove_csv(filename)
% where filename is a string, e.g. filename = 'C:\Inertia Studio\3.0.0\logfile-test_2.csv'

% % Example structure of the CSV file exported from Inertia Studio
% # Created with Inertia Studio v3.0.0 (ded972a)
% # Converted from logfile-test_1.itlog
% # Created at 2016-08-24 15:34:55
% # 
% # Node 123 (ProMove-mini revD, v0.10.5)
% # Global: Transmit Data: yes, Sampling Rate: 200 Hz, Sampling Mode: Synchronous, Transmit Type: Radio, No. of Nodes: 9, Frequency Channel: 26
% # Accelerometer: Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 200 Hz, Range: ±8 g
% # Gyroscope: Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 200 Hz, Range: ±2000 °/s
% # Compass: Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 100 Hz, Range: ±0 gauss
% # Accelerometer (High G): Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 200 Hz, Range: ±100 g
% # Temperature: Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 200 Hz, Range: -40 - 85 °C
% # Barometer: Enabled: yes, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 25 Hz, Range: 260 - 1260 hPa
% # IMA: Enabled: no, Transmit Data: no, Log to Flash: no, Sampling Rate: 200 Hz, IMA Interval: 10 s, Values per Message: 1
% # RTC: Enabled: no, Transmit Data: yes, Log to Flash: yes, Sampling Rate: 200 Hz, External Triggered: yes
% # 
% timestamp,node-id,ax,ay,az,cx,cy,cz,gx,gy,gz,roll,pitch,yaw
% 217.475,123,0.608127,0.794875,9.27274,0.408,0.408,-0.279,0,0,0.0609756,4.89951,-3.73854,-45.4937
% 217.48,123,0.61531,0.790087,9.26556,,,,0,-0.0609756,-0.0609756,4.89945,-3.73866,-45.4937

% The following fields are defined (presence depends on configuration):
%
% timestamp   - time instance of the sample [seconds]
% node-id     - ID-number of the node that took this sample
%
% ax, ay, ax  - accelerometer output; acceleration [m/s^2]
% cx, cy, cz  - compass sensor output; magnetic field strength [gauss]
% gx, gy, gz  - gyroscope output; angular velocity [deg/s]
% ax-hg, ay-hg, az-hg
%             - high-g accelerometer output; acceleration [m/s^2]
% quat1, quat2, quat3, quat4 
%             - orientation quaternion
% roll, pitch, yaw
%             - euler angles for orientation [deg]
%
% temp        - imu temperature [deg C]
% rssi        - radio signal strength indicator [dBm]
%
% rtc         - event time stamping
% io1,io2     - gateway i/o port status [0/1]
%
% cpuTemp     - CPU temperature [deg C]
% bat         - battery level [volt]
%
% seqNr       - sample sequence number (16bit)
% slot        - slot number of the node in the MAC protocol
% index       - sample index in MAC packet
% lostSamples - number of samples lost since last sample of this node

% Setting for placing all figures in one tabbed window instead of a new window
% for each figure
set(0, 'DefaultFigureWindowStyle', 'docked');

% First parse the beginning of the file and identify column headers
my_fid = fopen(filename);
my_headersLine = textscan(my_fid, '%s', 1, 'CommentStyle', '#');
my_headers = strsplit(my_headersLine{1}{1}, ',');
my_numColumns = size(my_headers, 2);
my_format = repmat('%f', 1, my_numColumns);
my_data = textscan(my_fid, my_format, 'Delimiter', ',');
fclose(my_fid);

% Find time column
my_time = my_data{find(strcmp(my_headers, 'timestamp'))};
 
% Plot acceleration
my_idxAx = find(strcmp(my_headers, 'ax'));
my_idxAy = find(strcmp(my_headers, 'ay'));
my_idxAz = find(strcmp(my_headers, 'az'));
if ~isempty(my_idxAx) && ~isempty(my_idxAy) && ~isempty(my_idxAz)
    figure('Name', 'Accelerometer', 'NumberTitle', 'off');
    my_ax = my_data{my_idxAx};
    my_ay = my_data{my_idxAy};
    my_az = my_data{my_idxAz};
    my_accel = [my_ax my_ay my_az];
    plot(my_time, my_accel);
    legend('X', 'Y', 'Z');
    ylabel('Acceleration [m/s^2]');
    xlabel('Time [sec]');
    title('Accelerometer');
end

% Plot acceleration from high-g sensor
my_idxAx = find(strcmp(my_headers, 'ax-hg'));
my_idxAy = find(strcmp(my_headers, 'ay-hg'));
my_idxAz = find(strcmp(my_headers, 'az-hg'));
if ~isempty(my_idxAx) && ~isempty(my_idxAy) && ~isempty(my_idxAz)
    figure('Name', 'Accelerometer (high-g)', 'NumberTitle', 'off');
    my_ax = my_data{my_idxAx};
    my_ay = my_data{my_idxAy};
    my_az = my_data{my_idxAz};
    my_accel = [my_ax my_ay my_az];
    plot(my_time, my_accel);
    legend('X', 'Y', 'Z');
    ylabel('Acceleration [m/s^2]');
    xlabel('Time [sec]');
    title('Accelerometer (high-g)');
end


% Plot gyro
my_idxGx = find(strcmp(my_headers, 'gx'));
my_idxGy = find(strcmp(my_headers, 'gy'));
my_idxGz = find(strcmp(my_headers, 'gz'));
if ~isempty(my_idxGx) && ~isempty(my_idxGy) && ~isempty(my_idxGz)
    figure('Name', 'Gyroscope', 'NumberTitle', 'off');
    my_gx = my_data{my_idxGx};
    my_gy = my_data{my_idxGy};
    my_gz = my_data{my_idxGz};
    my_gyro = [my_gx my_gy my_gz];
    plot(my_time, my_gyro);
    legend('X', 'Y', 'Z');
    ylabel('Turn rate [deg/s]');
    xlabel('Time [sec]');
    title('Gyroscope');
end

% Plot compass
my_idxCx = find(strcmp(my_headers, 'cx'));
my_idxCy = find(strcmp(my_headers, 'cy'));
my_idxCz = find(strcmp(my_headers, 'cz'));
if ~isempty(my_idxCx) && ~isempty(my_idxCy) && ~isempty(my_idxCz)
    figure('Name', 'Compass', 'NumberTitle', 'off');
    my_cx = my_data{my_idxCx};
    my_cy = my_data{my_idxCy};
    my_cz = my_data{my_idxCz};
    my_compass = [my_cx my_cy my_cz];
    plot(my_time(~isnan(my_compass(:,1))), my_compass(~isnan(my_compass(:,1)),:));
    legend('X', 'Y', 'Z');
    ylabel('Magnetic field intensity [Gauss]');
    xlabel('Time [sec]');
    title('Compass');
end
 
% Plot orientation Euler angles
my_idxRoll = find(strcmp(my_headers, 'roll'));
my_idxPitch = find(strcmp(my_headers, 'pitch'));
my_idxYaw = find(strcmp(my_headers, 'yaw'));
if ~isempty(my_idxRoll) && ~isempty(my_idxPitch) && ~isempty(my_idxYaw)
    figure('Name', 'Euler Angles', 'NumberTitle', 'off');
    my_roll = my_data{my_idxRoll};
    my_pitch = my_data{my_idxPitch};
    my_yaw = my_data{my_idxYaw};
    my_euler = [my_roll my_pitch my_yaw];
    plot(my_time, my_euler);
    legend('X', 'Y', 'Z');
    ylabel('Angle [deg]');
    xlabel('Time [sec]');
    title('Orientation Euler angles');
end

% Plot barometric pressure
my_idxPr = find(strcmp(my_headers, 'pressure'));
if ~isempty(my_idxPr)
    figure('Name', 'Barometric Pressure', 'NumberTitle', 'off');
    my_pressure = my_data{my_idxPr};
    plot(my_time(~isnan(my_pressure(:,1))), my_pressure(~isnan(my_pressure(:,1)),:));
    ylabel('Pressure [hPa]');
    xlabel('Time [sec]');
    title('Barometric Pressure');
end

% Plot IMU temperature
my_idxTm = find(strcmp(my_headers, 'temp'));
if ~isempty(my_idxTm)
    figure('Name', 'IMU Temperature', 'NumberTitle', 'off');
    my_temp = my_data{my_idxTm};
    plot(my_time(~isnan(my_temp(:,1))), my_temp(~isnan(my_temp(:,1)),:));
    ylabel('Temperature [deg]');
    xlabel('Time [sec]');
    title('IMU Temperature');
end


