Running a Script Once Upon Button Press

We have a NC Switch connected to input on channel 5, with a 1K pull up resistor connected to 5v . When the button is pressed (switch opens), we see the target value/position change from 0 to 255.

We want the script to run one time, each time the button is pressed.

Is this code implementation correct?

begin
5 get_position 200 greater_than while  
  1000 9047 5964 0 0 0 2 frame_0..5 # Y+ Frame 0
  5000 0 0 0 frame_0_1_5 # STOP
  4500 7512 5964 2 frame_0_1_5 # X- Frame 1
  8000 0 0 0 frame_0_1_5 # STOP
  4500 5936 5964 2 frame_0_1_5 # Y- Frame 2
  8000 0 0 0 frame_0_1_5 # STOP
  4500 4280 5964 1 frame_0_1_5 # X+ Frame 3
  8000 0 0 0 frame_0_1_5 # STOP
  4500 4383 4466 2 frame_0_1_5 # Z+ Frame 4
  9500 0 0 frame_0_1 # STOP
  6500 4383 7592 1 frame_0_1_5 # Z- Frame 5
  8000 0 0 0 frame_0_1_5 # STOP
  10000 9047 5964 2 frame_0_1_5 # RETURN TO START
  6500 0 0 0 frame_0_1_5 # STOP
quit
repeat

Hello.

I moved your post to the “Servo controllers and servos” section of the forum since it seems more appropriate.

It looks like your script is very close to doing what you described, except right now it ends when it reaches the QUIT command, so the Maestro will need to be power cycled or told to restart the script before you can push the button again. Also, it would probably be simpler to use an IF statement instead of a while loop to check your button.

It looks like you could get the whole thing working the way you want by just replacing your WHILE command with an IF command and your QUIT command with ENDIF.

Brandon

Thanks. With the present code, we could not get the script to run even once, so there may be more here before we get to the quit command

Can you please post edits to the code that I posted with an example of the IF statement implementation. We’ve tried but get no response to the button press.

Peter

Hello.

You are correct; the code in your first post would result in an error because of the way the WHILE command was implemented. The code would exit the WHILE loop and continue on to run your first subroutine, then when it reaches the RETURN statement in that subroutine, it would cause an error. To correctly implement the WHILE loop, you would need to put it inside of another BEGIN/REPEAT loop. This is why I suggested that it would be simpler to use an IF statement, which would only require your code to have a single loop.

If you tried replacing your WHILE command with IF and the QUIT command with ENDIF and it is still not working, could you post a copy of your settings file? You can save your settings file by selecting the “Save settings file” option under the “File” drop-down menu with the Maestro connected. Also, could you describe what happens when you try to run your script (e.g. are you getting any errors and if so which ones)?

Brandon

Yes, I changed the statements as you suggested, and either we have made an error or there is another underlying issue. No errors, the script just doesn’t execute.

Here’s the settings file:

