/**
* Add and initialise a Sensor
* @param name - Human readable name of the sensor
+ * @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
- * @returns Number of the sensor added
+ * @returns Number of actuators added so far
*/
-int Sensor_Add(const char * name, int user_id, ReadFn read, InitFn init, CleanFn cleanup, double max_error, double min_error, double max_warn, double min_warn)
+int Sensor_Add(const char * name, int user_id, ReadFn read, InitFn init, CleanFn cleanup, SanityFn sanity)
{
if (++g_num_sensors > SENSORS_MAX)
{
s->name = name;
s->read = read; // Set read function
s->init = init; // Set init function
+
+ // Start by averaging values taken over a second
+ s->sample_us = 1e6;
+ s->averages = 1;
+
+ // Set sanity function
+ s->sanity = sanity;
+
if (init != NULL)
- init(name, user_id); // Call it
+ {
+ if (!init(name, user_id))
+ Fatal("Couldn't init sensor %s", name);
+ }
- // Set warning/error thresholds
- s->thresholds.max_error = max_error;
- s->thresholds.min_error = min_error;
- s->thresholds.max_warn = max_warn;
- s->thresholds.min_warn = min_warn;
return g_num_sensors;
}
*/
#include "sensors/resource.h"
#include "sensors/strain.h"
-#include "sensors/piped.h"
+#include "sensors/pressure.h"
void Sensor_Init()
{
- Sensor_Add("cpu_stime", RESOURCE_CPU_SYS, Resource_Read, NULL, NULL, 1e50,-1e50,1e50,-1e50);
- Sensor_Add("cpu_utime", RESOURCE_CPU_USER, Resource_Read, NULL, NULL, 1e50,-1e50,1e50,-1e50);
+ 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);
{
// Set filename
char filename[BUFSIZ];
- const char *experiment_name = (const char*) arg;
+ const char *experiment_path = (const char*) arg;
+ int ret;
+
+ ret = snprintf(filename, BUFSIZ, "%s/sensor_%d", experiment_path, s->id);
- if (snprintf(filename, BUFSIZ, "%s_%d", experiment_name, s->id) >= BUFSIZ)
+ 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);
}
-/**
- * Checks the sensor data for unsafe or unexpected results
- * @param sensor_id - The ID of the sensor
- * @param value - The value from the sensor to test
- */
-void Sensor_CheckData(Sensor * s, double value)
-{
- if( value > s->thresholds.max_error || value < s->thresholds.min_error)
- {
- Log(LOGERR, "Sensor %s at %f is above or below its safety value of %f or %f\n",s->name,value, s->thresholds.max_error, s->thresholds.min_error);
- //new function that stops actuators?
- //Control_SetMode(CONTROL_EMERGENCY, NULL)
- }
- else if( value > s->thresholds.max_warn || value < s->thresholds.min_warn)
- {
- Log(LOGWARN, "Sensor %s at %f is above or below its warning value of %f or %f\n", s->name,value,s->thresholds.max_warn, s->thresholds.min_warn);
- }
-}
-
-
/**
* Record data from a single Sensor; to be run in a seperate thread
* @param arg - Cast to Sensor* - Sensor that the thread will handle
if (success)
{
-
-
- Sensor_CheckData(s, d.value);
+ if (s->sanity != NULL)
+ {
+ if (!s->sanity(s->user_id, d.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
}
else
Log(LOGWARN, "Failed to read sensor %s (%d,%d)", s->name, s->id,s->user_id);
- usleep(1e5); //TODO: Adjust appropriately
+ usleep(s->sample_us);
}
// Needed to keep pthreads happy
struct timeval now;
gettimeofday(&now, NULL);
double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
-
int id = 0;
const char * name = "";
double start_time = 0;
double end_time = current_time;
const char * fmt_str;
+ double sample_s = 0;
// key/value pairs
FCGIValue values[] = {
{"format", &fmt_str, FCGI_STRING_T},
{"start_time", &start_time, FCGI_DOUBLE_T},
{"end_time", &end_time, FCGI_DOUBLE_T},
+ {"sample_s", &sample_s, FCGI_DOUBLE_T}
};
// enum to avoid the use of magic numbers
FORMAT,
START_TIME,
END_TIME,
+ SAMPLE_S
} SensorParams;
// Fill values appropriately
return;
}
- Sensor * s = &(g_sensors[id]); // If id was not supplied, this defaults to &(g_sensors[0])
+ Sensor * s = NULL;
if (FCGI_RECEIVED(values[NAME].flags))
{
if (FCGI_RECEIVED(values[ID].flags))
FCGI_RejectJSON(context, "No sensor id or name supplied");
return;
}
+ else if (id < 0 || id >= g_num_sensors)
+ {
+ FCGI_RejectJSON(context, "Invalid sensor id");
+ return;
+ }
+ else
+ {
+ s = &(g_sensors[id]);
+ }
+ // Adjust sample rate if necessary
+ if (FCGI_RECEIVED(values[SAMPLE_S].flags))
+ {
+ if (sample_s < 0)
+ {
+ FCGI_RejectJSON(context, "Negative sampling speed!");
+ return;
+ }
+ s->sample_us = 1e6*sample_s;
+ }
+
+
DataFormat format = Data_GetFormat(&(values[FORMAT]));
// Begin response
// Finish response
Sensor_EndResponse(context, s, format);
+
}
/**