2014-07-14 11:49:44 +00:00
|
|
|
/** Generate random packages on stdout.
|
2014-06-05 09:34:47 +00:00
|
|
|
*
|
2015-05-06 11:36:51 +02:00
|
|
|
* @file
|
2015-06-02 21:53:04 +02:00
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2015-06-02 21:53:04 +02:00
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*
|
2015-05-06 11:36:51 +02:00
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
2015-06-02 21:53:04 +02:00
|
|
|
**********************************************************************************/
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2015-12-02 13:54:23 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "msg.h"
|
2015-06-02 22:06:15 +02:00
|
|
|
#include "timing.h"
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
#define CLOCKID CLOCK_REALTIME
|
|
|
|
|
2015-12-02 13:54:23 +01:00
|
|
|
enum SIGNAL_TYPE {
|
|
|
|
TYPE_RANDOM,
|
|
|
|
TYPE_SINE,
|
|
|
|
TYPE_SQUARE,
|
|
|
|
TYPE_TRIANGLE,
|
2016-02-07 01:11:51 +01:00
|
|
|
TYPE_RAMP,
|
2015-12-02 13:54:23 +01:00
|
|
|
TYPE_MIXED
|
|
|
|
};
|
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
void usage(char *name)
|
|
|
|
{
|
|
|
|
printf("Usage: %s SIGNAL [OPTIONS]\n", name);
|
|
|
|
printf(" SIGNAL is on of: 'mixed', 'random', 'sine', 'triangle', 'square', 'ramp'\n");
|
|
|
|
printf(" -v NUM specifies how many values a message should contain\n");
|
|
|
|
printf(" -r HZ how many messages per second\n");
|
|
|
|
printf(" -f HZ the frequency of the signal\n");
|
|
|
|
printf(" -a FLT the amplitude\n");
|
|
|
|
printf(" -d FLT the standard deviation for 'random' signals\n");
|
|
|
|
printf(" -l NUM only send LIMIT messages and stop\n\n");
|
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:47 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Some default values */
|
|
|
|
double rate = 10;
|
|
|
|
double freq = 1;
|
|
|
|
double ampl = 1;
|
|
|
|
double stddev = 0.02;
|
|
|
|
int type = TYPE_MIXED;
|
|
|
|
int values = 1;
|
|
|
|
int limit = -1;
|
2015-03-21 18:16:28 +01:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
int counter = 0;
|
2015-03-21 18:03:55 +01:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
if (argc < 2) {
|
|
|
|
usage(argv[0]);
|
2014-06-05 09:34:47 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-02-07 03:17:45 +01:00
|
|
|
|
2015-12-02 13:54:23 +01:00
|
|
|
/* Parse signal type */
|
|
|
|
if (!strcmp(argv[1], "random"))
|
|
|
|
type = TYPE_RANDOM;
|
|
|
|
else if (!strcmp(argv[1], "sine"))
|
|
|
|
type = TYPE_SINE;
|
|
|
|
else if (!strcmp(argv[1], "square"))
|
|
|
|
type = TYPE_SQUARE;
|
2016-02-07 01:11:51 +01:00
|
|
|
else if (!strcmp(argv[1], "triangle"))
|
|
|
|
type = TYPE_TRIANGLE;
|
|
|
|
else if (!strcmp(argv[1], "ramp"))
|
|
|
|
type = TYPE_RAMP;
|
2015-12-02 13:54:23 +01:00
|
|
|
else if (!strcmp(argv[1], "mixed"))
|
|
|
|
type = TYPE_MIXED;
|
2016-02-07 03:17:45 +01:00
|
|
|
|
|
|
|
/* Parse optional command line arguments */
|
|
|
|
char c, *endptr;
|
|
|
|
while ((c = getopt(argc-1, argv+1, "hv:r:f:l:a:d:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'l':
|
|
|
|
limit = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
case 'v':
|
|
|
|
values = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
case 'r':
|
|
|
|
rate = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'f':
|
|
|
|
freq = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'a':
|
|
|
|
ampl = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'd':
|
|
|
|
stddev = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
|
|
|
error("Failed to parse parse option argument '-%c %s'", c, optarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory for message buffer */
|
|
|
|
struct msg *m = msg_create(values);
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2016-02-07 01:11:51 +01:00
|
|
|
/* Print header */
|
2016-02-07 03:17:45 +01:00
|
|
|
printf("# S2SS signal params: type=%s, values=%u, rate=%f, limit=%u, amplitude=%f, freq=%f\n",
|
|
|
|
argv[1], values, rate, limit, ampl, freq);
|
|
|
|
printf("# %-20s\t\t%s\n", "sec.nsec(seq)", "data[]");
|
2016-02-07 01:11:51 +01:00
|
|
|
|
2014-06-05 09:34:47 +00:00
|
|
|
/* Setup timer */
|
2016-01-14 22:57:39 +01:00
|
|
|
int tfd = timerfd_create_rate(rate);
|
2015-05-06 12:05:07 +02:00
|
|
|
if (tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2015-12-02 13:54:23 +01:00
|
|
|
struct timespec start = time_now();
|
2015-06-02 22:43:57 +02:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
while (limit < 0 || counter < limit) {
|
2015-12-02 13:54:23 +01:00
|
|
|
struct timespec now = time_now();
|
|
|
|
double running = time_delta(&start, &now);
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
m->ts.sec = now.tv_sec;
|
|
|
|
m->ts.nsec = now.tv_nsec;
|
2016-02-07 01:11:51 +01:00
|
|
|
m->sequence = counter;
|
2015-12-02 13:54:23 +01:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
for (int i = 0; i < m->values; i++) {
|
2016-02-07 03:17:45 +01:00
|
|
|
int rtype = (type != TYPE_MIXED) ? type : i % 4;
|
2015-12-02 13:54:23 +01:00
|
|
|
switch (rtype) {
|
2016-02-07 03:17:45 +01:00
|
|
|
case TYPE_RANDOM: m->data[i].f += box_muller(0, stddev); break;
|
|
|
|
case TYPE_SINE: m->data[i].f = ampl * sin(running * freq * 2 * M_PI); break;
|
2016-02-07 01:11:51 +01:00
|
|
|
case TYPE_TRIANGLE: m->data[i].f = ampl * (fabs(fmod(running * freq, 1) - .5) - 0.25) * 4; break;
|
|
|
|
case TYPE_SQUARE: m->data[i].f = ampl * ( (fmod(running * freq, 1) < .5) ? -1 : 1); break;
|
2016-02-07 03:17:45 +01:00
|
|
|
case TYPE_RAMP: m->data[i].f = fmod(counter, rate / freq); /** @todo send as integer? */ break;
|
2015-12-02 13:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
msg_fprint(stdout, m, MSG_PRINT_ALL & ~MSG_PRINT_OFFSET, 0);
|
2015-05-06 12:05:07 +02:00
|
|
|
fflush(stdout);
|
2015-10-14 12:13:28 +02:00
|
|
|
|
2016-02-07 01:11:51 +01:00
|
|
|
/* Block until 1/p->rate seconds elapsed */
|
|
|
|
counter += timerfd_wait(tfd);
|
2015-05-06 12:05:07 +02:00
|
|
|
}
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2015-05-06 12:05:07 +02:00
|
|
|
close(tfd);
|
2016-01-14 22:59:57 +01:00
|
|
|
free(m);
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-06 11:36:51 +02:00
|
|
|
|
2015-08-07 01:11:43 +02:00
|
|
|
/** @} */
|