Compare commits
6 Commits
a948ca19fc
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 684f026f2a | |||
| a1f9a9deab | |||
| 3ea5736fb6 | |||
| c6a870a462 | |||
| c5c5f44ec4 | |||
| d4a21df8a8 |
56
src/array_utils.c
Normal file
56
src/array_utils.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <duckdb.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "arrays.h"
|
||||
|
||||
void allocate_array_for_db_data(float_array *array, const char *table, duckdb_connection *conn) {
|
||||
char query[100];
|
||||
sprintf(query, "Select count(*) from \"%s\"", table);
|
||||
duckdb_result result;
|
||||
duckdb_query(*conn, query, &result);
|
||||
|
||||
duckdb_data_chunk chunk = duckdb_fetch_chunk(result);
|
||||
if (!chunk) {
|
||||
return;
|
||||
}
|
||||
|
||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(chunk, 0);
|
||||
int32_t *col1_data = (int32_t *)duckdb_vector_get_data(col1);
|
||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||
|
||||
if (duckdb_validity_row_is_valid(col1_validity, 0)) {
|
||||
printf("capycity of array: %d\n", col1_data[0]);
|
||||
float_array_allocate(array, col1_data[0]);
|
||||
}
|
||||
duckdb_destroy_data_chunk(&chunk);
|
||||
duckdb_destroy_result(&result);
|
||||
}
|
||||
|
||||
void get_data_from_db(const char *query, duckdb_connection *conn, float_array *info) {
|
||||
duckdb_result result;
|
||||
|
||||
duckdb_query(*conn, query, &result);
|
||||
|
||||
while (true) {
|
||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||
if (!data_chunk) {
|
||||
break;
|
||||
}
|
||||
|
||||
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
||||
|
||||
// set column
|
||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||
void *col1_data = duckdb_vector_get_data(col1);
|
||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||
|
||||
for (idx_t row = 0; row < row_count; row++) {
|
||||
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
||||
float_array_set(info, ((float *)col1_data)[row], -1);
|
||||
}
|
||||
}
|
||||
|
||||
duckdb_destroy_data_chunk(&data_chunk);
|
||||
}
|
||||
duckdb_destroy_result(&result);
|
||||
}
|
||||
6
src/array_utils.h
Normal file
6
src/array_utils.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <duckdb.h>
|
||||
|
||||
#include "arrays.h"
|
||||
|
||||
void allocate_array_for_db_data(float_array *array, const char *table, duckdb_connection *conn);
|
||||
void get_data_from_db(const char *query, duckdb_connection *conn, float_array *info);
|
||||
17
src/arrays.c
17
src/arrays.c
@@ -1,5 +1,6 @@
|
||||
#include "arrays.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool bool_array_get(const bool_array *array, int32_t index) {
|
||||
if (index >= 0 && index < array->length) {
|
||||
@@ -280,12 +281,28 @@ void float_array_set(float_array *array, const float value, int32_t index) {
|
||||
|
||||
if (index == -1) {
|
||||
array->items[array->length - 1] = value;
|
||||
array->length++;
|
||||
return;
|
||||
}
|
||||
|
||||
array->items[index] = value;
|
||||
}
|
||||
|
||||
void float_array_allocate(float_array *array, int32_t capacity) {
|
||||
array->items = calloc(capacity, sizeof(float));
|
||||
array->capacity = capacity;
|
||||
}
|
||||
|
||||
void float_array_init(float_array *array) {
|
||||
array->capacity = 0;
|
||||
array->length = 0;
|
||||
}
|
||||
|
||||
void float_array_free(float_array *array) {
|
||||
free(array->items);
|
||||
float_array_init(array);
|
||||
}
|
||||
|
||||
double double_array_get(const double_array *array, int32_t index) {
|
||||
if (index >= 0 && index < array->length) {
|
||||
return array->items[index];
|
||||
|
||||
15
src/arrays.h
15
src/arrays.h
@@ -12,6 +12,7 @@ typedef struct {
|
||||
} bool_array;
|
||||
|
||||
bool bool_array_get(const bool_array *array, int32_t index);
|
||||
void bool_array_set(bool_array *array, const bool value, int32_t index);
|
||||
|
||||
// --- Char (Strings/Bytes) ---
|
||||
typedef struct {
|
||||
@@ -21,6 +22,7 @@ typedef struct {
|
||||
} char_array;
|
||||
|
||||
char char_array_get(const char_array *array, int32_t index);
|
||||
void char_array_set(char_array *array, const char value, int32_t index);
|
||||
|
||||
// --- Signed Integers ---
|
||||
typedef struct {
|
||||
@@ -30,6 +32,7 @@ typedef struct {
|
||||
} int8_array;
|
||||
|
||||
int8_t int8_array_get(const int8_array *array, int32_t index);
|
||||
void int8_array_set(int8_array *array, const int8_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
int16_t *items;
|
||||
@@ -38,6 +41,7 @@ typedef struct {
|
||||
} int16_array;
|
||||
|
||||
int16_t int16_array_get(const int16_array *array, int32_t index);
|
||||
void int16_array_set(int16_array *array, const int16_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
int32_t *items;
|
||||
@@ -46,6 +50,7 @@ typedef struct {
|
||||
} int32_array;
|
||||
|
||||
int32_t int32_array_get(const int32_array *array, int32_t index);
|
||||
void int32_array_set(int32_array *array, const int32_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
int64_t *items;
|
||||
@@ -54,6 +59,7 @@ typedef struct {
|
||||
} int64_array;
|
||||
|
||||
int64_t int64_array_get(const int64_array *array, int32_t index);
|
||||
void int64_array_set(int64_array *array, const int64_t value, int32_t index);
|
||||
|
||||
// --- Unsigned Integers ---
|
||||
typedef struct {
|
||||
@@ -63,6 +69,7 @@ typedef struct {
|
||||
} uint8_array;
|
||||
|
||||
uint8_t uint8_array_get(const uint8_array *array, int32_t index);
|
||||
void uint8_array_set(uint8_array *array, const uint8_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
uint16_t *items;
|
||||
@@ -71,6 +78,7 @@ typedef struct {
|
||||
} uint16_array;
|
||||
|
||||
uint16_t uint16_array_get(const uint16_array *array, int32_t index);
|
||||
void uint16_array_set(uint16_array *array, const uint16_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
uint32_t *items;
|
||||
@@ -79,6 +87,7 @@ typedef struct {
|
||||
} uint32_array;
|
||||
|
||||
uint32_t uint32_array_get(const uint32_array *array, int32_t index);
|
||||
void uint32_array_set(uint32_array *array, const uint32_t value, int32_t index);
|
||||
|
||||
typedef struct {
|
||||
uint64_t *items;
|
||||
@@ -87,6 +96,7 @@ typedef struct {
|
||||
} uint64_array;
|
||||
|
||||
uint64_t uint64_array_get(const uint64_array *array, int32_t index);
|
||||
void uint64_array_set(uint64_array *array, const uint64_t value, int32_t index);
|
||||
|
||||
// --- Floating Point ---
|
||||
typedef struct {
|
||||
@@ -96,6 +106,10 @@ typedef struct {
|
||||
} float_array;
|
||||
|
||||
float float_array_get(const float_array *array, int32_t index);
|
||||
void float_array_set(float_array *array, const float value, int32_t index);
|
||||
void float_array_allocate(float_array *array, int32_t capacity);
|
||||
void float_array_init(float_array *array);
|
||||
void float_array_free(float_array *array);
|
||||
|
||||
typedef struct {
|
||||
double *items;
|
||||
@@ -104,5 +118,6 @@ typedef struct {
|
||||
} double_array;
|
||||
|
||||
double double_array_get(const double_array *array, int32_t index);
|
||||
void double_array_set(double_array *array, const double value, int32_t index);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,16 +3,10 @@
|
||||
|
||||
#include "lapinfo.h"
|
||||
|
||||
void free_telemetry_info(TelemetryInfo *t) {
|
||||
free(t->data);
|
||||
free(t->validity);
|
||||
// free(t);
|
||||
}
|
||||
|
||||
void destroyLapinfo(LapInfo *info) {
|
||||
free_telemetry_info(&info->throttle_pos);
|
||||
free_telemetry_info(&info->brake_pos);
|
||||
free_telemetry_info(&info->steering_pos);
|
||||
free_telemetry_info(&info->speed);
|
||||
// free(info);
|
||||
free(info->throttle_pos.items);
|
||||
free(info->brake_pos.items);
|
||||
free(info->steering_pos.items);
|
||||
free(info->speed.items);
|
||||
free(info);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "arrays.h"
|
||||
|
||||
typedef enum {
|
||||
BOOL,
|
||||
INT,
|
||||
@@ -22,10 +24,10 @@ typedef struct {
|
||||
typedef struct {
|
||||
int lap_number;
|
||||
double start_time;
|
||||
TelemetryInfo throttle_pos; // freq: 50
|
||||
TelemetryInfo brake_pos; // freq: 50
|
||||
TelemetryInfo steering_pos; // freq: 100
|
||||
TelemetryInfo speed;
|
||||
float_array throttle_pos;
|
||||
float_array brake_pos;
|
||||
float_array steering_pos; // freq: 100
|
||||
float_array speed;
|
||||
} LapInfo;
|
||||
|
||||
void free_telemetry_info(TelemetryInfo *t);
|
||||
|
||||
93
src/main.c
93
src/main.c
@@ -5,6 +5,8 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "array_utils.h"
|
||||
#include "arrays.h"
|
||||
#include "exit_code.h"
|
||||
#include "lapinfo.h"
|
||||
|
||||
@@ -30,54 +32,6 @@ bool is_valid_path(const char *path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo *info) {
|
||||
duckdb_result result;
|
||||
|
||||
duckdb_query(*conn, query, &result);
|
||||
|
||||
while (true) {
|
||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||
if (!data_chunk) {
|
||||
break;
|
||||
}
|
||||
|
||||
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk
|
||||
|
||||
// set column
|
||||
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||
void *col1_data = duckdb_vector_get_data(col1);
|
||||
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||
|
||||
for (idx_t row = 0; row < row_count; row++) {
|
||||
if (duckdb_validity_row_is_valid(col1_validity, row)) {
|
||||
switch (info->type) {
|
||||
case FLOAT:
|
||||
((float *)info->data)[info->data_last_index] = ((float *)col1_data)[row];
|
||||
info->data_last_index++;
|
||||
break;
|
||||
case BOOL:
|
||||
((bool *)info->data)[info->data_last_index] = ((bool *)col1_data)[row];
|
||||
info->data_last_index++;
|
||||
break;
|
||||
case DOUBLE:
|
||||
((double *)info->data)[info->data_last_index] = ((double *)col1_data)[row];
|
||||
info->data_last_index++;
|
||||
break;
|
||||
case INT:
|
||||
((int32_t *)info->data)[info->data_last_index] = ((int32_t *)col1_data)[row];
|
||||
info->data_last_index++;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
printf("NULL");
|
||||
}
|
||||
}
|
||||
|
||||
duckdb_destroy_data_chunk(&data_chunk);
|
||||
}
|
||||
duckdb_destroy_result(&result);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
duckdb_database db;
|
||||
duckdb_connection conn;
|
||||
@@ -184,9 +138,44 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// get the per laptime data
|
||||
// throttle
|
||||
float_array throttle_data_all;
|
||||
float_array_init(&throttle_data_all);
|
||||
allocate_array_for_db_data(&throttle_data_all, "Throttle Pos", &conn);
|
||||
get_data_from_db("Select value as FLOAT from \"Throttle Pos\"\n", &conn, &throttle_data_all);
|
||||
for (int32_t i = 0; i < throttle_data_all.length; ++i) {
|
||||
// printf(" %f", float_array_get(&throttle_data_all, i));
|
||||
}
|
||||
|
||||
printf("Shutting down HardCompound!\n");
|
||||
// brake
|
||||
float_array brake_data_all;
|
||||
float_array_init(&brake_data_all);
|
||||
allocate_array_for_db_data(&brake_data_all, "Brake Pos", &conn);
|
||||
get_data_from_db("Select value as FLOAT from \"Brake Pos\"\n", &conn, &brake_data_all);
|
||||
for (int32_t i = 0; i < brake_data_all.length; ++i) {
|
||||
// printf(" %f", float_array_get(&brake_data_all, i));
|
||||
}
|
||||
|
||||
// steering
|
||||
float_array steering_data_all;
|
||||
float_array_init(&steering_data_all);
|
||||
allocate_array_for_db_data(&steering_data_all, "Steering Pos", &conn);
|
||||
get_data_from_db("Select value as FLOAT from \"Steering Pos\"\n", &conn, &steering_data_all);
|
||||
for (int32_t i = 0; i < steering_data_all.length; ++i) {
|
||||
// printf(" %f", float_array_get(&steering_data_all, i));
|
||||
}
|
||||
// select avg(((value1+value2+value3+value4)/4)*3.6) as speed from "Wheel Speed"
|
||||
// speed
|
||||
float_array speed_data_all;
|
||||
float_array_init(&speed_data_all);
|
||||
allocate_array_for_db_data(&speed_data_all, "Wheel Speed", &conn);
|
||||
get_data_from_db("select ((value1+value2+value3+value4)/4)*3.6 as speed from \"Wheel Speed\"", &conn,
|
||||
&speed_data_all);
|
||||
for (int32_t i = 0; i < speed_data_all.length; ++i) {
|
||||
// printf(" %f", float_array_get(&speed_data_all, i));
|
||||
}
|
||||
|
||||
printf("\nShutting down HardCompound!\n");
|
||||
|
||||
printf("Closing database and any connection.\n");
|
||||
goto cleanup;
|
||||
@@ -196,6 +185,10 @@ cleanup:
|
||||
free(NULL);
|
||||
free(session_data);
|
||||
free(lap_timestamps);
|
||||
float_array_free(&throttle_data_all);
|
||||
float_array_free(&brake_data_all);
|
||||
float_array_free(&steering_data_all);
|
||||
float_array_free(&speed_data_all);
|
||||
duckdb_disconnect(&conn);
|
||||
duckdb_close(&db);
|
||||
goto exit;
|
||||
|
||||
Reference in New Issue
Block a user