PIC16F877A - Object Detection and Rejection - Timer Interrupt

Thread Starter

daljeet795

Joined Jul 2, 2018
295
Will there be any benefit if I change the PIC16F877A?. I mean if I use 32 bit PIC Microcontroller that is 4th times faster than pic16F877A

If I follow shift register method. Do I need to buy shift register IC for accuracy or I need to make shift register in code ?
 

Ian Rogers

Joined Dec 12, 2012
1,136
I've been playing...You will benefit by using a slightly better Pic... I think you need a pic18f420 or so.. These have two interrupts... On a sim the encoder and last edge of object can occur simultaneously.. You will benefit from low / high priority.. Don't get me wrong.. It works on the pic16f877a but how fast will the conveyor be going.. What is the expected frequency of the encoder?
 

LesJones

Joined Jan 8, 2017
4,511
Hi Ian,
I don't think you will get an answer to that question. We have been asking it since the start of the thread without even a rough estimate. It has been asked in different ways like the speed of the conveyor or the RPM of the encoder shaft.

Les.
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
Hi Ian,
I don't think you will get an answer to that question. We have been asking it since the start of the thread without even a rough estimate. It has been asked in different ways like the speed of the conveyor or the RPM of the encoder shaft.

Les.
How to calculate conveyor speed?
I have 1400 RPM motor and roller diameter 72 mm
,a roller with a diameter of 72 mm, has a circumference of 72 x 3.14159 is 226.19448.
 

Ian Rogers

Joined Dec 12, 2012
1,136
the speed of the conveyor or the RPM of the encoder shaft.
The actual speed is irrelevant... As long as the code can keep up... The encoder will tie directly into Timer1 and with a prescale we can have up to 20Mhz + ... The counter will be serviced by the CCP module every falling edge... All I need to know is quantities of each product...

Now! If we are counting 3000 products and if we say the encoder has 20 counts per product the timer will need to be bigger than 16 bit.. This is assuming 20 counts on the encoder is enough...Personally I think we will get away with slightly less...
 

Ian Rogers

Joined Dec 12, 2012
1,136
1400rpm * 226.2 = 5.3 m/s... That's a bit fast for a conveyor belt... My last counter worked up to 18 m/s with a resolution of 16mm.

What was the product length again?
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
1400rpm * 226.2 = 5.3 m/s... That's a bit fast for a conveyor belt... My last counter worked up to 18 m/s with a resolution of 16mm.

What was the product length again?
Product length in a diagram = 190 mm and and there are five product between detection point to a rejection point
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
1400rpm * 226.2 = 5.3 m/s... That's a bit fast for a conveyor belt... My last counter worked up to 18 m/s with a resolution of 16mm.

What was the product length again?
There is VFD to control the speed of the motor so we can also test the program at low speed speed of conveyor. There is one button to increase/ decrease speed of conveyor
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
1,136
I thought you said there were several differing counts?

16500 encoder counts and the product count must equal 5.... That's easier than my first assumption. I would reference the count first so when product count gets to 5 then the encoder can have a tolerance... below x or above x and reject..

What happens at a rejection... By the time the belt stops 10 more products will have passed!
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I thought you said there were several differing counts?

16500 encoder counts and the product count must equal 5.... That's easier than my first assumption. I would reference the count first so when product count gets to 5 then the encoder can have a tolerance... below x or above x and reject..

What happens at a rejection... By the time the belt stops 10 more products will have passed!
sorry I didn't understand We are able to reject one object but how we will reject more than one object on the conveyor
 

jpanhalt

Joined Jan 18, 2008
11,087
I agree. The encoder reading at the point of detection is a "label". That label needs to be updated to what the encoder will read when that part reaches the reject point. Successive defective parts would just have successive labels indicating reject.

I don't see that a timer is needed. As mentioned way back, a circular buffer or its equivalent will probably be needed. Also, the encoder will need to be selected/geared so multiple loops within the detect/reject cycle cannot occur. Of course, each encoder reading as envisioned so far will probably need to be considered a window; however, if readings are made at fixed count intervals, that window might be made very narrow.

Edit: @Ian Rogers
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
Here is my attempt at showing some C code that implements a queue. The queue is 6 elements long since the TS stated no more than 5 items will fit on the conveyor belt at once. It also uses the counter delta (16500) that represents the time or distance an object takes to go from the test station to the reject station. It works independent of object size or spacing.

