Stack overflow error

Good Day

I am currently trying to program a servo paired with a distance sensor such that the servo extends when the sensor is being cover. The servo stays extended for a period of time (in this case it is open for 5 seconds) and then closes. This functionality i have been able to program succesfully.

However I would like to program the ability to extend this “open time” if the scanner detects that the sensor is still cover once it has reached its position, which ive tried to do using an if statement that checks the button and then a goto line that reroutes the program to before the if statement as if to check the sensor again to see if it needs to stay open again. below is my code:

begin
		button_a if sequence_a endif
repeat

sub button_a
		1 get_position 500 less_than
		return

sub sequence_a
		7400 0 servo
		2 0 acceleration
		10 0 speed
		200 delay

 	        3200 0 servo
		3400 delay

		5000 delay

		precheck:
		button_a
		if
			delay 5000
			goto precheck
		endif	

		7400 0 servo 
		2 0 acceleration
		10 0 speed 
		3400 delay
		return

Additionally, having an interupt sequence on the closing bit would also be great, where if the sensor is activated then the closing is interupted and brought back to being open.

Thank you

UPDATE:

Seems I am blind and swapped the position of the delay and time, as well as the goto not being necessary as I put it into a while loop. See updated code below:

begin
		button_a if sequence_a endif
repeat

sub button_a
		1 get_position 500 less_than
		return

sub sequence_a
#premovement accel and speed
		2 0 acceleration
		10 0 speed

#starting position
		7400 0 servo
		200 delay
#top position
 	3200 0 servo
		3400 delay
#Initial waiting time 
		5000 delay

		begin
		button_a
		while
			1000 delay
		repeat

		7400 0 servo 

#insert interupt here
 
		3400 delay
		return

However I am still interested in identifying if it is possible to insert an interupt code near the end, (this is to prevent my hand from being closed on in the sequence, as the servo controlls a lid.

Hello.

I moved your post to the “Servo controllers and servos” section of the forum since it is about the Maestro.

The Maestro scripting language does not support traditional interrupts, but you should be able to write your script in a way that handles the behavior you described. For example, instead of using the delay command, you could start a while loop that monitors the button/sensor input until the servo is back at its starting position.

Also, it looks like you’re using a 3400ms delay as a way to wait for the servo to reach its commanded position; since you are using speed/acceleration limiting, I recommend using the get_moving_state command to check if it has reached its position.

With those adjustments, your script might look something like this:

#initialize servo acceleration, speed, and starting position
2 0 acceleration
10 0 speed
7400 0 servo

begin
  button_a if sequence_a endif #wait for button press
repeat

sub button_a
  1 get_position 500 less_than
return

sub sequence_a
  3200 0 servo #move to top position
  wait_for_move #wait for servo to reach top position
  
  5000 delay #stay there for 5 seconds

  #until the servo is back at its starting position, stay in loop monitoring button input
  begin
    0 get_position 7400 not_equals while
      button_a if #check if button is pressed
        3200 0 servo #if it is, set servo to top position 
        wait_for_move #wait for it to get there
        1000 delay #stay at top position for 1 extra second after button is released
      else
        7400 0 servo #when button is not pressed, start moving back to starting position
      endif
  repeat
return
		
sub wait_for_move
  begin 
    get_moving_state while
  repeat
return

Please note that I also moved the commands to set the servo’s speed, acceleration, and starting position to before the start of the main loop.

Brandon

This worked exactly as intended! Thank you very much for the assistance.

1 Like