From a98fb8d2fddd21aa9047c7a4b951e335d5ae471e Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 16 Nov 2020 10:25:22 +0000 Subject: [PATCH] Praktikum: funktionierende version --- Bearbeiten2.c | 116 +++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/Bearbeiten2.c b/Bearbeiten2.c index 6adb8a8..fccdcd9 100644 --- a/Bearbeiten2.c +++ b/Bearbeiten2.c @@ -1,13 +1,13 @@ -//#include -//#include -//#include -//#include +#include +#include +#include +#include -#include "include/rtai_mbx.h" -#include "include/rtai_sched.h" -#include "include/rtai_sem.h" -#include "include/rtai_modbus.h" -#include +//#include "include/rtai_mbx.h" +//#include "include/rtai_sched.h" +//#include "include/rtai_sem.h" +//#include "include/rtai_modbus.h" +//#include #include @@ -32,7 +32,7 @@ MODULE_LICENSE("GPL"); static RT_TASK turntable_task, drill_task, tester_task, output_task; static MBX turntable_status_mbx, tester_status_mbx, output_status_mbx, drill_status_mbx; -static MBX output_data_mbx, drill_data_mbx; +static MBX output_data_mbx, drill_data_mbx, turntable_data_mbx; static SEM semaphore; int connection; @@ -61,7 +61,6 @@ void end(bool fail) { rt_printk("failed to read/send Modbus message.\n"); rt_printk("module needs to be restarted.\n"); } - exit(-1); } /** @@ -128,25 +127,25 @@ void enable(int actor) { void sendMail(MBX * mailbox, int msg) { int mbxStatus = rt_mbx_send(mailbox, &msg, sizeof(int)); - if(mbxStatus) end(true); + if(mbxStatus == EINVAL) end(true); } void sendMailNonBlocking(MBX * mailbox, int msg) { int mbxStatus = rt_mbx_send_if(mailbox, &msg, sizeof(int)); - if(mbxStatus) end(true); + if(mbxStatus == EINVAL) end(true); } int receiveMail(MBX * mailbox) { int msg = 0; int mbxStatus = rt_mbx_receive(mailbox, &msg, sizeof(int)); - if(mbxStatus) end(true); + if(mbxStatus == EINVAL) end(true); return msg; } int receiveMailNonBlocking(MBX * mailbox) { int msg = 0; int mbxStatus = rt_mbx_receive_if(mailbox, &msg, sizeof(int)); - if(mbxStatus) end(true); + if(mbxStatus == EINVAL) end(true); return msg; } @@ -155,7 +154,7 @@ void sleepStuff(void) { } static void turntable(long data) { - rt_printk("started turntable task"); + rt_printk("started turntable task\n"); disable(ACTOR_TURNTABLE); while (1) { @@ -163,9 +162,14 @@ static void turntable(long data) { receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx); int partOnTable = readData(SENSOR_PART_TURNTABLE); - rt_printk("%d", partOnTable); - enable(ACTOR_TURNTABLE); - disable(ACTOR_TURNTABLE); + int msg1 = receiveMailNonBlocking(&turntable_data_mbx); + int msg2 = receiveMailNonBlocking(&turntable_data_mbx); + if(partOnTable || msg1 || msg2) { + enable(ACTOR_TURNTABLE); + sleepStuff(); + disable(ACTOR_TURNTABLE); + sleepStuff(); + } sendMail(&tester_status_mbx, 1); sendMail(&drill_status_mbx, 1); sendMail(&output_status_mbx, 1); @@ -173,23 +177,23 @@ static void turntable(long data) { } static void tester(long data) { - rt_printk("started tester task"); + rt_printk("started tester task\n"); disable(ACTOR_TESTER); + sendMail(&turntable_status_mbx, 1); while (1) { receiveMail(&tester_status_mbx); int partOnTable = readData(SENSOR_PART_TESTER); - rt_printk("part: %d\n", partOnTable); if(partOnTable) { + sendMailNonBlocking(&turntable_data_mbx, 1); enable(ACTOR_TESTER); sleepStuff(); int partNormal = readData(SENSOR_PART_TEST); if(partNormal) { - sendMail(&drill_data_mbx, 1); - } else { - sendMail(&drill_data_mbx, 0); + sendMailNonBlocking(&drill_data_mbx, 1); + }else { + sendMailNonBlocking(&drill_data_mbx, 0); } - rt_printk("normal %d\n", partNormal); disable(ACTOR_TESTER); } sendMail(&turntable_status_mbx, 1); @@ -197,11 +201,9 @@ static void tester(long data) { } static void drill(long data) { - rt_printk("starting drill task"); disable(ACTOR_DRILL); disable(ACTOR_PART_HOLD); disable(ACTOR_DRILL_DOWN); - rt_printk("hello"); enable(ACTOR_DRILL_UP); if (!readData(SENSOR_DRILL_UP)) { @@ -209,11 +211,14 @@ static void drill(long data) { } while (readData(SENSOR_DRILL_UP) == 0); } disable(ACTOR_DRILL_UP); + sendMail(&turntable_status_mbx, 1); while (1) { receiveMail(&drill_status_mbx); - int part = readData(SENSOR_PART_DRILL); - if(part) { + int partOnTable = readData(SENSOR_PART_DRILL); + int msg = receiveMailNonBlocking(&drill_data_mbx); + if(partOnTable) { + sendMailNonBlocking(&turntable_data_mbx, 1); int msg = receiveMailNonBlocking(&drill_data_mbx); if(msg) { enable(ACTOR_PART_HOLD); @@ -224,7 +229,6 @@ static void drill(long data) { } while (readData(SENSOR_DRILL_DOWN) == 0); } sleepStuff(); - disable(ACTOR_PART_HOLD); disable(ACTOR_DRILL); disable(ACTOR_DRILL_DOWN); enable(ACTOR_DRILL_UP); @@ -233,17 +237,19 @@ static void drill(long data) { } while (readData(SENSOR_DRILL_UP) == 0); } disable(ACTOR_DRILL_UP); + disable(ACTOR_PART_HOLD); } + sendMailNonBlocking(&output_data_mbx, 1); } - sendMailNonBlocking(&output_data_mbx, 1); + + sendMail(&turntable_status_mbx, 1); } } static void output(long data) { - rt_printk("starting output task"); disable(ACTOR_EXIT); - + sendMail(&turntable_status_mbx, 1); while (1) { receiveMail(&output_status_mbx); int msg = receiveMailNonBlocking(&output_data_mbx); @@ -263,63 +269,65 @@ example_init(void) { start_rt_timer(0); modbus_init(); - rt_printk("init: task started\n"); + rt_printk("init: started\n"); if ((connection = rt_modbus_connect(node)) == -1) { rt_printk("init: could not connect to %s\n", node); - return 1; + return -1; } rt_sem_init(&semaphore, 1); if (rt_task_init(&turntable_task, turntable, 0, 1024, 0, 0, NULL)) { - printk("turntable: cannot initialize task\n"); - goto fail; + rt_printk("turntable: cannot initialize task\n"); + end(true); } if (rt_task_init(&drill_task, drill, 0, 1024, 0, 0, NULL)) { - printk("drill: cannot initialize task\n"); - goto fail; + rt_printk("drill: cannot initialize task\n"); + end(true); } if (rt_task_init(&output_task, output, 0, 1024, 0, 0, NULL)) { - printk("output: cannot initialize task\n"); - goto fail; + rt_printk("output: cannot initialize task\n"); + end(true); } if (rt_task_init(&tester_task, tester, 0, 1024, 0, 0, NULL)) { - printk("tester: cannot initialize task\n"); - goto fail; + rt_printk("tester: cannot initialize task\n"); + end(true); } - if (rt_mbx_init(&tester_mbx, sizeof(int))) { + if (rt_mbx_init(&turntable_status_mbx, sizeof(int))) { end(true); } - if (rt_mbx_init(&output_mbx, sizeof(int))) { + if (rt_mbx_init(&tester_status_mbx, sizeof(int))) { end(true); } - if (rt_mbx_init(&drill_mbx, sizeof(int))) { + if (rt_mbx_init(&drill_status_mbx, sizeof(int))) { end(true); } - if (rt_mbx_init(&output_mbx, sizeof(int))) { + if (rt_mbx_init(&output_status_mbx, sizeof(int))) { end(true); } - if (rt_mbx_init(&turntable_mbx, sizeof(int))) { + if (rt_mbx_init(&output_data_mbx, sizeof(int))) { end(true); } + if (rt_mbx_init(&drill_data_mbx, sizeof(int))) { + end(true); + } + if (rt_mbx_init(&turntable_data_mbx, sizeof(int))) { + end(true); + } 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"); + rt_printk("loaded module Bearbeiten2\n"); return (0); - - fail: - end(true); - return (1); } static void __exit example_exit(void) { end(false); - printk("module Bearbeiten2 unloaded\n"); + rt_printk("module Bearbeiten2 unloaded\n"); } module_exit(example_exit)