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!

2 Likes
#4

Hi,

I’m trying to control a sequence of servo movement frames that I made in the Maestro Control Center via the Arduino nano. I only have one servo plugged into the #0 servo port. However, I get a solid Red light and Yellow blinking light when I execute the code. Eventually, I will want to have a button and trigger the Mini Maestro (12), but for now I’m just trying to get the servo to move at startup.

Could you take a look at my code and let me know if you see anything wrong?

Arduino Code:

#ifdef SERIAL_PORT_HARDWARE_OPEN
  #define maestroSerial SERIAL_PORT_HARDWARE_OPEN
#else
  #include <SoftwareSerial.h>
  SoftwareSerial maestroSerial(0, 1);  //(RX,TX)
#endif


MiniMaestro maestro(maestroSerial);

#define highlander 0


void setup()
{
  maestroSerial.begin(9600);
}

void loop()
{
  maestro.restartScript(highlander);
  delay(4000);

  // Stop the running script.
  maestro.stopScript();

  maestro.restartScriptWithParameter(1, 2000);
  delay(4000);
}

Maestro Control Center:

#highlander
begin
  500 8000 0 0 0 0 0 
  0 0 0 0 0 0 frame_0..11 # Frame 0
  500 5964 frame_0 # Frame 1
  500 8000 frame_0 # Frame 2
  500 3968 frame_0 # Frame 3
  500 5103 frame_0 # Frame 4
  500 3968 frame_0 # Frame 5
  500 8000 frame_0 # Frame 6
  500 3968 frame_0 # Frame 7
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

Thank you!

-Austin

#5

Hello.

It seems like your code has the same mistake as discussed previously in this topic. Your Maestro program does not declare the highlander script as a subroutine, so your Arduino code’s restartScript() command will run the first declared subroutine in your Maestro script, which is frame_0...11. To run your intended code, you should make the servo frames script into a subroutine by putting sub highlander on the line before the BEGIN statement.

If you make that adjustment and continue to have problems, then run your code with the Maestro connected to a computer running the Maestro Control Center so that you can check the type of error that it reports. I can continue to help your troubleshoot if you post your updated code and share the errors that the Control Center reports.

-Patrick

#6

That worked! Thank you very much!

1 Like
#7

Other question I had is:

Im using arduino nano to communicate via serial with mp3 device (dfplayer mini) and the Minimaestro 12 at the same time. Do I had to have different baud rates or can they both be 9600? (Im now trying to press a button and have song play from dfplayermini at same time as servo movements from maestro)

Thank you

#8

You can set the Maestro to 9600 baud, but please note that it is unlikely for your system to work correctly if you daisy chain the Maestro with other serial devices not using the Pololu serial protocol, like your mp3 device. Regardless of their baud rates, their commands are likely to interfere with each other. I recommend using separate serial communications line. The Maestro library for Aruino makes it easy to control the Maestro with a software serial setup that uses an I/O pin instead of just the hardware serial lines on pins 0 and 1.

-Patrick