Skip to content

Commit 4733fe2

Browse files
committed
rclpy.spin() implementation
1 parent 39e15a7 commit 4733fe2

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

manager/manager/lint/linter.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,31 @@ def evaluate_code(self, code, ros_version, warnings=False, py_lint_source="pylin
5858
code = re.sub(r'\nimport cv2\n', '\nfrom cv2 import cv2\n', code)
5959

6060
# Avoids EOF error when iterative code is empty (which prevents other errors from showing)
61-
while_position = re.search(
62-
r'[^ ]while\s*\(\s*True\s*\)\s*:|[^ ]while\s*True\s*:|[^ ]while\s*1\s*:|[^ ]while\s*\(\s*1\s*\)\s*:', code)
63-
if while_position is None:
64-
while_error = "ERROR: While loop is required and was not found.\n"
65-
return while_error.strip()
66-
sequential_code = code[:while_position.start()]
67-
iterative_code = code[while_position.start():]
68-
iterative_code = re.sub(
69-
r'[^ ]while\s*\(\s*True\s*\)\s*:|[^ ]while\s*True\s*:|[^ ]while\s*1\s*:|[^ ]while\s*\(\s*1\s*\)\s*:', '\n', iterative_code, 1)
70-
iterative_code = re.sub(r'^[ ]{4}', '', iterative_code, flags=re.M)
71-
code = sequential_code + iterative_code
72-
73-
f = open("user_code.py", "w")
74-
f.write(code)
75-
f.close()
61+
loop_regex = (
62+
r'[^ ]while\s*\(\s*True\s*\)\s*:|'
63+
r'[^ ]while\s*True\s*:|'
64+
r'[^ ]while\s*1\s*:|'
65+
r'[^ ]while\s*\(\s*1\s*\)\s*:|'
66+
r'rclpy\.spin\(\s*\w+\s*\)'
67+
)
68+
loop_match = re.search(loop_regex, code)
69+
70+
if loop_match is None:
71+
return "ERROR: A loop is required — please use either 'while True:' or 'rclpy.spin(node)'."
72+
73+
if "rclpy.spin" in loop_match.group():
74+
# Keep the code as-is; don't modify it
75+
pass
76+
else:
77+
# Modify code for while True (add frequency control)
78+
sequential_code = code[:loop_match.start()]
79+
iterative_code = code[loop_match.start():]
80+
iterative_code = re.sub(loop_regex, '\n', iterative_code, 1)
81+
iterative_code = re.sub(r'^[ ]{4}', '', iterative_code, flags=re.M)
82+
code = sequential_code + iterative_code
83+
84+
with open("user_code.py", "w") as f:
85+
f.write(code)
7686

7787
command = ""
7888
if "humble" in str(ros_version):

manager/manager/manager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import black
77

8-
9-
108
sys.path.insert(0, '/RoboticsApplicationManager')
119

1210
import os
@@ -608,7 +606,7 @@ def prepare_RA_code(code_path):
608606
errors = self.linter.evaluate_code(code, self.ros_version)
609607
if errors == "":
610608

611-
code = self.add_frequency_control(code)
609+
# code = self.add_frequency_control(code)
612610
f = open(code_path, "w")
613611
f.write(code)
614612
f.close()

0 commit comments

Comments
 (0)