Button Function Touch_Z

More
03 May 2023 14:48 - 03 May 2023 22:31 #270571 by Vmax549
I have a question about the coding for this. I have it working BUT it is ugly.
ui_misc.set_current_notebook_page_by_id(self.notebook, 'notebook_offsets_fixed')
ui_misc.set_current_notebook_page_by_id(self.offsets_notebook, 'tool_offsets_fixed')
self.enqueue_button_press_release(self.button_list['touch_z'])                                  
ui_misc.set_current_notebook_page_by_id(self.notebook, 'notebook_main_fixed')

Is there a way to just  link to the button instead of actually changing page to make it work. If you run just the Touch_z button code from the main page it errors saying it needs the Widget. Which mean it need to be on the same page as the button.. So I then pushed it to the page ran the button then switched back to the main page. I just wanted to clean it up if I could.  

This programming is tough stuff for an old codger.

(;-) TP
Last edit: 03 May 2023 22:31 by snowgoer540.

Please Log in or Create an account to join the conversation.

More
03 May 2023 22:40 #270592 by snowgoer540
Replied by snowgoer540 on topic Button Function Touch_Z
There's always more than one solution. So I'll give you two.

Your code emulates the button press through tormach's enqueue method. It emulates a mouse click on the button, so it needs to be on the right page to do that. Also, the code checks if it's allowed.

So options are:

1. call the function that does the work directly, after modifying it to not check if you're on the right page:

Before:
    def on_touch_z_button_release_event(self, widget, data=None):
        
        if not widget == 'Vmax549':
            if not self.is_button_permitted(widget): return

        valid, dro_val, error_msg = self.conversational.validate_z_touch_val(self.dro_list['touch_z_dro'])
        if not valid:
            self.error_handler.write(error_msg, ALARM_LEVEL_LOW)
            return
        ind = 2
        # the G10 command wants values in mm if G21, but actual_postion and g5x_offsets are in machine units (in.)
        # so we take the sup value and turn it into machine units, then send the offset command in g20/21 units
        supplemental_offset = dro_val / self.get_axis_scale(ind)
        tool_number = self.status.tool_in_spindle
        # g10 doesn't like tool number = zero, and tool number will be -1 in some cases on startup
        if tool_number < 1:
            self.error_handler.write('Cannot set tool offset for tool zero.  Please enter a valid tool number in the tool DRO before using Touch Z button', ALARM_LEVEL_MEDIUM)
            return

        z_offset = self.status.actual_position[ind] - self.status.g5x_offset[ind] - supplemental_offset
        z_offset = z_offset * self.get_axis_scale(ind)

        # can't use self.issue_tool_offset_command() here as this takes L10, other calls take L1
        g10_command = "G10 L10 P%d Z%f" %(tool_number, dro_val)
        self.issue_mdi(g10_command)
        self.command.wait_complete()
        self.issue_mdi("G43")
        self.dro_list['touch_z_dro'].set_text(self.dro_long_format % dro_val)
        self.set_image('touch_z_image', 'touch_z_green_led.png')
        self.window.set_focus(None)
        # kludge for asyncronous refresh of tool table, handled in periodic
        self.tool_liststore_stale = 2

After:
    def on_touch_z_button_release_event(self, widget, data=None):
        
        if not widget == 'Vmax549':
            if not self.is_button_permitted(widget): return

        valid, dro_val, error_msg = self.conversational.validate_z_touch_val(self.dro_list['touch_z_dro'])
        if not valid:
            self.error_handler.write(error_msg, ALARM_LEVEL_LOW)
            return
        ind = 2
        # the G10 command wants values in mm if G21, but actual_postion and g5x_offsets are in machine units (in.)
        # so we take the sup value and turn it into machine units, then send the offset command in g20/21 units
        supplemental_offset = dro_val / self.get_axis_scale(ind)
        tool_number = self.status.tool_in_spindle
        # g10 doesn't like tool number = zero, and tool number will be -1 in some cases on startup
        if tool_number < 1:
            self.error_handler.write('Cannot set tool offset for tool zero.  Please enter a valid tool number in the tool DRO before using Touch Z button', ALARM_LEVEL_MEDIUM)
            return

        z_offset = self.status.actual_position[ind] - self.status.g5x_offset[ind] - supplemental_offset
        z_offset = z_offset * self.get_axis_scale(ind)

        # can't use self.issue_tool_offset_command() here as this takes L10, other calls take L1
        g10_command = "G10 L10 P%d Z%f" %(tool_number, dro_val)
        self.issue_mdi(g10_command)
        self.command.wait_complete()
        self.issue_mdi("G43")
        self.dro_list['touch_z_dro'].set_text(self.dro_long_format % dro_val)
        self.set_image('touch_z_image', 'touch_z_green_led.png')
        self.window.set_focus(None)
        # kludge for asyncronous refresh of tool table, handled in periodic
        self.tool_liststore_stale = 2

Then call the function like this:
self.on_touch_z_button_release_event('Vmax549')

Of course you can chance Vmax549 to whatever you'd like, you're just passing it a bogus widget name for your check :)

2. Copy the code in that button check, and make a new function
    def terrys_touch_z(self):
        valid, dro_val, error_msg = self.conversational.validate_z_touch_val(self.dro_list['touch_z_dro'])
        if not valid:
            self.error_handler.write(error_msg, ALARM_LEVEL_LOW)
            return
        ind = 2
        # the G10 command wants values in mm if G21, but actual_postion and g5x_offsets are in machine units (in.)
        # so we take the sup value and turn it into machine units, then send the offset command in g20/21 units
        supplemental_offset = dro_val / self.get_axis_scale(ind)
        tool_number = self.status.tool_in_spindle
        # g10 doesn't like tool number = zero, and tool number will be -1 in some cases on startup
        if tool_number < 1:
            self.error_handler.write('Cannot set tool offset for tool zero.  Please enter a valid tool number in the tool DRO before using Touch Z button', ALARM_LEVEL_MEDIUM)
            return

        z_offset = self.status.actual_position[ind] - self.status.g5x_offset[ind] - supplemental_offset
        z_offset = z_offset * self.get_axis_scale(ind)

        # can't use self.issue_tool_offset_command() here as this takes L10, other calls take L1
        g10_command = "G10 L10 P%d Z%f" %(tool_number, dro_val)
        self.issue_mdi(g10_command)
        self.command.wait_complete()
        self.issue_mdi("G43")
        self.dro_list['touch_z_dro'].set_text(self.dro_long_format % dro_val)
        self.set_image('touch_z_image', 'touch_z_green_led.png')
        self.window.set_focus(None)
        # kludge for asyncronous refresh of tool table, handled in periodic
        self.tool_liststore_stale = 2

Call the function like this:
self.terrys_touch_z()

That's 2 of probably 1000 ways to skin the proverbial cat. If you choose the new function route, you could probably pull some lines out that may not be necessary (like the focus grabs and releases, etc.), but I dont know without playing with it/looking at it harder.

Please Log in or Create an account to join the conversation.

More
03 May 2023 22:42 #270593 by snowgoer540
Replied by snowgoer540 on topic Button Function Touch_Z
Oops, the BEFORE for Option 1 should actually be this:
def on_touch_z_button_release_event(self, widget, data=None):
        
        if not self.is_button_permitted(widget): return

        valid, dro_val, error_msg = self.conversational.validate_z_touch_val(self.dro_list['touch_z_dro'])
        if not valid:
            self.error_handler.write(error_msg, ALARM_LEVEL_LOW)
            return
        ind = 2
        # the G10 command wants values in mm if G21, but actual_postion and g5x_offsets are in machine units (in.)
        # so we take the sup value and turn it into machine units, then send the offset command in g20/21 units
        supplemental_offset = dro_val / self.get_axis_scale(ind)
        tool_number = self.status.tool_in_spindle
        # g10 doesn't like tool number = zero, and tool number will be -1 in some cases on startup
        if tool_number < 1:
            self.error_handler.write('Cannot set tool offset for tool zero.  Please enter a valid tool number in the tool DRO before using Touch Z button', ALARM_LEVEL_MEDIUM)
            return

        z_offset = self.status.actual_position[ind] - self.status.g5x_offset[ind] - supplemental_offset
        z_offset = z_offset * self.get_axis_scale(ind)

        # can't use self.issue_tool_offset_command() here as this takes L10, other calls take L1
        g10_command = "G10 L10 P%d Z%f" %(tool_number, dro_val)
        self.issue_mdi(g10_command)
        self.command.wait_complete()
        self.issue_mdi("G43")
        self.dro_list['touch_z_dro'].set_text(self.dro_long_format % dro_val)
        self.set_image('touch_z_image', 'touch_z_green_led.png')
        self.window.set_focus(None)
        # kludge for asyncronous refresh of tool table, handled in periodic
        self.tool_liststore_stale = 2

I'd edit it, but the editor here ... sucks.

Please Log in or Create an account to join the conversation.

More
04 May 2023 12:20 #270636 by Vmax549
Replied by Vmax549 on topic Button Function Touch_Z
WOW we are way down that rabbit hole now. Thanks Gary. That makes sense now.

(;-) TP

Please Log in or Create an account to join the conversation.

Moderators: cncbasher
Time to create page: 0.074 seconds
Powered by Kunena Forum