Praktikum: funktionierende version

This commit is contained in:
Johannes Theiner 2020-11-16 10:25:22 +00:00
parent ffbdd40322
commit a98fb8d2fd
1 changed files with 62 additions and 54 deletions

View File

@ -1,13 +1,13 @@
//#include <rtai_mbx.h> #include <rtai_mbx.h>
//#include <rtai_sched.h> #include <rtai_sched.h>
//#include <rtai_sem.h> #include <rtai_sem.h>
//#include <sys/rtai_modbus.h> #include <sys/rtai_modbus.h>
#include "include/rtai_mbx.h" //#include "include/rtai_mbx.h"
#include "include/rtai_sched.h" //#include "include/rtai_sched.h"
#include "include/rtai_sem.h" //#include "include/rtai_sem.h"
#include "include/rtai_modbus.h" //#include "include/rtai_modbus.h"
#include <stdbool.h> //#include <stdbool.h>
#include <uint128.h> #include <uint128.h>
@ -32,7 +32,7 @@ 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 turntable_status_mbx, tester_status_mbx, output_status_mbx, drill_status_mbx; 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; static SEM semaphore;
int connection; int connection;
@ -61,7 +61,6 @@ void end(bool fail) {
rt_printk("failed to read/send Modbus message.\n"); rt_printk("failed to read/send Modbus message.\n");
rt_printk("module needs to be restarted.\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) { void sendMail(MBX * mailbox, int msg) {
int mbxStatus = rt_mbx_send(mailbox, &msg, sizeof(int)); int mbxStatus = rt_mbx_send(mailbox, &msg, sizeof(int));
if(mbxStatus) end(true); if(mbxStatus == EINVAL) end(true);
} }
void sendMailNonBlocking(MBX * mailbox, int msg) { void sendMailNonBlocking(MBX * mailbox, int msg) {
int mbxStatus = rt_mbx_send_if(mailbox, &msg, sizeof(int)); int mbxStatus = rt_mbx_send_if(mailbox, &msg, sizeof(int));
if(mbxStatus) end(true); if(mbxStatus == EINVAL) end(true);
} }
int receiveMail(MBX * mailbox) { int receiveMail(MBX * mailbox) {
int msg = 0; int msg = 0;
int mbxStatus = rt_mbx_receive(mailbox, &msg, sizeof(int)); int mbxStatus = rt_mbx_receive(mailbox, &msg, sizeof(int));
if(mbxStatus) end(true); if(mbxStatus == EINVAL) end(true);
return msg; return msg;
} }
int receiveMailNonBlocking(MBX * mailbox) { int receiveMailNonBlocking(MBX * mailbox) {
int msg = 0; int msg = 0;
int mbxStatus = rt_mbx_receive_if(mailbox, &msg, sizeof(int)); int mbxStatus = rt_mbx_receive_if(mailbox, &msg, sizeof(int));
if(mbxStatus) end(true); if(mbxStatus == EINVAL) end(true);
return msg; return msg;
} }
@ -155,7 +154,7 @@ void sleepStuff(void) {
} }
static void turntable(long data) { static void turntable(long data) {
rt_printk("started turntable task"); rt_printk("started turntable task\n");
disable(ACTOR_TURNTABLE); disable(ACTOR_TURNTABLE);
while (1) { while (1) {
@ -163,9 +162,14 @@ static void turntable(long data) {
receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx);
receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx);
int partOnTable = readData(SENSOR_PART_TURNTABLE); int partOnTable = readData(SENSOR_PART_TURNTABLE);
rt_printk("%d", partOnTable); int msg1 = receiveMailNonBlocking(&turntable_data_mbx);
int msg2 = receiveMailNonBlocking(&turntable_data_mbx);
if(partOnTable || msg1 || msg2) {
enable(ACTOR_TURNTABLE); enable(ACTOR_TURNTABLE);
sleepStuff();
disable(ACTOR_TURNTABLE); disable(ACTOR_TURNTABLE);
sleepStuff();
}
sendMail(&tester_status_mbx, 1); sendMail(&tester_status_mbx, 1);
sendMail(&drill_status_mbx, 1); sendMail(&drill_status_mbx, 1);
sendMail(&output_status_mbx, 1); sendMail(&output_status_mbx, 1);
@ -173,23 +177,23 @@ static void turntable(long data) {
} }
static void tester(long data) { static void tester(long data) {
rt_printk("started tester task"); rt_printk("started tester task\n");
disable(ACTOR_TESTER); disable(ACTOR_TESTER);
sendMail(&turntable_status_mbx, 1);
while (1) { while (1) {
receiveMail(&tester_status_mbx); receiveMail(&tester_status_mbx);
int partOnTable = readData(SENSOR_PART_TESTER); int partOnTable = readData(SENSOR_PART_TESTER);
rt_printk("part: %d\n", partOnTable);
if(partOnTable) { if(partOnTable) {
sendMailNonBlocking(&turntable_data_mbx, 1);
enable(ACTOR_TESTER); enable(ACTOR_TESTER);
sleepStuff(); sleepStuff();
int partNormal = readData(SENSOR_PART_TEST); int partNormal = readData(SENSOR_PART_TEST);
if(partNormal) { if(partNormal) {
sendMail(&drill_data_mbx, 1); sendMailNonBlocking(&drill_data_mbx, 1);
}else { }else {
sendMail(&drill_data_mbx, 0); sendMailNonBlocking(&drill_data_mbx, 0);
} }
rt_printk("normal %d\n", partNormal);
disable(ACTOR_TESTER); disable(ACTOR_TESTER);
} }
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);
@ -197,11 +201,9 @@ static void tester(long data) {
} }
static void drill(long data) { static void drill(long data) {
rt_printk("starting drill task");
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);
if (!readData(SENSOR_DRILL_UP)) { if (!readData(SENSOR_DRILL_UP)) {
@ -209,11 +211,14 @@ static void drill(long data) {
} while (readData(SENSOR_DRILL_UP) == 0); } while (readData(SENSOR_DRILL_UP) == 0);
} }
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
sendMail(&turntable_status_mbx, 1);
while (1) { while (1) {
receiveMail(&drill_status_mbx); receiveMail(&drill_status_mbx);
int part = readData(SENSOR_PART_DRILL); int partOnTable = readData(SENSOR_PART_DRILL);
if(part) { int msg = receiveMailNonBlocking(&drill_data_mbx);
if(partOnTable) {
sendMailNonBlocking(&turntable_data_mbx, 1);
int msg = receiveMailNonBlocking(&drill_data_mbx); int msg = receiveMailNonBlocking(&drill_data_mbx);
if(msg) { if(msg) {
enable(ACTOR_PART_HOLD); enable(ACTOR_PART_HOLD);
@ -224,7 +229,6 @@ static void drill(long data) {
} while (readData(SENSOR_DRILL_DOWN) == 0); } while (readData(SENSOR_DRILL_DOWN) == 0);
} }
sleepStuff(); sleepStuff();
disable(ACTOR_PART_HOLD);
disable(ACTOR_DRILL); disable(ACTOR_DRILL);
disable(ACTOR_DRILL_DOWN); disable(ACTOR_DRILL_DOWN);
enable(ACTOR_DRILL_UP); enable(ACTOR_DRILL_UP);
@ -233,17 +237,19 @@ static void drill(long data) {
} while (readData(SENSOR_DRILL_UP) == 0); } while (readData(SENSOR_DRILL_UP) == 0);
} }
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
} disable(ACTOR_PART_HOLD);
} }
sendMailNonBlocking(&output_data_mbx, 1); sendMailNonBlocking(&output_data_mbx, 1);
}
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);
} }
} }
static void output(long data) { static void output(long data) {
rt_printk("starting output task");
disable(ACTOR_EXIT); disable(ACTOR_EXIT);
sendMail(&turntable_status_mbx, 1);
while (1) { while (1) {
receiveMail(&output_status_mbx); receiveMail(&output_status_mbx);
int msg = receiveMailNonBlocking(&output_data_mbx); int msg = receiveMailNonBlocking(&output_data_mbx);
@ -263,44 +269,50 @@ example_init(void) {
start_rt_timer(0); start_rt_timer(0);
modbus_init(); modbus_init();
rt_printk("init: task started\n"); rt_printk("init: 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 1; return -1;
} }
rt_sem_init(&semaphore, 1); rt_sem_init(&semaphore, 1);
if (rt_task_init(&turntable_task, turntable, 0, 1024, 0, 0, NULL)) { if (rt_task_init(&turntable_task, turntable, 0, 1024, 0, 0, NULL)) {
printk("turntable: cannot initialize task\n"); rt_printk("turntable: cannot initialize task\n");
goto fail; end(true);
} }
if (rt_task_init(&drill_task, drill, 0, 1024, 0, 0, NULL)) { if (rt_task_init(&drill_task, drill, 0, 1024, 0, 0, NULL)) {
printk("drill: cannot initialize task\n"); rt_printk("drill: cannot initialize task\n");
goto fail; end(true);
} }
if (rt_task_init(&output_task, output, 0, 1024, 0, 0, NULL)) { if (rt_task_init(&output_task, output, 0, 1024, 0, 0, NULL)) {
printk("output: cannot initialize task\n"); rt_printk("output: cannot initialize task\n");
goto fail; end(true);
} }
if (rt_task_init(&tester_task, tester, 0, 1024, 0, 0, NULL)) { if (rt_task_init(&tester_task, tester, 0, 1024, 0, 0, NULL)) {
printk("tester: cannot initialize task\n"); rt_printk("tester: cannot initialize task\n");
goto fail; end(true);
} }
if (rt_mbx_init(&tester_mbx, sizeof(int))) { if (rt_mbx_init(&turntable_status_mbx, sizeof(int))) {
end(true); end(true);
} }
if (rt_mbx_init(&output_mbx, sizeof(int))) { if (rt_mbx_init(&tester_status_mbx, sizeof(int))) {
end(true); end(true);
} }
if (rt_mbx_init(&drill_mbx, sizeof(int))) { if (rt_mbx_init(&drill_status_mbx, sizeof(int))) {
end(true); end(true);
} }
if (rt_mbx_init(&output_mbx, sizeof(int))) { if (rt_mbx_init(&output_status_mbx, sizeof(int))) {
end(true); 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); end(true);
} }
@ -308,18 +320,14 @@ example_init(void) {
rt_task_resume(&drill_task); rt_task_resume(&drill_task);
rt_task_resume(&output_task); rt_task_resume(&output_task);
rt_task_resume(&tester_task); rt_task_resume(&tester_task);
printk("loaded module Bearbeiten2\n"); rt_printk("loaded module Bearbeiten2\n");
return (0); return (0);
fail:
end(true);
return (1);
} }
static void __exit static void __exit
example_exit(void) { example_exit(void) {
end(false); end(false);
printk("module Bearbeiten2 unloaded\n"); rt_printk("module Bearbeiten2 unloaded\n");
} }
module_exit(example_exit) module_exit(example_exit)