@@ -18,6 +18,62 @@ LOG_MODULE_REGISTER(sim_baro, LOG_LEVEL_INF);
1818#define MAX_LINE_LENGTH 512
1919#define MAX_CSV_ROWS 10000
2020
21+ // OpenRocket CSV Column definitions
22+ #define CSV_COL_TIMESTAMP 0
23+ #define CSV_COL_ALTITUDE 1
24+ #define CSV_COL_VERTICAL_VELO 2
25+ #define CSV_COL_VERTICAL_ACCEL 3
26+ #define CSV_COL_TOTAL_VELO 4
27+ #define CSV_COL_TOTAL_ACCEL 5
28+ #define CSV_COL_POS_EAST 6
29+ #define CSV_COL_POS_NORTH 7
30+ #define CSV_COL_GPS_LAT_DIST 8
31+ #define CSV_COL_GPS_LAT_DIR 9
32+ #define CSV_COL_GPS_LAT_VELO 10
33+ #define CSV_COL_GPS_LAT_ACCEL 11
34+ #define CSV_COL_LATITUDE 12
35+ #define CSV_COL_LONGITUDE 13
36+ #define CSV_COL_GRAVITY 14
37+ #define CSV_COL_ANGLE_ATTACK 15
38+ #define CSV_COL_ROLL_RATE 16
39+ #define CSV_COL_PITCH_RATE 17
40+ #define CSV_COL_YAW_RATE 18
41+ #define CSV_COL_MASS 19
42+ #define CSV_COL_MOTOR_MASS 20
43+ #define CSV_COL_LONG_MMOI 21
44+ #define CSV_COL_ROT_MMOI 22
45+ #define CSV_COL_CP_LOCATION 23
46+ #define CSV_COL_CG_LOCATION 24
47+ #define CSV_COL_STABILITY 25
48+ #define CSV_COL_MACH_NUMBER 26
49+ #define CSV_COL_REYNOLDS_NUMBER 27
50+ #define CSV_COL_THRUST 28
51+ #define CSV_COL_DRAG 29
52+ #define CSV_COL_DRAG_COEFF 30
53+ #define CSV_COL_AXIAL_DRAG_COEFF 31
54+ #define CSV_COL_FRIC_DRAG_COEFF 32
55+ #define CSV_COL_PRESSURE_DRAG_COEFF 33
56+ #define CSV_COL_BASE_DRAG_COEFF 34
57+ #define CSV_COL_NORM_FORCE_COEFF 35
58+ #define CSV_COL_PITCH_MOM_COEFF 36
59+ #define CSV_COL_YAW_MOM_COEFF 37
60+ #define CSV_COL_SIDE_FORCE_COEFF 38
61+ #define CSV_COL_ROLL_MOM_COEFF 39
62+ #define CSV_COL_ROLL_FORCING_COEFF 40
63+ #define CSV_COL_ROLL_DAMPING_COEFF 41
64+ #define CSV_COL_PITCH_DAMPING_COEFF 42
65+ #define CSV_COL_CORIOLIS_ACCEL 43
66+ #define CSV_COL_REF_LENGTH 44
67+ #define CSV_COL_REF_AREA 45
68+ #define CSV_COL_VERTICAL_ORIENT 46
69+ #define CSV_COL_LATERAL_ORIENT 47
70+ #define CSV_COL_WIND_SPEED 48
71+ #define CSV_COL_AIR_TEMP 49
72+ #define CSV_COL_AIR_PRESSURE 50
73+ #define CSV_COL_SPEED_OF_SOUND 51
74+ #define CSV_COL_SIM_TIMESTEP 52
75+ #define CSV_COL_COMPUTATION_TIME 53
76+
2177struct csv_row {
2278 int64_t timestamp_ms ;
2379 float pressure_mbar ; // mbar = hPa
@@ -44,30 +100,81 @@ struct sim_baro_data {
44100 uint32_t sample_count ;
45101};
46102
47- static int parse_csv_line (const char * line , struct csv_row * row )
48- {
49- int64_t timestamp ;
50- float altitude , baseline , pressure , temperature ;
51- int parsed ;
52103
53- // Read first 6 fields (we ignore the rest)
54- // Format: Timestamp (ms),State,Altitude (m),Baseline altitude (m),Pressure (mbar),Barom. Temp
55- // (0.01 C),...
56- parsed = sscanf (line , "%lld,%*[^,],%f,%f,%f,%f" , & timestamp , & altitude , & baseline , & pressure ,
57- & temperature );
58104
59- if (parsed != 5 ) {
60- return -1 ;
105+ // Helper to get Nth field from CSV line
106+ static const char * get_csv_field (const char * line , int field_index , char * buffer , size_t buf_size )
107+ {
108+ const char * start = line ;
109+ const char * end ;
110+ int current_field = 0 ;
111+
112+ // Skip to the desired field
113+ while (current_field < field_index && * start ) {
114+ if (* start == ',' ) {
115+ current_field ++ ;
116+ }
117+ start ++ ;
118+ }
119+
120+ if (current_field != field_index ) {
121+ return NULL ; // Field not found
122+ }
123+
124+ // Find the end of this field
125+ end = start ;
126+ while (* end && * end != ',' && * end != '\n' && * end != '\r' ) {
127+ end ++ ;
128+ }
129+
130+ // Copy to buffer
131+ size_t len = end - start ;
132+ if (len >= buf_size ) {
133+ len = buf_size - 1 ;
61134 }
135+
136+ strncpy (buffer , start , len );
137+ buffer [len ] = '\0' ;
138+
139+ return buffer ;
140+ }
141+
142+
143+
144+ // Helper macro to extract and parse a field
145+ #define GET_CSV_INT64 (line , col , dest ) do { \
146+ char _buf[64]; \
147+ if (!get_csv_field(line, col, _buf, sizeof(_buf))) return -1; \
148+ dest = atoll(_buf); \
149+ } while(0)
62150
63- row -> timestamp_ms = timestamp ;
64- row -> altitude_m = altitude ;
65- row -> pressure_mbar = pressure / 100.0f ; // Convert from cmbar to mbar (hPa)
66- row -> temperature_c = temperature / 100.0f ; // Convert from centi-°C to °C
151+ // Helper macro to extract and parse a field
152+ #define GET_CSV_FLOAT (line , col , dest ) do { \
153+ char _buf[64]; \
154+ if (!get_csv_field(line, col, _buf, sizeof(_buf))) return -1; \
155+ dest = atof(_buf); \
156+ } while(0)
67157
158+ static int parse_csv_line (const char * line , struct csv_row * row )
159+ {
160+ float pressure_raw , temp_raw , timestamp_raw , altitude_raw ;
161+
162+ GET_CSV_FLOAT (line , CSV_COL_TIMESTAMP , timestamp_raw );
163+ GET_CSV_FLOAT (line , CSV_COL_ALTITUDE , altitude_raw );
164+ GET_CSV_FLOAT (line , CSV_COL_AIR_PRESSURE , pressure_raw );
165+ GET_CSV_FLOAT (line , CSV_COL_AIR_TEMP , temp_raw );
166+
167+ // Apply unit conversions
168+ row -> timestamp_ms = timestamp_raw * 1000.0f ; // Convert from seconds to milliseconds
169+ row -> altitude_m = altitude_raw ;
170+ row -> pressure_mbar = pressure_raw ; // mbar = hPa
171+ row -> temperature_c = temp_raw ;
172+
68173 return 0 ;
69174}
70175
176+
177+
71178static int load_csv_data (struct sim_baro_data * data )
72179{
73180 LOG_INF ("═══════════════════════════════════════════════" );
0 commit comments