View unanswered posts | View active topics It is currently Sat Dec 20, 2014 10:59 pm






Reply to topic  [ 7 posts ] 
crashing RobotC with cos and sin 
Author Message
Rookie

Joined: Mon Mar 10, 2008 5:03 pm
Posts: 5
Post crashing RobotC with cos and sin
Hallo,
I have had some problems with RobotC lately. I tried to use some sine, cos and tangent calculations in a funktion, but every time I to download the file to the NXT RobotC closed itself. The compiler seems to work althought it gives me a "warning: Unreferenced procedure" at the beginning of the funktion. after this mistake oocured the first time, the compiler still finds no error, but the entire programm shuts down without an error every time, even when I removed the part that cuses the warning messages.

The problem is that I don't even know exacly, if I used the sine and cosine correctly, so it might be a help, if somebody could tell me.

[void Werteberechnung()
{
uual = DegreesToRadians(nMotorEncoder[motorB]);
uuar = DegreesToRadians(nMotorencoder[motorB]);

ual = (23 * (cos(uual)) / (sin(uual))) - 3.05;
uar = (23 * (cos(uuar)) / (sin(uuar))) + 3.05;

al = atan(ual);
ar = atan(uar);

}



al and ar are float variables and will be given to the other 2 motors

If you need more information of the code to be able to help me, you just have to ask for it. Thanks already for the effort.[/code]


Tue Mar 11, 2008 4:48 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
JPWingman wrote:

[void Werteberechnung()


is this the mistake? : [void

other possible errors:

sin(uual) must always be<>0 (no devision by zero!)
you have to handle the exceptions when sin(uual)==0!

Code:
if (sin(uual)!=0)

   ual = (23 * (cos(uual)) / (sin(uual))) - 3.05;
   uar = (23 * (cos(uuar)) / (sin(uuar))) + 3.05;
}
else // else what?
{...}



but cos/sin=cotangens=1/tan,
why don't you use that?
Code:
ual=(23*cot(uual))-3.05;


BTW: be aware that atan itself has undefined points (German: Unstetigkeitsstellen). see my solution:
Code:
// ArcusTangens mit Sonderfaellen
// x=Ankathete y=Gegenkathete Tangens=y/x

float atan2(float x, float y)
{
  float phi; //phi=radians

   if (x>0) {phi=atan(y/x);}
   else
   if ((x<0)&&(y>=0))  {phi=PI+atan(y/x);}
   else
   if ((x<0)&&(y<0))   {phi=-PI+atan(y/x);}
   else
   if ((x==0)&&(y>0))  {phi=PI/2;}
   else
   if ((x==0)&&(y<0))  {phi=-PI/2;}
   else
   if ((x==0)&&(y==0)) {phi=0;}

   return phi;
}


float atan2Degrees(float x, float y)
{
  float phi;  //phi=radians

   if (x>0) {phi=atan(y/x);}
   else
   if ((x<0)&&(y>=0))  {phi=PI+atan(y/x);}
   else
   if ((x<0)&&(y<0))   {phi=-PI+atan(y/x);}
   else
   if ((x==0)&&(y>0))  {phi=PI/2;}
   else
   if ((x==0)&&(y<0))  {phi=-PI/2;}
   else
   if ((x==0)&&(y==0)) {phi=0;}

   return radiansToDegrees(phi);
   
}

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Tue Mar 11, 2008 6:38 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: crashing RobotC with cos and sin
JPWingman wrote:
I have had some problems with RobotC lately. I tried to use some sine, cos and tangent calculations in a funktion, but every time I to download the file to the NXT RobotC closed itself.
ROBOTC should not "close itself" even if there were compile errors. If you send me the file (and not code snippet) that causes this I will debug and fix. Use dickswan@sbcglobal.net


Wed Mar 12, 2008 5:37 am
Profile
Rookie

Joined: Mon Mar 10, 2008 5:03 pm
Posts: 5
Post 
Thank you for the reply, I really appreciate it :D

the first mistake you pointed out is a posting mistake (sory),
" [void Werteberechnung() " just exists as " void Werteberechnung() " so it is not the big problem.

I also tried to avoid a devision by zero. I will give you some more code to give you some more information about it and how I tried it.

What i am trying to program is kind of a motorbike with 2 front wheels, like a car. When a car drives a kurve, the inner wheel has to have asteeper angle than the outer wheel. The angles should be calculated by the funktion, which I have problems with at the moment.

motorB is used as a remote control. I'd like to give the angle via motorB. The motors A and C should take the calculated angle they need to let the entire car drive a nice circle.



if ((nMotorEncoder[motorB]/3 > -82) && (-1 < nMotorEncoder[motorB]/3))
{
Werteberechnung_links();
Werteberechnung_rechts();
}
else
{
if ((1 < nMotorEncoder[motorB]/3) &&
(82 > nMotorEncoder[motorB]/3))
{
Werteberechnung_links();
Werteberechnung_rechts();
}

the code above is "supposed" to avoid a devision by 0 and also than the wheels can turn more than 180°.

the funktions Werteberechnung_links() and Werteberechnung_rechts() should calculate the values for the motors A and C

float Werteberechnung_links()
{
if ( nMotorEncoder[motorB] != 0)
{
uual = (PI/180) * nMotorEncoder[motorB];
ual = (23 * cot(uual)) - 3.05;
al = (180/PI) * atan(ual);
return al;
}
}

float Werteberechnung_rechts()
{
if ( nMotorEncoder[motorB] != 0)
{
uuar = (PI/180) * nMotorEncoder[motorB];
uar = (23 * cot(uuar)) + 3.05;
ar = (180/PI) * atan(uar);
return ar;
}
}




the remaining code works perfectly, including the one which let the motors turn at the end.

My problem now is that the message "Unreferenced procedure 'Werteberechnung_links' " still remains. It is the same with the other posted funktion " Werteberechnung_rechts() " and that the program itsself doesn't know " cot() " ( could it be that I have an older version of RobotC? ).

Already lots of thanks for any replies that come.
[/code]


Thu Mar 20, 2008 8:04 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
quick and dirty in German:

hast du beide Funktionen vor der aufrufenden Funktion definiert?
Falls nicht: schreib sie einfach nochmal ganz nach oben, aber ohne die {}, einfach mit Semikolon dahinter, also:
Code:
float Werteberechnung_links();
float Werteberechnung_rechts();
// (...)
// dann der Rest des Codes


Falls cot(x) unbekannt ist, schreib dafür einfach (1/tan(x)).
Dann darf der tan aber nicht gleich null werden (Nullstellen vorher ausschließen und als Sonderfälle definieren, wie bei meinem atan2!)

müsste doch mit dem Teufel :twisted: zugehen, wenn der Compiler dann immer noch meckert :wink:

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Mar 20, 2008 8:41 am
Profile
Rookie

Joined: Mon Mar 10, 2008 5:03 pm
Posts: 5
Post 
then a reply (dirty and quick in german as well)

Sieht so aus als Haettest du den Nagel auf den Kopf getroffen. Hab die Funktion jetzt einfach vor der Funktion definiert, in der sie dann angewendet werden.

Divisionen durch 0 gibt es nicht und das Programm wurde auch endlich auf den NXT gelanden.
(Tangens kennt er zwar nicht, aber da kann man sich ja noch mit dem sinus und Cosinus helfen)

Vielen Dank nochmal :D


Thu Mar 20, 2008 11:02 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
prego :D

kennst du eigentlich "das" deutsche Mindstorms-Forum?
mit RobotC Unterforum?

Image

http://www.mindstormsforum.de/viewforum.php?f=47&sid=232f0c0c2974030a414fee3895b36480

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Mar 20, 2008 12:19 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.