chipKIT® Development Platform

Inspired by Arduino™

Really odd thing with a loop, an if, and a trig function

Created Wed, 20 Nov 2013 19:28:45 +0000 by jperk51


jperk51

Wed, 20 Nov 2013 19:28:45 +0000

here is my code:

void loop() {
  float rps = 0;
  float old_rps = 0;
  float launch_angle = 0;
  float target_distance = 30;
  float pi = 3.14;
  int system_delay = 0;
  int static_checker = 0;
  float old_time = 0;
  float rev_time = 200;
  float mil_per_sec = 1000;
  float feet_per_sec = 0;
  float flight_time = 2.0; //assumption
  float circum = 6.3;
  float horiz_speed = 0;
  int encoder_release = 0;
  boolean launched = false;
  int run_once = 0;
  int thing = 1;
  
  
  while(run_once == 0)
  {  
      while(static_checker < 5)
    {
      if(static_checker%2 == 0){
        rps = mil_per_sec/rev_time;
        Serial.print("RPS = ");
        Serial.println(rps);
      }
        static_checker++;
    }
    float RPS_hold = rps;
    Serial.print("RPS_hold = ");
    Serial.println(RPS_hold);
    //change rps to feet per sec
    Serial.print("target_distance = ");
    Serial.println(target_distance);
    Serial.println("Calculations then launch"); 
    feet_per_sec = circum * RPS_hold;
    Serial.print("feet_per_sec = ");
    Serial.println(feet_per_sec);

    //calculate launch angle for rps and target distance and flight time
    horiz_speed = target_distance / flight_time;
    Serial.print("h_speed = ");
    Serial.println(horiz_speed);
    float hold = horiz_speed/feet_per_sec;
    Serial.print("Hold = ");
    Serial.println(hold);
    launch_angle = acos(hold);
    Serial.print("launch_angle = ");
    Serial.println(launch_angle);

    //convert angle into encoder number
    encoder_release = (((launch_angle / pi) * 512) + 256);
    Serial.print("encoder_release = ");
    Serial.println(encoder_release);


    run_once = 2;
  }
  while(run_once != 0){
  }

}

if I run this I get:

RPS = 5.00 RPS = 5.00 RPS = 5.00 RPS_hold = 5.00 target_distance = 30.00 Calculations then launch feet_per_sec = 31.50 h_speed = 15.00 Hold = 0.48 launch_angle = 4294967295.21474836472147483647 encoder_release = 2147483647

which is wrong

but if I change the static_checker loop to this:

while(static_checker < 5)
    {
      if(static_checker != 2){
        rps = mil_per_sec/rev_time;
        Serial.print("RPS = ");
        Serial.println(rps);
      }
        static_checker++;
    }

then I get this:

RPS = 5.00 RPS = 5.00 RPS = 5.00 RPS = 5.00 RPS_hold = 5.00 target_distance = 30.00 Calculations then launch feet_per_sec = 31.50 h_speed = 15.00 Hold = 0.48 launch_angle = 1.07 encoder_release = 431

which is right. I need the loop style of the first one to give me the right answer


majenko

Wed, 20 Nov 2013 19:47:45 +0000

Odd. I don't see the problem. What version of the IDE are you using?

I am using my UECIDE which uses the latest version of the chipKIT's MPIDE core and compiler, and the results I get with my Uno32 are:

RPS = 5.00
RPS = 5.00
RPS = 5.00
RPS_hold = 5.00
target_distance = 30.00
Calculations then launch
feet_per_sec = 31.50
h_speed = 15.00
Hold = 0.48
launch_angle = 1.07
encoder_release = 431

Lots of people use the 2012 version of MPIDE, which is full of bugs and shouldn't be used. Be sure you are running the latest version from [url]http://chipkit.s3.amazonaws.com/index.html[/url]


Jacob Christ

Fri, 28 Feb 2014 08:11:05 +0000

This feels like a -fno-short-double issue in older versions of MPIDE.

Jacob