|
| 1 | +import |
| 2 | + "https://raw.githubusercontent.com/CollaborativeStateMachines/Cirrina/refs/heads/develop/src/main/resources/pkl/csm/csml.pkl" |
| 3 | + |
| 4 | +import "events.pkl" as Events |
| 5 | +import "serviceTypes.pkl" as ServiceTypes |
| 6 | +import "variables.pkl" as Vars |
| 7 | + |
| 8 | +armFSM = new csml.StateMachine { |
| 9 | + transient { |
| 10 | + ["pickupSuccess"] = "true" |
| 11 | + ["errorMsg"] = "''" |
| 12 | + ["partsAssembled"] = "0" |
| 13 | + ["partsPerProduct"] = "3" |
| 14 | + } |
| 15 | + |
| 16 | + states { |
| 17 | + ["idleState"] = new csml.Initial { |
| 18 | + entry { |
| 19 | + new csml.Match { |
| 20 | + cases { |
| 21 | + new csml.Case { |
| 22 | + of = "!pickupSuccess" |
| 23 | + yields { |
| 24 | + new csml.Emit { event = new csml.Internal { topic = Events.ePickupNotSuccess } } |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + on { |
| 32 | + [Events.eJobDone] { |
| 33 | + to = "jobDoneState" |
| 34 | + } |
| 35 | + [Events.eArmPickup] { |
| 36 | + to = "pickupState" |
| 37 | + provided = "!\(Vars.vIsJobDone)" |
| 38 | + } |
| 39 | + [Events.ePickupNotSuccess] { |
| 40 | + to = "pickupState" |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + ["pickupState"] { |
| 46 | + entry { |
| 47 | + new csml.Invoke { |
| 48 | + type = ServiceTypes.stPickup |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + after { |
| 53 | + ["pickupTimeout"] { |
| 54 | + delay = "10000" |
| 55 | + triggers = new csml.Emit { |
| 56 | + event = new csml.Internal { topic = Events.eArmPickupUpdateStatusTimedout } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + on { |
| 62 | + [Events.eUpdatePickupStatus] { |
| 63 | + yields { |
| 64 | + new csml.Eval { expression = "pickupSuccess = $success" } |
| 65 | + new csml.Match { |
| 66 | + cases { |
| 67 | + new csml.Case { |
| 68 | + of = "$success" |
| 69 | + yields { |
| 70 | + new csml.Emit { |
| 71 | + event { topic = Events.ePickedUp } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + default = new csml.Eval { expression = "errorMsg = 'Pickup failed...'" } |
| 77 | + } |
| 78 | + new csml.Emit { event = new csml.Internal { topic = Events.eCheckPickUpSuccess } } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [Events.eCheckPickUpSuccess] { |
| 83 | + to = "assembleState" |
| 84 | + provided = "pickupSuccess" |
| 85 | + or = "errorState" |
| 86 | + } |
| 87 | + |
| 88 | + [Events.eArmPickupUpdateStatusTimedout] { |
| 89 | + to = "errorState" |
| 90 | + yields { |
| 91 | + new csml.Eval { expression = "pickupSuccess = false" } |
| 92 | + new csml.Eval { expression = "errorMsg = 'Pickup timeout...'" } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + ["errorState"] { |
| 99 | + entry { |
| 100 | + new csml.Emit { |
| 101 | + event { |
| 102 | + topic = Events.eProcessMessage |
| 103 | + data { ["msg"] = "'Fatal robotic arm failure: ' + errorMsg" } |
| 104 | + } |
| 105 | + target = "'messageProcessor'" |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + after { |
| 110 | + ["pickupRetryTimeout"] { |
| 111 | + delay = "2000" |
| 112 | + triggers = new csml.Emit { event = new csml.Internal { topic = Events.eRetry } } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + on { |
| 117 | + [Events.eRetry] { |
| 118 | + to = "assembleState" |
| 119 | + provided = "pickupSuccess" |
| 120 | + or = "returnState" |
| 121 | + } |
| 122 | + |
| 123 | + [Events.eUpdatePickupStatus] { |
| 124 | + yields { |
| 125 | + new csml.Eval { expression = "pickupSuccess = $success" } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + [Events.eJobDone] { |
| 130 | + to = "jobDoneState" |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + ["assembleState"] { |
| 136 | + entry { |
| 137 | + new csml.Invoke { |
| 138 | + type = ServiceTypes.stAssemble |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + after { |
| 143 | + ["assemblyTimeout"] { |
| 144 | + delay = "10000" |
| 145 | + triggers = new csml.Emit { |
| 146 | + event = new csml.Internal { topic = Events.eArmAssemblyUpdateStatusTimedout } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + on { |
| 152 | + [Events.eCheckAssembleSuccess] { |
| 153 | + yields { |
| 154 | + new csml.Match { |
| 155 | + cases { |
| 156 | + new csml.Case { |
| 157 | + of = "$success" |
| 158 | + yields { |
| 159 | + new csml.Emit { |
| 160 | + event = new csml.Internal { topic = Events.eAssemblySuccessful } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + new csml.Case { |
| 165 | + of = "!($success)" |
| 166 | + yields { |
| 167 | + new csml.Emit { |
| 168 | + event = new csml.Internal { topic = Events.eAssemblyUnsuccessful } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + [Events.eAssemblySuccessful] { |
| 178 | + to = "returnState" |
| 179 | + yields { |
| 180 | + new csml.Eval { expression = "partsAssembled = partsAssembled + 1" } |
| 181 | + new csml.Emit { |
| 182 | + event { topic = Events.eAssemblyComplete } |
| 183 | + target = "'monitor'" |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + [Events.eAssemblyUnsuccessful] { |
| 189 | + to = "errorState" |
| 190 | + yields { |
| 191 | + new csml.Eval { expression = "errorMsg = 'Assemble failed...'" } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + [Events.eArmAssemblyUpdateStatusTimedout] { |
| 196 | + to = "errorState" |
| 197 | + yields { |
| 198 | + new csml.Eval { expression = "errorMsg = 'Assembly timeout...'" } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + ["returnState"] { |
| 205 | + entry { |
| 206 | + new csml.Invoke { |
| 207 | + type = ServiceTypes.stReturnToStart |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + on { |
| 212 | + [Events.eResetArm] { |
| 213 | + to = "idleState" |
| 214 | + yields { |
| 215 | + new csml.Match { |
| 216 | + cases { |
| 217 | + new csml.Case { |
| 218 | + of = "partsAssembled >= partsPerProduct" |
| 219 | + yields { |
| 220 | + new csml.Eval { expression = "partsAssembled = 0" } |
| 221 | + new csml.Emit { |
| 222 | + event { topic = Events.eProductComplete } |
| 223 | + target = "'jobController'" |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + ["jobDoneState"] = new csml.Terminal {} |
| 235 | + } |
| 236 | +} |
0 commit comments