EV3 'C' Maths

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
sparramc
Posts: 83
Joined: 29 Sep 2010, 08:44
Location: Launceston, Tasmania, Australia
Contact:

EV3 'C' Maths

Post by sparramc »

Hi all,

I'm having issues getting my little-grey-cells around the following Trig Function Errors from my EV3 'C' Code:

Code: Select all

holonomic.c: In function 'Holonomic':
holonomic.c:64: warning: incompatible implicit declaration of built-in function 'cos'
holonomic.c:68: warning: incompatible implicit declaration of built-in function 'sin'
C:\Users\sparramc\AppData\Local\Temp\cceZc4vx.o: In function `Holonomic':
holonomic.c:(.text+0xd8): undefined reference to `cos'
holonomic.c:(.text+0x11c): undefined reference to `sin'
holonomic.c:(.text+0x160): undefined reference to `sin'
collect2: ld returned 1 exit status
make: *** [holonomic] Error 1
The code I'm playing around with is as follows:

Code: Select all

#include <stdio.h>
#include <unistd.h>

#include "I:\BricxCC\API\ev3_lcd.h"
#include "I:\BricxCC\API\ev3_timer.h"
#include "I:\BricxCC\API\ev3_command.h"
#include "I:\BricxCC\API\ev3_output.h"

//=================================================//
// This demo program is to demonstrate how to move //
// a robot fitted with 3 Holonomic wheels to move  //
// in a specified direction.                       //
//=================================================//

// This routine is to get an angle using the EV3
// controller buttons for demo use only.

int GetAngle (void)

{ int angle;

  TextOut(0, LCD_LINE1, "Right = increase");

  TextOut(0, LCD_LINE2, "Left = decrease");

  TextOut(0, LCD_LINE4, "Angle = ");

  angle=0;

  do

  { if ((BUTTON_ID_RIGHT) != 0)

      angle+=2;

    if ((BUTTON_ID_LEFT) != 0)

      angle-=2;

    TextOut(50, LCD_LINE4, "     "); // clears displayed number

    NumOut(50, LCD_LINE4, angle);

    Wait(300);

  } while ((BUTTON_ID_ENTER) != 0);

  return angle;

}


// -------------------------------

// Works on 3 Holonomic wheels

// using 3 EV3 motors at 60 degrees.

void Holonomic(int degree, int power)

{ int angle, powerA, powerB, powerC, max;

  powerA=cos(degree);

  angle=degree-30;

  powerB=sin(angle);

  angle=degree+30;

  powerC=-sin(angle);

  max=abs(powerA);

  if (max<abs(powerB))

    max=abs(powerB);

  if (max<abs(powerC))

    max=abs(powerC);

  powerA=powerA*power/max;

  powerB=powerB*power/max;

  powerC=powerC*power/max;

  SetPower(OUT_A,powerA);

  SetPower(OUT_B,powerB);

  SetPower(OUT_C,powerC);

}


int main()

{ int angle;

  angle=GetAngle();

  Holonomic(angle,100);

  Wait(3000);
}
Any help will be greatly accepted?
regards

Ray McNamara

www.rjmcnamara.com

'The Universe Consists of 1% Hydrogen, & the Rest is Ignorance!'
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 'C' Maths

Post by HaWe »

did you
#include <math.h>
?

http://www.cplusplus.com/reference/cmath/sin/

I'm curious if TextOut will work though...
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: EV3 'C' Maths

Post by mightor »

Make sure you link with -lm :)

= Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
sparramc
Posts: 83
Joined: 29 Sep 2010, 08:44
Location: Launceston, Tasmania, Australia
Contact:

Re: EV3 'C' Maths

Post by sparramc »

mightor wrote:Make sure you link with -lm :)

= Xander
Hi Xander,

Remember I'm somewhat thicker than most.....

I've had linked with 'Project Manager' :

I:\BricxCC\API\ev3_lcd.h
I:\BricxCC\API\ev3_timer.h
I:\BricxCC\API\ev3_command.h
I:\BricxCC\API\ev3_output.h

and added '#include <math.h>', but still resulting with the same errors..
regards

Ray McNamara

www.rjmcnamara.com

'The Universe Consists of 1% Hydrogen, & the Rest is Ignorance!'
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 'C' Maths

Post by HaWe »

i guess what Xander meant was:
link in the make file list!
don't link a header file (.h) by the project manager, the PM is just for .c files which have to be linked together to an object file (.o).

John once pointed out sth like this for linking with pthread, maybe this issue is sort of this:
https://sourceforge.net/apps/phpbb/mind ... 522#p17519
so maybe just add -lm instead or behind -lpthread:

Image

about the display and buttons we discussed some more issues in the same topic a few more pages down under ;):
https://sourceforge.net/apps/phpbb/mind ... =90#p17549
Last edited by HaWe on 06 Dec 2013, 19:10, edited 3 times in total.
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

Re: EV3 'C' Maths

Post by lvoc »

I am not sure why you get this error. I am using sine and cosine functions in my code and I have not to specifically the the math libraries. In any case, if you want to add this flag, you would have to do it under "Preferences" in the GCC linker settings. Something similar to what is shown at:
http://www.robotnav.com/wp-content/uplo ... mpiler.png
Instead of adding -static, add -lm.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 'C' Maths

Post by HaWe »

not sure if Lauro means sth different than me (just added the screenshot above), but to be honest:
most of it is incomprehensible to me anyway.
sparramc
Posts: 83
Joined: 29 Sep 2010, 08:44
Location: Launceston, Tasmania, Australia
Contact:

Re: EV3 'C' Maths

Post by sparramc »

:P Thanx Guys for all your help!

I discovered that the 'Makefile Template' read:

Code: Select all

LDFLAGS=-Wl,-R/media/card/lib
Adding '-lm' to the end of the LDFLAGS Statement:

Code: Select all

LDFLAGS=-Wl,-R/media/card/lib -lm
Solved my problems...

But I have to still use:

Code: Select all

#include <math.h>
regards

Ray McNamara

www.rjmcnamara.com

'The Universe Consists of 1% Hydrogen, & the Rest is Ignorance!'
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 'C' Maths

Post by HaWe »

you're welcome!
yes indeed, it's the same issue about multitasking:
if you #include <pthread> you'd also have to link to it in the make file list by -lpthread additionally.

I'm really curious about if your whole code now compiles without any error msg and if it's now working over all completely correctly as expected :?:
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: EV3 'C' Maths

Post by mightor »

The #include part is for the (pre) compiler and the -l part is for the linker. The header file simply contains the prototypes for the functions and that sort of thing but the actual stuff you want to link in, is contained within the .so (library) file. You may want to take a look at the compilation and linking process here:
http://stackoverflow.com/questions/6264 ... ocess-work

= Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests