Skip to content

Commit

Permalink
Add common message queue type
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusJull committed Jun 4, 2024
1 parent 2f855fc commit b43d727
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 126 deletions.
117 changes: 60 additions & 57 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
# Documentation
/docs/html/
*.pinfo

# Object files
*.o
*.ko
/obj/
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
fetcher
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb
compile_commands.json
.cache

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# Documentation
/docs/html/
*.pinfo

# Object files
*.o
*.ko
/obj/
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
fetcher
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb
compile_commands.json
.cache

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

# Editor files
*.code-workspace
1 change: 1 addition & 0 deletions src/collectors/collectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct {

collector_t collector_search(const char *sensor_name);


/* Collector threads */
void *sysclock_collector(void *args);
void *ms5611_collector(void *args);
Expand Down
22 changes: 11 additions & 11 deletions src/collectors/lsm6dso32_clctr.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../drivers/lsm6dso32/lsm6dso32.h"
#include "../drivers/sensor_api.h"
#include "collectors.h"
#include "sensor_api.h"
#include <stdio.h>

#define return_err(err) return (void *)((uint64_t)errno)
Expand Down Expand Up @@ -70,7 +70,7 @@ void *lsm6dso32_collector(void *args) {
return_err(err);
}

uint8_t data[sizeof(vec3d_t) + 1];
common_t msg;
int16_t temperature;
int16_t x;
int16_t y;
Expand All @@ -83,9 +83,9 @@ void *lsm6dso32_collector(void *args) {
if (err != EOK) {
fprintf(stderr, "LSM6DSO32 could not read temperature: %s\n", strerror(errno));
} else {
data[0] = TAG_TEMPERATURE;
*((float *)(data + 1)) = (float)temperature;
if (mq_send(sensor_q, (char *)data, sizeof(data), 0) == -1) {
msg.type = TAG_TEMPERATURE;
msg.data.FLOAT = (float)temperature;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "LSM6DSO32 couldn't send message: %s\n", strerror(errno));
}
}
Expand All @@ -96,9 +96,9 @@ void *lsm6dso32_collector(void *args) {
fprintf(stderr, "LSM6DSO32 could not read linear acceleration: %s\n", strerror(errno));
} else {
lsm6dso32_convert_accel(LA_FS_32G, &x, &y, &z);
data[0] = TAG_LINEAR_ACCEL_REL;
*((vec3d_t *)(data + 1)) = (vec3d_t){.x = x, .y = y, .z = z};
if (mq_send(sensor_q, (char *)data, sizeof(data), 0) == -1) {
msg.type = TAG_LINEAR_ACCEL_REL;
msg.data.VEC3D = (vec3d_t){.x = x, .y = y, .z = z};
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "LSM6DSO32 couldn't send message: %s\n", strerror(errno));
}
}
Expand All @@ -110,9 +110,9 @@ void *lsm6dso32_collector(void *args) {
fprintf(stderr, "LSM6DSO32 could not read angular velocity: %s\n", strerror(errno));
} else {
lsm6dso32_convert_angular_vel(G_FS_500, &x, &y, &z);
data[0] = TAG_ANGULAR_VEL;
*((vec3d_t *)(data + 1)) = (vec3d_t){.x = x, .y = y, .z = z};
if (mq_send(sensor_q, (char *)data, sizeof(data), 0) == -1) {
msg.type = TAG_ANGULAR_VEL;
msg.data.VEC3D = (vec3d_t){.x = x, .y = y, .z = z};
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "LSM6DSO32 couldn't send message: %s\n", strerror(errno));
}
}
Expand Down
20 changes: 4 additions & 16 deletions src/collectors/m10spg_clctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ union read_buffer {
UBXNavStatusPayload stat;
};

/** Type for sending measurements over the message queue. */
typedef struct {
uint8_t type; /**< Measurement type */
union {
uint32_t U32; /**< The message payload, interpreted as a uin32_t */
int32_t I32; /**< The message payload, interpreted as a int32_t */
uint8_t U8; /**< The message payload, interpreted as a uint8_t */
vec2d_t VEC2D; /**< The message payload, interpreted as a vec2d_t */
float FLOAT; /**< The message payload, interpreted as a float */
};
} __attribute__((packed)) message_t;

