Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 12:12 pm

Hi,

I am new to the SSI (Synchronous_Serial_Interface) through Pico. I am facing some difficulties in the program and connections. I have connected my rotary encoder [https://www.rls.si/eng/fileuploader/dow ... D06_02.pdf][RLS] sensor Data+, Data-, Clock+, Clock- to [https://www.hwhardsoft.de/english/proje ... 85-shield/] [RS422] Zihatec interface.I connect the RS422 TX and RX to GP6 and GP5 of Pico pins, respectively.

I have searched for many solutions and followed online suggestions. I haven't succeeded as I resulted with 8191 CPR for my 13-bit magnetic rotary encoder all the time even I have turned my magnetic shaft.

Is there any issue with the connections and MicroPython program will be highly appreciated?

All solutions and suggestions are appreciated.

Thanks in advance. :D :)

with regards,
Nutakki

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 2:31 pm

Strange the documentation only specify 12Bit maximum and you have a 13 bits one!

What exactly is your part number? RM44SC0012B?

Start clock needs to be high between 12.5 and 20 us. Not sure if you left the clock high what it will do ?
Oops I read from right the left. I should be ok then. This mean that the clock need to be High at least 20 us before starting again.

Clock frequency needs to be higher lower or equal to 4MHz. Oops frequency not period. Thanks fdufnews!

The SPI should work! You need to set it to the correct bit size and edge detection.
Last edited by danjperron on Wed Jul 28, 2021 5:02 pm, edited 2 times in total.

fdufnews
Posts: 352
Joined: Fri Oct 07, 2011 5:37 pm

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 3:59 pm

Clock frequency needs to be higher than 4MHz.
No lower than 4MHz

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 5:44 pm

No lower than 4MHz
thanks fdunews.


if you are using 12bits sensor you will need to set the SPI to 13bits and forget the first MSB bit.

clock on pin 9 (GP6)
signal on pin 6 (GP4)

Code: Select all

import utime
from machine import Pin, SPI
spi = SPI(0,baudrate=2000000,sck=Pin(6),miso=Pin(4), polarity=1, phase=0,bits=13)

while True:
   value = spi.read(2)
    print(value)
    utime.sleep_ms(50)
UPDATE Need to investigate tonight. Looks that bit size is wrong. I need to put a scope on


A bug was on previous firmware. it was about the setting of the polarity of the clock. It was set only when you send the data the first time.
I will have to check if it was fixed.
viewtopic.php?f=144&t=306297&hilit=spi#p1841686

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 7:04 pm

After investigation inside micropython code I found out that the byte size is set to 8 bits for DMA transfer.
Looks like that we can't use 9bits and up on SPI.

Maybe leaving the code at 8bits size should work.

Something like that,

Code: Select all

import utime
from machine import Pin, SPI
spi = SPI(0,baudrate=2000,sck=Pin(6),miso=Pin(4),mosi=Pin(7),polarity=1, phase=0)

while True:
   value = spi.read(2)
   valueH = (value[0] & 0x7f) << 5  #remove first MSB keep 7 bits 
   valueL  = (value[1] & 0xf8) >> 3  #keep first 5 bits for 12 in total 
   print(valueH + valueL)                 #add  7bits MSB to 5 bits LSB
   utime.sleep_ms(50)


Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 11:10 pm

danjperron wrote:
Wed Jul 28, 2021 2:31 pm
Strange the documentation only specify 12Bit maximum and you have a 13 bits one!

What exactly is your part number? RM44SC0012B?

Start clock needs to be high between 12.5 and 20 us. Not sure if you left the clock high what it will do ?
Oops I read from right the left. I should be ok then. This mean that the clock need to be High at least 20 us before starting again.

Clock frequency needs to be higher lower or equal to 4MHz. Oops frequency not period. Thanks fdufnews!

The SPI should work! You need to set it to the correct bit size and edge detection.
Thank you for your quick reply :) .
Yes, mine is a 13 bit but the documentation is until 12 bit.
My part number is RM58SI0013B______
I think the clock frequency is correct as per the RLS sensor datasheet.

I guess the outputs from RS422 are RX, TX, RE of which I am using TX (Data Output from RS422), RX (Clock input to RS422) and not using RE.
Last edited by Nutakki on Wed Jul 28, 2021 11:30 pm, edited 1 time in total.

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 11:12 pm

fdufnews wrote:
Wed Jul 28, 2021 3:59 pm
Clock frequency needs to be higher than 4MHz.
No lower than 4MHz
Thank you for your quick reply :)
Yes, that's true.

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 11:28 pm

danjperron wrote:
Wed Jul 28, 2021 5:44 pm

