Page 1 of 1

EV3 'C' Maths

Posted: 05 Dec 2013, 02:09
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?

Re: EV3 'C' Maths

Posted: 05 Dec 2013, 08:19
by HaWe
did you
#include <math.h>
?

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

I'm curious if TextOut will work though...

Re: EV3 'C' Maths

Posted: 05 Dec 2013, 18:55
by mightor
Make sure you link with -lm :)

= Xander

Re: EV3 'C' Maths

Posted: 06 Dec 2013, 02:00
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..

Re: EV3 'C' Maths

Posted: 06 Dec 2013, 07:46
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

Re: EV3 'C' Maths

Posted: 06 Dec 2013, 17:19
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.

Re: EV3 'C' Maths

Posted: 06 Dec 2013, 19:07
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.

Re: EV3 'C' Maths

Posted: 07 Dec 2013, 03:38
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>

Re: EV3 'C' Maths

Posted: 07 Dec 2013, 09:20
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 :?:

Re: EV3 'C' Maths

Posted: 09 Dec 2013, 07:30
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