Code Verbesserungen

This commit is contained in:
Johannes Theiner 2020-11-13 19:48:42 +01:00
parent 1a696eb52e
commit ffbdd40322
Signed by: joethei
GPG Key ID: 9D2B9A00FDA85BCD
1 changed files with 73 additions and 56 deletions

View File

@ -1,9 +1,15 @@
#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 "include/rtai_mbx.h"
#include "include/rtai_sched.h"
#include "include/rtai_sem.h"
#include "include/rtai_modbus.h"
#include <stdbool.h>
#include <uint128.h> #include <uint128.h>
#include <sys/rtai_modbus.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
@ -25,7 +31,8 @@
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, 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; static SEM semaphore;
int connection; int connection;
@ -39,8 +46,13 @@ void end(bool fail) {
rt_task_delete(&drill_task); rt_task_delete(&drill_task);
rt_task_delete(&output_task); rt_task_delete(&output_task);
rt_mbx_delete(&tester_mbx); rt_mbx_delete(&turntable_status_mbx);
rt_mbx_delete(&output_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); rt_sem_delete(&semaphore);
stop_rt_timer(); stop_rt_timer();
@ -49,6 +61,7 @@ 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);
} }
/** /**
@ -61,8 +74,6 @@ void end(bool fail) {
*/ */
int readAll(int type, int *result) { int readAll(int type, int *result) {
int res = 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; return res;
} }
@ -115,37 +126,58 @@ void enable(int actor) {
if(result) end(true); 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) { void sleepStuff(void) {
rt_sleep(1000 * nano2count(1000000)); rt_sleep(1000 * nano2count(1000000));
} }
static void turntable(long data) { static void turntable(long data) {
// rt_printk("started turntable task"); 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) {
sleepStuff();
}
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) { static void tester(long data) {
rt_printk("started tester task");
disable(ACTOR_TESTER); disable(ACTOR_TESTER);
while (1) { while (1) {
receiveMail(&tester_status_mbx);
int partOnTable = readData(SENSOR_PART_TESTER); int partOnTable = readData(SENSOR_PART_TESTER);
rt_printk("part: %d\n", partOnTable); rt_printk("part: %d\n", partOnTable);
if(partOnTable) { if(partOnTable) {
@ -153,27 +185,19 @@ static void tester(long data) {
sleepStuff(); sleepStuff();
int partNormal = readData(SENSOR_PART_TEST); int partNormal = readData(SENSOR_PART_TEST);
if(partNormal) { if(partNormal) {
//nachricht bohrer(ON) sendMail(&drill_data_mbx, 1);
int msg = 1;
int mbxStatus = rt_mbx_send(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
} else { } else {
//nachricht bohrer(OFF) sendMail(&drill_data_mbx, 0);
int msg = 0;
int mbxStatus = rt_mbx_send(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
} }
rt_printk("normal %d\n", partNormal); rt_printk("normal %d\n", partNormal);
disable(ACTOR_TESTER); disable(ACTOR_TESTER);
//Nachricht drehteller
} }
sendMail(&turntable_status_mbx, 1);
sleepStuff();
} }
} }
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);
@ -187,11 +211,10 @@ static void drill(long data) {
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
while (1) { while (1) {
receiveMail(&drill_status_mbx);
int part = readData(SENSOR_PART_DRILL); int part = readData(SENSOR_PART_DRILL);
if(part) { if(part) {
int msg = 0; int msg = receiveMailNonBlocking(&drill_data_mbx);
int mbxStatus = rt_mbx_receive(&drill_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
if(msg) { if(msg) {
enable(ACTOR_PART_HOLD); enable(ACTOR_PART_HOLD);
enable(ACTOR_DRILL); enable(ACTOR_DRILL);
@ -212,31 +235,25 @@ static void drill(long data) {
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
} }
} }
int msg = part; sendMailNonBlocking(&output_data_mbx, 1);
int mbxStatus = rt_mbx_send(&output_mbx, &msg, sizeof(int)); sendMail(&turntable_status_mbx, 1);
if(mbxStatus) end(true);
sleepStuff();
} }
} }
static void output(long data) { static void output(long data) {
rt_printk("starting output task");
disable(ACTOR_EXIT);
while (1) { while (1) {
//turntable msg receiveMail(&output_status_mbx);
int msg = receiveMailNonBlocking(&output_data_mbx);
int msg = 0;
int mbxStatus = rt_mbx_receive(&output_mbx, &msg, sizeof(int));
if(mbxStatus) end(true);
if(msg) { if(msg) {
enable(ACTOR_EXIT); enable(ACTOR_EXIT);
sleepStuff(); sleepStuff();
disable(ACTOR_EXIT); disable(ACTOR_EXIT);
} }
//nachricht an turntable sendMail(&turntable_status_mbx, 1);
sleepStuff();
} }
} }