
austin.m.bryan (Customer) asked a question.
Hi everyone,
I made a post last week on the Electric Field Device subforum about reading a 2048PPR encoder. I realized it was much too fast for the digital card I'm using (P1-16ND3) so I've exchanged it with a Koyo 360PPR (https://www.automationdirect.com/adc/shopping/catalog/sensors_-z-_encoders/rotary_encoders/medium_duty_absolute_encoders_(gray_code)/trd-na360nwd) encoder to measure an angle of rotation (I probably should have used this one to begin with!).
I'm attempting to read the gray code value that it's outputting and am doing so by reading each bit with the P1.readDiscrete function as shown below (full code):
This method is giving me a 9 bit gray code value but it is not following in sequence, so it does not start at 0 and work up to 360. I have made sure to include a line trimming 76 excess codes as mentioned in the data sheet of this particular encoder but the output values are still in a random order.
I'm wondering: is there a better way to read and decode each gray code pattern? It seems like reading each bit individually is not working but I really don't know an alternative way of approaching this.
Thanks in advance,
Here is my full code, I forgot to include it in the original post:
I dont really know C coding so I cant help you much with this, but I was wondering since you said "the numbers are not following in sequence" if you are trying to read it as binary directly, or if you accounted for the different way gray code counts? Gray code doesnt go in the same order as binary counting does. With gray, only a single bit changes each location, so the bit order change is different.
https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code
PouchesInc,
Sorry, should have been more specific. I meant that they gray code values it is outputting, once decoded to an integer, do not follow the correct sequence.
Thank you,
When reading individual bits one at a time the encoder could very well have moved from the time of the first read to the time of the last read as going through the base controller is not instant. if the encoder moves at all in between reads it will through the value way off. that is why I would suggest reading all the bits at once with a P1.readDiscrete(1) then use a for loop to fill your bit array.
z28z34man,
This makes a lot more sense, thank you. I'll try this out and report back!
i would read all the inputs at once using the flowing
allInputChannels = P1.readDiscrete(1)
it will return a HEX value. you then can convert the HEX in to individual bits. this should be much faster than going through the base controller 8 times.
Alright, I've got it set up to use P1.readDiscrete(1) now, I've attached a sample of what it's printing to the Serial monitor as an image, as well as my code. Note that the values are changing as I turn the encoder shaft which seems good.
My question now is: how would you convert the value to individual bits to store in the array? Just read the high/low values of each pin and trim where necessary?
this will convert to a binary array that you can then do with as you need
>This method is giving me a 9 bit gray code value but it is not following in sequence,
What sequence do you expect and what sequence do you observe?
<example image>
[Edit: the image is from this 'edn' article]
kewaki,
The expected gray code sequence should follow the one in the Koyo manual for the 360PPR encoder. The sequence I am getting is completely random: