@@ -4,18 +4,72 @@ import Configuration
4
4
import PlcMemory
5
5
import Linear
6
6
import java.util.concurrent.CancellationException
7
- import toBooleanFromBinary
7
+
8
8
class LinearOperation {
9
9
private var linearVariables: MutableMap <String , Double > = mutableMapOf<String , Double >()
10
- fun getNextValue (linear : Linear ): String {
11
- val x = linearVariables.getOrDefault(linear.symbol, linear.minX)
12
- if (x + linear.step < linear.maxX) {
13
- linearVariables[linear.symbol] = x + linear.step
10
+
11
+ private fun getNextValue (linear : Linear ): String {
12
+ var x = 0.0
13
+ if (linear.endX > linear.startX) {
14
+ // crescente
15
+ x = linearVariables.getOrDefault(linear.symbol, linear.startX)
16
+ if (x + linear.step <= linear.endX) {
17
+ linearVariables[linear.symbol] = x + linear.step
18
+ } else {
19
+ if (linear.replay) {
20
+ linearVariables[linear.symbol] = linear.startX
21
+ }
22
+ }
14
23
}else {
15
- if (linear.replay){
16
- linearVariables[linear.symbol] = linear.minX
24
+ // decrescente
25
+ x = linearVariables.getOrDefault(linear.symbol, linear.startX)
26
+ if (x - linear.step >= linear.endX) {
27
+ linearVariables[linear.symbol] = x - linear.step
28
+ } else {
29
+ if (linear.replay) {
30
+ linearVariables[linear.symbol] = linear.startX
31
+ }
17
32
}
18
33
}
19
34
return (linear.a * x + linear.b).toString()
20
35
}
36
+
37
+ fun process (element : Linear , configuration : Configuration , memory : PlcMemory ) {
38
+ var nextValue = getNextValue(element)
39
+ var variable = configuration.registers.getVarConfiguration(element.symbol)
40
+ if (variable == null ) {
41
+ println (" ERROR: Symbol ${element.symbol} not found during Linear execution" )
42
+ throw CancellationException (" Error - Linear" )
43
+ } else {
44
+ if (variable.addressType == AddressType .COIL || variable.addressType == AddressType .DISCRETE_INPUT ) {
45
+ println (" ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Linear operation" )
46
+ throw CancellationException (" Error - Linear" )
47
+ }
48
+
49
+ when (variable.addressType) {
50
+
51
+ AddressType .HOLDING_REGISTER -> {
52
+ // get the current value
53
+ // add
54
+ // set back the new value
55
+
56
+ if (variable.datatype == " FLOAT32" ) {
57
+
58
+ setHoldingRegisterFloat32(nextValue.toFloat(), memory, variable)
59
+ } else {
60
+
61
+ setHoldingRegisterInt16(memory, variable, nextValue.toFloat().toInt().toShort())
62
+ }
63
+ }
64
+
65
+ AddressType .INPUT_REGISTER -> {
66
+ memory.setInputRegister(variable.address.toInt(), nextValue.toFloat().toInt().toShort())
67
+ }
68
+
69
+ else -> {
70
+ throw CancellationException (" Error - Linear" )
71
+ }
72
+ }
73
+ }
74
+ }
21
75
}
0 commit comments