Using XOR to convert a Graycode Encoder

(instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Sat Aug 10, 2019 11:07 am

Roche wrote:I did just write it...


Lol that's cool. I've found the problem with the blank spots, the encoder is 12-24VDC, and I supplied it from a separate source (battery charger 12.8V), upon closer inspection of the encoders circuit diagram it appears the encoder needs the 0V from the 12V supply, and it was grounded at the dev boards pin. Checking the voltages at the outputs of the encoder measure 12+ volts. So it seems if I want to continue with these encoders I'll have to put a buffer inline. In theory it won't affect the coding of the PIC, its just a hardware issue. I'll fix that then come back to the coding.
If I buy these encoders again I'll do as ric suggested and get the binary output version, plus now I realise I also need the 5V version as well....

Do you mind if I use your asm code as a starting point?

I'm trying to make a feedback system where the encoders confirm the position of a motor driven spindle. The spindle is given a predetermined position, the geared motor drives the spindle the encoder makes sure it gets to the correct spot.

Regards
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: Using XOR to convert a Graycode Encoder

Postby Roche » Sat Aug 10, 2019 12:07 pm

Do you have the spec/part number of the encoder?
The code is there, free to use. Like I say, I think it could use refining. It was just to make sure I understood what was going on...
Sounds like an interesting application - where are you?
Roche
 
Posts: 72
Joined: Fri Jul 11, 2014 12:35 pm
PIC experience: Professional 5+ years with MCHP products

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Sat Aug 10, 2019 5:05 pm

Roche wrote:Do you have the spec/part number of the encoder?....

It's an Autonics EP50S8-1024-3F-P-24, and I have two of them. Not ideal, but I'll have to make do at the moment, I have other priorities. It's not like I don't have other things to get on with, I still need to get to grips with this 9 axis IMU.

Roche wrote:...The code is there, free to use. ...

Thank you.

Roche wrote:where are you?


I'm on the south coast in Kent, UK, and you?

Regards
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: Using XOR to convert a Graycode Encoder

Postby Roche » Sat Aug 10, 2019 5:58 pm

I'm near Reading in Berkshire.
I can see why you wouldn'y want to replace the encoder in a hurry. Good luck with the IMU.
Roche
 
Posts: 72
Joined: Fri Jul 11, 2014 12:35 pm
PIC experience: Professional 5+ years with MCHP products

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Sat Aug 10, 2019 7:19 pm

Roche wrote:I'm near Reading in Berkshire....

8-) I was half expecting you to be on the other side of the planet. My daughter lives in Reading I know it quite well, and a close friend I went to uni' with also lives there....I grew up in Slough/Burnham, and moved to Kent over 25 years ago.
Small world :D
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: Using XOR to convert a Graycode Encoder

Postby Roche » Sun Aug 11, 2019 9:03 am

I did come from the other side of the planet - about 30 years ago.

It depends on your application, but I always fancied trying a LVDT for angular position. Very high resolution. I got involved with one once which was used for positioning a 6m satellite dish. Ever since then I've been looking for an excuse to use one...
Roche
 
Posts: 72
Joined: Fri Jul 11, 2014 12:35 pm
PIC experience: Professional 5+ years with MCHP products

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Sun Aug 11, 2019 10:40 am

LVDT... I had to search it.... Looks like RDVT would be an option..my main issue is ruggedness, the application is prone to repeated vibration and shocks, so it's all about whether the internals would be up to the task.
The encoder I'm using has a delicate disc (with probably etched markings) and electronics inside, with a rather large "Do not impact" on the side...
Looking at a cutaway image on the web shows similar style of internals for the RDVT, but it'll probably come down to trial and error/evolution ...need to do more homework on that one...still replacing the one's I have is a long way off yet.

Thanks for the heads up.

Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Sun Sep 15, 2019 5:04 pm

OK, so finally got the encoder connected at the correct voltage levels using a pair of these boards....

Image

https://www.ebay.co.uk/itm/8-Channel-24V-to-5V-Optocoupler-Isolation-Module-Plc-Signal-Level-Voltage-Conver/132850844582?epid=23021860048&hash=item1eee8703a6:g:oqgAAOSw3fZaH~LA

The blank spots in the encoder have disappeared, and although I get a readout to terminal, it doesn't seem to be consistent. For a start it doesn't go from 0 to 1024 as I would have expected. I can't seem to get below 8, or above 494, with sections going incrementing and decrementing whilst rotating in the same direction and areas within the revolution repeating.

