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>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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
|
|
|
|
2016-11-20 02:45:39 -05:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/sample.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
#include <villas/sample_io.h>
|
2016-11-20 02:45:39 -05:00
|
|
|
#include <villas/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-10-22 20:37:02 -04:00
|
|
|
void usage()
|
2016-02-07 03:17:45 +01:00
|
|
|
{
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("Usage: villas-signal [OPTIONS] SIGNAL\n");
|
|
|
|
printf(" SIGNAL is on of the following signal types:\n");
|
|
|
|
printf(" mixed\n");
|
|
|
|
printf(" random\n");
|
|
|
|
printf(" sine\n");
|
|
|
|
printf(" triangle\n");
|
|
|
|
printf(" square\n");
|
|
|
|
printf(" ramp\n");
|
|
|
|
printf("\n");
|
|
|
|
printf(" OPTIONS is one or more of the following options:\n");
|
|
|
|
printf(" -d LVL set debug level\n");
|
|
|
|
printf(" -v NUM specifies how many values a message should contain\n");
|
|
|
|
printf(" -r HZ how many messages per second\n");
|
|
|
|
printf(" -n non real-time mode. do not throttle output.\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");
|
2016-02-07 03:17:45 +01:00
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:47 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-03-03 20:21:33 -04:00
|
|
|
struct log log;
|
2017-03-05 10:47:26 -04:00
|
|
|
struct timespec start, now;
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-03-05 10:47:26 -04:00
|
|
|
enum {
|
|
|
|
MODE_RT,
|
|
|
|
MODE_NON_RT
|
|
|
|
} mode = MODE_RT;
|
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Some default values */
|
|
|
|
double rate = 10;
|
|
|
|
double freq = 1;
|
|
|
|
double ampl = 1;
|
|
|
|
double stddev = 0.02;
|
2017-03-05 10:47:26 -04:00
|
|
|
double running;
|
2016-02-07 03:17:45 +01:00
|
|
|
int type = TYPE_MIXED;
|
|
|
|
int values = 1;
|
|
|
|
int limit = -1;
|
2017-03-06 19:14:24 -04:00
|
|
|
int counter, tfd, steps, level = V;
|
2015-03-21 18:03:55 +01:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Parse optional command line arguments */
|
|
|
|
char c, *endptr;
|
2017-05-03 19:26:58 +02:00
|
|
|
while ((c = getopt(argc, argv, "hv:r:f:l:a:D:d:n")) != -1) {
|
2016-02-07 03:17:45 +01:00
|
|
|
switch (c) {
|
2017-03-06 19:14:24 -04:00
|
|
|
case 'd':
|
|
|
|
level = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
2016-02-07 03:17:45 +01:00
|
|
|
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;
|
2017-03-06 19:14:24 -04:00
|
|
|
case 'D':
|
2016-02-07 03:17:45 +01:00
|
|
|
stddev = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
2017-03-05 10:47:26 -04:00
|
|
|
case 'n':
|
|
|
|
mode = MODE_NON_RT;
|
|
|
|
break;
|
2016-02-07 03:17:45 +01:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2016-10-30 22:05:29 -04:00
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2016-02-07 03:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
|
|
|
error("Failed to parse parse option argument '-%c %s'", c, optarg);
|
|
|
|
}
|
2017-05-03 19:26:58 +02:00
|
|
|
|
|
|
|
if (argc != optind + 1) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *typestr = argv[optind];
|
|
|
|
|
|
|
|
/* Parse signal type */
|
|
|
|
if (!strcmp(typestr, "random"))
|
|
|
|
type = TYPE_RANDOM;
|
|
|
|
else if (!strcmp(typestr, "sine"))
|
|
|
|
type = TYPE_SINE;
|
|
|
|
else if (!strcmp(typestr, "square"))
|
|
|
|
type = TYPE_SQUARE;
|
|
|
|
else if (!strcmp(typestr, "triangle"))
|
|
|
|
type = TYPE_TRIANGLE;
|
|
|
|
else if (!strcmp(typestr, "ramp"))
|
|
|
|
type = TYPE_RAMP;
|
|
|
|
else if (!strcmp(typestr, "mixed"))
|
|
|
|
type = TYPE_MIXED;
|
|
|
|
else
|
|
|
|
error("Invalid signal type: %s", typestr);
|
2017-03-06 19:14:24 -04:00
|
|
|
|
|
|
|
log_init(&log, level, LOG_ALL);
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Allocate memory for message buffer */
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *s = alloc(SAMPLE_LEN(values));
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2016-02-07 01:11:51 +01:00
|
|
|
/* Print header */
|
2016-06-08 23:21:42 +02:00
|
|
|
printf("# VILLASnode signal params: type=%s, values=%u, rate=%f, limit=%d, amplitude=%f, freq=%f\n",
|
2016-02-07 03:17:45 +01:00
|
|
|
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 */
|
2017-03-05 10:47:26 -04:00
|
|
|
if (mode == MODE_RT) {
|
|
|
|
tfd = timerfd_create_rate(rate);
|
|
|
|
if (tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
|
|
|
}
|
2017-03-07 08:05:04 -04:00
|
|
|
else
|
|
|
|
tfd = -1;
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2017-03-05 10:47:26 -04:00
|
|
|
start = time_now();
|
2015-06-02 22:43:57 +02:00
|
|
|
|
2016-06-08 22:36:38 +02:00
|
|
|
counter = 0;
|
2016-02-07 03:17:45 +01:00
|
|
|
while (limit < 0 || counter < limit) {
|
2017-03-05 10:47:26 -04:00
|
|
|
if (mode == MODE_RT) {
|
|
|
|
now = time_now();
|
|
|
|
running = time_delta(&start, &now);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct timespec offset;
|
|
|
|
|
|
|
|
running = counter * 1.0 / rate;
|
|
|
|
offset = time_from_double(running);
|
|
|
|
|
|
|
|
now = time_add(&start, &offset);
|
|
|
|
}
|
2015-12-02 13:54:23 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
s->ts.origin = now;
|
|
|
|
s->sequence = counter;
|
|
|
|
s->length = values;
|
2015-12-02 13:54:23 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
for (int i = 0; i < 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-07-11 18:19:23 +02:00
|
|
|
case TYPE_RANDOM: s->data[i].f += box_muller(0, stddev); break;
|
|
|
|
case TYPE_SINE: s->data[i].f = ampl * sin(running * freq * 2 * M_PI); break;
|
|
|
|
case TYPE_TRIANGLE: s->data[i].f = ampl * (fabs(fmod(running * freq, 1) - .5) - 0.25) * 4; break;
|
|
|
|
case TYPE_SQUARE: s->data[i].f = ampl * ( (fmod(running * freq, 1) < .5) ? -1 : 1); break;
|
|
|
|
case TYPE_RAMP: s->data[i].f = fmod(counter, rate / freq); /** @todo send as integer? */ break;
|
2015-12-02 13:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
sample_io_villas_fprint(stdout, s, SAMPLE_IO_ALL & ~SAMPLE_IO_OFFSET);
|
2015-05-06 12:05:07 +02:00
|
|
|
fflush(stdout);
|
2015-10-14 12:13:28 +02:00
|
|
|
|
2017-03-05 10:47:26 -04:00
|
|
|
/* Throttle output if desired */
|
|
|
|
if (mode == MODE_RT) {
|
|
|
|
/* Block until 1/p->rate seconds elapsed */
|
|
|
|
steps = timerfd_wait(tfd);
|
|
|
|
if (steps > 1)
|
|
|
|
warn("Missed steps: %u", steps);
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2017-03-05 10:47:26 -04:00
|
|
|
counter += steps;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
counter += 1;
|
2015-05-06 12:05:07 +02:00
|
|
|
}
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2017-03-05 10:47:26 -04:00
|
|
|
if (mode == MODE_RT)
|
|
|
|
close(tfd);
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
free(s);
|
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
|
|
|
/** @} */
|