Echtzeitdatenverarbeitung/Bearbeiten2.c

411 lines
11 KiB
C
Raw Normal View History

2020-11-30 10:06:11 +01:00
#include <rtai_mbx.h>
2020-11-16 11:25:22 +01:00
#include <rtai_sched.h>
#include <rtai_sem.h>
2020-11-30 10:06:11 +01:00
#include <sys/rtai_modbus.h>
2020-11-13 19:48:42 +01:00
2020-11-30 10:06:11 +01:00
//======================
2021-01-02 14:24:22 +01:00
//Author: Charlotte Friedemann(7011467), Johannes Theiner(7010923)
2020-11-30 10:06:11 +01:00
//Description: Praktikum EZDV Gruppe A5(Bearbeiten 2)
//Created: 19.10.2020
//Finished: 30.11.2020
//======================
2020-10-19 18:46:24 +02:00
2020-10-19 19:58:28 +02:00
#define SENSOR_PART_TURNTABLE 1<<0
#define SENSOR_PART_DRILL 1<<1
2020-11-02 11:31:29 +01:00
#define SENSOR_PART_TESTER 1<<2
2020-10-19 19:58:28 +02:00
#define SENSOR_DRILL_UP 1<<3
#define SENSOR_DRILL_DOWN 1<<4
#define SENSOR_TURNTABLE_POS 1<<5
#define SENSOR_PART_TEST 1<<6
#define ACTOR_DRILL 1<<0
#define ACTOR_TURNTABLE 1<<1
#define ACTOR_DRILL_DOWN 1<<2
#define ACTOR_DRILL_UP 1<<3
#define ACTOR_PART_HOLD 1<<4
#define ACTOR_TESTER 1<<5
#define ACTOR_EXIT 1<<6
#define ACTOR_ENTRANCE 1<<7
2020-10-19 18:46:24 +02:00
MODULE_LICENSE("GPL");
2020-10-21 16:15:24 +02:00
static RT_TASK turntable_task, drill_task, tester_task, output_task;
static MBX turntable_status_mbx, tester_status_mbx;
static MBX output_status_mbx, drill_status_mbx;
2020-11-16 11:25:22 +01:00
static MBX output_data_mbx, drill_data_mbx, turntable_data_mbx;
2020-10-21 16:15:24 +02:00
static SEM semaphore;
2020-10-19 18:46:24 +02:00
2020-10-26 10:38:48 +01:00
int connection;
2020-10-21 16:15:24 +02:00
char node[] = "modbus-node";
2020-11-30 10:06:11 +01:00
/**
* deinitialize all tasks, mailboxes, modbus connections & semaphores
* @param fail should a error message be printed.
*/
2020-10-28 14:30:09 +01:00
void end(bool fail) {
2020-10-26 10:38:48 +01:00
rt_modbus_disconnect(connection);
rt_task_delete(&turntable_task);
rt_task_delete(&tester_task);
rt_task_delete(&drill_task);
rt_task_delete(&output_task);
2020-11-13 19:48:42 +01:00
rt_mbx_delete(&turntable_status_mbx);
rt_mbx_delete(&tester_status_mbx);
rt_mbx_delete(&drill_status_mbx);
2020-11-19 12:22:01 +01:00
rt_mbx_delete(&output_status_mbx);
rt_mbx_delete(&turntable_data_mbx);
2020-11-13 19:48:42 +01:00
rt_mbx_delete(&drill_data_mbx);
2020-11-19 12:22:01 +01:00
rt_mbx_delete(&output_data_mbx);
2020-11-13 19:48:42 +01:00
2020-10-26 10:38:48 +01:00
rt_sem_delete(&semaphore);
stop_rt_timer();
2020-10-28 14:30:09 +01:00
if(fail) {
2020-11-30 10:06:11 +01:00
rt_printk("an error has occurred.\n");
2020-10-28 14:30:09 +01:00
rt_printk("module needs to be restarted.\n");
}
2020-10-26 10:38:48 +01:00
}
2020-10-21 16:15:24 +02:00
/**
2020-11-19 12:22:01 +01:00
* read all bits of the specified type
2020-10-21 16:15:24 +02:00
* this function is *not* thread safe.
* @param type (DIGITAL_IN, DIGITAL_OUT, ANALOG_IN, ANALOG_OUT)
* @param result value to write the result to
* @return status code(0: success, 1: failure)
*/
2020-10-26 10:38:48 +01:00
int readAll(int type, int *result) {
int res = rt_modbus_get(connection, type, 0,
(unsigned short *) result);
2020-11-02 11:31:29 +01:00
return res;
2020-10-19 19:58:28 +02:00
}
2020-10-21 16:15:24 +02:00
/**
2020-11-30 10:06:11 +01:00
* read a single bit for the specified part
2020-10-21 16:15:24 +02:00
* this function is thread safe.
2020-11-30 10:06:11 +01:00
* @param part bitmask to read the sensor/actor
* @return read bit.
2020-10-21 16:15:24 +02:00
*/
2020-10-28 14:30:09 +01:00
int readData(int part) {
2020-11-02 11:31:29 +01:00
int value = 0;
2020-11-30 10:06:11 +01:00
rt_sem_wait(&semaphore);
//the semaphore needs to be locked before this code executes,
// so we can't adhere to the ISO standard here.
2020-10-26 10:38:48 +01:00
int code = readAll(DIGITAL_IN, &value);
2020-11-02 11:31:29 +01:00
int result = (part & value);
2020-10-21 16:15:24 +02:00
rt_sem_signal(&semaphore);
2020-10-28 14:30:09 +01:00
if(code) end(true);
2020-11-02 11:31:29 +01:00
return result;
2020-10-19 19:58:28 +02:00
}
2020-10-21 16:15:24 +02:00
/**
* disable a specified actor of the system.
* this function is thread safe.
* @param actor bitmask of the actor to disable
*/
2020-10-26 10:38:48 +01:00
void disable(int actor) {
2020-11-30 10:06:11 +01:00
int value = 0;
2020-10-21 16:15:24 +02:00
rt_sem_wait(&semaphore);
2020-11-30 10:06:11 +01:00
if(readAll(DIGITAL_OUT, &value)) end(true);
else {
int result = rt_modbus_set(connection, DIGITAL_OUT, 0,
value &= ~actor);
2020-11-30 10:06:11 +01:00
rt_sem_signal(&semaphore);
if (result) end(true);
}
2020-10-19 19:58:28 +02:00
}
2020-10-21 16:15:24 +02:00
/**
* enable one specified actor of the system.
* this function is thread safe.
* @param actor bitmask of the actor to enable
*/
2020-10-26 10:38:48 +01:00
void enable(int actor) {
2020-11-30 10:06:11 +01:00
int output = 0;
2020-10-21 16:15:24 +02:00
rt_sem_wait(&semaphore);
2020-11-30 10:06:11 +01:00
if (readAll(DIGITAL_OUT, &output)) end(true);
else {
int result = rt_modbus_set(connection, DIGITAL_OUT, 0,
output |= actor);
2020-11-30 10:06:11 +01:00
rt_sem_signal(&semaphore);
if(result) end(true);
}
2020-10-19 19:58:28 +02:00
}
2020-11-30 10:06:11 +01:00
/**
* send a blocking message to the specified mailbox
* @param mailbox where should the message be send to ?
* @param msg message to send
*/
2020-11-13 19:48:42 +01:00
void sendMail(MBX * mailbox, int msg) {
int mbxStatus = rt_mbx_send(mailbox, &msg, sizeof(int));
2020-11-16 11:25:22 +01:00
if(mbxStatus == EINVAL) end(true);
2020-11-13 19:48:42 +01:00
}
2020-11-30 10:06:11 +01:00
/**
* send a non blocking message to the specified mailbox
* @param mailbox where should the message be send to ?
* @param msg message to send
*/
2020-11-13 19:48:42 +01:00
void sendMailNonBlocking(MBX * mailbox, int msg) {
int mbxStatus = rt_mbx_send_if(mailbox, &msg, sizeof(int));
2020-11-16 11:25:22 +01:00
if(mbxStatus == EINVAL) end(true);
2020-11-13 19:48:42 +01:00
}
2020-11-30 10:06:11 +01:00
/**
* receive a blocking message
* @param mailbox mailbox to receive the message
*/
2020-11-13 19:48:42 +01:00
int receiveMail(MBX * mailbox) {
int msg = 0;
int mbxStatus = rt_mbx_receive(mailbox, &msg, sizeof(int));
2020-11-16 11:25:22 +01:00
if(mbxStatus == EINVAL) end(true);
2020-11-13 19:48:42 +01:00
return msg;
}
2020-11-30 10:06:11 +01:00
/**
* receive a non blocking message
* @param mailbox mailbox to receive the message
*/
2020-11-13 19:48:42 +01:00
int receiveMailNonBlocking(MBX * mailbox) {
int msg = 0;
int mbxStatus = rt_mbx_receive_if(mailbox, &msg, sizeof(int));
2020-11-16 11:25:22 +01:00
if(mbxStatus == EINVAL) end(true);
2020-11-13 19:48:42 +01:00
return msg;
}
2020-11-30 10:06:11 +01:00
/**
* sleep for x milliseconds
* @param ms specified time to sleep in milliseconds.
*/
2020-11-19 12:22:01 +01:00
void sleepMs(int ms) {
rt_sleep(ms * nano2count(1000000));
2020-10-21 16:15:24 +02:00
}
2020-11-30 10:06:11 +01:00
/**
* turntable task
*/
2020-11-02 11:31:29 +01:00
static void turntable(long data) {
2020-11-30 10:06:11 +01:00
int times = 0;
2020-11-16 11:25:22 +01:00
rt_printk("started turntable task\n");
2020-11-30 10:06:11 +01:00
//reset everything first
2020-11-13 19:48:42 +01:00
disable(ACTOR_TURNTABLE);
2020-11-19 12:22:01 +01:00
disable(ACTOR_ENTRANCE);
2020-10-19 19:58:28 +02:00
2020-11-30 10:06:11 +01:00
//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
2020-11-13 19:48:42 +01:00
while (1) {
2020-11-30 10:06:11 +01:00
//receive status mail from: tester, drill & output
2020-11-13 19:48:42 +01:00
receiveMail(&turntable_status_mbx);
receiveMail(&turntable_status_mbx);
receiveMail(&turntable_status_mbx);
2020-11-30 10:06:11 +01:00
//always read from mailbox to make sure that no overflow occurs.
2020-11-16 11:25:22 +01:00
int msg1 = receiveMailNonBlocking(&turntable_data_mbx);
int msg2 = receiveMailNonBlocking(&turntable_data_mbx);
//if a part is on any of the sensors
if(readData(SENSOR_PART_TURNTABLE) || msg1 || msg2) {
2020-11-19 12:22:01 +01:00
enable(ACTOR_TURNTABLE);
2020-11-30 10:06:11 +01:00
sleepMs(100);
if (!readData(SENSOR_TURNTABLE_POS)) {
do {
sleepMs(10);
} while (readData(SENSOR_TURNTABLE_POS) == 0);
}
disable(ACTOR_TURNTABLE);
sleepMs(500);
2020-11-16 11:25:22 +01:00
}
2020-11-30 10:06:11 +01:00
//send status mails
2020-11-13 19:48:42 +01:00
sendMail(&tester_status_mbx, 1);
sendMail(&drill_status_mbx, 1);
sendMail(&output_status_mbx, 1);
}
2020-11-02 11:31:29 +01:00
}
2020-10-21 16:15:24 +02:00
2020-11-30 10:06:11 +01:00
/**
* tester task
*/
2020-11-02 11:31:29 +01:00
static void tester(long data) {
2020-11-16 11:25:22 +01:00
rt_printk("started tester task\n");
2020-11-02 11:31:29 +01:00
2020-11-30 10:06:11 +01:00
//reset everything first
2020-11-02 11:31:29 +01:00
disable(ACTOR_TESTER);
2020-11-16 11:25:22 +01:00
sendMail(&turntable_status_mbx, 1);
2020-11-30 10:06:11 +01:00
//start processing
2020-10-19 19:58:28 +02:00
while (1) {
2020-11-13 19:48:42 +01:00
receiveMail(&tester_status_mbx);
2020-11-30 10:06:11 +01:00
if(readData(SENSOR_PART_TESTER)) {
//turntable should turn on the next turn.
sendMailNonBlocking(&turntable_data_mbx, 1);
2020-11-02 11:31:29 +01:00
enable(ACTOR_TESTER);
2020-11-30 10:06:11 +01:00
sleepMs(500);
//should the drill be active on the next turn ?
if(readData(SENSOR_PART_TEST)) {
2020-11-16 11:25:22 +01:00
sendMailNonBlocking(&drill_data_mbx, 1);
}else {
sendMailNonBlocking(&drill_data_mbx, 0);
2020-11-02 11:31:29 +01:00
}
disable(ACTOR_TESTER);
}
2020-11-13 19:48:42 +01:00
sendMail(&turntable_status_mbx, 1);
2020-10-19 19:58:28 +02:00
}
2020-10-19 18:46:24 +02:00
}
2020-11-30 10:06:11 +01:00
/**
* drill task
*/
2020-11-02 11:31:29 +01:00
static void drill(long data) {
2020-11-30 10:06:11 +01:00
rt_printk("started drill task\n");
//reset everything first
2020-10-26 10:38:48 +01:00
disable(ACTOR_DRILL);
disable(ACTOR_PART_HOLD);
disable(ACTOR_DRILL_DOWN);
2020-10-21 16:15:24 +02:00
2020-10-26 10:38:48 +01:00
enable(ACTOR_DRILL_UP);
2020-10-28 14:30:09 +01:00
if (!readData(SENSOR_DRILL_UP)) {
2020-10-21 16:15:24 +02:00
do {
2020-11-30 10:06:11 +01:00
sleepMs(10);
2020-10-28 14:30:09 +01:00
} while (readData(SENSOR_DRILL_UP) == 0);
2020-10-21 16:15:24 +02:00
}
2020-10-26 10:38:48 +01:00
disable(ACTOR_DRILL_UP);
2020-11-16 11:25:22 +01:00
sendMail(&turntable_status_mbx, 1);
2020-11-30 10:06:11 +01:00
//start processing
2020-10-21 16:15:24 +02:00
while (1) {
2020-11-13 19:48:42 +01:00
receiveMail(&drill_status_mbx);
//if part is in drill
if(readData(SENSOR_PART_DRILL)) {
//turntable should be active on the next turn.
sendMailNonBlocking(&turntable_data_mbx, 1);
//read test result from mailbox.
if(receiveMailNonBlocking(&drill_data_mbx)) {
2020-11-02 11:31:29 +01:00
enable(ACTOR_PART_HOLD);
enable(ACTOR_DRILL);
enable(ACTOR_DRILL_DOWN);
if (!readData(SENSOR_DRILL_DOWN)) {
do {
2020-11-30 10:06:11 +01:00
sleepMs(10);
2020-11-02 11:31:29 +01:00
} while (readData(SENSOR_DRILL_DOWN) == 0);
}
2020-11-19 12:22:01 +01:00
sleepMs(1000);
2020-11-02 11:31:29 +01:00
disable(ACTOR_DRILL);
disable(ACTOR_DRILL_DOWN);
enable(ACTOR_DRILL_UP);
if (!readData(SENSOR_DRILL_UP)) {
do {
2020-11-30 10:06:11 +01:00
sleepMs(10);
2020-11-02 11:31:29 +01:00
} while (readData(SENSOR_DRILL_UP) == 0);
}
disable(ACTOR_DRILL_UP);
2020-11-16 11:25:22 +01:00
disable(ACTOR_PART_HOLD);
2020-11-02 11:31:29 +01:00
}
//should the output be active on the next turn ?
sendMailNonBlocking(&output_data_mbx, 1);
2020-11-02 11:31:29 +01:00
}
2020-11-13 19:48:42 +01:00
sendMail(&turntable_status_mbx, 1);
2020-10-21 16:15:24 +02:00
}
}
2020-11-30 10:06:11 +01:00
/**
* output task
*/
2020-11-02 11:31:29 +01:00
static void output(long data) {
2020-11-30 10:06:11 +01:00
rt_printk("started output task\n");
//reset everything first
2020-11-13 19:48:42 +01:00
disable(ACTOR_EXIT);
2020-11-16 11:25:22 +01:00
sendMail(&turntable_status_mbx, 1);
2020-11-30 10:06:11 +01:00
//start processing
2020-10-21 16:15:24 +02:00
while (1) {
2020-11-13 19:48:42 +01:00
receiveMail(&output_status_mbx);
//should the output be activated
if(receiveMailNonBlocking(&output_data_mbx)) {
2020-11-02 11:31:29 +01:00
enable(ACTOR_EXIT);
2020-11-30 10:06:11 +01:00
sleepMs(500);
2020-11-02 11:31:29 +01:00
disable(ACTOR_EXIT);
}
2020-11-13 19:48:42 +01:00
sendMail(&turntable_status_mbx, 1);
2020-10-21 16:15:24 +02:00
}
2020-10-19 18:46:24 +02:00
}
static int __init
2020-10-19 19:58:28 +02:00
example_init(void) {
rt_set_oneshot_mode();
start_rt_timer(0);
modbus_init();
2020-11-16 11:25:22 +01:00
rt_printk("init: started\n");
2020-10-26 10:38:48 +01:00
if ((connection = rt_modbus_connect(node)) == -1) {
rt_printk("init: could not connect to %s\n", node);
2020-11-16 11:25:22 +01:00
return -1;
2020-10-26 10:38:48 +01:00
}
2020-10-21 16:15:24 +02:00
rt_sem_init(&semaphore, 1);
2020-11-02 11:31:29 +01:00
if (rt_task_init(&turntable_task, turntable, 0, 1024, 0, 0, NULL)) {
2020-11-16 11:25:22 +01:00
rt_printk("turntable: cannot initialize task\n");
end(true);
2020-10-21 16:15:24 +02:00
}
if (rt_task_init(&drill_task, drill, 0, 1024, 0, 0, NULL)) {
2020-11-16 11:25:22 +01:00
rt_printk("drill: cannot initialize task\n");
end(true);
2020-10-21 16:15:24 +02:00
}
2020-11-02 11:31:29 +01:00
if (rt_task_init(&output_task, output, 0, 1024, 0, 0, NULL)) {
2020-11-16 11:25:22 +01:00
rt_printk("output: cannot initialize task\n");
end(true);
2020-11-02 11:31:29 +01:00
}
if (rt_task_init(&tester_task, tester, 0, 1024, 0, 0, NULL)) {
2020-11-16 11:25:22 +01:00
rt_printk("tester: cannot initialize task\n");
end(true);
2020-10-21 16:15:24 +02:00
}
2020-11-02 11:31:29 +01:00
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&turntable_status_mbx, sizeof(int))) {
2020-11-02 11:31:29 +01:00
end(true);
}
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&tester_status_mbx, sizeof(int))) {
2020-11-02 11:31:29 +01:00
end(true);
}
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&drill_status_mbx, sizeof(int))) {
2020-11-02 11:31:29 +01:00
end(true);
}
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&output_status_mbx, sizeof(int))) {
2020-11-02 11:31:29 +01:00
end(true);
}
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&output_data_mbx, sizeof(int))) {
2020-11-02 11:31:29 +01:00
end(true);
2020-10-19 19:58:28 +02:00
}
2020-11-16 11:25:22 +01:00
if (rt_mbx_init(&drill_data_mbx, sizeof(int))) {
end(true);
}
if (rt_mbx_init(&turntable_data_mbx, sizeof(int))) {
end(true);
}
2020-10-19 19:58:28 +02:00
2020-10-21 16:15:24 +02:00
rt_task_resume(&turntable_task);
2020-11-02 11:31:29 +01:00
rt_task_resume(&drill_task);
rt_task_resume(&output_task);
rt_task_resume(&tester_task);
2020-11-16 11:25:22 +01:00
rt_printk("loaded module Bearbeiten2\n");
2020-10-19 19:58:28 +02:00
return (0);
2020-10-19 18:46:24 +02:00
}
2020-10-26 10:38:48 +01:00
static void __exit
example_exit(void) {
2020-10-28 14:30:09 +01:00
end(false);
2020-11-16 11:25:22 +01:00
rt_printk("module Bearbeiten2 unloaded\n");
2020-10-26 10:38:48 +01:00
}
2020-10-19 18:46:24 +02:00
module_exit(example_exit)
module_init(example_init)