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

Lost interrupts
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

Re: Lost interrupts
PostPosted: Thu Feb 17, 2011 7:55 am     Reply with quote

picprogrammer wrote:
Hi,

I have made a (PIC18F) dual motor controller with H bridge and the motor has an AB encoder. The A signal is connected to the IRQ input and is counting the rotation using rising and falling edge direction from the B signal. The problem is sometimes the calibrated zero point moves. Zero is not counted zero.

Now i see the compiler messages: irq's disabled during.... The HIGH priority irq's are enabled but no difference. Any way i can not have the irq disable from the compiler?

Gr,


I'm taking "zero is not counting zero" to mean that you missed some counts.

Can you post your ALGORITHM for interpreting the quadrature encoder?? Interrupting on A and using B for the direction doesn't feel correct to me, and could mean a whole bunch of things until you describe it better.



My own algorithm, which seems pretty speedy, is to interrupt on BOTH channel A and B (use a B port interrupt).

Start with an intitial value of the quad encoder, shift it two bits right, and then store the same number in the low two bits. this will be indx

Make an array as follows

incr[ ] = [0 1 -1 0 -1 0 0 1 1 0 0 -1 0 0 -1 1 0 ]

when the interrupt comes, shift indx two bits to the right, put the readings on the A and B channels into the lower two bits. Take the low four bits of the result, and make than indx. Use that to index into incr, and add the result to your running count.

count = count + incr[index]


Every zero in their is probably best viewed as an error of some sort-- it means either two bits changed at the same time on your quad encoder and you missed a count, or the interrupt shouldn't have triggered, or you missed more than one count. perhaps setting an error flag would be the quickest action.

Aside from checking for errors, underflows or overflows, this algorithm uses no decisions, and is probably the quickest way to get a x4 quadrature encoder count

It looks complicated, but its just two lines of code in the interrupt, most of which are simple bit shifts.

To clarify just a hair, indx= [ b0001] would indicate a quad encoder change from A=0 B=0 to A=0, B=1, a change of 1 in one direction.

indx=[ b0010] indicates a change from 00 to 10, or a change of 1 in the other direction.

Noise and/or bounce on your encoder will absolutely kill this algorithm, and is a likely culprit for the problems you're having now, unless your encoder is optical. All the quad encoder "glue" chips and the 18Fxx31 series have built in algorithms to deal with noise.
Ttelmah



Joined: 11 Mar 2010
Posts: 20159

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 9:45 am     Reply with quote

This is quite good on a PIC16, but takes a long time on a PIC18!...

You have got a total of four bit shifts, an addition, then the index into the array. You'd also need a mask, to ensure that other bits don't get included in the sum. Compiled for a PIC18, it comes in as 70 instruction times (75 if the table is constant). There are 36 instructions just for handling "count = count + incr[index] " (more with the constant). Your code avoids avoids the rotations having to involve the 16/32bit value (hurrah), but still uses arrays, and these are quite inefficient on the PIC. The code as a whole, works much better on the PIC16, with the instruction count falling to just 40 instructions. Put the table as a constant, and the count rises to just 45 instructions here..

It's again an elegant solution, but not as fast as simply doing bit tests. Takes about 50% longer than the bit test version (PIC16), and over twice the time on a PIC18!....

Also, adds the problem (in common with all array based solutions), that if using INT_GLOBAL to get speed really high, you have to save several more registers for array accesses, than for the bit test version.

Best Wishes
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 4:22 pm     Reply with quote

Ttelmah wrote:


It's again an elegant solution, but not as fast as simply doing bit tests. Takes about 50% longer than the bit test version (PIC16), and over twice the time on a PIC18!....



What is the bit-test version? If you're interrupting on A and checking B for direction, that's a x2 implementation with half the resolution. Of course, having twice as many interrupts with twice as many ops isn't necessarily a good thing, either.

Could the array be handled more efficiently by just using a pointer into the array?

I'm curious now. Eventually I'll have to go the the assembly code and look. The mask should be very quick, and the shifts by two bits should be one operation each as rotations, no??

Or do you mean that I should make two I16's, one for increment and one for decrement, [0100 0001 1000 0010] and [0010 1000 0001 0100], and use my indx as the index into those for a bit test?? That's a pretty nifty idea!! Mayb adding an Error array for a third bit test
Ttelmah



Joined: 11 Mar 2010
Posts: 20159

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 2:55 am     Reply with quote

The bit test version, is the one I posted earlier in the thread.
It is a full quadrature decoder, bulkier than the ones suggested since, but still the fastest proposed.
It could probably be optimised by a couple more machine cycles, with some careful thought, but as posted, it handles a 32bit position, faster than any of the alternatives.

Best Wishes
picprogrammer



Joined: 10 Sep 2003
Posts: 35

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 3:10 am     Reply with quote

The calculation functions are now only called from the main loop. Now my code exploded from 72% to 84% rom!!

If I only call (but never real use) the calculation function from an IRQ is's back to 72% so removing a call increases the code.
Ttelmah



Joined: 11 Mar 2010
Posts: 20159

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 3:31 am     Reply with quote

Triple check that the version in the external code is using the same numeric 'types' as the version in your IRQ. Any size change should be minuscule, from shifting outside the ISR, but (for example), on the original, you were using int8, int16, int32, and float divisions. If one of the former has changed to use the latter, the size _will_ shoot up. A float division takes about 750 machine words to code, while an int16 version is only about 1/3rd of this....

Best Wishes
picprogrammer



Joined: 10 Sep 2003
Posts: 35

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 3:45 am     Reply with quote

This is the counting irq. The reason why only irq on A signal is i'm out of irq inputs. And this give double values.
Code:

/*******************************************************************************
 *  Description : INT1 X-axis
 *
 */
#int_EXT1 HIGH
void  EXT1_isr(void)
{
  if (input(end_x))                                      // end sensor X axis
  {
    x_on_end = false;
    if (((move_x_rev)&& !input(Xb)) || ((move_x_fwd)&&  input(Xb)))
    {
      hal_x_timer =hal_time;
      hal_x_error = false;
    }     
   
    if (pulse1_high) // IRQ
    {
      ext_int_edge( 1,H_TO_L);                     //Hal sensor EXT1 x axis
      pulse1_high = false;
      if (input(Xb)) pos_x++;  else pos_x--;
    }
    else
    {
      ext_int_edge( 1,L_TO_H);                     //Hal sensor EXT1 x axis
      pulse1_high = true;
      if (input(Xb)) pos_x--; else pos_x++;
    }     
  }
  else   
  {
    if (!x_on_end && !ignore_x_switch)                  // end signal one shot
    {
      x_on_end = true;   
      hal_x_timer =0;
      movefrom_end_x_timer = 3;
      ignore_x_switch = true;           
      ignore_x_switch_timer = ignore_switch_time;
      if (move_x_fwd )
      {
        max_x =  pos_x;     
        maxpos_saved = false;
        goal_x = pos_x;              // Stop
        frommax_x = pos_x - (signed int32) (step_per_decrees);      // do not stay at maximum
      }
      if (move_x_rev)
      {
        x_min_offset = pos_x;
        show_offset_timer = show_offset_time;
        pos_x = 0;                     
        goal_x = 0;                  // Stop       
        frommax_x = (signed int32) (step_per_decrees);              // do not stay at minimum       
      } 
    }
  }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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