It is only a skeleton. Since I don’t know the method used to test the item, there is a stub function which will perform that test. Since I don’t know how the μp reads the counter value, there is a stub function which will do that. Same for the mechanism to reject an object. Knowing these details is not necessary to implement the queue to perform the function desired.

I am providing the code as a proof of concept demonstration. It has not been compiled and hence there may be typos.

Here is the code:

C:
dim unsigned long queue[6];
volatile byte putIndex = 0;
volatile byte getIndex = 0;

volatile boolean emptyQueue = true;

// add an item to be rejected to the queue
void putQueue() {

const unsigned long positionDelay = 16500;

 queue[putIndex] = getCounter + positionDelay;
 putIndex=++putIndex%6;
 emptyQueue = false;
}


// get the next item to be rejected from the queue
unsigned long getQueue() {

 if(!emptyQueue) {
   byte tempIndex;
   tempIndex = getIndex
   getIndex=++getIndex%6;
   emptyQueue = (putIndex==getIndex);
   return queue[tempIndex]
   }
 else {
   return 0
   }
}

// obtain the current counter value
unsigned long getCounter() {

/* insert code to get the current counter value and return it as the function value. No parameters are necessary. */
}

// scan or test the object to determine if it should be rejected
boolean testObject) {

/* insert code to test the objects and  return false if it is a reject */
}


// physically reject a failed item
void reject() {

/* code to physically reject the object. */
}


// main processing loop
void loop () {

 unsigned long Counter;
 boolean rejectionDone= true;
 const unsigned long deltaCounter= 2;

 if (! testObject()) putQueue( );

 if (! emptyQueue && rejectionDone) {
   Counter = getQueue()); // next failed object
   rejectionDone = false;
   }

 if (! rejectionDone && 
       abs(Counter-getCounter()) <= deltaCounter) {
   // failed item is at rejection point
   reject();
   rejectionDone= true;
   Counter = 0;
   }

}
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I recalled the shift register approach when the TS added the requirement that multiple items be on the belt.
@JohnInTX I understand shift register but I don’t understand how it would help to reject multiple bad object

C:
#include<stdio.h>

int main(void)
{
       int a = 1;

       printf("\nNumber is Shifted By 1 Bit : %d",a << 1);
    
       printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
    
       printf("\nNumber is Shifted By 3 Bits : %d",a << 3);
    
       printf("\nNumber is Shifted By 4 Bits : %d",a << 4);
    
       printf("\nNumber is Shifted By 5 Bits : %d",a << 5);
    
       printf("\nNumber is Shifted By 6 Bits : %d",a << 6);
    
       printf("\nNumber is Shifted By 7 Bits : %d",a << 7);
    
       printf("\nNumber is Shifted By 8 Bits : %d",a << 8);
    
       return(0);
}
Number is Shifted By 1 Bit : 2
Number is Shifted By 2 Bits : 4
Number is Shifted By 3 Bits : 8
Number is Shifted By 4 Bits : 16
Number is Shifted By 5 Bits : 32
Number is Shifted By 6 Bits : 64
Number is Shifted By 7 Bits : 128
Number is Shifted By 8 Bits : 256
 

LesJones

Joined Jan 8, 2017
4,511
John explains the shift register approach clearly in post #102 At the detector position the bit in the shift register is set to a zero if the object is good. If it is bad (And needs to be rejected.) it is set to a one. The bit in the shift register follows the item down the conveyor. In John's example the shift register is shifted once for every 3600 pulses from the encoder so after 10 shifts there will have been 36000 pulses from the encoder which corresponds with the distance between the pass/fail detector and the reject point.
In your case you say that there 16500 pulses between the detector and the reject point. If we still use 10 position slots then we would need to shift the shift register once for every 1650 pulses from the encoder.

Les.
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
In your case you say that there 16500 pulses between the detector and the reject point. If we still use 10 position slots then we would need to shift the shift register once for every 1650 pulses from the encoder.

Les.
@LesJones If I follow John's approach Do I need to use a timer?
 
Last edited:

LesJones

Joined Jan 8, 2017
4,511
John's approach uses the timer/counter module in counter mode in the simple way that I used it in the simple test program I gave you. (This is functioning as a prescaler.) If you chose an encoder with a sensible number of pulses per revolution you would not need to prescale the input from the encoder. In theory you could not use the prescaler but you would need a 16500 stage shift register but this would be impractical.

Les.
 

JohnInTX

