Python Forum
How to map 360 degree angle over 1024 counts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to map 360 degree angle over 1024 counts
#1
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()
Reply
#2
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.
Reply
#3
Thanks for the suggestion. Imma try out it and get back to you. Have a nice day.
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 251 Feb-01-2024, 12:48 AM
Last Post: highland44
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,380 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,849 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,568 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  PyPi Download Counts and Warehouse Migration? tlee0818 1 1,289 Mar-20-2022, 07:41 PM
Last Post: snippsat
  How do I get the tangent of a degree SheeppOSU 3 4,553 Oct-24-2019, 02:13 PM
Last Post: ichabod801
  Converting Angle to X and Y Values: 90/180/270 deg qrani 1 2,763 Nov-21-2018, 06:41 PM
Last Post: woooee
  finding angle between three points on a 2d graph qrani 4 14,067 Nov-20-2018, 06:10 AM
Last Post: Gribouillis
  How do I create a timer that counts down? LavaCreeperKing 4 3,205 Jun-20-2018, 05:23 PM
Last Post: LavaCreeperKing
  Angle kripso 12 31,297 Oct-30-2017, 11:33 PM
Last Post: kripso

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020