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

Pololu Forum

Newbie question: Error message: PoloMotor1:17:1: error: a function-definition is not allowed here before '{' token {


#21

I am yet trying to get the second motor to work.

From your instructions:
“The set of statements you added to make device 14 exit safe-start are in the correct format, but you have placed them outside of the exitSafeStart() function (and outside of any other functions) in your program. Also, there is no single correct place for any of this code; the placement will depend on how you want the program to behave.”

I tried this and did not work.

void exitSafeStart()
{
smcSerial.write(0xAA); //This lets all of the attached devices know that this is the start of a Pololu protocol command packet
smcSerial.write(0x0D); //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03); //This is the specific command to tell the addressed device to exit safe-start
}

void exitSafeStart()
{
smcSerial.write(0xAA); //This lets all of the attached devices know that this is the start of a Pololu protocol command packet
smcSerial.write(0x0E); //This lets device number 14 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03); //This is the specific command to tell the addressed device to exit safe-start
}

I am sorry about all the questions.Yes, I have tried other forums, including Arduino. And read the two standard books. This program is way more complex than the “Blink” and has Pololu propriety code that no one else understands. Again thank you.


#22

Hello.

It looks like you are trying to define a new function with the exact same name as your previous one (exitSafeStart()). I suspect you would be getting an error about redefining the exitSafeStart() function if you try to compile that code. If you want to have separate functions for sending the exit safe start command to each controller like that, you should give them unique names. Also, note that you would need to call each function separately for them to both run.

Alternatively, you could just move the second set of serial bytes to the first function (and erase the duplicate function) if you always want to send the exit safe start command to both controllers.

By the way, the only thing specific to the Simple Motor Controller in your code is the bytes you are sending, which Nathan has already confirmed are correct. The syntax and code structure of C++ (which is what you seem to be having problems with now) is not specific to our Simple Motor Controller’s serial protocol.

Brandon


#23

Hello BrandonM

Thank you for the reply.

" If you want to have separate functions for sending the exit safe start command to each controller like that, you should give them unique names."

Like this?

motor1.exitSafeStart();
motor2.exitSafeStart();
motor3.exitSafeStart();

Or like this?

exitSafeStart(motor1);
exitSafeStart(motor2);
exitSafeStart(motor3);

Thank you


#24

Hello, Gam.

This tutorial about creating functions using the Arduino IDE (which uses the C++ language) might be helpful for you:

https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/

-Nathan


#25

Thank you. I have not been able to control motor 14, only motor 13. How do I control motor 14 too? I have looked at the references and links you sent. They have not helped.

If I place

 smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x0E);          //This lets device number 14 (0x0E in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
}
}

after the end of the original program

 setMotorSpeed(-3200);  // full-speed reverse

  delay(10000);
}

I don’t get any errors. But motor 14 does not move either. I only can get motor 13 to work with this program. I have not been able to control motor 14. How do I control motor 14 too? I have looked at the references and links you sent. They have not helped.

#include <SoftwareSerial.h>
#define rxPin 3  // pin 3 connects to smcSerial TX  
#define txPin 5  // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);
 
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
}

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
  if (speed < 0)
  {
    smcSerial.write(0x86);  // motor reverse command
    speed = -speed;  // make speed positive
  }
  else
  {
    smcSerial.write(0x85);  // motor forward command
  }
  smcSerial.write(speed & 0x1F);
  smcSerial.write(speed >> 5 & 0x7F);
}
 
void setup()
{
  // Initialize software serial object with baud rate of 19.2 kbps.
  smcSerial.begin(19200);
 
  // The Simple Motor Controller must be running for at least 1 ms
  // before we try to send serial data, so we delay here for 5 ms.
  delay(5);
 
  // If the Simple Motor Controller has automatic baud detection
  // enabled, we first need to send it the byte 0xAA (170 in decimal)
  // so that it can learn the baud rate.
  smcSerial.write(0xAA);
 
  // Next we need to send the Exit Safe Start command, which
  // clears the safe-start violation and lets the motor run.
  exitSafeStart();
}
 
void loop()

{

  setMotorSpeed(3200);  // full-speed forward

  delay(8000);

setMotorSpeed(-1000);  // half-speed reverse

  delay(4000);

 setMotorSpeed(1000);  // half-speed forward

  delay(4000);

  setMotorSpeed(-3200);  // full-speed reverse

  delay(10000);

  setMotorSpeed(3200);  // full-speed forward

  delay(8000);

setMotorSpeed(-1000);  // half-speed reverse

  delay(4000);

  delay(4000);

  setMotorSpeed(-3200);  // full-speed reverse

  delay(10000);

{
smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x0E);          //This lets device number 14 (0x0E in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
}
}
//SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);
 