Joined Jun 26, 2012
4,787
@LesJones If I follow John's approach Do I need to use a timer?
No, you don't. The shift register and most of the other approaches described here are based on distance as determined by the encoder. You would use one of the internal timer/counter peripherals in the uC as a counter that counts the pulses from the encoder to determine what distance the belt has traveled from the sensor.
I understand shift register but I don’t understand how it would help to reject multiple bad object
You should look again at the PLC example I posted if you want to understand the concept. You don't have to know PLC (I don't). It's the basic concept that you need to understand. After that, whether you use a shift register, an event list or any of the other distance-based ideas described here, the concept is the same. You are not interested in the numeric value of the contents of the shift register. You are interested in the bits themselves as they get shifted. The bits are like the stickers on the boxes in my example. The PLC example I linked is animated so that you can follow along.

At this point, the first thing to do is to understand the problem fully.
Solve the problem on paper. Draw pictures as the boxes proceed along the belt. Ask yourself questions like
  • How do I know where boxes are on the belt?
  • How do I know if a box has to be kicked?
  • How do I keep track of multiple boxes between the sensor and kicker?
  • How does the kicker know that the particular box under it needs to be kicked?
Don't worry about the implementation details, timers, PLCs etc. When you fully understand the problem and you have a solution that works on paper, how to implement the solution in software will become apparent. The most important thing is to solve the problem, pick ONE way to implement that solution, design the implementation and focus on getting that working.

Have fun!

EDIT: @LesJones beat me to it.
 
Last edited:

Thread Starter

daljeet795

Joined Jul 2, 2018
295
John's approach uses the timer/counter module in counter mode
No, you don't. The shift register and most of the other approaches described here are based on distance as determined by the encoder. You would use one of the internal timer/counter peripherals in the uC as a counter that counts the pulses from the encoder to determine what distance the belt has traveled from the sensor.
.
The distance between the sensor and the reject point is 1200 mm.

If we want to reject bad product we need to 16500 pulses of the encoder

There will be 16,500 counts from the inspection station to the reject station

So one count represents 1200mm /16500 = 0.073mm.

C:
#include <xc.h>
#define _XTAL_FREQ 20000000L
#pragma config FOSC=HS
#pragma config WDTE=OFF
void main(void)
{

    unsigned int ShiftReg = 0; //  Init to 0
    INTCON =0; // No interrupts
    PORTC = 0;
    TRISC = 3; // RC0 encoder & RC1sensor inputs
    TRISD = 0; // Portd outputs
    T1CON = 2;
    while(1)
   {
       while(RC1);        // wait while high.
       while(!RC1);       // wait while low.

       // code for shift register here ?

       TMR1H = 0xBF;        // Load starting value
       TMR1L = 0x8C;
       TMR1IF = 0;         // Clear interrupt flag
       TMR1ON = 1;          // Start TMR1

       while(!TMR1IF);      // Wait for timer spill

       TMR1ON = 0;         // timer off

       RD4 = 1;            // set output pulse high
       __delay_ms(800);    // Delay to see ouput ( inbuilt delay)
       RD4 = 0;            // set output pulse low
   }
}
 
Last edited:

Thread Starter

daljeet795

Joined Jul 2, 2018
295
At this point, the first thing to do is to understand the problem fully.
Solve the problem on paper. Draw pictures as the boxes proceed along the belt. Ask yourself questions like
  • How do I know where boxes are on the belt?
  • How do I know if a box has to be kicked?
  • How do I keep track of multiple boxes between the sensor and kicker?
  • How does the kicker know that the particular box under it needs to be kicked
I did the same before I know the pulses and distance

 

LesJones

Joined Jan 8, 2017
4,511
You seem to be having a problem understanding the concept of a bit in a shift register following a point on the conveyor. As you still seem to think in terms of 16500 pulses I will try to describe it in terms of a 16500 stage shift register. (Serial in and serial out.)Think of this shift register on the diagram above (The one in post #139) drawn below the line that says 16500 pulses. The serial in is the right hand end and the serial out is the left hand end. The serial in will be zero except when an item to be rejected is detected. The shift register is clocked with the encoder pulses. When an item to be rejected is detected the input to the shift register (Right hand end.) is set to a ONE state so the next encoder pulse will clock it into the first stage of the shift register.The shift register moves the bit one position to the left for every clock pulse (Encoder pulse.). So after 16500 pulses the ONE bit will have reached the output position (Left hand end.) This will be the same time that the item to be rejected reaches the reject point. The ONE bit at the output of the shift register can trigger the air jet to blow the faulty item off the conveyor.

Les.
 
Top