if you are using 12bits sensor you will need to set the SPI to 13bits and forget the first MSB bit.
As I have mentioned in an earlier reply the magnetic encoder is 13 bit.
UPDATE Need to investigate tonight. Looks that bit size is wrong. I need to put a scope on
Please, let me know about the update.

A bug was on previous firmware. it was about the setting of the polarity of the clock. It was set only when you send the data the first time.
I will have to check if it was fixed.
viewtopic.php?f=144&t=306297&hilit=spi#p1841686
Thank you for that and let me know about the polarity of the clock.


Here is my connection diagram:

RS422 <--> RLS sensor
B <--> Data-
A <--> Data+
Z <--> Clock+
Y <--> Clock-
Shield <--> ----

RS422 <--> Pico
TXD (GPIO14, Pin 8) --> MISO
RXD (GPIO15, Pin 10) <-- SCK
GND <--> GND
+3V3 (Pin 1) <--> +3V3
+5V (Pin 2) <--> VBUS
+5V (Pin 4) <--> VBUS

Pico <--> RLS sensor
VBUS <--> Vdd
GND <--> GND

Can I have a complete code and all suggestions in the connection diagram are accepted? :D

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Wed Jul 28, 2021 11:39 pm

danjperron wrote:
Wed Jul 28, 2021 7:04 pm
Looks like that we can't use 9bits and up on SPI.

Maybe leaving the code at 8bits size should work.

Something like that,
I haven't understood the above statements.
As my magnetic rotary encoder is 13 bit then the output will be 8192 CPR or something like 360 degrees.

In this SPI library, https://docs.micropython.org/en/latest/ ... b.SPI.html
The bits = 8 or 16.

It will be helpful with the complete MicroPython code that defines the 13 bit. 8-)

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 1:00 pm

danjperron wrote:
Wed Jul 28, 2021 7:04 pm
Thanks for your code. :D
The code results in some values for

Code: Select all

bits =13 
which are incorrect.
I am not sure how to change to a 13-bit transfer.

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 1:13 pm

Looking at your connection diagram everything is about Raspberry Pi!

I assume that your shield is not connected to a Raspberry Pi, otherwise it won't work, and you connect the TX and RX to the pico instead.

TX will be the clock output to X/Z
RX will be the data signal from A/B


Then connect from the shield GPIO14(TXD) to pico GP6 (pin 9), GPIO15(RXD) to pico GP4 (pin6).

P.S. don't forget to put all the powers and gnd needed. You need a gnd between the pico and the shield!


Maybe the first thing to try is to read 16 bits and see if you get something.

this is the code you should try
import utime
from machine import Pin, SPI
spi = SPI(0,baudrate=2000000,sck=Pin(6),miso=Pin(4), polarity=1, phase=0)

while True:
value = spi.read(2)
print(value)
utime.sleep_ms(50)
Could you post the first 20 values of the result, move the sensor 180 degree and post again 20 values.


N.B. About 13bits
Receiving or sending 9 to 16 bits is not possible. Looking at the micropython source code it is stock with 8 bits or less on transfer. So to send stuff we need to send them in packet of bytes. The sdk in C allows 16 bits transfer.

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 1:52 pm

I am connecting Pico, Shield, Magnetic encoder.

This resulted even when rotated 180 degrees. I connect from the shield GPIO14(TXD) to pico GP6 (pin 9), GPIO15(RXD) to pico GP4 (pin6).

Code: Select all

b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
I connect from the shield GPIO14(TXD) to pico GP4 (pin 6), GPIO15(RXD) to pico GP6 (pin9).
At 0 degrees,

Code: Select all

b'\x7f\xff'
b'\x00\x7f'
b'\x7f\xff'
b'\x00\x00'
b'\x7f\xff'
b'\xf0\x00'
b'\xfc\x00'
b'\xff\xc0'
b'\x00\x00'
b'\x00\x03'
b'\x7f\xff'
b'\x00\x00'
b'\xf0\x00'
b'\x0f\xff'
b'?\xff'
b'\xff\xc0'
b'\x00\x00'
b'\x00\x01'
b'\xc0\x00'
b'\x00\x00'
At 180 degrees,

Code: Select all

b'\x00\x00'
b'\x00\x03'
b'\x7f\xff'
b'\x00\x00'
b'\x00\x00'
b'\x00\x00'
b'\x00\x1f'
b'\x7f\xff'
b'\x00\x00'
b'\x00\x00'
b'\x80\x00'
b'\x7f\xff'
b'\x7f\xff'
b'\x00\x00'
b'\x7f\xff'
b'\x00\x00'
b'\xe0\x00'
b'\x00\x00'
b'\x7f\xff'
b'\x00\x00'
I hope,
TX is the data output from RS422 which is the data input to Pico.
RX is the clock input to RS422 which is clock output from Pico.

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 2:56 pm

