Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Maestro script control through arduino


#1

Hello,

This is a really stupid question as I have had this working before and it is waaaaay simple! Just need help finding the bug.

Situation:

  • 12 ch maestro, new
  • 6V power supply rated for 3 amps
  • one micro servo (HS-45HB) just for testing
  • arduino communication at 9600 baud TX going to maestro RX, common GND
  • in maestro control center have created 9 frames moving the channel 0 (single servo) to different positions
  • copied sequence to script (clean window)
  • Script is titles “SUB_merc1”
  • Trying to trigger the script through a simple pushbutton on arduino using this command: maestro.restartScript(SUB_merc1);

Doesn’t work, getting a “stack underflow / overflow” error (solid red LED) in maestro. Script works fine running through control center.

TIA!!

Arduino code:

#include <Bounce2.h>

#include <PololuMaestro.h>

#define HWSERIAL Serial1
MiniMaestro maestro(HWSERIAL);


// replace your sub numbers here
#define SUB_merc1 0
#define SUB_merc2 1
#define SUB_merc3 2


//maestro baud
unsigned long baud = 9600;

//maestro accel global
//maestro.setAcceleration(0, 10);

//button pins
#define BUTT_1 2
#define BUTT_2 3
#define BUTT_3 4


//define relay output pin
int relay = 13;

Bounce  bouncer_1  = Bounce();
Bounce  bouncer_2  = Bounce();
Bounce  bouncer_3  = Bounce();
Bounce  bouncer_4  = Bounce();

/* #######################################
 
 
 
           Setup 
 
 
 
 ######################################## */
void setup(void) {
  
  maestro.setAcceleration(0, 10); 
  
  delay(1000);

  Serial.begin(9600);

  HWSERIAL.begin(baud); // communication to hardware serial (maestro)

  //debounce buttons
  pinMode(BUTT_1, INPUT_PULLUP);
  bouncer_1.attach( BUTT_1 );
  bouncer_1.interval(50);

  pinMode(BUTT_2, INPUT_PULLUP);
  bouncer_2.attach( BUTT_2 );
  bouncer_2.interval(50);

  pinMode(BUTT_3, INPUT_PULLUP);
  bouncer_3.attach( BUTT_3 );
  bouncer_3.interval(50);

}

void loop() {
  bouncer_1.update();
  bouncer_2.update();
  bouncer_3.update();
 

  if (bouncer_1.read() == LOW) {
    // stop any running script
    maestro.stopScript();
    // start numbered script BLAST
    maestro.restartScript(SUB_merc1);
  } else if (bouncer_2.read() == LOW) {
    // stop any running script
    maestro.stopScript();
    // start numbered script Defaults
    maestro.restartScript(SUB_merc2);
  } else if (bouncer_3.read() == LOW) {
    // stop any running script
    maestro.stopScript();
    // start numbered script Defaults
    maestro.restartScript(SUB_merc3);

  }

}

…and for the whole story…

Maestro script:

# SUB_merc1
begin
  500 3968 0 0 0 0 0 
  0 0 0 0 0 0 frame_0..11 # Frame 0
  500 8000 frame_0 # Frame 1
  500 5962 frame_0 # Frame 2
  500 6753 frame_0 # Frame 3
  500 5126 frame_0 # Frame 4
  500 8000 frame_0 # Frame 5
  500 3968 frame_0 # Frame 6
  500 8000 frame_0 # Frame 7
  500 3968 frame_0 # Frame 8
repeat

sub frame_0..11
  11 servo
  10 servo
  9 servo
  8 servo
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_0
  0 servo
  delay
  return

#2

Hello.

Thank you for including a clear description of your problem and your complete code. It looks like you might not fully understand the way the script and the restartScript() command works. The sequences you create in the “Sequence” tab of the Maestro Control Center are different from subroutines in the script. For example, in your script, you currently have two subroutines (named frame_0..11 and frame_0). When you use the restartScript() command from the Maestro Arduino library with the argument of 0, it goes to the first subroutine in your script (frame_0..11 in this case). That subroutine ends with a return, but since the Maestro started at that subroutine, it does not know where to return to and triggers the error.

If you want the script you posted to be seen as subroutine 0, you can declare it as a subroutine by putting sub SUB_merc1 on the line before the BEGIN statement. Please note that since the BEGIN statement ends in a REPEAT, this will result in the script looping continuously. If you want it to only run through once and stop, you can call the QUIT command before REPEAT.

If you try adjusting your script and run into more problems, you can post your updated script along with a description of what it does, and I would be glad to take a look.

Brandon


#3

Brandon,

YOU ARE A STEELY-EYED MISSILE MAN! (that’s a good thing)

Thank you, that worked and I understand a bit more about that magical maestro.

Cheers!