[OSADL-svn-commits] r110 - trunks/fddi-trunk/tests
OSADL repository commits
osadl-svn-commits at lists.osadl.org
Tue Oct 2 11:48:17 CEST 2007
Author: robert
Date: Sat Sep 8 02:04:51 2007
New Revision: 110
Log:
Added:
trunks/fddi-trunk/tests/usecase_controller.c
Added: trunks/fddi-trunk/tests/usecase_controller.c
==============================================================================
--- (empty file)
+++ trunks/fddi-trunk/tests/usecase_controller.c Sat Sep 8 02:04:51 2007
@@ -0,0 +1,144 @@
+#include <signal.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+//#include <osadl/ldi.h>
+
+#define PV_CREATE(var, type) \
+ do { \
+ ret = pv_##type##_create(&pv_##var, &ldi, #var); \
+ if (ret) { \
+ fprintf(stderr, "error: could not register process variable '"#var"'\n"); \
+ goto out_pv_##var; \
+ } \
+ } while(0)
+
+#define PV_DESTROY(var, type) \
+ pv_##type##_destroy(&pv_##var); \
+ out_pv_##var: \
+ do {} while(0)
+
+#define PV_TRIGGER_CREATE(var) \
+ do { \
+ ret = ldi_trigger_define(&ldi, &pv_##var); \
+ if (ret) { \
+ fprintf(stderr, "error: could not create trigger on process variable "#var"\n"); \
+ goto out_trigger; \
+ } \
+ } while(0)
+
+#define PV_TRIGGER_DESTROY(var) \
+ out_trigger: do {} while(0)
+
+
+/* FIXME put into header file */
+
+ typedef struct ldi_attr {
+ } ldi_attr_t;
+
+ typedef struct ldi {
+ } ldi_t;
+ typedef struct pv {
+ } pv_t;
+
+ /* pv */
+ int pv_uint32_create(pv_t *pv, ldi_t *ldi, const char *name);
+ int pv_uint32_destroy(pv_t *pv);
+ uint32_t pv_uint32_get(pv_t *pv);
+ void pv_uint32_set(pv_t *pv, uint32_t val);
+
+ /* ldi_attr */
+ int ldi_attr_init_from_cmdline(ldi_attr_t *attr, int argc, char *argv[]);
+ int ldi_attr_destroy(ldi_attr_t *attr);
+
+ /* ldi */
+ int ldi_create(ldi_t *ldi, ldi_attr_t *attr);
+ int ldi_destroy(ldi_t *ldi);
+ int ldi_trigger_define(ldi_t *ldi, pv_t *pv);
+ int ldi_trigger_wait(ldi_t *ldi, uint64_t timeout_ns);
+
+
+static int shutdown = 0;
+static int Ta = (10*1000*1000); /* 10 ms */
+
+void
+signalhandler(int sig)
+{
+ if ((sig == SIGPIPE) || (sig == SIGHUP))
+ return;
+ if (sig == SIGTERM || sig == SIGINT)
+ shutdown = 1;
+}
+
+int main(int argc, char *argv[])
+{
+ struct sigaction action;
+ ldi_attr_t ldi_attr;
+ ldi_t ldi;
+ int ret;
+
+ pv_t pv_input;
+ pv_t pv_output;
+ pv_t pv_Kp;
+ pv_t pv_Ki;
+ pv_t pv_Kd;
+
+ /* setup signal handler */
+ action.sa_handler = signalhandler;
+ sigemptyset (&action.sa_mask);
+ action.sa_flags = 0;
+ sigaction (SIGTERM, &action, 0); /* catches kill from the shell */
+ sigaction (SIGINT, &action, 0); /* catches Ctrl-C from the shell */
+ sigaction (SIGPIPE, &action, 0); /* catches Broken Pipe exception */
+ sigaction (SIGHUP, &action, 0); /* catches HUP */
+
+ /* initialize ldi */
+ ret = ldi_attr_init_from_cmdline(&ldi_attr, argc, argv);
+ if (ret) {
+ fprintf(stderr, "error: could not init ldi_attr\n");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = ldi_create(&ldi, &ldi_attr);
+ if (ret) {
+ fprintf(stderr, "error: couldn't create ldi\n");
+ exit(EXIT_FAILURE);
+ }
+
+ PV_CREATE(input, uint32);
+ PV_CREATE(output, uint32);
+ PV_CREATE(Kp, uint32);
+ PV_CREATE(Ki, uint32);
+ PV_CREATE(Kd, uint32);
+
+ PV_TRIGGER_CREATE(input);
+
+ /* control loop */
+ while (!shutdown) {
+
+ uint32_t e, x, esum, ealt;
+
+ ret = ldi_trigger_wait(&ldi, 100*1000*1000); /* wait until trigger fires, with timeout in ns */
+ if (ret)
+ continue;
+
+ e = pv_uint32_get(&pv_input) - x; /* comparism */
+ esum = esum + e; /* I part */
+ pv_uint32_set(&pv_output, pv_uint32_get(&pv_Kp)*e + pv_uint32_get(&pv_Ki)*Ta*esum + pv_uint32_get(&pv_Kd)/Ta * (e ? ealt : e));
+ ealt = e;
+
+ }
+
+ PV_TRIGGER_DESTROY(input);
+
+ PV_DESTROY(Kd, uint32);
+ PV_DESTROY(Ki, uint32);
+ PV_DESTROY(Kp, uint32);
+ PV_DESTROY(output, uint32);
+ PV_DESTROY(input, uint32);
+
+ ldi_destroy(&ldi);
+ ldi_attr_destroy(&ldi_attr);
+ return ret;
+}
More information about the OSADL-svn-commits
mailing list