ROBOTC.net forums
http://robotc.net/forums/

Odd glitch?
http://robotc.net/forums/viewtopic.php?f=1&t=6346
Page 1 of 1

Author:  NeXT-Generation [ Mon Jul 22, 2013 9:00 pm ]
Post subject:  Odd glitch?

So in my continued endeavor to produce a working navigating robot in a limited timeframe, I have hit upon a bug in my code. I'm using motor encoders to supplement my GPS readings, but there's an odd problem. When I initialize the coordinates to the GPS position, the coordinates aren't moving up with the encoders. However, when I leave the coordinates at their initial value of 0, it increments them correctly. I have no idea why, so I'd appreciate a look at my code to see what I messed up. :oops:

Code:
#pragma config(Sensor, S1,     DGPS,           sensorI2CCustom)
#pragma config(Sensor, S4,     HTMC,           sensorI2CCustom)
#pragma config(Motor,  motorA,           ,             tmotorNXT, openLoop, reversed, encoder)
#pragma config(Motor,  motorB,           ,             tmotorNXT, openLoop, encoder)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//20 inches of movement per motor rotation

#include "C:\Program Files (x86)\Robomatter Inc\ROBOTC Development Environment\Sample Programs\NXT\3rd Party Sensor Drivers\drivers\dexterind-gps.h"
#include "C:\Program Files (x86)\Robomatter Inc\ROBOTC Development Environment\Sample Programs\NXT\3rd Party Sensor Drivers\drivers\hitechnic-compass.h"

//Test Latitude = 42.000389
//Test Longitude = -88.038040
//Longitude = x axis
//Latitude = y axis
//Smallest unit that GPS can measure equals roughly 5.282321853711464 inches.

task main()
{
   float latitude = 0;
   float Longitude = 0;

   int heading;
   wait1Msec(1000);

   while(nNxtButtonPressed != kEnterButton)
   {
      nxtDisplayCenteredTextLine(3, "%f", (float)DGPSreadLatitude(DGPS) / 1000000);
      nxtDisplayCenteredTextLine(4, "%f", (float)DGPSreadLongitude(DGPS) / 1000000);
      wait1Msec(500);
   }

   //float TarLong = (float)DGPSreadLongitude(DGPS) / 1000000;
   //float TarLat = (float)DGPSreadLatitude(DGPS) / 1000000;

   float TarLong = -88.038040;
   float TarLat = 42.000389;

   DGPSsetDestination(DGPS, TarLat, TarLong);

   time1[T1] = 0;

   nMotorEncoder[motorA] = 0;
   nMotorEncoder[motorB] = 0;
   nMotorEncoder[motorC] = 0;
   
   wait1Msec(1000);
   
   Longitude = (float)DGPSreadLongitude(DGPS) / 1000000;
   latitude = (float)DGPSreadLatitude(DGPS) / 1000000;

   while(true)
   {

      /*if(time1[T1] > 500)
      {
         Longitude = Longitude - (Longitude - ((float)DGPSreadLongitude(DGPS) / 1000000));
         latitude = (float)DGPSreadLatitude(DGPS) / 1000000;
         time1[T1] = 0;
      }*/

      if(Longitude > TarLong + 0.00001) HTMCsetTarget(HTMC, 270);
      else if(Longitude < TarLong - 0.00001) HTMCsetTarget(HTMC, 90);
      else if(Longitude > TarLong - 0.00001 && Longitude < TarLong + 0.00001) StopAllTasks();
      //if(Latitude > TarLat + 0.000025) HTMCsetTarget(HTMC, 180);
      //else if(Latitude < TarLat - 0.000025) HTMCsetTarget(HTMC, 0);
      //else if(Longitude > TarLong - 0.00001 && Longitude < TarLong + 0.00001  && Latitude > TarLat - 0.00001 && Latitude < TarLat + 0.00001) StopAllTasks();

      float compass = HTMCreadRelativeHeading(HTMC);

      nxtDisplayCenteredTextLine(0, "TLon: %f", TarLong);
      nxtDisplayCenteredTextLine(1, "TLat: %f", TarLat);
      nxtDisplayCenteredTextLine(2, "Lon: %f", Longitude);
      nxtDisplayCenteredTextLine(3, "Lat: %f", latitude);
      nxtDisplayCenteredTextLine(4, "Rel Comp: %d", compass);
      nxtDisplayCenteredTextLine(5, "Act Comp: %d", HTMCreadHeading(HTMC));
      nxtDisplayCenteredTextLine(6, "En: %d", nMotorEncoder[motorA]);

      float bPwr = 100 - (compass * 2);
      float cPwr = 100 + (compass * 2);

      if(bPwr < 50) motor[motorB] = 50;
      else motor[motorB] = bPwr;
      if(cPwr < 50) motor[motorC] = 50;
      else motor[motorC] = cPwr;
      motor[motorA] = (motorPWMLevel[motorB] + motorPWMLevel[motorC]) / 2;

      //if((nMotorEncoder[motorA] + nMotorEncoder[motorB] + nMotorEncoder[motorC]) / 3 = 95)
      if(nMotorEncoder[motorA] >= 95)
      {
         if(compass > 10 && compass < 170) Longitude = Longitude + (float)0.000001;
         else Longitude = Longitude - (float)0.000001;

         nMotorEncoder[motorA] = 0;
      }
   }
}


It's in early stages right now, so it's pretty messy.

Author:  NeXT-Generation [ Tue Jul 23, 2013 9:02 pm ]
Post subject:  Re: Odd glitch?

It seems that it will not increment the value unless it's at zero before the loop starts... Very odd, I cannot seem to find a reason for this, or a way to fix it...

Author:  maths222 [ Wed Jul 24, 2013 8:38 am ]
Post subject:  Re: Odd glitch?

Take a look here: http://www.robotc.net/wiki/Data_Types. When you have your lat/long as partially integers, you cannot subtract such a small number and have the result fit in a float, so you end up with the same number you started with.

Author:  NeXT-Generation [ Wed Jul 24, 2013 8:50 am ]
Post subject:  Re: Odd glitch?

Okay, I still don't really see why it doesn't work, but I guess I'll just figure out another way.

Author:  Ernest3.14 [ Wed Jul 24, 2013 7:53 pm ]
Post subject:  Re: Odd glitch?

I think what maths222 is trying to say is, your float isn't that accurate (you should use a double but RobotC doesn't have one), so when you cast it back and forth, nothing happens.

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