ahacking
Posts: 4
Joined: Mon Jul 25, 2016 7:58 pm

AS5050A rotary sensor SPI

Tue May 22, 2018 11:19 pm

Just wondering if anyone managed to get an AS5050A https://ams.com/as5050a up and running with an RPi? I've managed to get the 5040 chip running with SPI but am having a harder time getting this one to return any data.

Thanks

Adam

#Import Libraries
import time
import spidev

#https://pypi.org/project/spidev/
spi = spidev.SpiDev()
spi.open(0, 0) # Opens the SPI Slave State Port for communication
spi.mode = 0b01 #0b00=mode0, 0b01=mode1, 0b10=mode2 0b11=mode3
spi.bits_per_word = 8
spi.max_speed_hz = 50000
spi.cshigh = False

def get_data_from_encoder():
resp = spi.xfer2([0x10, 0x10]) #read 2 bytes
print("resp:",resp)
first_byte = '{0:08b}'.format(resp[0]) # format in binary
second_byte = '{0:08b}'.format(resp[1])# format in binary
extracted_data = first_byte[1:] + second_byte[:7] # remove first bit from first byte and keep first 3 bits from
extracted_data = str(extracted_data)
return extracted_data

def rotation_degrees(binary_data):
degree_of_rotation = int(binary_data, 2) / 2.844 # has 1024 positions/360 degrees = 2.844
if degree_of_rotation >= 180: # if leg flexed
degree_of_rotation = 360 - degree_of_rotation
else:
degree_of_rotation = -degree_of_rotation # if leg hyperextended
degree_of_rotation = round(degree_of_rotation, 1)
degree_of_rotation = str(degree_of_rotation)
return degree_of_rotation


while True:
binary_data = get_data_from_encoder()
print("", binary_data)
print("Angle:", rotation_degrees(binary_data))
time.sleep(.01)

spi.close()
sock.close()

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 10 guests