Help with my code please

here is what I have so far, and I know there are a lot of errors I just need some help please.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cmath>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;

const double ARM1_LENGTH=.87f;
const double ARM2_LENGTH=.59f;
double CurrentPosition[3] = [theta1, theta2, theta3];
//double Destination[3] = [1,1,1]; // also for compiling
//int CurrentPosition[3]=[0,0,0]; // for input data to compile
double P[3] = CurrentPosition[3], BMNewThetas[3];

int main() 
{
BMlastD = Distance(CurrentPosition[3], Destination[3]); // testing
BMNewThetas[3] = BM(P[3]); // testing
BMNewD = Distance (BMNewThetas[3]); //  testing

  while (BMlastD != BMNewD)
  {
    BMlastD = BMNewD;
    BMNewThetas[3] = BM(BMNewThetas[3]);
    BMNewD = Distance(forwardkinematics(BMNewThetas[3]), Destination[3]);
  } 

return (BMNewThetas[3])
}
{
double forwardkinematics (double T[3])

  x = cos(T[0])*(ARM1_LENGTH*(cos(T[1]))+(ARM2_LENGTH)*cos(T[1]+T[2]));
  y = sin(T[0])*(ARM1_LENGTH*(cos(T[1]))+(ARM2_LENGTH)*cos(T[1]+T[2]));
  z = (ARM1_LENGTH)*sin(T[1])+ARM2_LENGTH(sin(T[1]+T[2]));
  return([x,y,z]);
}

double distance (double A[3],double B[3])
{
  return sqrt((B[2]-A[2])*(B[2]-A[2])+(B[1]-A[1])*(B[1]-A[1])+(B[0]-A[0])*(B[0]-A[0]));
}
{
double BM(double T[3])
}
{
double theta1=T[0], theta2=T[1], theta3=T[2];
P[3] = forwardkinematics(T[3]);
shortestdistance = distance (P[3]);
int besti, bestj, bestk;
for(int i = -1; i<2; i++)
{
  for(int j = -1; j<2; j++)
  {
    for(int k = -1; k<2; k++)
    { 
      P[3] = forwardkinematics([theta1+i, theta2+j, theta3+k]);
      if (distance(P) < shortestdistance)
      {
         besti = i; bestj = j; bestk = k;
      }
    }
  }
}
return ([theta1+besti, theta2+bestj, theta3+bestk]);
}
[code]
[/code]
Last edited on
Firstly, please edit your post so it uses code tags - the <> button on the right. You might get more replies if you always do this. If you have compile errors, then post these in full as well.

2 things that stand out are the non-declaration of your variables, and the cases sensitiveness of variable / function names.

HTH

Edit: Also, make sure to have some decent indenting in your code as well.
Last edited on
Here is the compiling errors I get, and can you help me with the non-declaration, this is my first c++ project.

Thank you