<!--Pololu Maestro servo controller settings file, http://www.pololu.com/catalog/product/1350-->
<UscSettings version="1">
  <NeverSuspend>false</NeverSuspend>
  <SerialMode>UART_FIXED_BAUD_RATE</SerialMode>
  <FixedBaudRate>9600</FixedBaudRate>
  <SerialTimeout>0</SerialTimeout>
  <EnableCrc>false</EnableCrc>
  <SerialDeviceNumber>12</SerialDeviceNumber>
  <SerialMiniSscOffset>0</SerialMiniSscOffset>
  <Channels ServosAvailable="6" ServoPeriod="156">
    <!--Period = 19.968 ms-->
    <!--Channel 0-->
    <Channel name="Z-Axis" mode="Servo" min="2432" max="9600" homemode="Goto" home="8978" speed="0" acceleration="0" neutral="6000" range="1905" />
    <!--Channel 1-->
    <Channel name="Y-Axis" mode="Servo" min="2432" max="9600" homemode="Goto" home="5964" speed="0" acceleration="0" neutral="6000" range="1905" />
    <!--Channel 2-->
    <Channel name="" mode="Servo" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
    <!--Channel 3-->
    <Channel name="" mode="Servo" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
    <!--Channel 4-->
    <Channel name="" mode="Servo" min="3968" max="8000" homemode="Off" home="3968" speed="0" acceleration="0" neutral="6000" range="1905" />
    <!--Channel 5-->
    <Channel name="" mode="Input" min="0" max="1024" homemode="Ignore" home="0" speed="0" acceleration="0" neutral="1024" range="1905" />
  </Channels>
  <Sequences>
    <Sequence name="Calibration Sequence">
      <Frame name="Y+ Frame 0" duration="1000">9047 5964 0 0 0 2</Frame>
      <Frame name="STOP" duration="5000">0 0 0 0 0 0</Frame>
      <Frame name="X- Frame 1" duration="4500">7512 5964 0 0 0 2</Frame>
      <Frame name="STOP" duration="8000">0 0 0 0 0 0</Frame>
      <Frame name="Y- Frame 2" duration="4500">5936 5964 0 0 0 2</Frame>
      <Frame name="STOP" duration="8000">0 0 0 0 0 0</Frame>
      <Frame name="X+ Frame 3" duration="4500">4280 5964 0 0 0 1</Frame>
      <Frame name="STOP" duration="8000">0 0 0 0 0 0</Frame>
      <Frame name="Z+ Frame 4" duration="4500">4383 4466 0 0 0 2</Frame>
      <Frame name="STOP" duration="9500">0 0 0 0 0 2</Frame>
      <Frame name="Z- Frame 5" duration="6500">4383 7592 0 0 0 1</Frame>
      <Frame name="STOP" duration="8000">0 0 0 0 0 0</Frame>
      <Frame name="RETURN TO START" duration="10000">9047 5964 0 0 0 2</Frame>
      <Frame name="STOP" duration="6500">0 0 0 0 0 0</Frame>
    </Sequence>
  </Sequences>
  <Script ScriptDone="true">#Calibration Sequence
begin
5 get_position 200 greater_than if  
  1000 9047 5964 0 0 0 2 frame_0..5 # Y+ Frame 0
  5000 0 0 0 frame_0_1_5 # STOP
  4500 7512 5964 2 frame_0_1_5 # X- Frame 1
  8000 0 0 0 frame_0_1_5 # STOP
  4500 5936 5964 2 frame_0_1_5 # Y- Frame 2
  8000 0 0 0 frame_0_1_5 # STOP
  4500 4280 5964 1 frame_0_1_5 # X+ Frame 3
  8000 0 0 0 frame_0_1_5 # STOP
  4500 4383 4466 2 frame_0_1_5 # Z+ Frame 4
  9500 0 0 frame_0_1 # STOP
  6500 4383 7592 1 frame_0_1_5 # Z- Frame 5
  8000 0 0 0 frame_0_1_5 # STOP
  10000 9047 5964 2 frame_0_1_5 # RETURN TO START
  6500 0 0 0 frame_0_1_5 # STOP
endif
repeat

sub frame_0..5
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_1_5
  5 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_1
  1 servo
  0 servo
  delay
  return

</Script>
</UscSettings>

Thank you for posting your settings. I tried loading them onto a Maestro to reproduce your problem, but it seemed to be working fine for me. Are you sure your button is being registered correctly by the Maestro? Can you still see the target slider for channel 5 change from 0 to 255 in the “Status” tab when you press the button? This might sound silly, but are you sure the script is running (e.g. is the “Stop Script” button in the “Script” tab colored red or is it grayed out)?

Brandon

Yes, The button is being registered in the software. This was checked repeatedly. I did not look to see if the script was stopped before I left the office. It could be that simple, but I don’t think it’s silly. I won’t be back until Tuesday, but I’ll see if I can get someone else to test.

I do know that it was not executing when it was powered up and not connected via USB

Thanks

Peter

We never tried to run the script while the Maestro was connected via USB to the Control Center. I had one of my team connect to the controller to see if we had stopped the script as you suggested.

The issue was that we did not have the option checked for “run at startup”, so when we disconnected USB to run the controller off of the external power supply, the script was not running. He checked that option, applied the settings and then started the controller off of an eternal power supply.

It’s working as intended now.

Thanks for your help; we appreciate you Brandon.

Peter

1 Like