View unanswered posts | View active topics It is currently Sat Nov 22, 2014 11:36 pm






Reply to topic  [ 2 posts ] 
Major issue with variable space on PIC .5 
Author Message
Rookie

Joined: Wed Jan 23, 2013 3:03 am
Posts: 1
Post Major issue with variable space on PIC .5
I am playing around with a fairly basic piece of code and I have run into a serious problem. The 0.5 PIC with RobotC 3.5.4 is running out of global variable space it seems.

Here are the global variables I have assigned:

Code:
#pragma config(Sensor, in1,    IR,             sensorPotentiometer)
#pragma config(Sensor, in3,    rot_Lt,         sensorQuadEncoder, int5)
#pragma config(Sensor, in2,    rot_Rt,         sensorQuadEncoder, int3)
#pragma config(Sensor, in4,    b_oscIRLim,          sensorTouch)
#pragma config(Motor,  port1,           m_IR,         tmotorNormal, openLoop)
#pragma config(Motor,  port7,           m_Rt,        tmotorNormal, openLoop)
#pragma config(Motor,  port8,           m_Lt,        tmotorNormal, openLoop, reversed)

int PID_SC = 112;//Speed Constant (accounts for wheel diameter and gearing)
int PID_AC = 5;//Angle Constant (accounts for wheel diameter and gearing)
int PID_sKP = 2; //P-Constant  (Acceleration)
int PID_sKI = 1;// Integral Constant Rate of compounding error correction
int PID_sKD =12;// Derivative Constant
int PID_hKP = 15; //P-Constant  (Acceleration)
int PID_hKI = 28;// Integral Constant Rate of compounding error correction
int PID_hKD =3;// Derivative Constant
int PID_TSpd = 0;//Target Speed/distance
int PID_THead = 0;//Target heading
int PID_MaxHAdj = 20; //max turn rate
bool PID_HAdj = false; //are we sending a relative heading adjustment?
bool PID_EMG = false; // signal an emergancy stop;
int PID_spd; // current speed in ~mm/s
int PID_hdg=0; //current heading rel. to origin
int PID_dist=0; //distance travelled
int Espd,Ehead;

const int OIR_SENSE[11] = {800,350,275,200,150,120,105,93,88,75,70};
const int OIR_TIMING[7] = {200, 40, 50, 60, 70, 20, 30};
int OIR_RAW[7] = {0,0,0,0,0,0,0};

//int CRUISE_Risk[5] 1<--when I add this last array it crashes
int OIR_CT1=0;
int OIR_CT1EP=0;




Basically when I add in the last array shown above (or any other further variables) the controller starts behaving erratically and throwing runtime errors.

There are no compile errors or other indications showing that the memory is full and I cannot find any documentation on the PIC limitations (I found out the hard way there is no floating point capability and am frustrated enough with that already... and now this).


Here's some output from the compiler regarding the assignment of global variables
Code:
**Debug**:Allocate global scope variable 'PID_SC' at static address 0. Pass 'BuildSyntax'. HWM 2
**Debug**:Allocate global scope variable 'PID_AC' at static address 2. Pass 'BuildSyntax'. HWM 4
**Debug**:Allocate global scope variable 'PID_sKP' at static address 4. Pass 'BuildSyntax'. HWM 6
**Debug**:Allocate global scope variable 'PID_sKI' at static address 6. Pass 'BuildSyntax'. HWM 8
**Debug**:Allocate global scope variable 'PID_sKD' at static address 8. Pass 'BuildSyntax'. HWM 10
**Debug**:Allocate global scope variable 'PID_hKP' at static address 10. Pass 'BuildSyntax'. HWM 12
**Debug**:Allocate global scope variable 'PID_hKI' at static address 12. Pass 'BuildSyntax'. HWM 14
**Debug**:Allocate global scope variable 'PID_hKD' at static address 14. Pass 'BuildSyntax'. HWM 16
**Debug**:Allocate global scope variable 'PID_TSpd' at static address 16. Pass 'BuildSyntax'. HWM 18
**Debug**:Allocate global scope variable 'PID_THead' at static address 18. Pass 'BuildSyntax'. HWM 20
**Debug**:Allocate global scope variable 'PID_MaxHAdj' at static address 20. Pass 'BuildSyntax'. HWM 22
**Debug**:Allocate global scope variable 'PID_HAdj' at static address 22. Pass 'BuildSyntax'. HWM 23
**Debug**:Allocate global scope variable 'PID_DAdj' at static address 23. Pass 'BuildSyntax'. HWM 24
**Info***:'PID_DAdj' is written but has no read references
**Debug**:Allocate global scope variable 'PID_EMG' at static address 24. Pass 'BuildSyntax'. HWM 25
**Debug**:Allocate global scope variable 'PID_spd' at static address 26. Pass 'BuildSyntax'. HWM 28
**Debug**:Allocate global scope variable 'PID_hdg' at static address 28. Pass 'BuildSyntax'. HWM 30
**Debug**:Allocate global scope variable 'PID_dist' at static address 30. Pass 'BuildSyntax'. HWM 32
**Debug**:Allocate global scope variable 'Espd' at static address 32. Pass 'BuildSyntax'. HWM 34
**Debug**:Allocate global scope variable 'Ehead' at static address 34. Pass 'BuildSyntax'. HWM 36
**Debug**:Allocate global scope variable 'OIR_SENSE' at static address 36. Pass 'BuildSyntax'. HWM 58
**Debug**:Allocate global scope variable 'OIR_TIMING' at static address 58. Pass 'BuildSyntax'. HWM 72
**Debug**:Allocate global scope variable 'OIR_RAW' at static address 72. Pass 'BuildSyntax'. HWM 86
**Debug**:Allocate global scope variable 'CRUISE_Risk' at static address 86. Pass 'BuildSyntax'. HWM 96
**Debug**:Allocate global scope variable 'OIR_CT1' at static address 96. Pass 'BuildSyntax'. HWM 98
**Info***:'OIR_CT1' is written but has no read references
**Debug**:Allocate global scope variable 'OIR_CT1EP' at static address 98. Pass 'BuildSyntax'. HWM 100
**Info***:'OIR_CT1EP' is written but has no read references



Rest of the code (Please ignore complete lack of style and good practice... i'm just trying to mess around in a hurry and learn the platform)

Code:
void STurn(int deg, int rate) //Smooth turn (right
{
   int k = 1;
   if(deg<0)
   {
      k=-1;
      deg*=-1;
   }
  for(int i = 0; i<deg; i++)
  {
     PID_THead+=k;
     wait1Msec(rate);
  }
     
}


task PM_2wd()
{

   int CurrHead, sIntegral, hIntegral, Derivative,Adj;
   int rHead = 0;
   int lHead = 0;
   int rSpd = 0;
   int lSpd = 0;
   CurrHead = 0;
   sIntegral = 0;
   hIntegral = 0;
   Espd = 0;
   
   ClearTimer(T2);
   while(true)
   {
      hogCPU();

      //Get Speed;
      PID_spd =  (SensorValue(rot_Rt) +  SensorValue(rot_Lt))/2;
      PID_spd = (PID_spd*PID_SC)/time10(T2);
      PID_spd/=10;
   
      PID_dist += PID_spd*time10(T2)/100;
      
      //Get Heading;
      PID_hdg += (SensorValue(rot_Lt) -  SensorValue(rot_Rt)) / PID_AC;
               
      
      //House Keeping
      ClearTimer(T2);
      SensorValue(rot_Lt) = 0;
      SensorValue(rot_Rt) = 0;
      
      
      //Speed Control
      Derivative = Espd;
      Espd = PID_TSpd - PID_spd;
      sIntegral += Espd;
      if(Espd == 0 || abs(Espd) > 40)
         sIntegral = 0;
      Derivative = Espd - Derivative;
      Adj = (PID_sKP*Espd)/10+(PID_sKI*sIntegral)/1000+(PID_sKD*Derivative)/100;
      rSpd += Adj;
      lSpd += Adj;   
   
      //Heading Control
      Derivative = Ehead;
      if(PID_HAdj) // consolidate a relative adjustment
      {
         PID_HAdj = false;
         PID_THead = PID_hdg + PID_THead;
      }
      Ehead =PID_THead - PID_hdg;
      hIntegral += Ehead;
      if(abs(Ehead) < 2 || abs(Ehead) >30)
         hIntegral = 0;
      Derivative = Ehead - Derivative;
   
      Adj = (PID_hKP*Ehead)/10+(PID_hKI*hIntegral)/100+(PID_hKD*Derivative)/100;
      
      if(Adj>PID_MaxHAdj)
         Adj = PID_MaxHAdj;
      else if (Adj < -PID_MaxHAdj)
         Adj = -PID_MaxHAdj;
         
      rHead = -Adj;
      lHead = Adj;
      motor[m_Rt] = rSpd+rHead;
      motor[m_Lt] = lSpd+lHead;
      
      if(PID_EMG == true)
      {
         motor[m_Rt] = 0;
         motor[m_Lt] = 0;
         rSpd = 0;
         lSpd = 0;
         PID_TSpd = 0;
         PID_EMG = false;
      }   
      
      releaseCPU();
         wait1Msec(50);
      
   }
}

int OIR_rawToDist(int raw)
{
   int i;
   for( i = 10; raw > OIR_SENSE[i]; i--)
      if(i<0)
         return 0;
   
   return i*10;
}


// *** *** *** Oscillating IR distance sensor


task oscIR()
{
   motor[m_IR] = 50;
   while(SensorValue(b_oscIRLim) != true)
      wait1Msec(10);
   motor[m_IR] = 0;
      
   while(true)
   {
      //ClearTimer(T1);
      motor[m_IR] = -35;
      wait1Msec(OIR_TIMING[0]);
      OIR_RAW[0] = SensorValue(IR);
      wait1Msec(OIR_TIMING[1]);
      OIR_RAW[1] = SensorValue(IR);
      wait1Msec(OIR_TIMING[2]);
      OIR_RAW[2] = SensorValue(IR);
      wait1Msec(OIR_TIMING[3]);
      OIR_RAW[3] = SensorValue(IR);
      wait1Msec(OIR_TIMING[4]);
      OIR_RAW[4] = SensorValue(IR);
      wait1Msec(OIR_TIMING[5]);
      OIR_RAW[5] = SensorValue(IR);
      wait1Msec(OIR_TIMING[6]);
      OIR_RAW[6] = SensorValue(IR);
      
      /* Calibration
      for(int i = 0; i<44; i++)
      {
         if(SensorValue(IR) > OIR_SENSE[2])
         {
            if( OIR_CT1EP < abs(OIR_CT1 - time1(T1)) && OIR_CT1EP != 0 )
               OIR_CT1EP = abs(OIR_CT1 - time1(T1));
            else if(OIR_CT1EP == 0 && OIR_CT1 != 0)
               OIR_CT1EP = abs(OIR_CT1 - time1(T1));
            OIR_CT1 = time1(T1);
         }
         wait1Msec(10);
      }
      */
      
      
      motor[m_IR] = 100;
      
      //Calculate distances;
      hogCPU();
      for(int i = 0; i<7; i++)
    OIR_RAW[i] = OIR_rawToDist(OIR_RAW[i]);
      releaseCPU();
      
      while(SensorValue(b_oscIRLim) != true)
      {
         
         wait1Msec(10);
      }
   }
}





task cruise()
{   
   int brdir,brval;
   //Calculate risk
   while(true)
   {
      hogCPU();
      for(int i = 0; i<5; i++)
      {
         if(i<2){
            CRUISE_Risk[i] = 100 - OIR_RAW[i] - OIR_RAW[i+1];
         }
         else if(i ==2){
            CRUISE_Risk[i] = 100 - OIR_RAW[i+1] - (OIR_RAW[i+2] + OIR_RAW[i])/2;
            if(OIR_RAW[i+1] < 20 || OIR_RAW[i] <30 || OIR_RAW[i+2] <30 )
               CRUISE_Risk[i] = 100;   
         }
         else {
            CRUISE_Risk[i] = 100 - OIR_RAW[i+1] - OIR_RAW[i+2];
         }
      }
      
      //Emergancy stop?
      if(CRUISE_Risk[2] == 100)
         PID_EMG = true;
      //Assess risk and adjust speed
      else if(CRUISE_Risk[2] > 50)
         PID_TSpd = 5;
      else if(CRUISE_Risk[2] >0)
         PID_TSpd = 7;
      else if(CRUISE_Risk[2] >-25)
         PID_TSpd = 8;
      else if(CRUISE_Risk[2] >-50)
         PID_TSpd = 9 ;
      else if(CRUISE_Risk[2] >-100)
         PID_TSpd = 10;
      else
         PID_TSpd = 12;

   //Adjust heading
      brdir=2;
      brval=CRUISE_Risk[2];
      
      for(int i = 0; i<5; i++)
      {
         if(CRUISE_Risk[i] < brval){
            brdir = i;
            brval = CRUISE_Risk[i];
         }
      }
      
      switch (brdir)
     {
        case 0: //Bridge Closed

           PID_THead = -80;
           PID_HAdj = true;
            break;
         case 1: //Bridge Closed

           PID_THead = -30;
           PID_HAdj = true;
            break;
         case 3: //Bridge Closed

           PID_THead = 30;
           PID_HAdj = true;
            break;
         case 4: //Bridge Closed

           PID_THead = 80;
           PID_HAdj = true;
            break;
      }
      
      releaseCPU();
      
      wait1Msec(98);
      
   }
}
   
   
task main()
{
   //StartTask(oscIR);
   StartTask(PM_2wd);
   //StartTask(cruise);
   /*
      StartTask(oscIR);
   PID_TSpd = 30;
      while(PID_dist < 70)
         wait1Msec(100);
   PID_TSpd = 10;
      while(PID_dist < 100)
         wait1Msec(100);
   PID_TSpd = 0;
   wait1Msec(1000);
   STurn(-90, 30);
   
   PID_dist = 0;
   PID_TSpd = 15;
      while(PID_dist < 30)
         wait1Msec(100);
   PID_TSpd = 0;
   */
   //
   
   while(true)
   {
      wait1Msec(1000);
   }
   
}



I'm trying to learn this thing as I need to use it for an engineering competition that starts tomorrow, any feedback on how to get around this issue would be much appreciated.


Wed Jan 23, 2013 3:22 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 609
Post Re: Major issue with variable space on PIC .5
This is a known issue specific to the PIC microcontroller and ROBOTC 3.54 that we have been debugging and should have a fix for in the next available build, which we hope to release soon. If anyone else is using the PIC microcontroller and is having similar issues, please let us know and we will be more than happy to assist you in solving the problem. Thank you!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Jan 24, 2013 3:47 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.