praktikum

This commit is contained in:
Johannes Theiner 2020-11-02 11:31:29 +01:00
parent 810311fabb
commit 83ce4c4c1b
2 changed files with 138 additions and 57 deletions

View File

@ -1,17 +1,13 @@
#include <rtai_mbx.h> #include <rtai_mbx.h>
#include <rtai_sched.h> #include <rtai_sched.h>
#include <rtai_sem.h>
#include <uint128.h> #include <uint128.h>
#include <sys/rtai_modbus.h> #include <sys/rtai_modbus.h>
#include <stdbool.h>
#include "include/rtai_lxrt.h"
#include "include/rtai_modbus.h"
#include "include/rtai_sem.h"
#include "include/rtai_mbx.h"
#define SENSOR_PART_TURNTABLE 1<<0 #define SENSOR_PART_TURNTABLE 1<<0
#define SENSOR_PART_DRILL 1<<1 #define SENSOR_PART_DRILL 1<<1
#define SENSOR_PART_MESURING 1<<2 #define SENSOR_PART_TESTER 1<<2
#define SENSOR_DRILL_UP 1<<3 #define SENSOR_DRILL_UP 1<<3
#define SENSOR_DRILL_DOWN 1<<4 #define SENSOR_DRILL_DOWN 1<<4
#define SENSOR_TURNTABLE_POS 1<<5 #define SENSOR_TURNTABLE_POS 1<<5
@ -29,7 +25,7 @@
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
static RT_TASK turntable_task, drill_task, tester_task, output_task; static RT_TASK turntable_task, drill_task, tester_task, output_task;
static MBX tester_mbx, output_mbx; static MBX tester_mbx, output_mbx, drill_mbx, turntable_mbx;
static SEM semaphore; static SEM semaphore;
int connection; int connection;
@ -64,26 +60,26 @@ void end(bool fail) {
* @return status code(0: success, 1: failure) * @return status code(0: success, 1: failure)
*/ */
int readAll(int type, int *result) { int readAll(int type, int *result) {
return rt_modbus_get(connection, type, 0, (unsigned short *) &result)); int res = rt_modbus_get(connection, type, 0, (unsigned short *) result);
//rt_printk("%d\n", res);
//rt_printk("%d", &result);
return res;
} }
/** /**
* *
* this function is thread safe. * this function is thread safe.
* @param connection
* @param part * @param part
* @param result value to write the result to * @return
* @return status code(0: success, 1: failure)
*/ */
int readData(int part) { int readData(int part) {
rt_sem_wait(&semaphore); rt_sem_wait(&semaphore);
int value; int value = 0;
int * result;
int code = readAll(DIGITAL_IN, &value); int code = readAll(DIGITAL_IN, &value);
*result = (value & part) != 0; int result = (part & value);
rt_sem_signal(&semaphore); rt_sem_signal(&semaphore);
if(code) end(true); if(code) end(true);
return *result; return result;
} }
/** /**
@ -94,10 +90,10 @@ int readData(int part) {
*/ */
void disable(int actor) { void disable(int actor) {
rt_sem_wait(&semaphore); rt_sem_wait(&semaphore);
int value; int value = 0;
int code = readAll(DIGITAL_OUT, &value); int code = readAll(DIGITAL_OUT, &value);
if(code) end(true); if(code) end(true);
int result = rt_modbus_set(connection, DIGITAL_OUT, value &= ~actor) int result = rt_modbus_set(connection, DIGITAL_OUT, 0, value &= ~actor);
rt_sem_signal(&semaphore); rt_sem_signal(&semaphore);
if (result) end(true); if (result) end(true);
} }
@ -110,72 +106,135 @@ void disable(int actor) {
*/ */
void enable(int actor) { void enable(int actor) {
rt_sem_wait(&semaphore); rt_sem_wait(&semaphore);
int output; int output = 0;
int code = readAll(DIGITAL_OUT, &output); int code = readAll(DIGITAL_OUT, &output);
if (code) end(true); if (code) end(true);
int result = rt_modbus_set(connection, DIGITAL_OUT, output |= actor) int result;
result = rt_modbus_set(connection, DIGITAL_OUT, 0, output |= actor);
rt_sem_signal(&semaphore); rt_sem_signal(&semaphore);
if(result) end(true); if(result) end(true);
} }
void sleepStuff() { void sleepStuff(void) {
rt_sleep(1000 * nano2count(1000000)); rt_sleep(1000 * nano2count(1000000));
} }
static void turntable(long x) { static void turntable(long data) {
rt_printk("turntable: MODBUS communication opened\n"); // rt_printk("started turntable task");
// disable(ACTOR_TURNTABLE);
disable(ACTOR_TURNTABLE); //
// while (1) {
// int partOnTable = readData(SENSOR_PART_TURNTABLE);
// rt_printk("%d", partOnTable);
// if (partOnTable) {
// enable(ACTOR_TURNTABLE);
// } else {
// disable(ACTOR_TURNTABLE);
// }
//
// sleepStuff();
// }
while (1) { while (1) {
int partOnTable = readData(SENSOR_PART_TURNTABLE);
sleepStuff();
}
}
static void tester(long data) {
disable(ACTOR_TESTER);
while (1) {
int partOnTable = readData(SENSOR_PART_TESTER);
rt_printk("part: %d\n", partOnTable);
if(partOnTable) { if(partOnTable) {
enable(ACTOR_TURNTABLE); enable(ACTOR_TESTER);
sleepStuff();
int partNormal = readData(SENSOR_PART_TEST);
if(partNormal) {
//nachricht bohrer(ON)
int msg = 1;
int mbxStatus = rt_mbx_send(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
} else { } else {
disable(ACTOR_TURNTABLE); //nachricht bohrer(OFF)
int msg = 0;
int mbxStatus = rt_mbx_send(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
}
rt_printk("normal %d\n", partNormal);
disable(ACTOR_TESTER);
//Nachricht drehteller
} }
rt_task_resume(&drill_task); sleepStuff();
rt_task_resume(&tester_task); }
rt_task_resume(&output_task);
} }
} static void drill(long data) {
static void drill(long x) {
rt_printk("drill: MODBUS communication opened\n");
disable(ACTOR_DRILL); disable(ACTOR_DRILL);
disable(ACTOR_PART_HOLD); disable(ACTOR_PART_HOLD);
disable(ACTOR_DRILL_DOWN); disable(ACTOR_DRILL_DOWN);
rt_printk("hello");
enable(ACTOR_DRILL_UP); enable(ACTOR_DRILL_UP);
rt_sem_wait(&semaphore);
if (!readData(SENSOR_DRILL_UP)) { if (!readData(SENSOR_DRILL_UP)) {
do { do {
} while (readData(SENSOR_DRILL_UP) == 0); } while (readData(SENSOR_DRILL_UP) == 0);
} }
rt_sem_signal(&semaphore);
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
while (1) { while (1) {
int part = readData(SENSOR_PART_DRILL);
if(part) {
int msg = 0;
int mbxStatus = rt_mbx_receive(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
if(msg) {
enable(ACTOR_PART_HOLD);
enable(ACTOR_DRILL);
enable(ACTOR_DRILL_DOWN);
if (!readData(SENSOR_DRILL_DOWN)) {
do {
} while (readData(SENSOR_DRILL_DOWN) == 0);
}
sleepStuff();
disable(ACTOR_PART_HOLD);
disable(ACTOR_DRILL);
disable(ACTOR_DRILL_DOWN);
enable(ACTOR_DRILL_UP);
if (!readData(SENSOR_DRILL_UP)) {
do {
} while (readData(SENSOR_DRILL_UP) == 0);
}
disable(ACTOR_DRILL_UP);
}
}
int msg = part;
int mbxStatus = rt_mbx_send(&output_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
sleepStuff(); sleepStuff();
} }
} }
static void tester(long x) { static void output(long data) {
while (1) { while (1) {
//turntable msg
int msg = 0;
int mbxStatus = rt_mbx_receive(&output_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
if(msg) {
enable(ACTOR_EXIT);
sleepStuff(); sleepStuff();
disable(ACTOR_EXIT);
} }
} //nachricht an turntable
static void output(long x) {
while (1) {
sleepStuff(); sleepStuff();
} }
@ -190,12 +249,12 @@ example_init(void) {
rt_printk("init: task started\n"); rt_printk("init: task started\n");
if ((connection = rt_modbus_connect(node)) == -1) { if ((connection = rt_modbus_connect(node)) == -1) {
rt_printk("init: could not connect to %s\n", node); rt_printk("init: could not connect to %s\n", node);
return; return 1;
} }
rt_sem_init(&semaphore, 1); rt_sem_init(&semaphore, 1);
if (rt_task_init(&turntable_task, turntable, 0, 1024, 10, 0, NULL)) { if (rt_task_init(&turntable_task, turntable, 0, 1024, 0, 0, NULL)) {
printk("turntable: cannot initialize task\n"); printk("turntable: cannot initialize task\n");
goto fail; goto fail;
} }
@ -203,16 +262,35 @@ example_init(void) {
printk("drill: cannot initialize task\n"); printk("drill: cannot initialize task\n");
goto fail; goto fail;
} }
if (rt_task_init(&turntable_task, tester, 0, 1024, 0, 0, NULL)) { if (rt_task_init(&output_task, output, 0, 1024, 0, 0, NULL)) {
printk("tester: cannot initialize task\n");
goto fail;
}
if (rt_task_init(&turntable_task, output, 0, 1024, 0, 0, NULL)) {
printk("output: cannot initialize task\n"); printk("output: cannot initialize task\n");
goto fail; goto fail;
} }
if (rt_task_init(&tester_task, tester, 0, 1024, 0, 0, NULL)) {
printk("tester: cannot initialize task\n");
goto fail;
}
if (rt_mbx_init(&tester_mbx, sizeof(int))) {
end(true);
}
if (rt_mbx_init(&output_mbx, sizeof(int))) {
end(true);
}
if (rt_mbx_init(&drill_mbx, sizeof(int))) {
end(true);
}
if (rt_mbx_init(&output_mbx, sizeof(int))) {
end(true);
}
if (rt_mbx_init(&turntable_mbx, sizeof(int))) {
end(true);
}
rt_task_resume(&turntable_task); rt_task_resume(&turntable_task);
rt_task_resume(&drill_task);
rt_task_resume(&output_task);
rt_task_resume(&tester_task);
printk("loaded module Bearbeiten2\n"); printk("loaded module Bearbeiten2\n");
return (0); return (0);

View File

@ -7,6 +7,9 @@ start
:Drehteller ausschalten; :Drehteller ausschalten;
while(Dauerschleife) is (true) while(Dauerschleife) is (true)
:Prüfer fertig?<
:Bohrer fertig?<
:Auswerfer fertig?<
if(Sensor aktiv ?) then (true) if(Sensor aktiv ?) then (true)
:Drehteller anschalten; :Drehteller anschalten;
else (false) else (false)
@ -32,7 +35,7 @@ while(Dauerschleife) is (true)
:Prüfer einfahren; :Prüfer einfahren;
else (false) else (false)
endif endif
:Prüfer fertig>
endwhile (false) endwhile (false)
stop stop
} }
@ -62,7 +65,7 @@ if(Werkstück vorhanden?) then(true)
else (false) else (false)
endif endif
:Bohrer fertig>
endwhile(false) endwhile(false)
stop stop
@ -77,7 +80,7 @@ while(Dauerschleife) is (true)
:Auswerfer< :Auswerfer<
:Auswerfen; :Auswerfen;
:Auswerfer fertig>
endwhile(false) endwhile(false)
stop stop