Restructuring my code help

Hello, recently had some time to jump back on my robot project.
Currently I have code written using my ping sensors for object avoidance (also another code I can load using a webcam to follow an object of certain color). Although as of now I've removed the cheap webcam and I'm starting to use my Kinect (on head of robot).
My code, as of right now, is very disorganized and also have seperate code to do different things..
I thought I'd write down a kind of code structure I was thinking about trying, along with a few questions.
*No complete or compiling code atm...just some functons as well as trying the best way to implement them into one program (hope this is OK).
Also, for the moment won't bother/focus on data recieved from the Kinect here. Using the webcam, Roborealm and the help of Paul a while back I've managed to get data from the PC to the Arduino and think I have that covered..

Giving a recap of what is going on, or what I'd like to aim for:
-Gather the incoming data from 4 ping sensors. In front of the bot there are 3 sensors: 1 center aiming straight forward and 2 on either side facing outward. Also 1 ping sensor in the rear facing straight backwards.
-depending on the current distance of the center and 2 outer ping sensors; speed up, slow down and turn motors in a direction where there are no obstructions. When turning, turn toward the direction of the greatest ping distance value, etc...
-Use an increase and decrease speed function for the motors (so starting from neutral or a fast speed to stop isn't so hard on the motors).

I may add something I may have forgotten while posting this.

I've been looking into different statements/methods of doing this.
Using the while some condition is happening, keep doing something else(along with the break statement). If there is a change in a value(s) do another thing...
This will probably end up being a long while, if mess of code.

Another is using the switch statement using different cases (depending on certain values...i.e ping distances).

Here are some of the functions I thought about using inside the code:

moveForward();
moveBackward();
moveLeft();
moveRight();
motorsStop();

decreaseSpeed();
increaseSpeed();

pingRange();  //distance data from ping sensor calculations

kinectData();  //maybe get to this a bit later.

Q1: Is this a good way to use decrease and increase speed as separate functions or should I incorporate them into each movement function (forward, backward, left, etc...)?

Q2: Reading back in my C++ book. It states there are 3 forms of function calls which I'm still trying to wrap my head around:

void blink1(){
}

The above is where blink1 has no parameter and no return value. So 'parameter' would be some range of values? *ok, parameter could be a variable?
Also, no return value? If this function is placed/called in the main loop, doesn't this function calculate some value and injects it into the main loop?

void blink2(int count){
}

Above states that blink2 takes a single parameter but does not return a value.
So the variable int count is the parameter and is calculated within the void blink2(int count) function?
*Ok, just thought of something on returning a value... It doesn't return a value to void blink2(int count) or it's own function?

int blink3(int period){
}

The above is the 3rd form.
Any help understanding the forms of the above functions a little better would be much appreciated.

I know I had a few more questions from my notes. I'll try and add them to this post asap.

t

Say you want a function to calculate the volume of a cube.

int mycube(int h, int w, int l){
int v = h * w * l;
return v;
}

You call this with:

volume = mycube(1,2,3);
or  vol = mycube(x,y,z);

or whatever combination of the 3 sizes you need the volume calculated for (as long as the result doesn't exceed the capacity of an int.) .
Using the form:

void mycube2(int h, int w, int l){
k = h * w * l;
}

needs k to be declared as a global variable and is called with:mycube2(3,2,9);or whatever combination of the 3 sizes you need the volume calculated for.
Some functions don't need parameters or to return a value, such as your moveLeft() function.

Henry_Best:
Say you want a function to calculate the volume of a cube.

int mycube(int h, int w, int l){

int v = h * w * l;
return v;
}



You call this with:

volume = mycube(1,2,3);
or  vol = mycube(x,y,z);


or whatever combination of the 3 sizes you need the volume calculated for (as long as the result doesn't exceed the capacity of an int.) .
Using the form:

void mycube2(int h, int w, int l){
k = h * w * l;
}


needs k to be declared as a global variable and is called with:`mycube2(3,2,9);`or whatever combination of the 3 sizes you need the volume calculated for.
Some functions don't need parameters or to return a value, such as your moveLeft() function.

Thanks for the reply.
(let me know if I'm wrong below)
So the function in the first code box has 3 parameters: h, w and l, which are all integers.

int mycube(int h, int w, int l){
int v = h * w * l;
return v;
}

In the second code box (which would go in the main loop), you can assign values to those 3 parameters and/or use them as variables.

void loop(){
//...
//...
volume = mycube(1,2,3);
// or  vol = mycube(x,y,z);
}

When those 3 parameters get assigned some integer value, those values go to the function in the first code box to calculate the volume (v)

int mycube(int h, int w, int l){
int v = h * w * l;
return v;
}

With:

return v;

Sends the calculated volume to the main loop.
So this would be an example of the 3rd form of a function that I posted above. Takes the 3 parameters and returns a value (which is the volume?).

t

thomas3120:
Thanks for the reply.
(let me know if I'm wrong below)

I think you've got it!

Now you have got that you might wonder if you can return more than one value from a function ......

UKHeliBob:
Now you have got that you might wonder if you can return more than one value from a function ......

I looked in my C++ book as well as some googling. There seems to be a couple of ways, maybe more.
Using pointers to, I believe, redirect the variable back(or return to that function?). I haven't reached that part in my C++ book yet but I skimmed over a few things which kind of made sense.
I think the other one was something about vectors?

When you posted this, I immediately thought to put an array in the function but from what I've read, it won't work.

I was also wondering is the return only one value set in stone in C++? The group of people who work on the language, update it, etc..., can they change the limit of variables or values returned?

t

I was also wondering is the return only one value set in stone in C++?

Yes. The return mechanism causes the single returned value to be popped off the stack.

The group of people who work on the language, update it, etc..., can they change the limit of variables or values returned?

Not without a LOT of work. And, why? There are ways to return a pointer to boatloads of data or to use reference variables that both the caller and called function understand. If the language supported returning multiple values, you'd need to rewrite every compiler and every application ever written to return first the number of values then the values.

Next, you'd expect to be able to return different types of values. It's that path that caused the designers to limit the return mechanism to ONE value of a known type.

PaulS:

I was also wondering is the return only one value set in stone in C++?

Yes. The return mechanism causes the single returned value to be popped off the stack.

The group of people who work on the language, update it, etc..., can they change the limit of variables or values returned?

Not without a LOT of work. And, why? There are ways to return a pointer to boatloads of data or to use reference variables that both the caller and called function understand. If the language supported returning multiple values, you'd need to rewrite every compiler and every application ever written to return first the number of values then the values.

Next, you'd expect to be able to return different types of values. It's that path that caused the designers to limit the return mechanism to ONE value of a known type.

Hey Paul, it's good to hear from you :slight_smile:

Thomas, I'm also working a robot with my young sons that behaves similarly to what you have described. I'd be happy to offer a few coding ideas for your robot.

Can you describe what kind of motor controller you are using. This will set the requirements for what arguments you will need to pass your forward(), backward() functions.