void *m10spg_collector(void *args) {

/* Open message queue. */
Expand All @@ -43,7 +31,7 @@ void *m10spg_collector(void *args) {
for (;;) {
// TODO - Don't read if the next epoch hasn't happened
union read_buffer buf;
message_t msg;
common_t msg;
/* err = m10spg_send_command(&loc, UBX_NAV_STAT, &buf, sizeof(UBXNavStatusPayload)); */
/* // Check if we have a valid fix, no point logging bad data */
/* if (err == EOK) { */
Expand All @@ -70,14 +58,14 @@ void *m10spg_collector(void *args) {
err = m10spg_send_command(&loc, UBX_NAV_POSLLH, &buf, sizeof(UBXNavPositionPayload));
if (err == EOK) {
msg.type = TAG_COORDS;
msg.VEC2D.x = ((float)buf.pos.lat / LAT_SCALE_TO_DEGREES);
msg.VEC2D.y = ((float)buf.pos.lon / LON_SCALE_TO_DEGREES);
msg.data.VEC2D.x = ((float)buf.pos.lat / LAT_SCALE_TO_DEGREES);
msg.data.VEC2D.y = ((float)buf.pos.lon / LON_SCALE_TO_DEGREES);

if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "M10SPG couldn't send message: %s.\n", strerror(errno));
}
msg.type = TAG_ALTITUDE_SEA;
msg.FLOAT = ((float)buf.pos.hMSL / ALT_SCALE_TO_METERS);
msg.data.FLOAT = ((float)buf.pos.hMSL / ALT_SCALE_TO_METERS);
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "M10SPG couldn't send message: %s.\n", strerror(errno));
}
Expand Down
28 changes: 11 additions & 17 deletions src/collectors/ms5611_clctr.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include "../drivers/ms5611/ms5611.h"
#include "../drivers/sensor_api.h"
#include "collectors.h"
#include "sensor_api.h"
#include <stdio.h>

#define return_errno(err) return (void *)((uint64_t)err)

/** Type for easily sending measurements over the message queue. */
struct ms5611_message {
uint8_t type; /**< Measurement type (temperature, pressure or altitude) */
float data; /**< Measurement data (temperature, pressure or altitude) */
} __attribute__((packed));

/**
* Collector thread for the MS5611 sensor.
* @param args Arguments in the form of `collector_args_t`
Expand Down Expand Up @@ -56,7 +50,7 @@ void *ms5611_collector(void *args) {
}

// Data storage
struct ms5611_message measurement;
common_t msg;
double pressure;
double altitude;
double temperature;
Expand All @@ -73,23 +67,23 @@ void *ms5611_collector(void *args) {
}

// Transmit temperature
measurement.type = TAG_TEMPERATURE;
measurement.data = (float)temperature;
if (mq_send(sensor_q, (char *)&measurement, sizeof(measurement), 0) == -1) {
msg.type = TAG_TEMPERATURE;
msg.data.FLOAT = (float)temperature;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "MS5611 couldn't send message: %s.\n", strerror(errno));
}

// Transmit pressure
measurement.type = TAG_PRESSURE;
measurement.data = (float)pressure;
if (mq_send(sensor_q, (char *)&measurement, sizeof(measurement), 0) == -1) {
msg.type = TAG_PRESSURE;
msg.data.FLOAT = (float)pressure;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "MS5611 couldn't send message: %s.\n", strerror(errno));
}

// Transmit altitude
measurement.type = TAG_ALTITUDE_REL;
measurement.data = (float)altitude;
if (mq_send(sensor_q, (char *)&measurement, sizeof(measurement), 0) == -1) {
msg.type = TAG_ALTITUDE_REL;
msg.data.FLOAT = (float)altitude;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "MS5611 couldn't send message: %s.\n", strerror(errno));
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/collectors/sht41_clctr.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#include <stdint.h>
#include <stdio.h>
#define SHT41_USE_CRC_LOOKUP
#include "../drivers/sensor_api.h"
#include "../drivers/sht41/sht41.h"
#include "collectors.h"
#include "sensor_api.h"

/** Type to simplify sending measurements. */
struct sht41_msg_t {
uint8_t type; /**< The measurement type (temperature, humidity). */
float data; /**< The measurement data. */
} __attribute__((packed));

/** Macro to cast `errno_t` to void pointer before returning. */
#define return_errno(err) return (void *)((uint64_t)err)
Expand Down Expand Up @@ -45,7 +39,7 @@ void *sht41_collector(void *args) {
// Data storage
float temperature;
float humidity;
struct sht41_msg_t msg;
common_t msg;

for (;;) {

Expand All @@ -54,14 +48,14 @@ void *sht41_collector(void *args) {

// Send temperature
msg.type = TAG_TEMPERATURE;
msg.data = temperature;
msg.data.FLOAT = temperature;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "SHT41 couldn't send message: %s\n", strerror(errno));
}

// Send humidity
msg.type = TAG_HUMIDITY;
msg.data = humidity;
msg.data.FLOAT = humidity;
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "SHT41 couldn't send message: %s\n", strerror(errno));
}
Expand Down
12 changes: 3 additions & 9 deletions src/collectors/sysclock_clctr.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#include "../drivers/sensor_api.h"
#include "collectors.h"
#include "sensor_api.h"
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

/** Type to simplify sending time data. */
struct sysclock_msg_t {
uint8_t type; /**< The type of message (always time). */
uint32_t millis; /**< The number of milliseconds since launch. */
} __attribute__((packed));

/**
* Collector thread for the system clock.
* @param args Arguments in the form of `collector_args_t`
Expand All @@ -34,7 +28,7 @@ void *sysclock_collector(void *args) {
struct timeval tval;

// Infinitely check the time
struct sysclock_msg_t msg;
common_t msg;
msg.type = TAG_TIME;
for (;;) {

Expand All @@ -43,7 +37,7 @@ void *sysclock_collector(void *args) {

// Calculate elapsed time from launch
time_t elapsed_s = tval.tv_sec - start_unix_time;
msg.millis = (elapsed_s * 1000) + (tval.tv_usec / 1000);
msg.data.U32 = (elapsed_s * 1000) + (tval.tv_usec / 1000);

// Infinitely send the time
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
Expand Down
16 changes: 16 additions & 0 deletions src/drivers/sensor_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ typedef enum {
TYPE_VEC2D, /**< vec2d_t */
} SensorTagDType;

/** Describes a message that can be sent on a message queue and recognized by both fetcher and packager */
typedef struct {
uint8_t type; /**< Measurement type */
union {
float FLOAT;
uint32_t U32;
uint16_t U16;
uint8_t U8;
int32_t I32;
int16_t I16;
int8_t I8;
vec3d_t VEC3D;
vec2d_t VEC2D;
} data; /**< The way the contents of this struct should be interpreted */
} common_t;

/** Stores information about each tag. */
typedef struct {
/** The name of the data the tag is associated with. */
Expand Down
Loading

0 comments on commit b43d727

Please sign in to comment.