I hope,
TX is the data output from RS422 which is the data input to Pico.
RX is the clock input to RS422 which is clock output from Pico.
From the Raspberry Pi point of view TXD is the data out from PI to peripheral,
RXD is the data received from the peripheral to the PI.

Then on your shield TXD is in input (getdata from PI).
and and RXD is an output ( send data to PI).

Look at the schematic https://www.hwhardsoft.de/app/download/ ... 1571238851

from I.C. iso3080
pin 3 is output data from A/B --> directly to GPIO15 (RXD)
pin 6 is input to send data on X/Y --> directly to GPIO14 (TXD)

Now the question why values are all zero (Only the first set of values are correct)

What is you dip switch setting?
Attachments
iso3080.png
iso3080.png (13.18 KiB) Viewed 534 times

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 3:56 pm

Thank you for your reply. :)

The Dip switch settings for RS422 are as follows in the image:
*SW1: 4: Set GPIO18 to the high level to transmit protocols (represented in the datasheet).

I have arranged it as in the datasheet.
Attachments
DIP Switch.PNG
DIP Switch.PNG (96.27 KiB) Viewed 525 times

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 4:47 pm

Your switch are ok.

At this point you need to set a oscilloscope. Maybe the first thing is to try with a raspberry Pi.

The other method will to code PIO and create a real 13bits waveform instead of the SPI. It is not so hard.

best regards,

Daniel

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 6:44 pm

this is a PIO version . It is not tested.

Code: Select all

import utime
import rp2
from rp2 import PIO, asm_pio
from machine import Pin,mem32

#
#     CLOCK  A     B       B            B         C
#     ______      ___     ___     .....___     _________
#           \____/   \___/   \___/    /   \___/
#
#     DATA           D12     D11          D0
#     _________ ___________________......______       ________
#              \_________/\______/       \____/\_____/   
#     A = First clock (same has B but no data in)
#     B = 13*clock (store data info on falling edge)
#     C = data will stay low for at least 12.5 to 20 us after clock rising


FIFO_FSTAT=  0x50200004
    
@asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=13)
def Degree_PIO():
    set(x,13)                   # set x to 13 bits (loop)
    set(pins,1) [31]            # clock high for 31us
    set(pins,0)                 # clock low 1 us
    set(pins,1)                 # clock high 1 us
    label('Bitloop')
    set(pins,0)                 # clock low 1 us
    in_(pins,1)                 # get bit info anf shift to osr
    set(pins,1)
    jmp(x_dec,'Bitloop')
    label('endloop')
    jmp('endloop')
    
class position:
    
    def __init__(self,clockPin,dataPin,smID=1):
        self.dataPin = dataPin
        self.clockPin = clockPin
        self.smID = smID
        self.dataPin.init(Pin.IN, Pin.PULL_UP)
        self.clockPin.init(Pin.OUT)
        self.sm= rp2.StateMachine(smID)
        self.count=0
        self.data=[]

    def read(self):
        self.clockPin.value(1)
        utime.sleep_ms(100)
        #start state machine
        self.sm.init(Degree_PIO,freq=1000000,
                     set_base=self.clockPin,
                     in_base=self.dataPin)
        self.sm.active(1)
        return self.sm.get()
    
        
if __name__ == "__main__":
    from machine import Pin
    import utime
    capteur_data = Pin(4,Pin.IN,Pin.PULL_UP)
    capteur_clock = Pin(6,Pin.OUT)
    utime.sleep_ms(1)
    capteur = position(capteur_clock,capteur_data)
    while True:
        Value = capteur.read()
        print(Value)
        utime.sleep_ms(500)

Last edited by danjperron on Thu Jul 29, 2021 7:33 pm, edited 1 time in total.

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Thu Jul 29, 2021 6:49 pm

Thank you for your suggestion. At present, I have a Raspberry pi pico and will work on it.
I will update the outcome. :D :)

Nutakki
Posts: 10
Joined: Wed Jul 28, 2021 11:44 am

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Fri Jul 30, 2021 11:33 am

I obtain this output all the time even when I have turned to 180 degrees. :geek:
python output2.PNG
python output2.PNG (25.7 KiB) Viewed 468 times

danjperron
Posts: 3839
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SSI Communication using Raspberry Pico for Magnetic Rotary Encoder

Fri Jul 30, 2021 1:23 pm

ok looks like that the data lines is always high. You will need to put an oscilloscope on the signal and see if output and input lines works.

On my side I will simulate the sensor with another Pico and see if my PIO code works. I didn't check the signal yesterday.


B.T.W. for scope you could use pi scope on a raspberry pi. (it won't be able to check the rs242 but the signal at the PICO at least).
https://abyz.me.uk/rpi/pigpio/piscope.html

Return to “MicroPython”