start refactor
This commit is contained in:
parent
d7907f3cbe
commit
4eb994c5c8
187
Bearbeiten2.c
187
Bearbeiten2.c
@ -1,124 +1,129 @@
|
||||
/*
|
||||
* Beispiel: Grundgeruest einer Modbus-Kommunikation
|
||||
*
|
||||
* Aufbau einer Modbus-Kommunikation ohne weitere Funktion
|
||||
*
|
||||
*/
|
||||
|
||||
#include <rtai_mbx.h>
|
||||
#include <rtai_sched.h>
|
||||
|
||||
#include <uint128.h>
|
||||
#include <sys/rtai_modbus.h>
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
#define SENSOR_PART_TURNTABLE 1<<0
|
||||
#define SENSOR_PART_DRILL 1<<1
|
||||
#define SENSOR_PART_MESURING 1<<2
|
||||
#define SENSOR_DRILL_UP 1<<3
|
||||
#define SENSOR_DRILL_DOWN 1<<4
|
||||
#define SENSOR_TURNTABLE_POS 1<<5
|
||||
#define SENSOR_PART_TEST 1<<6
|
||||
|
||||
#define ACTOR_DRILL 1<<0
|
||||
#define ACTOR_TURNTABLE 1<<1
|
||||
#define ACTOR_DRILL_DOWN 1<<2
|
||||
#define ACTOR_DRILL_UP 1<<3
|
||||
#define ACTOR_PART_HOLD 1<<4
|
||||
#define ACTOR_TESTER 1<<5
|
||||
#define ACTOR_EXIT 1<<6
|
||||
#define ACTOR_ENTRANCE 1<<7
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
static RT_TASK entry_task;
|
||||
|
||||
/* Control Task */
|
||||
int readAll(int connection, int *value) {
|
||||
if (rt_modbus_get(fd_node, DIGITAL_IN, 0, (unsigned short *) &value)) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read(int connection, int part, int *result) {
|
||||
int value;
|
||||
int code = readAll(connection, &value);
|
||||
*result = (value & part) != 0;
|
||||
return code;
|
||||
}
|
||||
|
||||
int disable(int connection, int part) {
|
||||
int value;
|
||||
int code = readAll(connection, &value);
|
||||
if (code) return code;
|
||||
if (rt_modbus_set(connection, DIGITAL_OUT, output &= part))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int enable(int connection, int part) {
|
||||
int value;
|
||||
int code = readAll(connection, &value);
|
||||
if (code) return code;
|
||||
if (rt_modbus_set(connection, DIGITAL_OUT, output |= part))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
entry(long x)
|
||||
{
|
||||
int fd_node;
|
||||
int ready = 0;
|
||||
turntable(long x) {
|
||||
int connection;
|
||||
int ready = 0;
|
||||
|
||||
rt_printk("control: task started\n");
|
||||
rt_printk("turntable: task started\n");
|
||||
|
||||
/* Verbindung zum Modbus-Knoten herstellen */
|
||||
if ((fd_node = rt_modbus_connect("modbus-node")) == -1) {
|
||||
rt_printk("Acontrol: task exited\n");
|
||||
return;
|
||||
}
|
||||
rt_printk("control: MODBUS communication opened\n");
|
||||
if ((connection = rt_modbus_connect("modbus-node")) == -1) {
|
||||
rt_printk("turntable: task exited\n");
|
||||
return;
|
||||
}
|
||||
rt_printk("turntable: MODBUS communication opened\n");
|
||||
|
||||
while(1) {
|
||||
int value = 0;
|
||||
int output = 0;
|
||||
if(rt_modbus_get(fd_node, DIGITAL_IN, 0, (unsigned short *) &value))
|
||||
goto fail;
|
||||
if(rt_modbus_get(fd_node, DIGITAL_OUT, 0, (unsigned short *) &output))
|
||||
goto fail;
|
||||
while (1) {
|
||||
int partOnTable;
|
||||
if (read(connection, SENSOR_PART_TURNTABLE, &partOnTable)) goto fail;
|
||||
|
||||
if(value >32) {
|
||||
rt_printk("halloo welt");
|
||||
if(rt_modbus_set(fd_node, DIGITAL_OUT, 0, output |= 0b00000010))
|
||||
goto fail;
|
||||
if (partOnTable) {
|
||||
rt_printk("halloo welt");
|
||||
if (enable(connection, ACTOR_TURNTABLE)) goto fail;
|
||||
|
||||
} else {
|
||||
if (disable(connection, ACTOR_TURNTABLE)) goto fail;
|
||||
}
|
||||
|
||||
// if(rt_modbus_set(fd_node, DIGITAL_IN, 0, 00000000))
|
||||
// goto fail;
|
||||
// }
|
||||
}else {
|
||||
if(rt_modbus_set(fd_node, DIGITAL_OUT, 0, output &= 0b00000000))
|
||||
goto fail;
|
||||
}
|
||||
if (ready)
|
||||
goto fail;
|
||||
|
||||
/* Evtl. Ausgang zum Aufraemen */
|
||||
if (ready)
|
||||
goto fail;
|
||||
|
||||
/* Fair zu Linux sein und Zeit abgeben */
|
||||
rt_sleep(1000 * nano2count(1000000));
|
||||
}
|
||||
rt_sleep(1000 * nano2count(1000000));
|
||||
}
|
||||
|
||||
/* Aufraeumen */
|
||||
fail:
|
||||
rt_modbus_disconnect(fd_node);
|
||||
rt_printk("control: task exited\n");
|
||||
fail:
|
||||
rt_modbus_disconnect(connection);
|
||||
rt_printk("turntable: task exited with failure\n");
|
||||
}
|
||||
|
||||
/* Funktion die beim Entfernen des Moduls aufgerufen wird */
|
||||
static void __exit
|
||||
example_exit(void)
|
||||
{
|
||||
/* Task loeschen */
|
||||
rt_task_delete(&entry_task);
|
||||
/* Timer stoppen */
|
||||
stop_rt_timer();
|
||||
printk("rtai_example unloaded\n");
|
||||
|
||||
example_exit(void) {
|
||||
rt_task_delete(&entry_task);
|
||||
stop_rt_timer();
|
||||
printk("module Bearbeiten2 unloaded\n");
|
||||
}
|
||||
|
||||
/* Funktion die beim Laden des Moduls aufgerufen wird */
|
||||
static int __init
|
||||
example_init(void)
|
||||
{
|
||||
/* variables Timing basierend auf dem CPU-Takt */
|
||||
rt_set_oneshot_mode();
|
||||
/* Timer starten */
|
||||
start_rt_timer(0);
|
||||
/* Modbuskommunikation initialisieren */
|
||||
modbus_init();
|
||||
|
||||
/* Taskinitialisierung
|
||||
*
|
||||
* &task = Adresse des zu initialisierenden TCB
|
||||
* control = zum Task gehörende Funktion
|
||||
* 0 = Uebergabeparameter an die zum Task gehoerende Funktion (long)
|
||||
* 1024 = Stacksize
|
||||
* 0 = Priorität des Tasks (0 = groesste)
|
||||
* 0 = uses_fpu (Fliesskommaeinheit nicht benutzen); im Praktikum sollte es 0 bleiben
|
||||
* NULL = &signal_handler; Referenz zu einer Fkt., die bei jeder Aktivierung
|
||||
* des Tasks aufgerufen wird, ansonsten NULL
|
||||
*
|
||||
* Achtung: Nach der Initialisierung ist der Task "suspended"!
|
||||
*
|
||||
*/
|
||||
if (rt_task_init(&entry_task, entry, 0, 1024, 0, 0, NULL)) {
|
||||
printk("cannot initialize control task\n");
|
||||
goto fail;
|
||||
}
|
||||
example_init(void) {
|
||||
rt_set_oneshot_mode();
|
||||
start_rt_timer(0);
|
||||
modbus_init();
|
||||
|
||||
/* suspend -> ready bzw. running */
|
||||
rt_task_resume(&entry_task);
|
||||
printk("rtai_example loaded\n");
|
||||
return (0);
|
||||
if (rt_task_init(&entry_task, entry, 0, 1024, 0, 0, NULL)) {
|
||||
printk("cannot initialize control task\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail:
|
||||
/* Aufraeumen */
|
||||
stop_rt_timer();
|
||||
return (1);
|
||||
rt_task_resume(&entry_task);
|
||||
printk("loaded module Bearbeiten2\n");
|
||||
return (0);
|
||||
|
||||
fail:
|
||||
stop_rt_timer();
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Modulein- und ausstieg festlegen */
|
||||
module_exit(example_exit)
|
||||
module_init(example_init)
|
||||
|
Loading…
Reference in New Issue
Block a user