# condensing terms

Just wondering what the easiest way of doing this is, I would like to have a number of waypoints in my system, whats the easiest way of turning them into single terms, so instead of having: int waypointx1 = 1; int waypointy1 = 1; I could instead have: int waypoint1(x, y);

Tongrer: Just wondering what the easiest way of doing this is, I would like to have a number of waypoints in my system, whats the easiest way of turning them into single terms, so instead of having: int waypointx1 = 1; int waypointy1 = 1; I could instead have: int waypoint1(x, y);

``````struct waypoint_t {int x; int y;}; // type declaration of your own struct type

waypoint_t waypoint={1,1};  // variable declaration (initialized with x=1, y=1)
``````

``````  waypoint.x=5;
waypoint.y=10;
``````

Also arrays of such struct types are possible.

So this is how it would work?

``````struct waypoint_t {int wxt; int wyt;}; // type declaration of your own struct type

waypoint_1 waypoint={1,1};
waypoint_2 waypoint={2,1};

waypoint_1.x ==1
waypoint_2.y ==1
``````

Tongrer: So this is how it would work?

Nope.

This would work:

``````struct waypoint_t {int wxt; int wyt;}; // type declaration of your own struct type

waypoint_t waypoint_1={1,1};
waypoint_t waypoint_2={2,1};

// And within a function:
waypoint_1.wxt =1;
waypoint_2.wyt =1;
``````

You are a lifesaver, thank you very much! I will have one more question about how I can use waypoint_t in my code but i'll ask in 5 mins when i've worked out what that is

Right in a function could do:

``````Case t:
distance_x = wxt - wxt--
distance_y = wyt - wyt--
t = t++
``````

Or something like that to stop me having to add a new case for every waypoint

Tongrer:
Or something like that to stop me having to add a new case for every waypoint

The main advantages using your own ‘struct’ types show up when using “array of struct”.

• Declare an Array of 10 waypoints (WP0…WP9)
• Calculate all distances between all waypoints and show on serial

Example:

``````struct waypoint_t {int x; int y;}; // type declaration of your own struct type

waypoint_t waypoints[]= {
{1,1},   // 0
{2,3},   // 1
{4,5},   // 2
{6,7},   // 3
{8,9},   // 4
{10,11}, // 5
{12,13}, // 6
{14,15}, // 7
{16,17}, // 8
{18,20}, // 9
};
#define NUMWAYPOINTS (sizeof(waypoints)/sizeof(waypoints[0]))

void setup() {
Serial.begin(9600);
int i,j;
for (i=0;i<NUMWAYPOINTS;i++)
{
for (j=i+1;j<NUMWAYPOINTS;j++)
{
float distance= sqrt(sq(waypoints[i].x-waypoints[j].x)+sq(waypoints[i].y-waypoints[j].y));
Serial.print("Distance from WP");Serial.print(i);
Serial.print(" to WP");Serial.print(j);
Serial.print(" = ");Serial.println(distance);
}
}
}

void loop() {
}
``````

You then can access your waypoints by their index in the array and by their element ‘x’ or ‘y’ within your code.

``````distance_x = wxt - wxt--
``````

That's a dangerous piece of code there. Better to decrement your variable in a separate line.

If you want distance_x to be 0, then just set it to zero and do your decrement on the next line.

Can you just give this a once over and let me know if you see any problems, eventually all the serial.print(“move forward”); and stuff will be replaced with actual commands telling the rover to move but until its been built i’m having to test it with just a board and various chips

``````void loop()
{
int i,j;
for (j=0;j<NUMWAYPOINTS;j++)
{
getVectors();
headingTarget = 180 / PI * atan((waypoints[j].x-waypoints[i].x)/(waypoints[j].y-waypoints[i].y));
{
delay(100);
}
else if (vectorx != waypoints[j].x || vectory != waypoints[j].y)
{
getVectors(); // there is a n=n+1 and delay(10) in here
if (n % 20){
Serial.print("Move foward "); Serial.print(sqrt(sq(waypoints[j].x - vectorx)+sq(waypoints[j].y-vectory))); Serial.println(" meters");}
}
else
{
if (j>0)  {i++;}
Serial.print("Next Waypoint = "); Serial.print(j+1);
}
}
}
``````

Tongrer: Can you just give this a once over and let me know if you see any problems

As you don't tell what it is, I have to guess.

What you want is possibly a waypoint-follower algorithm and control some vehicle from a starting point along a list of waypoints to a final destination.

From what you show as a code I see nothing else than big problems.

1. Problem: Heading If your heading is not accurate up to 1 degree, the vehicle will always turn itself:

2. Problem: Moving while not on target If your vehicle is not accurately on its target waypoint, it will move. In normal live, the position would possibly be based on (somewhat inaccurate) GPS data, and you never will reach the exact destination position by 1 meter. Normally you'd define a distance range of let's say 20 meters and if the vehicle is within that distance, the "reached waypoint" condition becomes true and the vehicle steps over to reach the next waypoint.

But 3. problem: Your vehicle must reach a waypoints 100% location while driving at a 100% accurate direction, otherwise the "reached waypont" condition never becomes true and the vehicle will never try to reach the next waypoint on its list.

On the other hand: You seem to understand now, how waypoints are stored in a list and how you can retrieve them by an index in the array.

The main problem with this code that I can see is this:

``````    else if (vectorx != waypoints[j].x || vectory != waypoints[j].y)
``````

The vehicle will chase its tail round and round trying to land exactly on the waypoint.

One fix for this might be to think in terms of the orthogonal distance between your heading and the waypoint, to include algorithms like "If I have gone past the waypoint, then meh - move onto the next one".

Thanks for the feedback, the dissertation supervisor is not letting me use a gps system, instead hes given me an accelerator to try and judge the distance which I am aware won't work, but he does want to see proof of concept so the waypoints are only going to be a meter or so apart so i don't loose to much in terms of accuracy. I'll add n a bit of code now to make sure the heading is +- 3 degrees and i can go over the waypoint