Skip to content

Commit

Permalink
Upddate g_code_logger
Browse files Browse the repository at this point in the history
- Adding prefix and postfix
- Adding commands that are not part of the interpolation at the end
- Removing scientific notation at the end of number prompts
  • Loading branch information
philipp1604 committed May 27, 2024
1 parent c4bcd87 commit c045d13
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/pybullet_industrial/g_code_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,51 @@ def __init__(self, robot: RobotBase):
self.g_code_joint_position = []

Check warning on line 21 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L20-L21

Added lines #L20 - L21 were not covered by tests

@staticmethod
def write_g_code(g_code: list, textfile: str):
def write_g_code(g_code: list, textfile: str, prefix: str = None,
postfix: str = None):
"""
Write the given G-code commands to a text file.
Write the given G-code commands to a text file, with optional prefix and postfix.
Args:
g_code (list): List of dictionaries representing G-code commands.
textfile (str): Path to the text file where G-code will be written.
prefix (str, optional): String to be written at the beginning of the file. Defaults to None.
postfix (str, optional): String to be written at the end of the file. Defaults to None.
"""
def format_value(value):

Check warning on line 35 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L35

Added line #L35 was not covered by tests
# Ensure the value is not in scientific notation and remove trailing zeros
formatted_value = f'{value:.15f}' if isinstance(

Check warning on line 37 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L37

Added line #L37 was not covered by tests
value, float) else str(value)
return formatted_value.rstrip('0').rstrip('.') if '.' in formatted_value else formatted_value

Check warning on line 39 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L39

Added line #L39 was not covered by tests

with open(textfile, 'w') as file:

Check warning on line 41 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L41

Added line #L41 was not covered by tests
# Write the prefix if it exists
if prefix is not None:
file.write(prefix + '\n')

Check warning on line 44 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L43-L44

Added lines #L43 - L44 were not covered by tests

for command in g_code:
# Define the order in which keys should be written
order = ['G', 'X', 'Y', 'Z', 'A', 'B', 'C',

Check warning on line 47 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L46-L47

Added lines #L46 - L47 were not covered by tests
'RA1', 'RA2', 'RA3', 'RA4', 'RA5', 'RA6']
# Construct the line by joining key-value pairs
line_items = []

Check warning on line 49 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L49

Added line #L49 was not covered by tests

# Write keys in the specified order
for key in order:
if key in command:
if key.startswith('RA'):
line_items.append(f'{key}={command[key]}')
else:
line_items.append(f'{key}{command[key]}')
line = ' '.join(line_items)
file.write(line + '\n')
formatted_value = format_value(command[key])
line_items.append(f'{key}={formatted_value}' if key.startswith(

Check warning on line 55 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L52-L55

Added lines #L52 - L55 were not covered by tests
'RA') else f'{key}{formatted_value}')

# Write keys that are not in the specified order
for key in command:
if key not in order:
formatted_value = format_value(command[key])
line_items.append(f'{key}{formatted_value}')

Check warning on line 62 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L59-L62

Added lines #L59 - L62 were not covered by tests

file.write(' '.join(line_items) + '\n')

Check warning on line 64 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L64

Added line #L64 was not covered by tests

# Write the postfix if it exists
if postfix is not None:
file.write(postfix + '\n')

Check warning on line 68 in src/pybullet_industrial/g_code_logger.py

View check run for this annotation

Codecov / codecov/patch

src/pybullet_industrial/g_code_logger.py#L67-L68

Added lines #L67 - L68 were not covered by tests

def update_g_code(self):
"""
Expand Down

0 comments on commit c045d13

Please sign in to comment.