* @param user_id - User identifier
* @param read - Function to call whenever the sensor should be read
* @param init - Function to call to initialise the sensor (may be NULL)
- * @param max_error - Maximum error threshold; program will exit if this is exceeded for the sensor reading
- * @param min_error - Minimum error threshold; program will exit if the sensor reading falls below this value
- * @param max_warn - Maximum warning threshold; program will log warnings if the value exceeds this threshold
- * @param min_warn - Minimum warning threshold; program will log warnings if the value falls below this threshold
+ * @param cleanup - Function to call whenever to deinitialise the sensor (may be NULL)
+ * @param sanity - Function to call to check that the sensor value is sane (may be NULL)
* @returns Number of actuators added so far
*/
int Sensor_Add(const char * name, int user_id, ReadFn read, InitFn init, CleanFn cleanup, SanityFn sanity)
s->name = name;
s->read = read; // Set read function
s->init = init; // Set init function
- if (init != NULL)
- init(name, user_id); // Call it
// Start by averaging values taken over a second
- s->sample_us = 1e6;
+ DOUBLE_TO_TIMEVAL(1, &(s->sample_time));
s->averages = 1;
+ s->num_read = 0;
// Set sanity function
s->sanity = sanity;
+
+ if (init != NULL)
+ {
+ if (!init(name, user_id))
+ Fatal("Couldn't init sensor %s", name);
+ }
+
+ s->current_data.time_stamp = 0;
+ s->current_data.value = 0;
+ s->averaged_data.time_stamp = 0;
+ s->averaged_data.value = 0;
return g_num_sensors;
}
#include "sensors/resource.h"
#include "sensors/strain.h"
#include "sensors/pressure.h"
+#include "sensors/dilatometer.h"
+#include "sensors/microphone.h"
void Sensor_Init()
{
- Sensor_Add("cpu_stime", RESOURCE_CPU_SYS, Resource_Read, NULL, NULL, NULL);
- Sensor_Add("cpu_utime", RESOURCE_CPU_USER, Resource_Read, NULL, NULL, NULL);
+ //Sensor_Add("cpu_stime", RESOURCE_CPU_SYS, Resource_Read, NULL, NULL, NULL);
+ //Sensor_Add("cpu_utime", RESOURCE_CPU_USER, Resource_Read, NULL, NULL, NULL);
Sensor_Add("pressure_high0", PRES_HIGH0, Pressure_Read, Pressure_Init, Pressure_Cleanup, NULL);
Sensor_Add("pressure_high1", PRES_HIGH1, Pressure_Read, Pressure_Init, Pressure_Cleanup, NULL);
Sensor_Add("pressure_low0", PRES_LOW0, Pressure_Read, Pressure_Init, Pressure_Cleanup, NULL);
//Sensor_Add("../testing/count.py", 0, Piped_Read, Piped_Init, Piped_Cleanup, 1e50,-1e50,1e50,-1e50);
- //Sensor_Add("strain0", STRAIN0, Strain_Read, Strain_Init, 5000,0,5000,0);
- //Sensor_Add("strain1", STRAIN1, Strain_Read, Strain_Init, 5000,0,5000,0);
- //Sensor_Add("strain2", STRAIN2, Strain_Read, Strain_Init, 5000,0,5000,0);
- //Sensor_Add("strain3", STRAIN3, Strain_Read, Strain_Init, 5000,0,5000,0);
+ //Sensor_Add("strain0_endhoop", STRAIN0, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity);
+ //Sensor_Add("strain1_endlong", STRAIN1, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity);
+ //Sensor_Add("strain2_midhoop", STRAIN2, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity);
+ //Sensor_Add("strain3_midlong", STRAIN3, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity);
+
+ //Sensor_Add("microphone", 0, Microphone_Read, Microphone_Init, Microphone_Cleanup, Microphone_Sanity);
//Sensor_Add("pressure0", PRESSURE0, Pressure_Read, Pressure_Init, 5000,0,5000,0);
//Sensor_Add("pressure1", PRESSURE1, Pressure_Read, Pressure_Init, 5000,0,5000,0);
//Sensor_Add("pressure_feedback", PRESSURE_FEEDBACK, Pressure_Read, Pressure_Init, 5000,0,5000,0);
- //Sensor_Add("enclosure", ENCLOSURE, Enclosure_Read, Enclosure_Init, 1,1,1,1);
- //Sensor_Add("dilatometer", DILATOMETER, Dilatometer_Read, Dilatometer_Init, -1,-1,-1,-1);
+ //Sensor_Add("enclosure", ENCLOSURE, Enclosure_Read, Enclosure_Init, 1,1,1,1); // Does not exist...
+
+ //NOTE: DO NOT ENABLE DILATOMETER WITHOUT FURTHER TESTING; CAUSES SEGFAULTS
+ //Sensor_Add("dilatometer0", 0, Dilatometer_Read, Dilatometer_Init, Dilatometer_Cleanup, NULL);
+ //Sensor_Add("dilatometer1",1, Dilatometer_Read, Dilatometer_Init, Dilatometer_Cleanup, NULL);
}
/**
if (s->cleanup != NULL)
s->cleanup(s->user_id);
}
+ g_num_sensors = 0;
}
/**
{
// Set filename
char filename[BUFSIZ];
- const char *experiment_name = (const char*) arg;
+ const char *experiment_path = (const char*) arg;
+ int ret;
- if (snprintf(filename, BUFSIZ, "%s_%d", experiment_name, s->id) >= BUFSIZ)
+ ret = snprintf(filename, BUFSIZ, "%s/sensor_%d", experiment_path, s->id);
+
+ if (ret >= BUFSIZ)
{
- Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
+ Fatal("Experiment path \"%s\" too long (%d, limit %d)",
+ experiment_path, ret, BUFSIZ);
}
Log(LOGDEBUG, "Sensor %d with DataFile \"%s\"", s->id, filename);
*/
void Sensor_SetModeAll(ControlModes mode, void * arg)
{
+ if (mode == CONTROL_START)
+ Sensor_Init();
for (int i = 0; i < g_num_sensors; i++)
Sensor_SetMode(&g_sensors[i], mode, arg);
+ if (mode == CONTROL_STOP)
+ Sensor_Cleanup();
}
// Until the sensor is stopped, record data points
while (s->activated)
{
- DataPoint d;
- d.value = 0;
- bool success = s->read(s->user_id, &(d.value));
+
+ bool success = s->read(s->user_id, &(s->current_data.value));
- struct timeval t;
- gettimeofday(&t, NULL);
- d.time_stamp = TIMEVAL_DIFF(t, *Control_GetStartTime());
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ s->current_data.time_stamp = TIMEVAL_DIFF(t, *Control_GetStartTime());
if (success)
{
if (s->sanity != NULL)
{
- if (!s->sanity(s->user_id, d.value))
+ if (!s->sanity(s->user_id, s->current_data.value))
{
Fatal("Sensor %s (%d,%d) reads unsafe value", s->name, s->id, s->user_id);
}
}
- Data_Save(&(s->data_file), &d, 1); // Record it
+ s->averaged_data.time_stamp += s->current_data.time_stamp;
+ s->averaged_data.value = s->current_data.value;
+
+ if (++(s->num_read) >= s->averages)
+ {
+ s->averaged_data.time_stamp /= s->averages;
+ s->averaged_data.value /= s->averages;
+ Data_Save(&(s->data_file), &(s->averaged_data), 1); // Record it
+ s->num_read = 0;
+ s->averaged_data.time_stamp = 0;
+ s->averaged_data.value = 0;
+ }
}
else
- Log(LOGWARN, "Failed to read sensor %s (%d,%d)", s->name, s->id,s->user_id);
+ {
+ // Silence because strain sensors fail ~50% of the time :S
+ //Log(LOGWARN, "Failed to read sensor %s (%d,%d)", s->name, s->id,s->user_id);
+ }
+
- usleep(s->sample_us);
+ clock_nanosleep(CLOCK_MONOTONIC, 0, &(s->sample_time), NULL);
+
}
// Needed to keep pthreads happy
/**
* Helper: Begin sensor response in a given format
* @param context - the FCGIContext
- * @param id - ID of sensor
+ * @param s - Sensor to begin the response for
* @param format - Format
*/
void Sensor_BeginResponse(FCGIContext * context, Sensor * s, DataFormat format)
/**
* Helper: End sensor response in a given format
* @param context - the FCGIContext
- * @param id - ID of the sensor
+ * @param s - Sensor to end the response for
* @param format - Format
*/
void Sensor_EndResponse(FCGIContext * context, Sensor * s, DataFormat format)
*/
void Sensor_Handler(FCGIContext *context, char * params)
{
- struct timeval now;
- gettimeofday(&now, NULL);
+ struct timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
-
int id = 0;
const char * name = "";
double start_time = 0;
FCGI_RejectJSON(context, "Negative sampling speed!");
return;
}
- s->sample_us = 1e6*sample_s;
+ DOUBLE_TO_TIMEVAL(sample_s, &(s->sample_time));
}
return g_sensors[id].name;
}
+/**
+ * Returns the last DataPoint that is currently available.
+ * @param id - The sensor ID for which to retrieve data from
+ * @return The last DataPoint
+ */
+DataPoint Sensor_LastData(int id)
+{
+ Sensor * s = &(g_sensors[id]);
+ return s->current_data;
+}