ROBOTC.net forumshttp://www.robotc.net/forums/ crashing RobotC with cos and sinhttp://www.robotc.net/forums/viewtopic.php?f=1&t=441 Page 1 of 1

 Author: JPWingman [ Tue Mar 11, 2008 4:48 am ] Post subject: 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]

Author:  Ford Prefect [ Tue Mar 11, 2008 6:38 am ]
Post subject:

 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/xfloat 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);   }

Author:  Dick Swan [ Wed Mar 12, 2008 5:37 am ]
Post subject:  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

 Author: JPWingman [ Thu Mar 20, 2008 8:04 am ] Post subject: Thank you for the reply, I really appreciate it 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]

Author:  Ford Prefect [ Thu Mar 20, 2008 8:41 am ]
Post subject:

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 zugehen, wenn der Compiler dann immer noch meckert

 Author: JPWingman [ Thu Mar 20, 2008 11:02 am ] Post subject: 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

 Author: Ford Prefect [ Thu Mar 20, 2008 12:19 pm ] Post subject: prego kennst du eigentlich "das" deutsche Mindstorms-Forum? mit RobotC Unterforum? http://www.mindstormsforum.de/viewforum.php?f=47&sid=232f0c0c2974030a414fee3895b36480

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/