-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfestoFunctions.c
executable file
·181 lines (143 loc) · 5.11 KB
/
festoFunctions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* file: festoFunctions.c
* project: BScMech2-SoSe14-PRP2
* version: 1.4.0 (01.05.2014 10:00)
* - 0.9.0 first Version (not tested with real machine)
* - 1.0.0 stable and tested for PRP2-A1
* - 1.1.0 motor safety added
* - 1.2.0 new function setOutput added
* - 1.2.1 several bugfixes and isTriggered enhanced
* - 1.3.0 isTriggered rewritten
* - 1.3.1 bug in setBitInOutput motor safety fixed
* - 1.3.2 updateProcessImage logic improved
* - 1.4.0 new functions saveActors and restoreActors
*
*
* Created by Jannik Beyerstedt
* jannik.beyerstedt.de
* Copyright: all code under creative commons license: CC BY-NC-SA 3.0
*
*
* HAW Hamburg - Labor Programmieren 2
* festo conveyor belt system - system functions library
*/
#include <stdio.h>
#include "festoFunctions.h"
#include "mycbw.h" // ONLY FOR TESTING
//#include "cbw.h"
typedef unsigned short int Byte;
Image actorsImage = 0x0000;
Image sensorsImage = 0x0000;
Image changed0to1 = 0x0000;
Image changed1to0 = 0x0000;
Image lastReadingImage = 0x0000;
Image actorsImageStore = 0x0000;
void initializeSystem() {
cbDConfigPort(BNR, FIRSTPORTA, DIGITALOUT);
cbDConfigPort(BNR, FIRSTPORTB, DIGITALIN);
cbDConfigPort(BNR, FIRSTPORTCL, DIGITALOUT);
cbDConfigPort(BNR, FIRSTPORTCH, DIGITALIN);
}
void updateProcessImage() { // reads all sensor values AND sorts by 0->1 and 1->0 changes
Byte reading = 0x00;
Image changedYes = 0x0000;
lastReadingImage = sensorsImage;
cbDIn(BNR, FIRSTPORTB, &reading);
sensorsImage = reading;
cbDIn(BNR, FIRSTPORTCH, &reading);
sensorsImage += (reading << 8);
// 0 to 1
changedYes = lastReadingImage ^ sensorsImage;
changed0to1 = changedYes & sensorsImage;
// 1 to 0
changed1to0 = changedYes & lastReadingImage;
}
void applyProcessToOutput() { // writes local process image actor states to output ports
Byte actorOutput = 0x00;
// MOTOR SAFETY, last instance (should be made ok before this step)
Image bothMotorsSet = actorsImage & (MOTOR_R | MOTOR_L);
if (bothMotorsSet == (MOTOR_R | MOTOR_L)) {
clearBitInOutput(MOTOR_R | MOTOR_L);
printf("ERROR: applyProcessToOutput: Motor L and R are set");
}
actorOutput = actorsImage;
cbDOut(BNR, FIRSTPORTA, actorOutput);
actorOutput = (actorsImage >> 8);
cbDOut(BNR, FIRSTPORTCL, actorOutput);
}
int hasTriggered (Image mask) { // NEW: checks whether some sensor has changed it´s state (0->1, 1->0)
Image maskedBitIsOK = 0x0000;
if ((mask & HAS_TRIG_VALID) == mask) {
// to filter some sensors which are ONLY interstig for one change type (0->1 or 1->0)
maskedBitIsOK = ((changed0to1 & SENSORS_NO) | (changed1to0 & SENSORS_NC)) & mask;
if (maskedBitIsOK == mask) {
return 1;
}else {
return 0;
}
}
else {
printf("ERROR: hasTriggered: invalid mask");
return -1; // error code
}
}
int isTriggered (Image mask, int state) { // NEW: checks whether some sensor has active/ triggered state
Image maskedBitIsOK = 0x0000;
maskedBitIsOK = sensorsImage & mask;
if ((mask & IS_TRIG_VALID) == mask) { // mask is valid
switch (state) {
case 1: // check for 1 bits
if (maskedBitIsOK == mask ) {
return 1;
}else {
return 0;
}
break;
case 0: // check for 0 bits
if (maskedBitIsOK == 0x0000) {
return 1;
}else {
return 0;
}
break;
default:
printf("ERROR: isTriggered: other error");
return -1;
break;
}
}else {
printf("ERROR: isTriggered: invalid mask");
return -1;
}
}
void setBitInOutput (Image mask) { // gets bitmask and sets these bits in processimage (actors)
// --- MOTOR SAFETY ---
// checks if motor is switched to MOTOR_R at same time as MOTOR_L is enabeled
if ( ((mask & MOTOR_R) == MOTOR_R) & ((actorsImage & MOTOR_L) == MOTOR_L) ) {
clearBitInOutput(MOTOR_L);
applyProcessToOutput();
printf("ATTENTION: MOTOR SAFETY triggered");
}else if ( ((mask & MOTOR_L) == MOTOR_L) & ((actorsImage & MOTOR_R) == MOTOR_R) ) {
clearBitInOutput(MOTOR_R);
applyProcessToOutput();
printf("ATTENTION: MOTOR SAFETY triggered");
}else;
actorsImage = actorsImage | mask;
}
void clearBitInOutput (Image mask) { // gets bitmask and deletes these bits in processimage (actors)
actorsImage = actorsImage & (~mask);
}
void setOutput (Image mask) { // sets output to mask
actorsImage = mask;
}
void resetOutputs () { // sets all actors to a save value and writes to output ports (e.g. E-Stop)
actorsImage = E_SAVE;
}
void saveActors () {
actorsImageStore = actorsImage;
printf("INFO: actorsImage saved\n");
}
void restoreActors () {
actorsImage = actorsImageStore;
printf("INFO: actorsImage restored\n");
}