Pages: [1] 2   Go Down
Author Topic: I need help with serial comands.  (Read 921 times)
0 Members and 1 Guest are viewing this topic.
Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this code was given to me and i cant seem to make it work. I placed the code into the ide and verified it. It hangs up on "void EXITSAFESTART(uint8_t device)" with the error of "expected unqualified-id before numeric constant." what does that error mean and what does unit8_t device mean? The two devices that i have connected on the same serial line are pololu simple motor controllers. The controllers have built in protocols that can number the devices. http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf (page 61). My two motor controllers are wired to the same serial tx line as shown on page 35 http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf
The code will control one of the boards but I am trying to rewrite in to control both separately. They explain how on page 64 http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf

Code:
#include <NewSoftSerial.h>

#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX

NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// pululu codes
#define NEWCMD 0xAA
#define EXITSAFESTART 0x03
#define FORWARD 0x05
#define REVERSE 0x06

// device id's
 #define Motor1 = 10;
 #define Motor2 = 13;


void EXITSAFESTART(uint8_t device)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);
  mySerial.print(EXITSAFESTART, BYTE);
}


void setMotorSpeed(uint8_t device, int speed)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);

  if (speed < 0)
  {
    mySerial.print(REVERSE, BYTE); // motor reverse command
    speed = -speed; // make speed positive
  }
  else
  {
    mySerial.print(FORWARD, BYTE); // motor forward command
  }
  speed = constrain(speed, 0, 3200);  // speed should be a number between 0 to 3200
  mySerial.print((unsigned char)(speed & 0x1F), BYTE);
  mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
  mySerial.begin(19200);
  delay(5);
  mySerial.print(0xAA, BYTE);
  EXITSAFESTART();
}

void loop()
{
  int speed = analogRead(A0) * 6 - 3072;  // use an LDR or potmeter or ...
  setMotorSpeed(motor1, speed); // full-speed forward
  delay(1000);

  int speed = analogRead(A1) * 6 - 3072;
  setMotorSpeed(motor2, -speed); // full-speed reverse
  delay(1000);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define EXITSAFESTART 0x03
Code:
void EXITSAFESTART(uint8_t device)
Is EXITSAFESTART a constant or a function? You can't have it both ways.
Logged

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it is a command sent to the motor controller to before the motor will start. " mySerial.print(0x83, BYTE); "Page 63 http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it is a command sent to the motor controller to before the motor will start.
Then, why do you have a function with the same name?

The pre-processor will substitute all occurrences of the name EXISTSAFESTART with the value 0x03, resulting in this:
Code:
void 3(uint8_t device)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);
  mySerial.print(3, BYTE);
}
Since 3 is not a valid function name, you will have all kinds of problems.
Logged

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm very new to serial commands, the original code that i got from the maker of the boards looked like this.
the motor controllers have a built in protocol to number the devices. http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf (page 61
Code:
#include <NewSoftSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(38400);
// 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
mySerial.print(0xAA, BYTE); // send baud-indicator byte
// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
}

but i wanted to rewrite it to control two devices on the same serial line separately.
« Last Edit: April 03, 2011, 11:08:04 am by alphamike88 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On page 61, it says that you need to send three or more bytes of data to control multiple devices. The first byte tells the controller that the rest of the data is for a specific device. The second byte defines which device. The rest of the data is the command for that device.

There was nothing on that page that told you to rename the ExitSafeStart() function to have the same name as the EXITSAFESTART command constant. So, why did you do that?
Logged

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did not rename the safestart command. it came off of this post. http://arduino.cc/forum/index.php/topic,57286.0.html
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I did not rename the safestart command.
It has one name in the code in your original post, and another name in the code posted in reply #4. If you didn't rename it, you need better control over the access to your code on your computer.
Logged

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you talking about going from ExitSafeStart to EXITSAFESTART? Yes, I did make that change. I just thought that he made a mistake in typing. As you  can tell, I am very new to writing complex code.
« Last Edit: April 03, 2011, 11:57:35 am by alphamike88 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Are you talking about going from ExitSafeStart to EXITSAFESTART?
Yes, I am.

Quote
I just thought that he made a mistake in typing.
I don't think he did.
Logged

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the code back to
Code:
#include <NewSoftSerial.h>

#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX

NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// pululu codes
#define NEWCMD 0xAA
#define EXITSAFESTART 0x03
#define FORWARD 0x05
#define REVERSE 0x06

// device id's
uint8_t Motor1 = 10;
uint8_t Motor2 = 13;


void exitSafeStart(uint8_t device)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);
  mySerial.print(EXITSAFESTART, BYTE);
}


void setMotorSpeed(uint8_t device, int speed)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);

  if (speed < 0)
  {
    mySerial.print(REVERSE, BYTE); // motor reverse command
    speed = -speed; // make speed positive
  }
  else
  {
    mySerial.print(FORWARD, BYTE); // motor forward command
  }
  speed = constrain(speed, 0, 3200);  // speed should be a number between 0 to 3200
  mySerial.print((unsigned char)(speed & 0x1F), BYTE);
  mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
  mySerial.begin(38400);
  delay(5);
  mySerial.print(0xAA, BYTE);
  exitSafeStart();
}

void loop()
{
  int speed = analogRead(A0) * 6 - 3072;  // use an LDR or potmeter or ...
  setMotorSpeed(motor1, speed); // full-speed forward
  delay(1000);

  int speed = analogRead(A1) * 6 - 3072;
  setMotorSpeed(motor2, -speed); // full-speed reverse
  delay(1000);
}
.
At the same place it hangs on "void exitSafeStart(uint8_t device)" with the error " too few arguments to fuction 'void exitSafeStart(uint8_t)'
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
At the same place it hangs on "void exitSafeStart(uint8_t device)" with the error " too few arguments to fuction
Zero is fewer than one.

Code:
exitSafeStart();
Zero.
Code:
void exitSafeStart(uint8_t device)
One.

If you call it, you need to give as many arguments as you specified, or use defaulted arguments.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the difference between the two. sorry i'm really new to this.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you define "void exitSafeStart", you told the compiler that it is a function that doesn't return a value ("void") and takes one unsigned eight bit argument called "device".
When you called "exitSafeStart" in "setup", you didn't provide an argument.
That's what the compiler complained about.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greenville, SC
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Nearly everything is too good to throw out.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So does that mean i need to remove exitSafeStart() from
Code:
void setup()
{
  mySerial.begin(19200);
  delay(5);
  mySerial.print(0xAA, BYTE);
  exitSafeStart();
}
Logged

Pages: [1] 2   Go Up
Jump to: