Go Down

Topic: void function that returns an equation? or anything? (Read 195 times) previous topic - next topic

golightlyj

So for my next code for school we are required to use a function to set rpm of a servo using an equation . We were specifically instructed to use void as the function specifier yet we want it to return something. My professors explanation was that we are asking it to return an equation not a value. This makes no sense to me. As I understand it the void tells the function not to return anything?

Any thoughts?

Coding Badly

#1
Oct 17, 2014, 03:13 am Last Edit: Oct 17, 2014, 03:16 am by Coding Badly Reason: 1
This makes no sense to me.


You are not alone.

Quote
As I understand it the void tells the function not to return anything?


Correct.

Quote
Any thoughts?


There are two other ways to get data out of a function.  Maybe the professor wants you to explore those.

(At moments like these I am very grateful to have had Dr. Whitehead as my professor for Structured Programming.)

golightlyj

#2
Oct 17, 2014, 03:38 am Last Edit: Oct 17, 2014, 04:02 am by golightlyj Reason: 1
Quote
There are two other ways to get data out of a function.


This raises more questions than it answers. Any chance you could tell me where I might do some reading on the matter?

*UPDATE*
So I've been reading. Am I to understand that I can just use the function to save a value to memory without technically returning data? If so, what advantage does this have vs just returning data?

JimboZA

Quote
Am I to understand that I can just use the function to save a value to memory without technically returning data?


That would be my take, using a global variable and sticking the answer into that. But I have no idea what this means:

Quote
we are asking it to return an equation not a value.


I think the only advantage of assigning the answer to a variable over a return, is that you can do as many calcs as you like and as many assignments to variables in a function, but it may only return one.

As to this:

Quote
set rpm of a servo


..... the purists will frown. A servo doesn't have rpm, it goes to a certain position. If the servo is modded so that it rotates, and the pulse you send it is now the speed, is it still a servo? At least one member here uses the phrase "continuous rotation ex-servo".

That raises another question: for one of those (ex-) servos, there is as far as I know, no specific correlation between the pulse width and the rotation speed. I suspect the pulse vs speed will depend on the mechanics of the servo, and may even vary across instances of the same model. So how do you know what pulse width to send it for a required rpm?
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Coding Badly


benjaminf

#5
Oct 17, 2014, 12:52 pm Last Edit: Oct 17, 2014, 01:00 pm by benjaminf Reason: 1
Quote
. We were specifically instructed to use void as the function specifier yet we want it to return something.


As stated above, use references.

Code: [Select]

void something( int &whatYouNeedToReturn) {
   whatYouNeedToReturn = 999;
}

int theReturn;
something(theReturn);

// theReturn now equals 999


Quote
My professors explanation was that we are asking it to return an equation not a value

Sounds like he wants you to return a function pointer.

AWOL

Quote
As I understand it the void tells the function not to return anything?

The "void" tells the function not to return a value via the function name.
There are other ways of getting values out of a function.
"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.

golightlyj

#7
Oct 17, 2014, 01:39 pm Last Edit: Oct 17, 2014, 01:42 pm by golightlyj Reason: 1
Quote


..... the purists will frown. A servo doesn't have rpm, it goes to a certain position. If the servo is modded so that it rotates, and the pulse you send it is now the speed, is it still a servo? At least one member here uses the phrase "continuous rotation ex-servo".

That raises another question: for one of those (ex-) servos, there is as far as I know, no specific correlation between the pulse width and the rotation speed. I suspect the pulse vs speed will depend on the mechanics of the servo, and may even vary across instances of the same model. So how do you know what pulse width to send it for a required rpm?


To begin, my take on the definition of a servo is: a motor that uses an signal to control speed. The servo that only goes to a certain position and back is a specialized type of servo?

As for the RPM, we took several measurements of the RPM at different pulse width values, put them into excel and derived an equation that allows us to input RPM and the program changes that into the appropriate pulse width value based on our equation.

As for pointers, we have not gotten that far yet. I think that will be this weeks topic.

Thanks for the help guys! I will check out those websites Coding Badly

wildbill

Quote
As for the RPM, we took several measurements of the RPM at different pulse width values, put them into excel and derived an equation that allows us to input RPM and the program changes that into the appropriate pulse width value based on our equation.


That makes it quite sensible to have a function SetRPM that takes RPM as a parameter and instructs a servo accordingly. It would be reasonable that such a function return nothing, as your professor suggested. What do you want to return?

In reality, SetRPM would probably take another parameter specifying which servo to operate on too.

JimboZA

Quote
The servo that only goes to a certain position and back is a specialized type of servo?


No that is "A" servo; the other kind is a converted "ex" servo.

But be that as it may, if you have a rpm vs pulse width thingy, that's cool. I'd be interested to see the curve. I've never used an "ex" servo, but always assumed the curve was kind of linear. How will you know, though, that it actually got to the right speed? You took measurements under certain load conditions, and under heavier conditions it will take longer to get to speed, and indeed may never get there.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

golightlyj

#10
Oct 17, 2014, 04:03 pm Last Edit: Oct 17, 2014, 04:12 pm by golightlyj Reason: 1
To wildbill

Quote

That makes it quite sensible to have a function SetRPM that takes RPM as a parameter and instructs a servo accordingly. It would be reasonable that such a function return nothing, as your professor suggested. What do you want to return?


I think I might be starting to grasp this. So, I'm not trying to return a value, I am just trying to change a parameter?


Quote

In reality, SetRPM would probably take another parameter specifying which servo to operate on too.


Yes, 2 independent servos

To JimboZA
Quote

if you have a rpm vs pulse width thingy, that's cool. I'd be interested to see the curve. I've never used an "ex" servo, but always assumed the curve was kind of linear.How will you know, though, that it actually got to the right speed? You took measurements under certain load conditions, and under heavier conditions it will take longer to get to speed, and indeed may never get there.


The line is linear. For whatever reason I can't seem to paste the graph. As for load conditions and whatnot, this is a beginner level class, we are just programming parallax robots for now. Our semester project is to design and build our own that has to follow a line on the floor.

JimboZA

You need to work on your tags  8)... the opening on has no / at either end.... easiest to select the text and hit the quote icon above the  :*
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

JimboZA

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

golightlyj

I have read that reference already jimboZA. The problem reading does not mean understanding all of what is has to say.

Go Up