Sample code for python SPI encoder communication using Raspberry Pi. The sample is specifically built to work with Westwood Robotics MPS encoders, but feel free to modify it to fit other SPI encoders.
The pinout and dimentions of the Westwood Robotics MPS encoder is as shown below:
Connect the pins accordingly to your Pi, or any compatible device of your choice.
The connector is a 6-pos JST connector with part # BM06B-SRSS-TB(LF)(SN). Board thickness is 1.2mm.
Install wiringpi and spidev packages before using.
-
Add these files into your project and just simply put
from MPS import *
at the beginning of your code and you are good to use this package. Refer to 'sensor_orientation_update.py' and 'simple_read.py' as some sample usage. -
Even though there are only 2 CS pins for SPI communication on Pi, you can still link more than 2 devices by using GPIO pins as CS pins. When doing so, please set
gpio
toTrue
when creating anMPS_Encoder
instance and use GPIO numbering for your CS pin number. -
Use
MPS_Encoder
with single encoder; useMPS_Encoder_Cluster
if you wish to bundle several encoders using GPIO for ChipSelect. -
AVOID USING PIN CE0.
Enjoy!