8 years, 8 months ago.

Where is the bug?

I am trying to implement P controller for my robot and in the code I cannot understand although there are pulse counts, why the variable trying to process it is zero. That part of my codes is as follow:

  while (1) {
        
        
        rightPulses=R_Encoder.getPulses();
        rightVelocity = (rightPulses- rightPrevPulses)/ RATE;
        pc.printf("Right PulsesA: %i\n", rightPulses);
        pc.printf("Right Pulses - RightPrevPulses:%i\n",(rightPulses - rightPrevPulses));
        pc.printf("Right Velocity is: %i\n", rightVelocity);
        rightPrevPulses = rightPulses;
      //  pc.printf("Left pulses -  
        wait(RATE);
        
        
        
      //  L_Encoder.reset();
      //  R_Encoder.reset();
    }

And the serial output to my pc is as follow

Right PulsesA: 23335
Right Pulses - RightPrevPulses:0
Right Velocity is: 0
Right PulsesA: 23335
Right Pulses - RightPrevPulses:0
Right Velocity is: 0
Right PulsesA: 23335
Right Pulses - RightPrevPulses:0
Right Velocity is: 0
Right PulsesA: 23335
Right Pulses - RightPrevPulses:0
Right Velocity is: 0
Right PulsesA: 23335

I don't understand why the 'Right Velocity' and 'Right Pulses - Right Previous Pulses' are still zeros They are declared as volatile above as follow

//--------------------
// Working variables.
//--------------------
volatile int leftPulses     = 0;
volatile int leftPrevPulses = 0;
volatile float leftPwmDuty  = 1.0;
volatile int leftVelocity = 0.0;

 volatile int rightPulses =0;
 volatile int rightPrevPulses = 0;
 volatile float rightPwmDuty = 1.0;
 volatile int rightVelocity = 0.0;

Please help me. Thanks

I think you need to rethink this. You can only see zero beacuse the speed is constant. You will only see non-zero values if you have a change in velocity i.e. accelerating or de-accellerating.

To correct this you need: divide rightPulses by the RATE and then reset right pulses to zero and cycle

posted by Martin Simpson 21 Aug 2015

Oh..Thanks .. I was careless..yes .. I looked at the results when the wheel stopped. .. i saw the outcome when rotating :D.

posted by Sao Kham 21 Aug 2015
Be the first to answer this question.