CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Where the integer gets its first value?[SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
erhane



Joined: 01 Jul 2014
Posts: 41

View user's profile Send private message Send e-mail

Where the integer gets its first value?[SOLVED]
PostPosted: Sat Jul 19, 2014 11:55 am     Reply with quote

Hello friends, i have that source code but couldnt figure out where "decelLocation" integer gets its first value? It says it is predefined but defined where?

Value of "decelLocation" is 6400.

Thank You!

here is code:

Code:

#include <18F452.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES H4                       //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOCPD                    //No EE protection
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads

#use delay(clock=32000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

#define ENGINE_RATE 25000  // be sure timer interrupt matches this
#define SPEED_OFFSET 16777216  // 2^24 The is the offset of the accumulator 2^24 units = 1 step

#define PIN_X_STEP PIN_A1     // pin corrector to step of stepper board
#define PIN_X_DIR  PIN_A2     // pin connected to direction pin of stepper board
#define DIRECTION_FORWARD 1   
#define DIRECTION_REVERSE -1

signed int32 currentLocation;    // store current location in steps so we can use absolute moves
int32 decelLocation;             // pre-calculated point we start to decelerated
signed int8  motionDirection;     // what direction do we move 1 = fwd -1 = rev
short bEnableMotion;             // this turns on/off the 25kHz Pulse Engine
short bDecel;                    // in decel mode

int32 currentSpeed;              // current speed in accum units per engine tick
int32 accel;                     // accel rate in accum units per engine tick
int32 maxSpeed;                  // max speed in accum units per tick
int32 accumulator;

signed int32 targetLocation;     // target position in steps
 
long moveAccelSteps;             // accel rate in steps / sec /sec
long moveMaxSpeedSteps;          // max speed in steps /sec



// this is the pulsing engine...the the file header for more info
#int_TIMER2
void  TIMER2_isr(void)
{
   // we only do anything if this motion is enabled and we are not at the target position
   if (bEnableMotion && (currentLocation != targetLocation) )
   {
      // set the current speed
      // If we are before the decel point we accelerate or coast
      // else we decel
     
      if (currentLocation == decelLocation)
         bDecel = true;
     
      if (!bDecel)
      {
         // if we are not at max speed and accel to the speed
         if (currentSpeed < maxSpeed)
         {
            currentSpeed += accel;
         }
      }
      else // decel
      {
         currentSpeed -= accel;
         if (currentSpeed <= 0) // never let it get to zero otherwise we could get stuck
         {
            currentSpeed = accel;
         }
     
      }
     
      accumulator += currentSpeed;
   
      //see if we have accumulated enough for a step step
      If (accumulator > SPEED_OFFSET)
      {
         
         
         currentLocation += motionDirection;
         
         
         output_high(PIN_X_STEP);  // turn on step pin
         delay_us(4);            // wait a while
         output_low(PIN_X_STEP);   // turn it off
         
         
         accumulator -= SPEED_OFFSET;
      }
     
   
   }
   else
   {
     
      bEnableMotion = false;     
   }
}


/* this is the simple "motion planner".  It does all the intesive calulations
   It calculated the acceleration per engine tick
   It calculated the max speed per engine tick
   It determines the motion direction
   It resets the current speed to 0
   
   It turns on the Pulse Engine
*/
void doMotion()
{
   float tempAccel;
   float tempMaxSpeed;
   float t;
   int32 distToTarget;
   int32 accelDist;
   
   

   distToTarget = abs(targetLocation - currentLocation); // figure out the distance we will travel on this move
   
   tempAccel = moveAccelSteps;// * stepsInch; // in steps per sec   
   tempAccel = tempAccel / (ENGINE_RATE * ENGINE_RATE);
   tempAccel = tempAccel * SPEED_OFFSET;
   accel = (int32)tempAccel;
   
   tempMaxSpeed = moveMaxSpeedSteps;
   tempMaxSpeed = (tempMaxSpeed / ENGINE_RATE) * SPEED_OFFSET;   
   maxSpeed = (int32)tempMaxSpeed;
   
   //determine Accel distance
   //     d = 1/2 a t^2
   t = (float)moveMaxSpeedSteps / (float)moveAccelSteps;
   accelDist = (moveAccelSteps / 2) * t * t;
   
   
   
   // determine the direction
   if (targetLocation > currentLocation)
   {
      motionDirection = DIRECTION_FORWARD;
      decelLocation = currentLocation + decelLocation;
      output_low(PIN_X_DIR);
     
     if (accelDist >= distToTarget / 2)
         decelLocation = targetLocation - distToTarget / 2;
      else
         decelLocation = targetLocation - accelDist;
     
   }
   else
   {
      motionDirection = DIRECTION_REVERSE;
      decelLocation = currentLocation - decelLocation;
      output_high(PIN_X_DIR);
     
      if (accelDist >= distToTarget / 2)
         decelLocation = targetLocation + distToTarget / 2;
      else
         decelLocation = targetLocation + accelDist;
     
   }
   
   delay_us(4); // time for direction to "take" before steps start
   
 
   bDecel = false;
   
   currentSpeed = 0; // initialize this
   bEnableMotion = true; // all set time to move
   
}


void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,159,2);
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_1);
   
   currentLocation = 0;
   bEnableMotion = false;
   
   
   enable_interrupts(INT_TIMER2);
   //enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   output_low(PIN_X_STEP);
   output_high(PIN_X_DIR);
   
 
   while(true)
   {
     
      moveAccelSteps = 1000;
      moveMaxSpeedSteps = 2000;
      targetLocation = 200 * 8;
      doMotion();
      delay_ms(2000);
     
      moveAccelSteps = 50000;
      moveMaxSpeedSteps = 20000;
      targetLocation = 0;
      doMotion();
      delay_ms(1500);
   
   }




Last edited by erhane on Sat Jul 19, 2014 3:31 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Sat Jul 19, 2014 2:16 pm     Reply with quote

You miss-read.

It doesn't say it is predefined. It says it is pre-calculated.

It is calculated from the distance to target, as soon as the motion code is called.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 19, 2014 2:45 pm     Reply with quote

It should be initialized to some value. Since it's not initialized, it probably
is set to 0 upon power-up (due to the behavior of the PIC's silicon).
But you shouldn't depend upon that.

In his code in main(), he initializes these two variables as shown:
Code:

currentLocation = 0;

targetLocation = 200 * 8;

In his doMotion() routine, the first 'if' statement is this:
Code:
// determine the direction
   if (targetLocation > currentLocation)
   {
      motionDirection = DIRECTION_FORWARD;
      decelLocation = currentLocation + decelLocation;
      output_low(PIN_X_DIR);

Because 1600 is greater than 0, the if() statement will be executed.
The 'currentLocation' is added to the existing value of 'decelLocation',
but 'decelLocation' was never initialized. That's not good.
Probably you should just initialize it to 0 at the beginning of main().

You got that code from this page:
http://www.buildlog.net/pic/pic_basic_motion.html
He has a "contact" email address on that page. Why not ask him about it ?

Also, he has this fuse, which is probably a mistake:
Quote:
#FUSES LVP

Hardly anyone has a LVP programmer. Change it to NOLVP for safety.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Sun Jul 20, 2014 3:05 am     Reply with quote

Look at it again.

The first calc you point to, is thrown away!. Just a couple of lines later you have:
Code:

     if (accelDist >= distToTarget / 2)
         decelLocation = targetLocation - distToTarget / 2;
      else
         decelLocation = targetLocation - accelDist;

(and the inverse example for when moving in the other direction).

This sets decelLocation, based based on where you want to go 'to', relative to where you 'are'.

The value for decelLocation, calculated in the first line, is never used (just wasted code....).

So it is 'precalculated'.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group