Python Forum

Full Version: How to map 360 degree angle over 1024 counts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am interfacing an absolute encoder to a RPi following this guides:
https://hareshmiriyala.wordpress.com/201...pberry-pi/
https://github.com/HareshMiriyala/Absolu...encoder.py

The counts from 0-1023 are able to be shown but i am stuck at how to map out the angles to the counts. I tried using int function to divide but keep on getting errors. Any advice would be greatly.

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

PIN_CLK = 2
PIN_DAT = [3,14]
PIN_CS  = 4
delay = 0.0000005
ns = 1 # number of sensors attached
# totally 10 bits to be extracted from SSI signal
bitcount = 16

# pin setup done here
try:
    GPIO.setup(PIN_CLK,GPIO.OUT)
    GPIO.setup(PIN_DAT[:],GPIO.IN)
    GPIO.setup(PIN_CS,GPIO.OUT)                                                                                                    
    GPIO.output(PIN_CS,1)
    GPIO.output(PIN_CLK,1)
except:
    print "ERROR. Unable to setup the configuration requested"                                     

#wait some time to start
time.sleep(0.5)

print "GPIO configuration enabled"

def clockup():
    GPIO.output(PIN_CLK,1)
def clockdown():
    GPIO.output(PIN_CLK,0)
def MSB():
    # Most Significant Bit
    clockdown()

def readpos():
    GPIO.output(PIN_CS,0)
    time.sleep(delay*2)
    MSB()
    data = [0]*ns
    
    for i in range(0,bitcount):
        if i<10:
            #print i
            clockup()
            for j in range(0,ns):
                data[j]<<=1  
                data[j]|=GPIO.input(PIN_DAT[j])
            clockdown()
        else:
            for k in range(0,6):
                clockup()
                clockdown()
    GPIO.output(PIN_CS,1)
    return data

try:
    while(1):
        print readpos()
        time.sleep(0.001)
        #break
        
finally:
    print "cleaning up GPIO"
    GPIO.cleanup()
I didn't work with raspberry pi and embedded
python, but I suspect that PIN_DAT = [3,14] should be PIN_DAT = [3.14], if 3,14 stands for pi here...?!

def deg2count(angle, maxcount=1023):
    """ 0 <= angle <= 360 """
    return int(angle / 360.0 * maxcount)

def count2deg(count, maxcount=1023):
    return int(count / maxcount * 360)
Since your code includes series of numbers 3,1,4 (pi approx 3.14), I suspect that
angles should be given in radians; in this case my code should be changed, e.g. 360 must be replaced with, e.g. 3.1415 * 2.
Thanks for the suggestion. Imma try out it and get back to you. Have a nice day.
Hi, i managed to solve the issue albeit with a different code.
Line 60 changed to:
print ([x*0.3519 for x in readpos()])

Nonetheless, i greatly appreciate your attention and advice!