Programming an absolute encoder

  absolute, encoder, python, python-3.x, raspberry-pi4

Currently, I’m trying to program the absolute encoder AMT222A-V on python using Rspberry Pi 4 B.
I found an Arduino SPI Sample Code Tutorial , and I tried to interpret it in Python, word for word.

You can find the code down below :

import time
import RPi.GPIO as GPIO
import spidev
import math
import array as arr
import struct
import operator
import binascii

BAUDRATE = 115200
AMT22_NOP = 0x00
AMT22_RESET = 0x60
AMT22_ZERO = 0x70

NEWLINE = 0x0A
TAB = 0x09

RES12 = 12
ENC_1 = 8
SPI_MOSI = 10
SPI_MISO = 9
SPI_SCLK = 11

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

GPIO.setup(SPI_MOSI, GPIO.OUT)
GPIO.setup(SPI_MISO, GPIO.IN)
GPIO.setup(SPI_SCLK, GPIO.OUT)
GPIO.setup(ENC_1, GPIO.OUT)

GPIO.output(ENC_1, True)
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 500
binaryArray = arr.array('d', [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

def spiWriteRead(sendByte, encoder, releaseLine):
    
    sendByte =bytearray()
    encoder =bytearray()
    releaseLine =bytearray()
    data =bytearray()
    
    GPIO.output(encoder, False)
    time.sleep(0.000003)
    data = spi.xfer([sendByte])
#     spi.writebytes([sendByte])
#     data = spi.readbytes(16)

    time.sleep(0.000003)
    GPIO.output(encoder, releaseLine)
    
    return data

def getPositionSPI(encoder,resolution):
    
    resolution =bytearray()
    encoder =bytearray()
    currentPosition =bytearray()
    
    #A_list =(spiWriteRead(AMT22_NOP, encoder, False))
    #strings = [str(integer) for integer in A_list]
    #a_string = "".join(strings)
    #an_integer = int(a_string)
    #currentPosition = operator.__lshift__(an_integer,8)
    
    currentPosition =(spiWriteRead(AMT22_NOP, encoder, False))<< 8
    
    time.sleep(0.000003)
    
    
    currentPosition |= spiWriteRead(AMT22_NOP, encoder, true)
    for i in range(16):
       binaryArray[i] = (0x01) & (currentPosition >> i)
    if ((binaryArray[15] is math.factorial(binaryArray[13]^binaryArray[11]^binaryArray[9]^binaryArray[7]^binaryArray[5]^binaryArray[3]^binaryArray[1])) and (binaryArray[14] is math.factorial(binaryArray[12]^binaryArray[10]^binaryArray[8]^binaryArray[6]^binaryArray[4]^binaryArray[2]^binaryArray[0]))):
       currentPosition & 0x3FFF
    else:
       currentPosition & 0xFFFF

    if (currentPosition != 0xFFFF):
       currentPosition >> 2

    return currentPosition

try:
    encoderPosition =bytearray()
    attempts =bytearray()
    
    while True:
        attempts = 0
        encoderPosition = getPositionSPI(ENC_1,RES12)

        while (encoderPosition == 0xFFFF and attempts < 3):
            attempts += 1
            encoderPosition = getPositionSPI(ENC_1,RES12)

            if (encoderPosition == 0xFFFF):
                print("Encoder 0 error. Attempts: ")
                print(attempts)
            else:
                print("Encoder 0: ")
                print(encoderPosition)

            time.sleep(0.5)

except KeyboardInterrupt:
    print("Ctl C pressed - ending program")

GPIO.cleanup()
close()
print("Done")

However, I’ve got this error :

Traceback (most recent call last):
  File "/home/pi/AMT222-V.py", line 92, in <module>
    encoderPosition = getPositionSPI(ENC_1,RES12)
  File "/home/pi/AMT222-V.py", line 68, in getPositionSPI
    currentPosition =(spiWriteRead(AMT22_NOP, encoder, False))<< 8
  File "/home/pi/AMT222-V.py", line 45, in spiWriteRead
    GPIO.output(encoder, False)
ValueError: Channel must be an integer or list/tuple of integers

I’m supposedly getting a byte from the spiWriteRead function and left shift it in getPositionSPI function in order to get that first byte (8 bits) into the top half of the "currentPosition" variable which is a 16bit variable.

I’m note sure of the cause of this error.

Any advice can help me.

Thank you in advance.

Source: Python-3x Questions

LEAVE A COMMENT