// required to allow motors to move
// must be called when controller restarts and after any error
//void exitSafeStart()
//{
//smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
//smcSerial.write(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
//smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
//}

// speed should be a number from -3200 to 3200
//void setMotorSpeed(int speed)
//{
//  if (speed < 0)
//  {
//    smcSerial.write(0x86);  // motor reverse command
//    speed = -speed;  // make speed positive
//  }
//  else
//  {
//    smcSerial.write(0x85);  // motor forward command
//  }
//  smcSerial.write(speed & 0x1F);
//  smcSerial.write(speed >> 5 & 0x7F);
//}
 
//void setup()
//{
  // Initialize software serial object with baud rate of 19.2 kbps.
//  smcSerial.begin(19200);
 
  // The Simple Motor Controller must be running for at least 1 ms
  // before we try to send serial data, so we delay here for 5 ms.
//  delay(5);
 
  // If the Simple Motor Controller has automatic baud detection
  // enabled, we first need to send it the byte 0xAA (170 in decimal)
  // so that it can learn the baud rate.
//  smcSerial.write(0xAA);
 
  // Next we need to send the Exit Safe Start command, which
  // clears the safe-start violation and lets the motor run.
//  exitSafeStart();
//}
 
//void loop()

//{

//  setMotorSpeed(3200);  // full-speed forward

//  delay(8000);

//setMotorSpeed(-1000);  // half-speed reverse

//  delay(4000);

// setMotorSpeed(1000);  // half-speed forward

//  delay(4000);

//  setMotorSpeed(-3200);  // full-speed reverse

//  delay(10000);

//  setMotorSpeed(3200);  // full-speed forward

//  delay(8000);

//setMotorSpeed(-1000);  // half-speed reverse

//  delay(4000);

//  delay(4000);

//  setMotorSpeed(-3200);  // full-speed reverse

//  delay(10000);

#26

Maybe it will be helpful if I explain how the example code we provide in the SMC user’s guide is structured and how it executes.There are 4 functions in that code: void exitSafeStart(), void setMotorSpeed(int speed), void setup(), and void loop(). The void setup() and void loop() functions are a standard part of the Arduino development environment. The code in void setup() is executed once when the microcontroller is first powered on or reset. The code in void loop() is then executed repeatedly (in a loop) once the code in void setup() is done executing. This is standard Arduino behavior and most of the example sketches in the Arduino IDE have these two functions.

The code in the other two functions (void exitSafeStart() and void setMotorSpeed(int speed)) is only executed when those functions are called by the void setup() or void loop() functions. So when the void setup() function is executed and it gets to line 45 in that example code, then all of the code in the void exitSafeStart() function is run, and after that, there is no more code in void setup() to run, so the code void loop() is run repeatedly. Every time the void loop() function calls the void setMotorSpeed(int speed) function (it does this on lines 50 and 52 in that example) then the code in the void setMotorSpeed(int speed) function (lines 16 to 26 in that example) is run.

The order of execution of statements in the code is determined by the language (C++) and development environment and, as I have mentioned before, there are numerous other references (books, online courses, tutorials) that discuss that sort of thing. Those are not specific to the motor controller. In general, to sequence a bunch of these motor controllers, you will need to be able to parse the language (C++) yourself to understand how to put together your own code that will behave the way you want it to. If the online resources I have pointed to are not helping, you might try to find a local C++ programming course or a programmer in your area familiar with C++.

-Nathan


#27

Thank you for the very comprehensive answer. I yet cannot get two motors to work separately. I have not been able to control motor 14, only motor 13. Or I can get motor 14 but not 13 to work. How do I control both motors separately?

Thank you.

 smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x0E);          //This lets device number 14 (0x0E in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
}
}

after the end of the original program

 setMotorSpeed(-3200);  // full-speed reverse

  delay(10000);
}

I don’t get any errors. But motor 14 does not move either. I only can get motor 13 to work with this program. I have not been able to control motor 14. How do I control motor 14 too? I have looked at the references and links you sent. They have not helped.

#include <SoftwareSerial.h>
#define rxPin 3  // pin 3 connects to smcSerial TX  
#define txPin 5  // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);
 
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
smcSerial.write(0xAA);          //This lets all of the attached devices know that this is the start of a Pololu protocol command packette(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x0D);          //This lets device number 13 (0x0D in hex) know that it is the target of the command (and lets all other devices know they do not respond to the command)
smcSerial.write(0x03);          //This is the specific command to tell the addressed device to exit safe-start
}

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
  if (speed < 0)
  {
    smcSerial.write(0x86);  // motor reverse command
    speed = -speed;  // make speed positive
  }
  else
  {