-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpick_and_place.py
More file actions
1224 lines (1023 loc) · 41.9 KB
/
Copy pathpick_and_place.py
File metadata and controls
1224 lines (1023 loc) · 41.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""pick_and_place.py - one-pump-thread CNC pick-and-place line (CONSOLE renderer).
Python port of cpp/examples/pick_and_place/ (console back-end only; the Win32
GDI window is dropped - Python has no Win32 here). A pick-and-place line runs on
ONE Runtime pump thread:
- Flow_LoadPicker moves a raw part zone A -> zone B (Pick then Place tasks)
- Flow_Stage machines it at B (Prepare -> Process -> Cleanup tasks)
- Flow_UnloadPicker moves the finished part zone B -> zone C (Pick then Place)
- Flow_Orchestrator schedules every module's next task by state; the pickers
and the stage never sequence themselves.
FEATURE FOCUS:
- orchestrator + state polling: one perpetual Schedule task launches each
module's next task when that module IsIdle, chosen from plain member reads.
- lock-free B-zone handoff on ONE pump: the two pickers must NEVER both sit in
zone B. Solved purely by reading peer members (InsideZoneB / Carrying); no
locks, because every module advances on the same pump thread round-robin.
- async-poll command acks: the Stage's start/cleanup commands are SubmitAsync
workers, polled by AsyncId with a StayTimeout timeout catch - the pump never
blocks on them.
- multi-task module: one Uniflow module (the Stage) holds three tasks and the
orchestrator runs them one at a time.
NOTE ON UNITS: the C++ port measures durations in milliseconds; the Python
VirtualClock / UFTimer report SECONDS, so the same waits are written in seconds
(process 5.0 s, ack timeout 2.0 s, hw settle 0.05 s, etc).
NOTE ON MOTION: the C++ port integrates its MotorAxis objects on a side thread
(MotorIOFactory). Here the axes integrate per pump tick (dt since last poll)
inside a tiny MotorAxis helper - the simplest faithful approach on one thread.
"""
import os
import sys
import random
import threading
import time
# --- import shim: make python/ importable, then the sibling console helper ---
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import uniflow # noqa: E402
import console # noqa: E402 (same dir as this file)
# ============================================================================
# globals - dimensional constants, timing, line-level state (mirrors globals.*)
# ============================================================================
class Geometry:
ZONE_A_MM = 200.0
ZONE_B_MM = 700.0
ZONE_C_MM = 1200.0
X_MAX_MM = 1400.0
B_SAFETY_GAP_MM = 250.0
STAGE_TRAVEL_MM = 80.0
Z_UP_MM = 0.0
Z_DOWN_MM = 120.0 # down is positive
X_SPEED_MM_PER_S = 300.0
Z_SPEED_MM_PER_S = 200.0
FINGER_OPEN_MM = 24.0
PART_WIDTH_MM = 20.0
FINGER_SPEED_MM_PER_S = 200.0
@staticmethod
def inside_zone_b(x_mm):
return abs(x_mm - Geometry.ZONE_B_MM) < Geometry.B_SAFETY_GAP_MM
# The machining phases, made explicit so the orchestrator can launch the
# Stage's tasks one at a time: RawPartLoaded -> (Prepare) -> Prepared ->
# (Process) -> Machined -> (Cleanup) -> ProcessedPartReady.
class StageState:
IDLE = "Idle"
RAW_PART_LOADED = "RawPartLoaded"
PREPARED = "Prepared"
MACHINED = "Machined"
PROCESSED_PART_READY = "ProcessedPartReady"
# Line-level environment. Plain pump-thread state (the pickers/stage/orchestrator
# all run on the one pump), except Stop which the main/stdin thread sets.
class Env:
_zone_a_part = False
_delivered = 0
_stop = False
@classmethod
def zone_a_has_part(cls):
return cls._zone_a_part
@classmethod
def create_fake_zone_a_part(cls):
cls._zone_a_part = True
@classmethod
def consume_zone_a_part(cls):
cls._zone_a_part = False
@classmethod
def delivered_count(cls):
return cls._delivered
@classmethod
def inc_delivered(cls):
cls._delivered += 1
@classmethod
def stop(cls):
return cls._stop
@classmethod
def request_stop(cls):
cls._stop = True
# ============================================================================
# motor_io - MotorAxis / DigitalLatch integrated per pump tick (mirrors
# motor_io_factory.*). The C++ port integrates these on a side thread; here a
# step polls Update(dt) once per round, so position advances on the one pump
# thread. No locks needed - everything touches them on the pump thread.
# ============================================================================
class MotorAxis:
"""1D linear axis that integrates its own position toward a commanded target
at a fixed speed. A step only commands (move/home) and polls (in_position).
update(dt_s) is driven once per pump round by the owning module."""
def __init__(self, name, initial_mm, speed_mm_per_s):
self.name = name
self._pos = initial_mm
self._target = initial_mm
self._speed = speed_mm_per_s
self._home = initial_mm
self._moving = False
def move(self, target_mm):
self._target = target_mm
self._moving = abs(self._pos - target_mm) > 1e-6
def home(self):
self.move(self._home)
def position(self):
return self._pos
def in_position(self):
return not self._moving
def update(self, dt_s):
if not self._moving:
return
remaining = self._target - self._pos
step = self._speed * dt_s
if abs(remaining) <= step:
self._pos = self._target
self._moving = False
else:
self._pos += step if remaining > 0.0 else -step
class DigitalLatch:
"""A digital input that latches true a random delay after Arm() - the demo's
model for a hardware handshake. The delay is counted down by update(dt)."""
def __init__(self, name, min_delay_s, max_delay_s):
self.name = name
self._min = min_delay_s
self._max = max_delay_s
self._remaining = 0.0
self._armed = False
self._ready = False
def arm(self):
self._remaining = random.uniform(self._min, self._max)
self._armed = True
self._ready = False
def reset(self):
self._armed = False
self._ready = False
self._remaining = 0.0
def is_ready(self):
return self._ready
def update(self, dt_s):
if not self._armed:
return
self._remaining -= dt_s
if self._remaining <= 0.0:
self._ready = True
self._armed = False
class _DeviceClock:
"""Per-module helper: tracks dt between pump rounds and ticks a device list.
Each module calls tick() once at the top of every step so its borrowed axes
advance with real elapsed time. Mirrors the C++ factory thread's 4ms loop,
but folded onto the pump thread."""
def __init__(self):
self._devices = []
self._last = None
def add(self, device):
self._devices.append(device)
return device
def tick(self):
now = time.monotonic()
if self._last is None:
self._last = now
return
dt = now - self._last
self._last = now
for d in self._devices:
d.update(dt)
# ============================================================================
# snapshot - pump -> render hand-off (mirrors snapshot.*). The render thread
# reads it under g_snap_mu; the only cross-thread lock in the demo.
# ============================================================================
class Snapshot:
def __init__(self):
self.load_x_mm = Geometry.ZONE_A_MM
self.load_z_mm = Geometry.Z_UP_MM
self.load_carry = False
self.load_phase = "-"
self.unload_x_mm = Geometry.ZONE_C_MM
self.unload_z_mm = Geometry.Z_UP_MM
self.unload_carry = False
self.unload_phase = "-"
self.stage_table_x_mm = Geometry.ZONE_B_MM
self.stage_table_y_mm = 0.0
self.stage_state = StageState.IDLE
self.stage_phase = "-"
self.zone_a_has_part = False
self.delivered = 0
g_snap = Snapshot()
g_snap_mu = threading.Lock()
def read_snapshot():
with g_snap_mu:
import copy
return copy.copy(g_snap)
# ============================================================================
# Flow_LoadPicker - carries a raw part A -> B. Two tasks: Pick (A) -> Place (B).
# ============================================================================
class Flow_LoadPicker(uniflow.Uniflow):
def __init__(self, rt):
super().__init__(rt, name="Flow_LoadPicker")
self._dev = _DeviceClock()
self.x = self._dev.add(MotorAxis("load_x", Geometry.ZONE_A_MM,
Geometry.X_SPEED_MM_PER_S))
self.z = self._dev.add(MotorAxis("load_z", Geometry.Z_UP_MM,
Geometry.Z_SPEED_MM_PER_S))
self.finger = self._dev.add(MotorAxis("load_finger", Geometry.FINGER_OPEN_MM,
Geometry.FINGER_SPEED_MM_PER_S))
self.carrying = False
self.ctx_pick = self.Task_Pick()
self.AddTask(self.ctx_pick)
self.ctx_place = self.Task_Place()
self.AddTask(self.ctx_place)
# -- Motion state read by the unload picker's B-zone gate and the snapshot --
def x_mm(self):
return self.x.position()
def z_mm(self):
return self.z.position()
def carrying_flag(self):
return self.carrying
def inside_zone_b(self):
return Geometry.inside_zone_b(self.x_mm())
def partner_in_zone_b(self):
return App.inst().unload.inside_zone_b()
# ----- Task: Pick (zone A) -----
class Task_Pick(uniflow.Task):
def Entry(self):
return self.Step1_CmdMoveToSource()
def Step1_CmdMoveToSource(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: move to zone A")
f.x.move(Geometry.ZONE_A_MM)
return self.Next(self.Step2_WaitAtSource)
def Step2_WaitAtSource(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("approaching A")
if f.x.in_position():
return self.Next(self.Step3_CmdLowerToPick)
return self.Stay()
def Step3_CmdLowerToPick(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lower to pick")
f.z.move(Geometry.Z_DOWN_MM)
return self.Next(self.Step4_WaitAtPickDown)
def Step4_WaitAtPickDown(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lowering to pick")
if f.z.in_position():
return self.Next(self.Step5_HandGrip)
return self.Stay()
def Step5_HandGrip(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("closing gripper")
f.finger.move(0.0)
if not f.finger.in_position():
return self.Stay()
Env.consume_zone_a_part()
f.carrying = True
return self.Next(self.Step6_CmdLiftWithPart)
def Step6_CmdLiftWithPart(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lift with part")
f.z.move(Geometry.Z_UP_MM)
return self.Next(self.Step7_WaitAtPickUp)
def Step7_WaitAtPickUp(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lifting with part")
# Pick done: part is up and carried. The flow goes idle; the
# orchestrator launches Place next (it sees carrying()).
if f.z.in_position():
return self.Done()
return self.Stay()
# ----- Task: Place (zone B) - gated by stage readiness + partner -----
class Task_Place(uniflow.Task):
def Entry(self):
return self.Step1_CmdMoveToDest()
def Step1_CmdMoveToDest(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
stage = App.inst().stage
# lock-free B-zone handoff: read the stage's readiness and the
# partner's position - plain member reads on the one pump thread.
may_enter_b = (stage.ready_to_receive_raw_part()
and not f.partner_in_zone_b())
if not f.inside_zone_b() and not may_enter_b:
f.x.move(Geometry.ZONE_B_MM - Geometry.B_SAFETY_GAP_MM)
if not f.x.in_position():
self.Describe("moving to A-gap")
return self.Stay()
self.Describe("parked at A-gap: stage=", stage.state(),
" partner_in_B=", f.partner_in_zone_b())
return self.Stay()
self.Describe("cmd: move to zone B")
f.x.move(Geometry.ZONE_B_MM)
return self.Next(self.Step2_WaitAtDest)
def Step2_WaitAtDest(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("approaching B")
if f.x.in_position():
return self.Next(self.Step3_CmdLowerToPlace)
return self.Stay()
def Step3_CmdLowerToPlace(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lower to place")
f.z.move(Geometry.Z_DOWN_MM)
return self.Next(self.Step4_WaitAtPlaceDown)
def Step4_WaitAtPlaceDown(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lowering to place")
if f.z.in_position():
return self.Next(self.Step5_HandRelease)
return self.Stay()
def Step5_HandRelease(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("opening gripper")
f.finger.move(Geometry.FINGER_OPEN_MM)
if not f.finger.in_position():
return self.Stay()
App.inst().stage.on_raw_part_received()
f.carrying = False
return self.Next(self.Step6_CmdLiftEmpty)
def Step6_CmdLiftEmpty(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lift empty")
f.z.move(Geometry.Z_UP_MM)
return self.Next(self.Step7_WaitAtPlaceUp)
def Step7_WaitAtPlaceUp(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lifting empty")
if f.z.in_position():
return self.Next(self.Step8_CmdRetreat)
return self.Stay()
def Step8_CmdRetreat(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: retreat to A")
f.x.move(Geometry.ZONE_A_MM)
return self.Next(self.Step9_WaitAtRetreat)
def Step9_WaitAtRetreat(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("retreating")
if f.x.in_position():
self.Describe("flow done")
return self.Done()
return self.Stay()
# ============================================================================
# Flow_UnloadPicker - carries the finished part B -> C. Same shape, but the
# SOURCE is the contested B zone (Pick at B -> Place at C).
# ============================================================================
class Flow_UnloadPicker(uniflow.Uniflow):
def __init__(self, rt):
super().__init__(rt, name="Flow_UnloadPicker")
self._dev = _DeviceClock()
self.x = self._dev.add(MotorAxis("unload_x", Geometry.ZONE_C_MM,
Geometry.X_SPEED_MM_PER_S))
self.z = self._dev.add(MotorAxis("unload_z", Geometry.Z_UP_MM,
Geometry.Z_SPEED_MM_PER_S))
self.finger = self._dev.add(MotorAxis("unload_finger", Geometry.FINGER_OPEN_MM,
Geometry.FINGER_SPEED_MM_PER_S))
self.carrying = False
self.ctx_pick = self.Task_Pick()
self.AddTask(self.ctx_pick)
self.ctx_place = self.Task_Place()
self.AddTask(self.ctx_place)
def x_mm(self):
return self.x.position()
def z_mm(self):
return self.z.position()
def carrying_flag(self):
return self.carrying
def inside_zone_b(self):
return Geometry.inside_zone_b(self.x_mm())
def partner_in_zone_b(self):
return App.inst().load.inside_zone_b()
# ----- Task: Pick (zone B, contested) -----
class Task_Pick(uniflow.Task):
def Entry(self):
return self.Step1_CmdMoveToSource()
def Step1_CmdMoveToSource(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
load = App.inst().load
st = App.inst().stage.state()
stage_past_loading = st in (StageState.PREPARED, StageState.MACHINED,
StageState.PROCESSED_PART_READY)
# load.carrying catches "approaching B with a part"; inside_zone_b
# catches "still lifting/retreating in B after the handoff".
load_threatens_b = load.carrying_flag() or load.inside_zone_b()
may_enter_b = stage_past_loading and not load_threatens_b
if not f.inside_zone_b() and not may_enter_b:
f.x.move(Geometry.ZONE_B_MM + Geometry.B_SAFETY_GAP_MM)
if not f.x.in_position():
self.Describe("moving to C-gap")
return self.Stay()
self.Describe("parked at C-gap: stage=", st,
" load_carry=", load.carrying_flag(),
" load_in_B=", load.inside_zone_b())
return self.Stay()
self.Describe("cmd: move to zone B")
f.x.move(Geometry.ZONE_B_MM)
return self.Next(self.Step2_WaitAtSource)
def Step2_WaitAtSource(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("approaching B")
if f.x.in_position():
return self.Next(self.Step3_CmdLowerToPick)
return self.Stay()
def Step3_CmdLowerToPick(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
if not App.inst().stage.ready_to_hand_off_processed_part():
self.Describe("hovering above B: stage=", App.inst().stage.state())
return self.Stay()
self.Describe("cmd: lower to pick")
f.z.move(Geometry.Z_DOWN_MM)
return self.Next(self.Step4_WaitAtPickDown)
def Step4_WaitAtPickDown(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lowering to pick")
if f.z.in_position():
return self.Next(self.Step5_HandGrip)
return self.Stay()
def Step5_HandGrip(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("closing gripper")
f.finger.move(0.0)
if not f.finger.in_position():
return self.Stay()
App.inst().stage.on_processed_part_taken()
f.carrying = True
return self.Next(self.Step6_CmdLiftWithPart)
def Step6_CmdLiftWithPart(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lift with part")
f.z.move(Geometry.Z_UP_MM)
return self.Next(self.Step7_WaitAtPickUp)
def Step7_WaitAtPickUp(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lifting with part")
if f.z.in_position():
return self.Done()
return self.Stay()
# ----- Task: Place (zone C) -----
class Task_Place(uniflow.Task):
def Entry(self):
return self.Step1_CmdMoveToUnload()
def Step1_CmdMoveToUnload(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: move to zone C")
f.x.move(Geometry.ZONE_C_MM)
return self.Next(self.Step2_WaitAtUnload)
def Step2_WaitAtUnload(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("approaching C")
if f.x.in_position():
return self.Next(self.Step3_CmdLowerToPlace)
return self.Stay()
def Step3_CmdLowerToPlace(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lower to place")
f.z.move(Geometry.Z_DOWN_MM)
return self.Next(self.Step4_WaitAtPlaceDown)
def Step4_WaitAtPlaceDown(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lowering to place")
if f.z.in_position():
return self.Next(self.Step5_HandRelease)
return self.Stay()
def Step5_HandRelease(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("opening gripper")
f.finger.move(Geometry.FINGER_OPEN_MM)
if not f.finger.in_position():
return self.Stay()
Env.inc_delivered()
f.carrying = False
return self.Next(self.Step6_CmdLiftEmpty)
def Step6_CmdLiftEmpty(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: lift empty")
f.z.move(Geometry.Z_UP_MM)
return self.Next(self.Step7_WaitAtPlaceUp)
def Step7_WaitAtPlaceUp(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("lifting empty")
if f.z.in_position():
return self.Next(self.Step8_CmdRetreat)
return self.Stay()
def Step8_CmdRetreat(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("cmd: retreat to C")
f.x.move(Geometry.ZONE_C_MM)
return self.Next(self.Step9_WaitAtRetreat)
def Step9_WaitAtRetreat(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("retreating")
if f.x.in_position():
self.Describe("flow done")
return self.Done()
return self.Stay()
# ============================================================================
# Flow_Stage - machining cell at zone B. A multi-task module: one flow per part,
# three unit operations - Prepare -> Process -> Cleanup - launched by state.
# ============================================================================
class Flow_Stage(uniflow.Uniflow):
PROCESS_DURATION_S = 5.0
TABLE_SPEED_MM_PER_S = 5000.0 # near direct position control for the figure-8
def __init__(self, rt):
super().__init__(rt, name="Flow_Stage")
self._dev = _DeviceClock()
self.table_x = self._dev.add(MotorAxis("stage_table_x", Geometry.ZONE_B_MM,
self.TABLE_SPEED_MM_PER_S))
self.hw_ready = self._dev.add(DigitalLatch("stage_hw_ready", 0.2, 0.7))
self.table_y_offset_mm = 0.0
self.state_ = StageState.IDLE
self.ctx_prepare = self.Task_Prepare()
self.AddTask(self.ctx_prepare)
self.ctx_process = self.Task_Process()
self.AddTask(self.ctx_process)
self.ctx_cleanup = self.Task_Cleanup()
self.AddTask(self.ctx_cleanup)
def state(self):
return self.state_
def table_x_mm(self):
return self.table_x.position()
def table_y_mm(self):
return self.table_y_offset_mm
def ready_to_receive_raw_part(self):
return self.state_ == StageState.IDLE
def ready_to_hand_off_processed_part(self):
return self.state_ == StageState.PROCESSED_PART_READY
def on_raw_part_received(self):
self.state_ = StageState.RAW_PART_LOADED
self.Describe("raw part loaded")
def on_processed_part_taken(self):
self.state_ = StageState.IDLE
self.Describe("empty")
# ----- Task: Prepare - start cmd (async), ack, then wait HW ready -----
class Task_Prepare(uniflow.Task):
def __init__(self):
super().__init__()
self.settle = None
def OnEnter(self):
# re-arm the hw-ready settle timer on the runtime clock.
self.settle = uniflow.UFTimer(self.flow()._rt.clock)
def Entry(self):
return self.Step1_SendStart()
@staticmethod
def _simulate_start_cmd():
time.sleep(0.3)
return True
def Step1_SendStart(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
# Guarded bootstrap: only a freshly loaded raw part may begin.
if f.state_ != StageState.RAW_PART_LOADED:
return self.Fail()
self.Describe("send start cmd")
# async-poll command ack: submit the worker, carry its id to the
# poller; id 0 means rejected (in-flight cap).
cmd = self.SubmitAsync(self._simulate_start_cmd, "start_cmd", None)
if cmd == 0:
self.Describe("start cmd rejected")
return self.Fail()
return self.Next(self.Step2_WaitStartAck, cmd)
def Step2_WaitStartAck(self, cmd):
f = self.flow()
f._dev.tick()
r = self.AsyncResult(cmd)
if r.pending():
self.Describe("wait start ack")
return self.StayTimeout(2.0, self.Step_StartAckTimeout)
if not r.ok() or not r.return_value:
self.Describe("start cmd failed")
return self.Fail()
self.Describe("wait hw ready")
f.hw_ready.arm()
return self.Next(self.Step3_WaitHwReady)
def Step_StartAckTimeout(self):
self.Describe("start ack timeout")
self.ClearAsync()
return self.Fail()
def Step3_WaitHwReady(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
# HeldFor proceeds once ready has held STABLE for 50ms (settling),
# not on the first transient high. settle was re-armed in OnEnter.
if self.settle.HeldFor(f.hw_ready.is_ready(), 0.05):
f.state_ = StageState.PREPARED
self.Describe("prepared")
return self.Done()
self.Describe("wait hw ready")
return self.StayTimeout(3.0, self.Step4_HwTimeout)
def Step4_HwTimeout(self):
self.Describe("hw ready timeout")
return self.Fail()
# ----- Task: Process - figure-8 (Gerono lemniscate) machining run -----
class Task_Process(uniflow.Task):
def __init__(self):
super().__init__()
self.run = None
def OnEnter(self):
self.run = uniflow.UFTimer(self.flow()._rt.clock)
def Entry(self):
return self.Step1_Process()
def Step1_Process(self):
import math
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
elapsed = self.run.Elapsed()
frac = min(1.0, elapsed / Flow_Stage.PROCESS_DURATION_S)
# Figure-8: x = sin(t), y = sin(2t). The 2:1 ratio makes the '8'
# cross at zone centre; four loops over the process duration.
tau = 6.2831853071795864
loops = 4
amp_y = 30.0
phase = frac * tau * loops
sweep_mm = Geometry.STAGE_TRAVEL_MM * math.sin(phase)
f.table_x.move(Geometry.ZONE_B_MM + sweep_mm)
f.table_y_offset_mm = amp_y * math.sin(phase * 2.0)
if elapsed >= Flow_Stage.PROCESS_DURATION_S:
f.state_ = StageState.MACHINED
self.Describe("machined")
return self.Done()
self.Describe("processing")
return self.Stay()
# ----- Task: Cleanup - cleanup cmd (async), ack, return to pick pos -----
class Task_Cleanup(uniflow.Task):
def Entry(self):
return self.Step1_SendCleanup()
@staticmethod
def _simulate_cleanup_cmd():
time.sleep(0.2)
return True
def Step1_SendCleanup(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
self.Describe("send cleanup cmd")
cmd = self.SubmitAsync(self._simulate_cleanup_cmd, "cleanup_cmd", None)
if cmd == 0:
self.Describe("cleanup cmd rejected")
return self.Fail()
return self.Next(self.Step2_WaitCleanupAck, cmd)
def Step2_WaitCleanupAck(self, cmd):
f = self.flow()
f._dev.tick()
r = self.AsyncResult(cmd)
if r.pending():
self.Describe("wait cleanup ack")
return self.StayTimeout(2.0, self.Step_CleanupAckTimeout)
if not r.ok() or not r.return_value:
self.Describe("cleanup failed")
return self.Fail()
self.Describe("return to pick pos")
f.table_x.move(Geometry.ZONE_B_MM)
return self.Next(self.Step3_ReturnToPickPos)
def Step_CleanupAckTimeout(self):
self.Describe("cleanup ack timeout")
self.ClearAsync()
return self.Fail()
def Step3_ReturnToPickPos(self):
f = self.flow()
f._dev.tick()
if Env.stop():
return self.Done()
f.table_y_offset_mm = 0.0
if not f.table_x.in_position():
return self.Stay()
f.state_ = StageState.PROCESSED_PART_READY
self.Describe("ready to hand off")
return self.Done()
# ============================================================================
# Flow_Orchestrator - line-level scheduler. One perpetual Schedule task whose
# single step polls the line every pump round and launches each module's next
# task when it IsIdle. The pickers / stage never sequence themselves.
# ============================================================================
class Flow_Orchestrator(uniflow.Uniflow):
def __init__(self, rt):
super().__init__(rt, name="Flow_Orchestrator")
self.ctx_schedule = self.Task_Schedule()
self.AddTask(self.ctx_schedule)
class Task_Schedule(uniflow.Task):
def Entry(self):
return self.Step1_Tick()
def Step1_Tick(self):
if Env.stop():
return self.Done()
self._try_create_raw_part()
self._try_drive_load_picker()
self._try_drive_stage()
self._try_drive_unload_picker()
return self.Stay()
def _try_create_raw_part(self):
# A fresh raw part is staged the instant zone A is empty.
if Env.zone_a_has_part():
return
Env.create_fake_zone_a_part()
def _try_drive_load_picker(self):
picker = App.inst().load
if not picker.IsIdle():
return
# Carrying -> deliver (Place); else grab the next one (Pick).
if picker.carrying_flag():
picker.ctx_place.StartFlow()
elif Env.zone_a_has_part():
picker.ctx_pick.StartFlow()
def _try_drive_stage(self):
stage = App.inst().stage
if not stage.IsIdle():
return
# One task per machining phase, launched as the previous completes.
st = stage.state()
if st == StageState.RAW_PART_LOADED:
stage.ctx_prepare.StartFlow()
elif st == StageState.PREPARED:
stage.ctx_process.StartFlow()
elif st == StageState.MACHINED:
stage.ctx_cleanup.StartFlow()
def _try_drive_unload_picker(self):
picker = App.inst().unload
if not picker.IsIdle():
return
if picker.carrying_flag():
picker.ctx_place.StartFlow()
return
# Prefetch Pick as soon as a part is incoming, so the picker is
# already hovering above B when the stage finishes.
st = App.inst().stage.state()
stage_has_part_incoming = st in (StageState.PREPARED, StageState.MACHINED,
StageState.PROCESSED_PART_READY)
if stage_has_part_incoming:
picker.ctx_pick.StartFlow()
# ============================================================================
# Flow_Visualization - pump-side snapshot writer. One perpetual Snapshot task
# copies live line state into g_snap every round (mirrors uf_visualization.cpp).
# ============================================================================
class Flow_Visualization(uniflow.Uniflow):
def __init__(self, rt):
super().__init__(rt, name="Flow_Visualization")
self.ctx_snapshot = self.Task_Snapshot()
self.AddTask(self.ctx_snapshot)