So I'm wondering if perhaps although the code used previously did convert the basic values, would something like this be better?

Code: Select all
    volatile uint8_t G_PortB;                      // Variable for the 8bits of PORTB.
    volatile uint8_t G_PortC_0;                    // Variable for the 1bits(0) of PORTC.
    volatile uint8_t G_PortC_1;                    // Variable for the 1bits(1) of PORTC.
    volatile uint16_t TwoBitsCombined;             // Variable for the 2bits of PORTC combined.
    volatile uint16_t GrayInput;                   // Variable for the complete input from the encoder.     
    volatile uint16_t TwoBitsCombined_Shifted;      //
    volatile uint16_t i;
    unsigned short temp[10];
    unsigned short num;
    unsigned short Gray2DecOutput;
   
   
  // Function to XOR 
unsigned short grayToDecimal(unsigned short num)
{
        temp = num[0];
    for (int i = 0; i < strlen(num) - 1; i++)
    {
        /* XOR operation */
        if (temp[i] == num[i+1])
            temp = num + "0";
        else
            temp = num + "1";
    return temp;   
}   
}   
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
    PIN_MANAGER_Initialize();
    INTERRUPT_Initialize();
    EUSART1_Initialize();
   
        // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

void EUSART1_Initialize(void);
{
    printf("\rEasyPIC V7-8MHz Date 03/08/2019\r\n");
    printf("USART Communications 8-bit Rx & Tx\r\n\n");
    printf("10bit Encoder to port B(0:7) & PortC(0:1)\r\n\n");
    printf("DIPS set to pull down. \r\n\n");
}

    while (1)
    {
    G_PortC_0 = IO_RC0_PORT;                   //bit1 of PORTC (8bits)
        // 00000001 example
    G_PortC_1 = IO_RC1_PORT;                    //bit2 of PORTC (8bits)
        // 00000010 example
   
    TwoBitsCombined = (G_PortC_1)|(G_PortC_0);  //take bit2 and concatenate both bits
        // 0000000000000011 example
   
    TwoBitsCombined_Shifted = TwoBitsCombined<<8;
        // 0000001100000000  example
   
    G_PortB = PORTB;                            //Pin RB0-7 8bit Binary Input
        // Result 11111111
    GrayInput = (TwoBitsCombined_Shifted | G_PortB);   //OR the above together
        // 0000001111111111 example
   
    Gray2DecOutput =grayToDecimal(GrayInput);

    printf("\rBinary Output:  %4d",Gray2DecOutput);
}
}
/**
 End of File
*/


Regards
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

Re: Using XOR to convert a Graycode Encoder

Postby ric » Sun Sep 15, 2019 9:36 pm

Why not just compare the "before" and "after" values to see what's going wrong?
e.g. if you can get to 8 out, you should be able to step down and see these values

Code: Select all
Before After
Grey   Binary
===========
000    000
001    001   
003    002   
002    003   
006    004   
007    005   
005    006   
008    007   
00C    008    <---
00D    009
00F    00A
00E    00B
00A   00C
00B    00D
009    00E
008    00F
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Using XOR to convert a Graycode Encoder

Postby GettinBetter » Wed Sep 18, 2019 9:36 pm

When I turn the spindle the leds on the dev board (and the niffty boards I just bought) indicate the greycode being input to the PIC. the values I have checked on terminal, are: (just using the last four bits for now)
Code: Select all
1111 = 10 //i.e. works correctly
1000 = 15 // i.e.works correctly
1100 = 8 // i.e. works correctly
1101 = 9 // i.e. works correctly

all leds on should be 682 but terminal shows 341
greycode 0 is also zero in decimal, but I can't get all the leds to extinguish at the same time. The fourth bit always remains on, when the spindle gets to the position I'd expect zero to be.
Alarm bells are ringing with regards to these encoders.

Here's some of the output to terminal that I'm getting.
^ means increasing incrementally.
v means decreasing incrementally.
> means jumping ahead to.

8^23 v16 >47 v40 ^55 v48 >79 v73 ^87 v80 >111 v104 ^119.....
Maybe there's a pattern there somewhere? I hope that makes some sense.

Regards
Les
History teaches us that history doesn't teach us.
User avatar
GettinBetter
 
Posts: 43
Joined: Wed Jun 06, 2018 8:48 pm

PreviousNext

Return to 16-Bit Core

Who is online

Users browsing this forum: No registered users and 9 guests

cron