From ffbdd4032296930f28357e19d76476559f3e53d1 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 13 Nov 2020 19:48:42 +0100 Subject: [PATCH] Code Verbesserungen --- Bearbeiten2.c | 129 ++++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 56 deletions(-) diff --git a/Bearbeiten2.c b/Bearbeiten2.c index b740bb7..6adb8a8 100644 --- a/Bearbeiten2.c +++ b/Bearbeiten2.c @@ -1,9 +1,15 @@ -#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 #define SENSOR_PART_TURNTABLE 1<<0 #define SENSOR_PART_DRILL 1<<1 @@ -25,7 +31,8 @@ MODULE_LICENSE("GPL"); static RT_TASK turntable_task, drill_task, tester_task, output_task; -static MBX tester_mbx, output_mbx, drill_mbx, turntable_mbx; +static MBX turntable_status_mbx, tester_status_mbx, output_status_mbx, drill_status_mbx; +static MBX output_data_mbx, drill_data_mbx; static SEM semaphore; int connection; @@ -39,8 +46,13 @@ void end(bool fail) { rt_task_delete(&drill_task); rt_task_delete(&output_task); - rt_mbx_delete(&tester_mbx); - rt_mbx_delete(&output_mbx); + rt_mbx_delete(&turntable_status_mbx); + rt_mbx_delete(&tester_status_mbx); + rt_mbx_delete(&output_status_mbx); + rt_mbx_delete(&drill_status_mbx); + rt_mbx_delete(&output_data_mbx); + rt_mbx_delete(&drill_data_mbx); + rt_sem_delete(&semaphore); stop_rt_timer(); @@ -49,6 +61,7 @@ void end(bool fail) { rt_printk("failed to read/send Modbus message.\n"); rt_printk("module needs to be restarted.\n"); } + exit(-1); } /** @@ -61,8 +74,6 @@ void end(bool fail) { */ int readAll(int type, int *result) { int res = rt_modbus_get(connection, type, 0, (unsigned short *) result); - //rt_printk("%d\n", res); - //rt_printk("%d", &result); return res; } @@ -115,37 +126,58 @@ void enable(int actor) { if(result) end(true); } +void sendMail(MBX * mailbox, int msg) { + int mbxStatus = rt_mbx_send(mailbox, &msg, sizeof(int)); + if(mbxStatus) end(true); +} + +void sendMailNonBlocking(MBX * mailbox, int msg) { + int mbxStatus = rt_mbx_send_if(mailbox, &msg, sizeof(int)); + if(mbxStatus) end(true); +} + +int receiveMail(MBX * mailbox) { + int msg = 0; + int mbxStatus = rt_mbx_receive(mailbox, &msg, sizeof(int)); + if(mbxStatus) 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); + return msg; +} + void sleepStuff(void) { rt_sleep(1000 * nano2count(1000000)); } static void turntable(long data) { -// rt_printk("started turntable task"); -// 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) { - - sleepStuff(); - } + rt_printk("started turntable task"); + disable(ACTOR_TURNTABLE); + while (1) { + receiveMail(&turntable_status_mbx); + receiveMail(&turntable_status_mbx); + receiveMail(&turntable_status_mbx); + int partOnTable = readData(SENSOR_PART_TURNTABLE); + rt_printk("%d", partOnTable); + enable(ACTOR_TURNTABLE); + disable(ACTOR_TURNTABLE); + sendMail(&tester_status_mbx, 1); + sendMail(&drill_status_mbx, 1); + sendMail(&output_status_mbx, 1); + } } static void tester(long data) { + rt_printk("started tester task"); disable(ACTOR_TESTER); while (1) { - + receiveMail(&tester_status_mbx); int partOnTable = readData(SENSOR_PART_TESTER); rt_printk("part: %d\n", partOnTable); if(partOnTable) { @@ -153,27 +185,19 @@ static void tester(long data) { 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); + sendMail(&drill_data_mbx, 1); } else { - //nachricht bohrer(OFF) - int msg = 0; - int mbxStatus = rt_mbx_send(&drill_mbx, &msg, sizeof(int)); - if(mbxStatus) end(true); + sendMail(&drill_data_mbx, 0); } rt_printk("normal %d\n", partNormal); disable(ACTOR_TESTER); - //Nachricht drehteller } - - sleepStuff(); + sendMail(&turntable_status_mbx, 1); } } static void drill(long data) { - + rt_printk("starting drill task"); disable(ACTOR_DRILL); disable(ACTOR_PART_HOLD); disable(ACTOR_DRILL_DOWN); @@ -187,11 +211,10 @@ static void drill(long data) { disable(ACTOR_DRILL_UP); while (1) { + receiveMail(&drill_status_mbx); 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); + int msg = receiveMailNonBlocking(&drill_data_mbx); if(msg) { enable(ACTOR_PART_HOLD); enable(ACTOR_DRILL); @@ -212,31 +235,25 @@ static void drill(long data) { disable(ACTOR_DRILL_UP); } } - int msg = part; - int mbxStatus = rt_mbx_send(&output_mbx, &msg, sizeof(int)); - if(mbxStatus) end(true); - - sleepStuff(); + sendMailNonBlocking(&output_data_mbx, 1); + sendMail(&turntable_status_mbx, 1); } } static void output(long data) { + rt_printk("starting output task"); + disable(ACTOR_EXIT); while (1) { - //turntable msg - - int msg = 0; - int mbxStatus = rt_mbx_receive(&output_mbx, &msg, sizeof(int)); - if(mbxStatus) end(true); + receiveMail(&output_status_mbx); + int msg = receiveMailNonBlocking(&output_data_mbx); if(msg) { enable(ACTOR_EXIT); sleepStuff(); disable(ACTOR_EXIT); } - //nachricht an turntable - - sleepStuff(); + sendMail(&turntable_status_mbx, 1); } }