Pages: [1] 2   Go Down
Author Topic: error on my sketch  (Read 1268 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i cant fix this error can someone help me?this project i have copied from buildautomnous line follower

Code:
long sensors_average;
int sensors_sum;
int position;
long sensors[] = {0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
sensors.
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;








void setup()
{
  
}
void loop()
sensors_read(); //Reads sensor values and computes sensor sum and weighted average
pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
calc_turn(); //Computes the error to be corrected
motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}


void pid_calc()
{ position = int(sensors_average / sensors_sum);
proportional = position – set_point; // Replace set_point by your set point
integral = integral + proportional;
derivative = proportional - last_proportional;
last_proportional = proportional;
error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{
sensors_average = 0;
sensors_sum = 0;
for (int i = 0; i < 5; i++)
{
sensors[i] = analogRead(i);
sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
readings
sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
}
}

void calc_turn()
{ //Restricting the error value between +256.
if (error_value < -256)
{
error_value = -256;
}
if (error_value > 256)
{
error_value = 256;
}
// If error_value is less than zero calculate right turn speed values
if (error_value < 0)
{
right_speed = max_speed + error_value;
left_speed = max_speed;
}
// If error_value is greater than zero calculate left turn values
else
{
right_speed = max_speed;
left_speed = max_speed - error_value;
}
}

the error:











sketch_feb13a:40: error: stray '\' in program
sketch_feb13a:6: error: expected constructor, destructor, or type conversion before '.' token
sketch_feb13a:31: error: expected initializer before 'sensors_read'
sketch_feb13a:32: error: expected constructor, destructor, or type conversion before ';' token
sketch_feb13a:33: error: expected constructor, destructor, or type conversion before ';' token
sketch_feb13a:34: error: expected constructor, destructor, or type conversion before '(' token
sketch_feb13a:35: error: expected declaration before '}' token
« Last Edit: February 13, 2013, 01:00:58 am by xulingxuling23 » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Too many mistakes. Do you know how to write a simple program in C?
Code:
long sensors[] = {0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
sensors.

Code:
void loop()
sensors_read(); //Reads sensor values and computes sensor sum and weighted average
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks now i will fix the code
Code:
long sensors_average;
int sensors_sum;
int position;
long sensors[] = {0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;
int threshold;
int readings;







void setup()
{
  
}
void loop()
{
sensors_read(); //Reads sensor values and computes sensor sum and weighted average
pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
calc_turn(); //Computes the error to be corrected
//motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}


void pid_calc()
{
position = int(sensors_average / sensors_sum);
proportional = position – set_point; // Replace set_point by your set point
integral = integral + proportional;
derivative = proportional - last_proportional;
last_proportional = proportional;
error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{ sensors_average = 0;
sensors_sum = 0;
for (int i = 0; i < 5; i++)
{
sensors[i] = analogRead(i);
// Readings less than threshold are filtered out for noise
if (sensors[i] < threshold)
sensors[i] = 0;
sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
//readings
sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
}
}

void calc_turn()
{ //Restricting the error value between +256.
if (error_value < -256)
{
error_value = -256;
}
if (error_value > 256)
{
error_value = 256;
}
// If error_value is less than zero calculate right turn speed values
if (error_value < 0)
{
right_speed = max_speed + error_value;
left_speed = max_speed;
}
// If error_value is greater than zero calculate left turn values
else
{
right_speed = max_speed;
left_speed = max_speed - error_value;
}
}


errors:










sketch_feb13a:42: error: stray '\' in program
sketch_feb13a.ino: In function 'void pid_calc()':
sketch_feb13a:42: error: expected `;' before 'u2013'
« Last Edit: February 13, 2013, 01:12:10 am by xulingxuling23 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I fixed up the stray, and formatted it.

Code:
long sensors_average;
int sensors_sum;
int position;
long sensors[] = {
  0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;
int threshold;
int readings;

void setup()
{

}

void loop()
{
  sensors_read(); //Reads sensor values and computes sensor sum and weighted average
  pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
  calc_turn(); //Computes the error to be corrected
  //motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}

void pid_calc()
{
  position = int(sensors_average / sensors_sum);
  proportional = position - set_point; // Replace set_point by your set point
  integral = integral + proportional;
  derivative = proportional - last_proportional;
  last_proportional = proportional;
  error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{
  sensors_average = 0;
  sensors_sum = 0;
  for (int i = 0; i < 5; i++)
  {
    sensors[i] = analogRead(i);
    // Readings less than threshold are filtered out for noise
    if (sensors[i] < threshold)
      sensors[i] = 0;
    sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
    //readings
    sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
  }
}

void calc_turn()
{ //Restricting the error value between +256.
  if (error_value < -256)
  {
    error_value = -256;
  }
  if (error_value > 256)
  {
    error_value = 256;
  }
  // If error_value is less than zero calculate right turn speed values
  if (error_value < 0)
  {
    right_speed = max_speed + error_value;
    left_speed = max_speed;
  }
  // If error_value is greater than zero calculate left turn values
  else
  {
    right_speed = max_speed;
    left_speed = max_speed - error_value;
  }
}


I still get the error message about "set_point" but as the comment says, you are supposed to put your set point there. I don't know what that is.
Logged


Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27089
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

int set_point;
is missing from the declarations
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick, I tried a few things and couldn't find the stray \

Where was it?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thans Nick and crossroads
@liudr thats whay im ask to this topic smiley-sad

oke now i want to try on my bot smiley-grin thanks
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Nick, I tried a few things and couldn't find the stray \

Where was it?

Here, although it's hard to see:

Code:
proportional = position – set_point; // Replace set_point by your set point

That ain't no minus sign. It's hex 0x96. Minus is 0x2D.

Fixed version:

Code:
proportional = position - set_point; // Replace set_point by your set point

See the difference?
« Last Edit: February 13, 2013, 02:38:52 am by Nick Gammon » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cant see sir
i dont know looks the code is same

now i enable driver motor and got the prob


Code:
// map motor poles to Arduino pins
#define motor1pole1 2
#define motor1pole2 3
#define motor2pole1 7
#define motor2pole2 8

// map L293d motor enable pins to Arduino pins
#define enablePin1 9
#define enablePin2 10

#define M1_MAX_SPEED 100
#define M2_MAX_SPEED 100

#define motordelay 30
#define debugmotorsec 3000

long sensors_average;
int sensors_sum;
int position;
long sensors[] = {
  0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;
int threshold;
int readings;
int set_point;
int motor_drive;
void setup()
{
  // set mapped L293D motor1 and motor 2 enable pins on Arduino to output (to turn on/off motor1 and motor2 via L293D)
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  // set mapped motor poles to Arduino pins (via L293D)
  pinMode( motor1pole1 , OUTPUT);
  pinMode( motor1pole2, OUTPUT);
  pinMode( motor2pole1, OUTPUT);
  pinMode( motor2pole2 , OUTPUT);
}

void loop()
{
  sensors_read(); //Reads sensor values and computes sensor sum and weighted average
  pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
  calc_turn(); //Computes the error to be corrected
  motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}

void pid_calc()
{
  position = int(sensors_average / sensors_sum);
  proportional = position - set_point; // Replace set_point by your set point
  integral = integral + proportional;
  derivative = proportional - last_proportional;
  last_proportional = proportional;
  error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{
  sensors_average = 0;
  sensors_sum = 0;
  for (int i = 0; i < 5; i++)
  {
    sensors[i] = analogRead(i);
    // Readings less than threshold are filtered out for noise
    if (sensors[i] < threshold)
      sensors[i] = 0;
    sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
    //readings
    sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
  }
}

void calc_turn()
{ //Restricting the error value between +256.
  if (error_value < -256)
  {
    error_value = -256;
  }
  if (error_value > 256)
  {
    error_value = 256;
  }
  // If error_value is less than zero calculate right turn speed values
  if (error_value < 0)
  {
    right_speed = max_speed + error_value;
    left_speed = max_speed;
  }
  // If error_value is greater than zero calculate left turn values
  else
  {
    right_speed = max_speed;
    left_speed = max_speed - error_value;
  }
}








sketch_feb13a.ino: In function 'void loop()':
sketch_feb13a:55: error: 'motor_drive' cannot be used as a function
« Last Edit: February 13, 2013, 02:50:40 am by xulingxuling23 » Logged

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

Code:
int motor_drive;
Because you declared it as an "int" variable.

Quote
i dont know looks the code is same
Not to the compiler it doesn't.
If ever I see the error message "stray '\'...", I can put a pretty certain bet that the code has been copied from a website.
« Last Edit: February 13, 2013, 02:52:57 am by AWOL » 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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i dont know looks the code is same

Look closely:




See the different length of the minus sign?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@nick
gosh its diffrent thx for help me nick

@awol
so i must use double not int?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4257
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.  You declared motor_drive as an int then tried to use it as a function
Code:
int motor_drive;
Code:
motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
Declaring it as another type will not solve the problem.
Should there be a function in your code that causes both motors to run ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote


See the difference?

Umm no.
Logged

Pages: [1] 2   Go Up
Jump to: