Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Golomb Rulers on a Tiva C Launchpad

$
0
0

While waiting on some new MOSFETs for my project and since I have a working compiler I can actually use! Once again Thank You Energia!!!

 

I took one of my "just for fun" programs I like to play around with - calculating Golomb Rulers and did a quick port to the Energia project space. This is a NP-HARD problem. The larger the solution the more terms you must evaluate. I.e. if L = Length of ruler then the number of terms to calculate is (L*(L-1)/2.

 

I know my simple code isnt all that optimized - but Im happy with the performance of this little board. The future seams a bit brighter than is did a few days ago!

 

 

Just in case anyone else wants to play around with it.... I left all the ugly in... 

#define MAXBUCKETS 9
#define TICKSCOUNT (MAXBUCKETS*(MAXBUCKETS-1)/2)

int buckets[MAXBUCKETS];
int ticks[TICKSCOUNT];
int currentPosition;
int ticksCount;
int maxV;

inline int check( void )
{
  int i,j,k,t,g;
  t = 0;

 
  for(i=0;i<MAXBUCKETS;i++) if( buckets[i] >= maxV ) return( 0 );

  for(i=0;i<MAXBUCKETS;i++)
  {
    for(j=i+1;j<MAXBUCKETS;j++)
    {
      k = buckets[j] - buckets[i];
      //cout << k << " ";
      if( k < 0 )
      {
        currentPosition = j;
        return( 0 );
      }
      ticks[t] = k;
      t++;
      if( t > 1 )
      {
        for(g=0;g<t-1;g++)
        {
          if( ticks[g] == k )
          {
            //cout << " x\n";
            currentPosition = j;
            return(0);
          }
        }
      }
    }
  }
  //cout << "\n";
  return( 1 );
}




void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  analogWrite(RED_LED,   0); analogWrite(GREEN_LED, 0); analogWrite(BLUE_LED,  0);  
}

void loop()
{
  // put your main code here, to run repeatedly:
  int i,j,r,terminate=0;

  currentPosition = MAXBUCKETS-1;
  ticksCount = TICKSCOUNT-1;
  maxV = MAXBUCKETS * MAXBUCKETS;

  Serial.print("Starting with ");
  Serial.println(MAXBUCKETS);
  

  for(i=0;i<MAXBUCKETS;i++) buckets[i]=i;
//  for(i=0;i<MAXBUCKETS;i++) cout << buckets[i] << " ";
  //cout << "\n";


  while( 1 )
  {
    analogWrite(RED_LED,   1); analogWrite(GREEN_LED, 0); analogWrite(BLUE_LED,  0);    
    while( ! (r=check()) )
    {
      //cout << "check(): " << r << "\n";
      if( currentPosition == 0 ) break;
      if( !r )
      {
        //cout << "pos=" << currentPosition << "\n";

        buckets[MAXBUCKETS-1]++;

        for(j=MAXBUCKETS-1;j>0;j--)
        {
          if( buckets[j] >= maxV )
          {
             analogWrite(RED_LED,   0); analogWrite(GREEN_LED, 0); analogWrite(BLUE_LED,  0);
             analogWrite(RED_LED,   0); analogWrite(GREEN_LED, 0); analogWrite(BLUE_LED,  1);
             if( j == 1 ) { terminate=1; break; }
             buckets[j] = buckets[j-1] + 2;
             buckets[j-1]++;
             if( buckets[1] == maxV ) { terminate=1; break; }
          }
        }
        if( terminate ) break;
      }
      if( terminate ) break;
    }
    if( terminate ) break;
    if( currentPosition == 0 ) break;
    analogWrite(RED_LED,   0); analogWrite(GREEN_LED,1); analogWrite(BLUE_LED,  0);      
    Serial.print("Found: ");
    //cout << "Found: ";
    for(i=0;i<MAXBUCKETS;i++) 
    {
      Serial.print(buckets[i]);
      Serial.print(" ");
    }
 
    //cout << "\n";
    maxV = buckets[MAXBUCKETS-1] ;
    //buckets[MAXBUCKETS-1] = maxV;
    Serial.print("  maxV: ");
    Serial.println(maxV);
  }
  Serial.println("Done!");
}

Viewing all articles
Browse latest Browse all 2077

Trending Articles