Floating Point Math

Hello,

I was recently working with the Vex Quadrature Encoders and I was trying to solve for distance using the Circumfrence of the wheel times the number of revolutions.
However this did not work too well considering that the two float type variables do not want to print out in the terminal window. I do not see a %f directive for printing float variables and after testing every directive and looking through the forum I came to the conclusion that printing float variables cannot be done… However I would still like the same accuracy (2-6 decimal places) instead of rounding out to the nearest one. If float integer math is being done in the background and the only restriction is terminal window printing of the float result, then that would be good. If not, can accurate floating point processing and computation be done in EasyC Pro?

I also have a question in regards to shift bit registers. For my purposes, I want a variable to be set to zero when a program starts, and after each loop the variable updates to the value that was calculated inside the loop… This off course brings in the shift register bit part. I was looking through some articles about shift bit registers in C but I didn’t fully comprehend them (something about << and >>??) Can you provide me with a code that starts with a variable set to zero and then counts up by ones (up to 10) every 500ms and the new calculated numbers (1…2…3…4…5…etc.) replace the previous numbers stored in the variable? (shifting the numbers in)

Thanks

Technic-R-C

Code below:

#include "Main.h"

void OperatorControl ( unsigned long ulTime )
{
      int EncoderTimer; 
      int YAxisDrive; 
      int XAxisDrive; 
      int RF = 2; // drive motor
      int RB = 4; // drive motor
      int LF = 1; // drive motor
      int LB = 3; // drive motor
      int DifferenceEncoders; 
      float Circumfrence = 12.566370; 
      float revolutions; 
      float distance; 
      int TerminalWindowTimer; 

      StartTimer ( 1 ) ;
      StartTimer ( 2 ) ;
      PresetTimer ( 1 , 0 ) ;
      PresetTimer ( 2 , 0 ) ;
      while ( 1 )
      {
            YAxisDrive = GetRxInput ( 1 , 2 ) ;
            XAxisDrive = GetRxInput ( 1 , 1 ) ;
            Arcade4 ( 1 , 2 , 1 , LF , RF , LB , RB , 1 , 1 , 1 , 1 ) ;
            EncoderTimer = GetTimer ( 1 ) ;
            TerminalWindowTimer = GetTimer ( 2 ) ;
            RightQuadEncoder = GetQuadEncoder ( 1 , 5 ) ;
            LeftQuadEncoder = GetQuadEncoder ( 2 , 6 ) ;
            // calculating distance traveled 
            if ( TerminalWindowTimer >= 1000 )
            {
                  revolutions = ((RightQuadEncoder + LeftQuadEncoder) / 2) / 90;
                  distance = Circumfrence * revolutions  ;
                  PrintToScreen ( "distance =%d\n" , distance ) ; //I tried all directives %f, etc...
                  //I do not need a rounded off answer, rather I need an accurate one!
                  PresetTimer ( 2 , 0 ) ;
            }

etc.......

The microchip compiler cannot PrintToScreen (printf) floating point math. The PIC inside the vex controller is only 8-bits so doing allot floating point math in a while loop that runs every 5ms will eat allot of processing time. In your example you use a 1 second timer so thats not much, but you need to aware of the issue.

To print the floating point numbers, you need to convert the floating point number into a int or a long depending on how many digits you want and then multiply it times 100, 1000, 10000, or 100000 depending on how many places you want to shift decimal.

This is a good example of just because you can’t see it doesn’t mean it’s not there. :slight_smile: