Browsed by
Month: May 2015

AS5040 on Beaglebone Black, using Python: preliminary

AS5040 on Beaglebone Black, using Python: preliminary

After waaaaaaay too long, I finally got around to porting the Arduino AS5040 code to python. This is very preliminary, but it does work. The Mad Scientist Hut.

As self-hosted versions of wordpress appear to do a lousy job of formatting, and python is formatting-specific, I’ll put the code on github where I can guarantee that it looks the way it should: https://github.com/smellsofbikes/AS5040_python_beaglebone

———————————————

#!/usr/bin/python

import Adafruit_BBIO.GPIO as GPIO
import time

def read_raw_val(data, chip_select, clock):
GPIO.setup(data, GPIO.IN)
GPIO.setup(chip_select, GPIO.OUT)
GPIO.setup(clock, GPIO.OUT)

a = 0
output = 0
readbit = 0
GPIO.output(chip_select, GPIO.HIGH)
time.sleep(0.01)
GPIO.output(clock, GPIO.HIGH)
time.sleep(0.01)
GPIO.output(chip_select, GPIO.LOW)
time.sleep(0.01)
GPIO.output(clock, GPIO.LOW)
time.sleep(0.01)
while a < 16:
GPIO.output(clock, GPIO.HIGH)
time.sleep(0.01)
readbit = GPIO.input(data)
output = ((output << 1) + readbit)
GPIO.output(clock, GPIO.LOW)
time.sleep(0.01)
a += 1
return output

while 1:
rawval = read_raw_val(“P9_15”, “P9_11”, “P9_12”)
print “read: ” + str(rawval)
print “raw rotation: ” + str(rawval >> 6)
time.sleep(1)

——————————————-

I still need to strip and decode errors, extend it another two bits for the AS5045, and make the whole thing into an importable library.

Note that on the Beaglebone, the Adafruit GPIO library doesn’t yet fully support setting pull-up or pull-down resistors on pins.  As such, you may have trouble with some pins not working.  I know these three pins work, because I’ve tested them.  The code should work for Raspberry Pi without any problem, and at some point I’ll port this to Bonescript, where I do have control over setting the internal pull-up and pull-down resistors, so any set of I/O pins can be used.