anonymous@anonymous-Linux ~/Desktop/Arm Code $ gcc acn.cpp
acn.cpp:10:30: error: ‘theta1’ was not declared in this scope
acn.cpp:10:38: error: ‘theta2’ was not declared in this scope
acn.cpp:10:46: error: ‘theta3’ was not declared in this scope
acn.cpp:10:52: error: type ‘<lambda>’ with no linkage used to declare function ‘void<lambda>::operator()() const’ with linkage [-fpermissive]
acn.cpp: In lambda function:
acn.cpp:10:53: error: expected ‘{’ before ‘;’ token
acn.cpp: At global scope:
acn.cpp:10:53: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x [enabled by default]
acn.cpp:13:32: error: array must be initialized with a brace-enclosed initializer
acn.cpp: In function ‘int main()’:
acn.cpp:17:1: error: ‘BMlastD’ was not declared in this scope
acn.cpp:17:40: error: ‘Destination’ was not declared in this scope
acn.cpp:17:54: error: ‘Distance’ was not declared in this scope
acn.cpp:18:25: error: ‘BM’ was not declared in this scope
acn.cpp:19:1: error: ‘BMNewD’ was not declared in this scope
acn.cpp:23:15: error: ‘BMNew’ was not declared in this scope
acn.cpp:25:55: error: ‘forwardkinematics’ was not declared in this scope
acn.cpp:29:1: error: expected ‘;’ before ‘}’ token
acn.cpp: At global scope:
acn.cpp:30:1: error: expected unqualified-id before ‘{’ token
acn.cpp:43:1: error: expected unqualified-id before ‘{’ token
acn.cpp:46:1: error: expected unqualified-id before ‘{’ token
double CurrentPosition[3] = [theta1, theta2, theta3];
needs theta1, theta2, and theta3, variables that are declared inside function BM. When the function BM ends, the variable goes out of scope.
Last edited on
The variables of theta1, theta2, and theta3. Will be given to me on the test day. They will change, how do I fix the other issues?

EDIT: Thank you so much for helping and sorry for all the questions, and noobish answers.
Last edited on
acn.cpp:10:30: error: ‘theta1’ was not declared in this scope
acn.cpp:10:38: error: ‘theta2’ was not declared in this scope
acn.cpp:10:46: error: ‘theta3’ was not declared in this scope


This says that the variables on line 10 are not declared. You must declare variables with a type, and you also must initialise them, before you use them in an expression.

acn.cpp:10:52: error: type ‘<lambda>’ with no linkage used to declare function ‘void<lambda>::operator()() const’ with linkage [-fpermissive]
acn.cpp:10:53: error: expected ‘{’ before ‘;’ token
acn.cpp:10:53: error: expected ‘{’ before ‘;’ token

This one is due to the square brackets:
double CurrentPosition[3] = [theta1, theta2, theta3];

You should always use braces to initialise arrays:

double CurrentPosition[3] = {theta1, theta2, theta3};

acn.cpp:13:32: error: array must be initialized with a brace-enclosed initializer


Same thing here:
double P[3] = CurrentPosition[3], BMNewThetas[3];

Except that P[3] is an array of 3 doubles, you are trying to initialise it with two arrays that contain 3 items each - doesn't make sense.

This code:
BMlastD = Distance(CurrentPosition[3], Destination[3]); // testing

Produces these errors:

acn.cpp: In function ‘int main()’:
acn.cpp:17:1: error: ‘BMlastD’ was not declared in this scope
acn.cpp:17:40: error: ‘Destination’ was not declared in this scope
acn.cpp:17:54: error: ‘Distance’ was not declared in this scope


BMlastD is not declared.
Distance is a function call, but the function is named distance.


BMNewThetas[3] = BM(P[3]); // testing

You can't assign arrays like that.

acn.cpp:25:55: error: ‘forwardkinematics’ was not declared in this scope


This is because of the extra & mismatched braces further down, that the compiler doesn't recognise forwardkinematics as a function definition on lines 31 -37. With braces, if you are using an IDE - you can get it to do closing braces automatically.

Hope all goes well.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i = -1; i<2; i++)
{
  for(int j = -1; j<2; j++)
  {
    for(int k = -1; k<2; k++)
    { 
      P[3] = forwardkinematics([theta1+i, theta2+j, theta3+k]);
      if (distance(P) < shortestdistance)
      {
         besti = i; bestj = j; bestk = k;
      }
    }
  }
}


With this, arrays start at zero, so your for loops should as well. You have unnecessary braces.

1
2
3
4
5
6
7
8
for(int i = 0; i<3; i++)
  for(int j = 0; j<3; j++)
    for(int k = 0; k<3; k++) { 
      P[3] = forwardkinematics(theta1, theta2, theta3);
      if (distance(P) < shortestdistance) {
         besti = i; bestj = j; bestk = k;
      }
    }


I would use different variable names - i & j can get confusing. x, y , z would be OK in this case because they represent spatial coordinates. Otherwise you use East, North, Height as an example.

Now that I have rewritten it, the whole thing doesn't make sense.
Last edited on
Thank You so much, this helps a LOT! You're a life saver. I fixed issues and it is working. Thank you! Thank you! Thank you!
One final thing - Don't put multiple statements on one line.

Does your code produce the right answers?
Topic archived. No new replies allowed.