In another thread, the user required accuracy and got mired down in the implementation of floating point arithmetic for a processor which has no native floating point processing ability.
On the VEX controller, FP operations can take many hundreds (or many thousands) of machine cycles to execute.
I’d like to show everyone how proper integer arithmetic can be very useful…
Original (floating point) code:
#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.......
An integer math alternative:
#include "Main.h"
// Distance per tick is 100 * PI (22/7) * diameter of wheel (4) / counts per revolution (2 * 90)
// This should be accuarate to 0.04% or about +/-0.06" in 12 feet.
// The result is in 100th of an inch (2000 --> 20 inches)
// If the result is stored in a signed int, the max value is 327 inches before rollover.
#define DISTANCE_PER_TICK_NUMERATOR` 440
#define DISTANCE_PER_TICK_DENOMINATOR 63
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;
int distance;
int encoder_ticks;
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 )
{
encoder_ticks = RightQuadEncoder + LeftQuadEncoder;
distance = (encoder_ticks * DISTANCE_PER_TICK_NUMERATOR) / DISTANCE_PER_TICK_DENOMINATOR;
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.......
Lesson over.
Regards,
Mike