• Example of controlling a BLDC motor without an encoder

import sarakitdevices as sk

#number of motor pole pairs
sk.BLDCMotor_PolePairs(0,11);

#When the movement is finished, use 10% of the motor power to maintain the position after 3 seconds
#Attention, BLDC motors get very hot, do not leave them running at too high power
#there is a risk of burns and fire!

sk.BLDCMotor_IdleTorque(0, 10, 2500);

sk.BLDCMotor_On(0,True);



print('180 degree rotation of motor 0 to the right')
sk.BLDCMotor_MoveByAngle(0, 180, 1, 40, True)
sk.sleepms(2000)

print('rotation to zero degrees')
sk.BLDCMotor_MoveToAngle(0, 0, 2, 40, True)
sk.sleepms(2000)

print('rotation to 180 degrees')
sk.BLDCMotor_MoveToAngle(0, 180, 2, 40, True)
sk.sleepms(2000)

print('rotation by -10.5 radians')
sk.BLDCMotor_MoveByAngle(0, -10.5, 10, 40, False)
sk.sleepms(2000)

print('continuous movement 5 seconds')
sk.BLDCMotor_MoveContinuousVelocity(0, 1, 40, 20, False)
sk.sleepms(5000)
   
sk.BLDCMotor_MoveStop(0)

GitHub Link: https://github.com/SaraEye/SaraKIT-Python-Examples/tree/main/SaraKIT-BLDC-Gimbal-Motor-FOC-Raspberry-Pi

 

 

  • Example of controlling a BLDC motor with an encoder

import sarakitdevices as sk

#number of motor pole pairs
sk.BLDCMotor_PolePairs(0,11);

#search for encoder zero position

#run once, get info
sk.BLDCMotor_InitFOC(0,0,0,0)
exit(0)
#and paste below
#sk.BLDCMotor_InitFOC(0,0,-1,1.268451);

#Attention, BLDC motors get very hot, do not leave them running at too high power
#there is a risk of burns and fire!

print('show position [radians]')
print("%.2f" % (sk.Encoder_Get(0).angle))

print('180 degree rotation of motor 0 to the right')
sk.BLDCMotor_MoveByAngle(0, 180, 1, 40, True)
sk.sleepms(2000)

print('rotation to zero degrees')
sk.BLDCMotor_MoveToAngle(0, 0, 2, 40, True)
sk.sleepms(2000)

print('rotation to 180 degrees')
sk.BLDCMotor_MoveToAngle(0, 180, 2, 40, True)
sk.sleepms(2000)

print('rotation by -10.5 radians')
sk.BLDCMotor_MoveByAngle(0, -10.5, 10, 40, False)
sk.sleepms(2000)

print('continuous movement 5 seconds')
sk.BLDCMotor_MoveContinuousVelocity(0, 1, 40, 20, False)
sk.sleepms(5000)
   
sk.BLDCMotor_MoveStop(0)

GitHub Link: https://github.com/SaraEye/SaraKIT-Python-Examples/tree/main/SaraKIT-BLDC-Gimbal-Motor-FOC-Raspberry-Pi

 

 

  • Example of synchronous control of BLDC motors

    Exactly like in this video:

import sarakitdevices as sk

#number of motors pole pairs
sk.BLDCMotor_PolePairs(0,11);
sk.BLDCMotor_PolePairs(1,11);

#search for encoder zero position

#run once, get info
sk.BLDCMotor_InitFOC(0,0,0,0)
#exit(0)
#and paste below
#sk.BLDCMotor_InitFOC(0,0,-1,1.268451);

#Attention, BLDC motors get very hot, do not leave them running at too high power
#there is a risk of burns and fire!

print('Rotate motor 0 (with encoder) - motor 1 will rotate exactly the same as motor 0')

while 1:
    e0=-sk.Encoder_Get(0).angle
    sk.BLDCMotor_MoveToAngle(1, e0*sk.RAD_TO_DEG, 1, 50, True)
    sk.sleepms(1)

sk.BLDCMotor_MoveStop(0)

GitHub Link: https://github.com/SaraEye/SaraKIT-Python-Examples/tree/main/SaraKIT-BLDC-Gimbal-Motor-FOC-Raspberry-Pi

 

 

  • Simple example of reading from Encoder (AS5048A, AS5600)

import sarakitdevices as sk;

sk.Encoder_On(0,True);

while 1:
    print("Angle in degrees: %.2f" % (sk.Encoder_Get(0).angleDeg));
    sk.sleepms(50);

GitHub Link: https://github.com/SaraEye/SaraKIT-Python-Examples/tree/main/SaraKIT-AS5048A-AS5600-encoder-Raspberry-Pi

 

 

  • Simple example of reading from LSM6DS3TR (temperature, accelerometer, gyroscope)

import sarakitdevices as sk

sk.Sensors_On(True)

while 1:
    InfoAccGyro=sk.getAccGyro()
    print("Temp: %.1f; AccX: %.2f; AccY: %.2f; AccZ: %.2f; GyroX: %.2f; GyroY: %.2f; GyroZ: %.2f; " % (sk.getTemperature(),InfoAccGyro.accX,InfoAccGyro.accY,InfoAccGyro.accZ,InfoAccGyro.gyroX,InfoAccGyro.gyroY,InfoAccGyro.gyroZ))
    sk.sleepms(50)

GitHub Link: https://github.com/SaraEye/SaraKIT-Python-Examples/tree/main/SaraKIT-LSM6DS3TR-Accelerometer-Gyroscope-Temperature-Raspberry-Pi

 

 

  • LED Ring

    if you want to connect the LED ring based on the ws281x system, connect 5V and GND respectively to the pins as described, and pin 0c to, for example, pin 13 on SaraKIT, then:
sudo apt-get install pip
sudo pip3 install rpi_ws281x
wget --backups=1 https://saraai.com/_SaraKIT/ledring.py
sudo python3 ledring.py -c
   



  • Simple example of reading from LIS3DH on Mainboard (accelerometer)

# from https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH
#
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
#
# sudo pip3 install adafruit-circuitpython-lis3dh

import time
import board
import busio
import adafruit_lis3dh

# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
# otherwise check I2C pins.
if hasattr(board, "ACCELEROMETER_SCL"):
    i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
    lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
else:
    i2c = board.I2C()  # uses board.SCL and board.SDA
    # i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
    lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)

# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
lis3dh.range = adafruit_lis3dh.RANGE_2_G

# Loop forever printing accelerometer values
while True:
    # Read accelerometer values (in m / s ^ 2).  Returns a 3-tuple of x, y,
    # z axis values.  Divide them by 9.806 to convert to Gs.
    x, y, z = [
        value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration
    ]
    print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z))
    # Small delay to keep things responsive but give time for interrupt processing.
    time.sleep(0.1)