python_code
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python_code [2025/03/26 19:48] – better sections brandon | python_code [2025/05/15 03:28] (current) – add notes about how to use this page brandon | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Python Code ====== | ====== Python Code ====== | ||
| - | ===== Python | + | This a hub page for Python code linked to other pages using the '' |
| - | ===== Python User String ===== | + | |
| + | Example of how to embed a code section in another page in DokuWiki: \\ | ||
| + | '' | ||
| + | <wrap lo> | ||
| + | |||
| + | Any script with dependencies should use [[https:// | ||
| + | |||
| + | ===== Create Button Flag ===== | ||
| + | |||
| + | --> | ||
| + | <code python asi_create_button_flag.py> | ||
| + | def create_button_code(at: | ||
| + | assert at in range(4), "Must be in the range 0-3." | ||
| + | assert home in range(4), "Must be in the range 0-3." | ||
| + | assert joystick in range(4), "Must be in the range 0-3." | ||
| + | assert zero_halt == 0 or zero_halt | ||
| + | |||
| + | button_code | ||
| + | |||
| + | # bit masks | ||
| + | BITMASK_AT | ||
| + | BITMASK_HOME | ||
| + | BITMASK_JS | ||
| + | BITMASK_ZERO | ||
| + | |||
| + | # set bits | ||
| + | button_code &= ~BITMASK_AT | ||
| + | button_code |= at & BITMASK_AT | ||
| + | |||
| + | button_code &= ~BITMASK_HOME | ||
| + | button_code |= (home << 2) & BITMASK_HOME | ||
| + | |||
| + | button_code &= ~BITMASK_JS | ||
| + | button_code |= (joystick << 4) & BITMASK_JS | ||
| + | |||
| + | button_code &= ~BITMASK_ZERO | ||
| + | button_code |= (zero_halt << 6) & BITMASK_ZERO | ||
| + | |||
| + | return button_code | ||
| + | |||
| + | def main(): | ||
| + | button_code = create_button_code(at=1, | ||
| + | print(button_code) | ||
| + | # prints 5 | ||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | |||
| + | </ | ||
| + | <-- | ||
| + | |||
| + | ===== Parse Button Flag ===== | ||
| + | |||
| + | --> | ||
| + | <code python asi_parse_button_flag.py> | ||
| + | # the value returned from EXTRA M? | ||
| + | button_flag_byte = 127 | ||
| + | |||
| + | # bit masks | ||
| + | mask_at | ||
| + | mask_home = 0x0C # 00001100 | ||
| + | mask_js | ||
| + | mask_zero = 0xC0 # 11000000 | ||
| + | |||
| + | # get the button states from button_flag_byte | ||
| + | btn_at | ||
| + | btn_home = (button_flag_byte & mask_home) >> 2 | ||
| + | btn_js | ||
| + | btn_zero = (button_flag_byte & mask_zero) >> 6 | ||
| + | |||
| + | # show the results in decimal and binary | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | |||
| + | # console output: | ||
| + | # button_flag_byte = 127 (binary 01111111) | ||
| + | # btn_at = 3 (binary 11) | ||
| + | # btn_home = 3 (binary 11) | ||
| + | # btn_js = 3 (binary 11) | ||
| + | # btn_zero = 1 (binary 01) | ||
| + | </ | ||
| + | <-- | ||
| + | |||
| + | ===== Status Byte ===== | ||
| + | |||
| + | --> Python Status Byte | ||
| + | |||
| + | <code python asi_status_byte.py> | ||
| + | # /// script | ||
| + | # dependencies = [" | ||
| + | # /// | ||
| + | import serial | ||
| + | from enum import Flag, auto | ||
| + | |||
| + | class StatusByte(Flag): | ||
| + | """ | ||
| + | The Status Byte returned by the RDSBYTE (RB) command. | ||
| + | The value parameter should be an 8-bit int, 0-255. | ||
| + | 00000001 <- Bit 0 is 1, commanded move in progress. | ||
| + | """ | ||
| + | COMMANDED_MOVE = auto() | ||
| + | AXIS_ENABLED = auto() | ||
| + | MOTOR_ACTIVE = auto() | ||
| + | JS_KNOB_ENABLED = auto() | ||
| + | MOTOR_RAMPING = auto() | ||
| + | MOTOR_RAMPING_UP = auto() | ||
| + | AT_UPPER_LIMIT = auto() | ||
| + | AT_LOWER_LIMIT = auto() | ||
| + | |||
| + | def main() -> None: | ||
| + | # use an empty string for MS2000 (card_address = "" | ||
| + | card_address = " | ||
| + | |||
| + | # which axes to query (for a single axis use: axes = [" | ||
| + | # axes_byte_len is the number of bytes that need to be read for RDSBYTE | ||
| + | axes = [" | ||
| + | axes_str = " " | ||
| + | axes_byte_len = len(axes) + 3 # 3 bytes for ':', | ||
| + | |||
| + | with serial.Serial(" | ||
| + | # query the controller for the status byte of each axis | ||
| + | command = f" | ||
| + | serial_port.write(bytes(command, | ||
| + | response = serial_port.read(axes_byte_len) | ||
| + | |||
| + | # report and check for errors | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | if b" | ||
| + | print(" | ||
| + | return | ||
| + | |||
| + | # view as bytes | ||
| + | response_bytes = [byte for byte in response] | ||
| + | print(f" | ||
| + | |||
| + | # get the status byte for each axis and skip the first byte (':' | ||
| + | status_bytes = [response[i] for i in range(1, len(axes) + 1)] | ||
| + | print(f" | ||
| + | |||
| + | # create a dictionary that maps uppercase axis names to status bytes | ||
| + | status_bytes_dict = {axis.upper(): | ||
| + | print(f" | ||
| + | |||
| + | # check the status of each axis | ||
| + | for axis in axes: | ||
| + | status_byte = status_bytes_dict.get(axis.upper()) | ||
| + | # check a single flag | ||
| + | is_at_upper_limit = StatusByte.AT_UPPER_LIMIT in status_byte | ||
| + | # check multiple flags | ||
| + | is_js_and_axis_enabled = StatusByte.JS_KNOB_ENABLED | StatusByte.AXIS_ENABLED in status_byte | ||
| + | # check if one flag is True and the other is False | ||
| + | is_motor_on_and_not_cmd_move = StatusByte.MOTOR_ACTIVE in status_byte and StatusByte.COMMANDED_MOVE not in status_byte | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== User String ===== | ||
| --> Python User String | --> Python User String | ||
| - | <code python | + | <code python |
| - | import serial | + | # /// script |
| + | # dependencies = ["pyserial>=3.5"] | ||
| + | # /// | ||
| + | import serial | ||
| def main() -> None: | def main() -> None: | ||
Address: 29391 W Enid Rd. Eugene, OR 97402, USA | Phone: +1 (541) 461-8181
python_code.1743032935.txt.gz · Last modified: by brandon
