Cosine function
Cosine function
Hey there everyone,
I've started using the NXT brick with NXC language just a couple days ago, but I worked with the old NQC robot a bit too.
This time I'm working on a project with a friend of mine for school, and we're thinking big.
We're planning to make a robot who can detect 'trash', then pick it up and bring it back to its starting place (next to the trash can for example).
However, to be able to make the robot calculate it's way back (after moving about for a while), we need to use the cosine rule.
So here my question/problem is: how to use the cos() function?
Currently our calculation is like this:
dist_start = sqrt(a^2+b^2-2*a*b*cos(RADIANS_PER_DEGREE*turn));
all the variables (dist_start, a, b and turn) have been defined and used in calculations before.
When we try to compile the program it errors out:
line 19: Error: Undefined Identifier cos
line 19: Error: Too many arguments
line 19: Error: ")" expected
line 19: Error: ';' expected
When we cut out the cos() from the calculations, no errors are returned.
Any help on how to use the cos() function?
If you need any more information or simply need the whole file, I can send it over or upload it, just ask
All help is appreciated!
Greetings,
Rien
I've started using the NXT brick with NXC language just a couple days ago, but I worked with the old NQC robot a bit too.
This time I'm working on a project with a friend of mine for school, and we're thinking big.
We're planning to make a robot who can detect 'trash', then pick it up and bring it back to its starting place (next to the trash can for example).
However, to be able to make the robot calculate it's way back (after moving about for a while), we need to use the cosine rule.
So here my question/problem is: how to use the cos() function?
Currently our calculation is like this:
dist_start = sqrt(a^2+b^2-2*a*b*cos(RADIANS_PER_DEGREE*turn));
all the variables (dist_start, a, b and turn) have been defined and used in calculations before.
When we try to compile the program it errors out:
line 19: Error: Undefined Identifier cos
line 19: Error: Too many arguments
line 19: Error: ")" expected
line 19: Error: ';' expected
When we cut out the cos() from the calculations, no errors are returned.
Any help on how to use the cos() function?
If you need any more information or simply need the whole file, I can send it over or upload it, just ask
All help is appreciated!
Greetings,
Rien
Re: Cosine function
Make sure you did everything right here.
Make sure it's saved as a .nxc file, and your default language is NXC. Make sure you have NBC/NXC EF 1.31, and set compiler options to "Use Enhanced Firmware", "Firmware 1.2x", and optionally "Internal Compiler".
Make sure you're using the right types (float or int).
*And most importantly, make sure you're a human. Robots are not advanced enough to program yet, on Earth (AFAIK).
Make sure it's saved as a .nxc file, and your default language is NXC. Make sure you have NBC/NXC EF 1.31, and set compiler options to "Use Enhanced Firmware", "Firmware 1.2x", and optionally "Internal Compiler".
Code: Select all
dist_start = sqrt(pow(a, 2.0) + pow(b, 2.0) - 2*a*b*cosd(turn));
*And most importantly, make sure you're a human. Robots are not advanced enough to program yet, on Earth (AFAIK).
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Cosine function
I didn't actually know of that guide so I told my partner to follow it (he's got the brick at home), so I guess that would be a good start.
Besides that, doesn't ^ work for power calculations? I replaced it with the pow() function now anyways.
Secondly, why do you use cosd() instead of cos()? Was that a typo or is that really a different function?
I tried to attach the .nxc file but that extension is not allowed, if you still would need to see in to it I'll make a couple of screencaptures or I'll upload it as .txt
Thanks so far!
Besides that, doesn't ^ work for power calculations? I replaced it with the pow() function now anyways.
Secondly, why do you use cosd() instead of cos()? Was that a typo or is that really a different function?
I tried to attach the .nxc file but that extension is not allowed, if you still would need to see in to it I'll make a couple of screencaptures or I'll upload it as .txt
Thanks so far!
-
- Posts: 73
- Joined: 29 Sep 2010, 12:05
Re: Cosine function
Neither Standard C nor NXC support ^ as a exponential operator. You have to use the pow() library function.
The library function cosd() is a variant on cos() that interprets its argument as being in degrees. Saves you the trouble of having to explicitly convert from degrees to radians.
The library function cosd() is a variant on cos() that interprets its argument as being in degrees. Saves you the trouble of having to explicitly convert from degrees to radians.
Last edited by m-goldberg on 22 Dec 2010, 02:41, edited 1 time in total.
Regards, Morton
-
- Posts: 26
- Joined: 12 Oct 2010, 08:19
Re: Cosine function
I'm only a little familiar with C.
In NXC, for cos() or cosd() to work, must a header file be used?
Something like #include <math.h> before the task main.
If so, perhaps the include line or math.h is missing. Or maybe something is wrong with the "math.h" file.
Could that be the problem?
Dave
In NXC, for cos() or cosd() to work, must a header file be used?
Something like #include <math.h> before the task main.
If so, perhaps the include line or math.h is missing. Or maybe something is wrong with the "math.h" file.
Could that be the problem?
Dave
Re: Cosine function
You're correct that, he'd need math.h (or cmath), if he was using C/C++.dad-and-adam wrote:Something like #include <math.h> before the task main.
The only include files NXC has are NXTDefs.h, NXCDefs.h, and NBCCommon.h. They are automatically included in your program by default. (Unless you specify not to, in the compiler options.)
The ^ (caret) operator means to bitwise XOR the two operands that it's between.
Can you post the [entire] code?
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Cosine function
Thanks guys, the problem was solved.
The firmware was indeed the problem we had, but it's fixed now.
We're now going to continue developing this robot, let's hope things will work out.
(We're quite struggling with the ultralight sensor because it seems to measure the distance in a fairly wide range instead of one line straight forward.)
Thanks again!
The firmware was indeed the problem we had, but it's fixed now.
We're now going to continue developing this robot, let's hope things will work out.
(We're quite struggling with the ultralight sensor because it seems to measure the distance in a fairly wide range instead of one line straight forward.)
Thanks again!
-
- Posts: 26
- Joined: 12 Oct 2010, 08:19
Re: Cosine function
I'm assuming you meant ultrasonic sensor. An infrared distance sensor (such as sold by MindSensors) has a very narrow "field of view".(We're quite struggling with the ultralight sensor because it seems to measure the distance in a fairly wide range instead of one line straight forward.)
Their long distance infrared sensor measures to 140 cm (55 inches). I have one and it works great. It seems to have a near pin point field of view. Something to be aware of for that infrared sensor: if it doesn't have something within the 140 cm (55 inches) limit it can give unpredictable readings.
Dave
Re: Cosine function
Yeah I meant the ultrasonic sensor.
We're using the one that comes with the nxt brick on default.
When we tested it (using the 'view' option on the nxt menu) it returned values up to 6cm on close and mostly 140cm as its maximum range, though sometimes it got close to 2 meters as well.
It's just that when we position something about 20 cm away from the sensor and we place it like 7cm higher, it still finds it and returns 20cm, when we want it to look underneath it and return nothing (or another value if there is something on the same height as the sensor).
We're using the one that comes with the nxt brick on default.
When we tested it (using the 'view' option on the nxt menu) it returned values up to 6cm on close and mostly 140cm as its maximum range, though sometimes it got close to 2 meters as well.
It's just that when we position something about 20 cm away from the sensor and we place it like 7cm higher, it still finds it and returns 20cm, when we want it to look underneath it and return nothing (or another value if there is something on the same height as the sensor).
-
- Posts: 26
- Joined: 12 Oct 2010, 08:19
Re: Cosine function
Yes. the ultrasonic sensor "sees" the nearest object in a fairly wide field of view. One technique is the rotate the sensor to find directions where the distance value increases significantly. The obect is then assumed to be the average of those directions. -This only works is there are not any other objects around.
You might gain some insight to the ultrasonic sensor's abilitlies in an ultrasonic to infrared distance sensor comparison I just posted in the HardWare section.
Dave
You might gain some insight to the ultrasonic sensor's abilitlies in an ultrasonic to infrared distance sensor comparison I just posted in the HardWare section.
Dave
Who is online
Users browsing this forum: No registered users and 4 guests