Praktikum: abgabe

This commit is contained in:
Johannes Theiner 2020-11-30 09:06:11 +00:00
parent 7f23e3f994
commit 001628f14d
1 changed files with 116 additions and 52 deletions

View File

@ -1,15 +1,14 @@
/*#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_sched.h" //Author: Charlotte Friedemann, Johannes Theiner
#include "include/rtai_sem.h" //Description: Praktikum EZDV Gruppe A5(Bearbeiten 2)
#include "include/rtai_modbus.h" //Created: 19.10.2020
#include <stdbool.h> //Finished: 30.11.2020
//======================
//#include <uint128.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
@ -39,6 +38,10 @@ int connection;
char node[] = "modbus-node"; char node[] = "modbus-node";
/**
* deinitialize all tasks, mailboxes, modbus connections & semaphores
* @param fail should a error message be printed.
*/
void end(bool fail) { void end(bool fail) {
rt_modbus_disconnect(connection); rt_modbus_disconnect(connection);
rt_task_delete(&turntable_task); rt_task_delete(&turntable_task);
@ -59,7 +62,7 @@ void end(bool fail) {
stop_rt_timer(); stop_rt_timer();
if(fail) { if(fail) {
rt_printk("failed to read/send Modbus message.\n"); rt_printk("an error has occurred.\n");
rt_printk("module needs to be restarted.\n"); rt_printk("module needs to be restarted.\n");
} }
} }
@ -77,14 +80,15 @@ int readAll(int type, int *result) {
} }
/** /**
* * read a single bit for the specified part
* this function is thread safe. * this function is thread safe.
* @param part * @param part bitmask to read the sensor/actor
* @return * @return read bit.
*/ */
int readData(int part) { int readData(int part) {
rt_sem_wait(&semaphore);
int value = 0; int value = 0;
rt_sem_wait(&semaphore);
//the semaphore needs to be locked before this code executes, so we can't adhere to the ISO standard here.
int code = readAll(DIGITAL_IN, &value); int code = readAll(DIGITAL_IN, &value);
int result = (part & value); int result = (part & value);
rt_sem_signal(&semaphore); rt_sem_signal(&semaphore);
@ -98,13 +102,14 @@ int readData(int part) {
* @param actor bitmask of the actor to disable * @param actor bitmask of the actor to disable
*/ */
void disable(int actor) { void disable(int actor) {
int value = 0;
rt_sem_wait(&semaphore); rt_sem_wait(&semaphore);
int value = 0; if(readAll(DIGITAL_OUT, &value)) end(true);
int code = readAll(DIGITAL_OUT, &value); else {
if(code) end(true); int result = rt_modbus_set(connection, DIGITAL_OUT, 0, 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); }
} }
/** /**
@ -113,26 +118,41 @@ void disable(int actor) {
* @param actor bitmask of the actor to enable * @param actor bitmask of the actor to enable
*/ */
void enable(int actor) { void enable(int actor) {
int output = 0;
rt_sem_wait(&semaphore); rt_sem_wait(&semaphore);
int output = 0; if (readAll(DIGITAL_OUT, &output)) end(true);
int code = readAll(DIGITAL_OUT, &output); else {
if (code) end(true); int result = rt_modbus_set(connection, DIGITAL_OUT, 0, output |= actor);
int result; rt_sem_signal(&semaphore);
result = rt_modbus_set(connection, DIGITAL_OUT, 0, output |= actor); if(result) end(true);
rt_sem_signal(&semaphore); }
if(result) end(true);
} }
/**
* send a blocking message to the specified mailbox
* @param mailbox where should the message be send to ?
* @param msg message to send
*/
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 == EINVAL) end(true); if(mbxStatus == EINVAL) end(true);
} }
/**
* send a non blocking message to the specified mailbox
* @param mailbox where should the message be send to ?
* @param msg message to send
*/
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 == EINVAL) end(true); if(mbxStatus == EINVAL) end(true);
} }
/**
* receive a blocking message
* @param mailbox mailbox to receive the message
*/
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));
@ -140,6 +160,10 @@ int receiveMail(MBX * mailbox) {
return msg; return msg;
} }
/**
* receive a non blocking message
* @param mailbox mailbox to receive the message
*/
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));
@ -147,48 +171,81 @@ int receiveMailNonBlocking(MBX * mailbox) {
return msg; return msg;
} }
/**
* sleep for x milliseconds
* @param ms specified time to sleep in milliseconds.
*/
void sleepMs(int ms) { void sleepMs(int ms) {
rt_sleep(ms * nano2count(1000000)); rt_sleep(ms * nano2count(1000000));
} }
/**
* turntable task
*/
static void turntable(long data) { static void turntable(long data) {
int times = 0;
rt_printk("started turntable task\n"); rt_printk("started turntable task\n");
//reset everything first
disable(ACTOR_TURNTABLE); disable(ACTOR_TURNTABLE);
disable(ACTOR_ENTRANCE); disable(ACTOR_ENTRANCE);
//throw out all parts that might be on the table.
do {
enable(ACTOR_TURNTABLE);
sleepMs(1000);
disable(ACTOR_TURNTABLE);
sleepMs(500);
enable(ACTOR_EXIT);
sleepMs(500);
disable(ACTOR_EXIT);
times++;
}while (times < 5);
//start processing
while (1) { while (1) {
//receive status mail from: tester, drill & output
receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx);
receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx);
receiveMail(&turntable_status_mbx); receiveMail(&turntable_status_mbx);
int partOnTable = readData(SENSOR_PART_TURNTABLE); //always read from mailbox to make sure that no overflow occurs.
int msg1 = receiveMailNonBlocking(&turntable_data_mbx); int msg1 = receiveMailNonBlocking(&turntable_data_mbx);
int msg2 = receiveMailNonBlocking(&turntable_data_mbx); int msg2 = receiveMailNonBlocking(&turntable_data_mbx);
if(partOnTable || msg1 || msg2) { if(readData(SENSOR_PART_TURNTABLE) || msg1 || msg2) {//if a part is on any of the sensors
enable(ACTOR_TURNTABLE); enable(ACTOR_TURNTABLE);
sleepMs(1000); sleepMs(100);
disable(ACTOR_TURNTABLE); if (!readData(SENSOR_TURNTABLE_POS)) {
sleepMs(1000); do {
sleepMs(10);
} while (readData(SENSOR_TURNTABLE_POS) == 0);
}
disable(ACTOR_TURNTABLE);
sleepMs(500);
} }
//send status mails
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);
} }
} }
/**
* tester task
*/
static void tester(long data) { static void tester(long data) {
rt_printk("started tester task\n"); rt_printk("started tester task\n");
//reset everything first
disable(ACTOR_TESTER); disable(ACTOR_TESTER);
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);
//start processing
while (1) { while (1) {
receiveMail(&tester_status_mbx); receiveMail(&tester_status_mbx);
int partOnTable = readData(SENSOR_PART_TESTER); if(readData(SENSOR_PART_TESTER)) {
if(partOnTable) { sendMailNonBlocking(&turntable_data_mbx, 1);//turntable should turn on the next turn.
sendMailNonBlocking(&turntable_data_mbx, 1);
enable(ACTOR_TESTER); enable(ACTOR_TESTER);
sleepMs(1000); sleepMs(500);
int partNormal = readData(SENSOR_PART_TEST); if(readData(SENSOR_PART_TEST)) {//should the drill be active on the next turn ?
if(partNormal) {
sendMailNonBlocking(&drill_data_mbx, 1); sendMailNonBlocking(&drill_data_mbx, 1);
}else { }else {
sendMailNonBlocking(&drill_data_mbx, 0); sendMailNonBlocking(&drill_data_mbx, 0);
@ -199,7 +256,12 @@ static void tester(long data) {
} }
} }
/**
* drill task
*/
static void drill(long data) { static void drill(long data) {
rt_printk("started drill task\n");
//reset everything first
disable(ACTOR_DRILL); disable(ACTOR_DRILL);
disable(ACTOR_PART_HOLD); disable(ACTOR_PART_HOLD);
disable(ACTOR_DRILL_DOWN); disable(ACTOR_DRILL_DOWN);
@ -207,24 +269,23 @@ static void drill(long data) {
enable(ACTOR_DRILL_UP); enable(ACTOR_DRILL_UP);
if (!readData(SENSOR_DRILL_UP)) { if (!readData(SENSOR_DRILL_UP)) {
do { do {
sleepMs(10);
} 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); sendMail(&turntable_status_mbx, 1);
//start processing
while (1) { while (1) {
receiveMail(&drill_status_mbx); receiveMail(&drill_status_mbx);
int partOnTable = readData(SENSOR_PART_DRILL); if(readData(SENSOR_PART_DRILL)) {//if part is in drill
int msg = receiveMailNonBlocking(&drill_data_mbx); sendMailNonBlocking(&turntable_data_mbx, 1);//turntable should be active on the next turn.
if(partOnTable) { if(receiveMailNonBlocking(&drill_data_mbx)) {//read test result from mailbox
sendMailNonBlocking(&turntable_data_mbx, 1);
int msg = receiveMailNonBlocking(&drill_data_mbx);
if(msg) {
enable(ACTOR_PART_HOLD); enable(ACTOR_PART_HOLD);
enable(ACTOR_DRILL); enable(ACTOR_DRILL);
enable(ACTOR_DRILL_DOWN); enable(ACTOR_DRILL_DOWN);
if (!readData(SENSOR_DRILL_DOWN)) { if (!readData(SENSOR_DRILL_DOWN)) {
do { do {
sleepMs(10);
} while (readData(SENSOR_DRILL_DOWN) == 0); } while (readData(SENSOR_DRILL_DOWN) == 0);
} }
sleepMs(1000); sleepMs(1000);
@ -233,29 +294,32 @@ static void drill(long data) {
enable(ACTOR_DRILL_UP); enable(ACTOR_DRILL_UP);
if (!readData(SENSOR_DRILL_UP)) { if (!readData(SENSOR_DRILL_UP)) {
do { do {
sleepMs(10);
} while (readData(SENSOR_DRILL_UP) == 0); } while (readData(SENSOR_DRILL_UP) == 0);
} }
disable(ACTOR_DRILL_UP); disable(ACTOR_DRILL_UP);
disable(ACTOR_PART_HOLD); disable(ACTOR_PART_HOLD);
} }
sendMailNonBlocking(&output_data_mbx, 1); sendMailNonBlocking(&output_data_mbx, 1);//should the output be active on the next turn ?
} }
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);
} }
} }
/**
* output task
*/
static void output(long data) { static void output(long data) {
rt_printk("started output task\n");
//reset everything first
disable(ACTOR_EXIT); disable(ACTOR_EXIT);
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);
//start processing
while (1) { while (1) {
receiveMail(&output_status_mbx); receiveMail(&output_status_mbx);
int msg = receiveMailNonBlocking(&output_data_mbx); if(receiveMailNonBlocking(&output_data_mbx)) {//should the output be activated
if(msg) {
enable(ACTOR_EXIT); enable(ACTOR_EXIT);
sleepMs(1000); sleepMs(500);
disable(ACTOR_EXIT); disable(ACTOR_EXIT);
} }
sendMail(&turntable_status_mbx, 1); sendMail(&turntable_status_mbx, 1);