Hello,
I wanted to try to handle a new library a long time ago but presently. Does that make sense? Maybe so or maybe not but the source works and so does the machinery along w/ the electrical board!
...
So, I have altered the source along w/ some help from years back! I have also made a small script in python3 to handle the motion of the small stepper motor(s) along w/ a slide and ball screw...
So, this simple task needs these components:
- 12VDC Lead Acid Battery and/or SLA type
- Motor Bridge Cape
- Two Wires for Power and GND to and from the Cape and Battery
- Two quick disconnects of the correct size...use whatever disconnect fits your battery terminals!
- BeagleBone Green Gateway or other board from beagleboard.org family of boards, i.e. excludes the BBBlue, BBAI, and/or PocketBeagle unless you want to make it work for those boards (it is not plug and play with those three boards).
- Oh...you may need a small slotted screwdriver to handle the connectors on the Cape.
- Micro USB to USB Type A cable
- Ethernet or Wifi access to get the scripts and other tools/utilities
Okay...so we may or may have not gathered our tools so far. If we have and if we are ready to start, let us start.
...
...
Here you can see the wires along w/ the quick disconnects I had chosen for this project.
Also...there is an interactive Python3 script that handles our stepper motor usage:
https://github.com/silver2row/MotorBridgeCapeforBBG_BBB/tree/master
Above you will see my fork of the Seeed Studio Motor Bridge Cape lib.
I changed some of the items to handle specific data transitions, library changes, and libraries being deprecated. I also added a few libs. and changed them too. Luckily, w/ some little experience in this hardware realm and adding time to that idea, I have been able to adjust to changes easily.
...
So, let us go through the changes:
On MotorBridge.py, let us change some specific lines...
import Adafruit_GPIO.I2C as I2C
import Adafruit_BBIO.GPIO as GPIO
import time
Reset = "P9_23"
MotorBridge = I2C.Device(0x4b, 2)
GPIO.setup
(Reset, GPIO.OUT)
This source turns into this idea:
from smbus2 import SMBus
import time
import pathlib
# reset pin is P9.23, i.e. gpio1.17
reset_pin = pathlib.Path('/sys/class/gpio/gpio49/direction')
reset_pin.write_text('low')
bus = SMBus('/dev/i2c-2')
Now, this is on the 4.19.x kernel from their updated image from 2020 on their bbb.io/latest-images page online.
...
And this...
def WriteByte(Reg, Value):
data = [0 for i in range(2)]
data[0] = Reg
data[1] = Value
MotorBridge.writeList(WriteMode,data)
def WriteHalfWord(Reg,Value):
data = [0 for i in range(3)]
data[0] = Reg
data[1] = Value & 0xff
data[2] = (Value>>8) & 0xff
MotorBridge.writeList(WriteMode,data)
def WriteOneWord(Reg,Value):
data = [0 for i in range(5)]
data[0] = Reg
data[1] = Value & 0xff
data[2] = (Value>>8) & 0xff
data[3] = (Value>>16) & 0xff
data[4] = (Value>>24) & 0xff
MotorBridge.writeList(WriteMode,data)
def SetDefault():
WriteOneWord(CONFIG_VALID,0x00000000)
class MotorBridgeCape:
def __init__(self):
GPIO.output(Reset, GPIO.HIGH)
time.sleep(1)
We will change this source to handle specific libs. chosen at the time of writing this lib. We are going to use smbus2 instead of whatever they used and pure python instead of Adafruit_BBIO b/c of its deprecated quality.
def WriteByte(Reg,Value):
data = [0 for i in range(2)]
data[0] = Reg
data[1] = Value
bus.write_i2c_block_data(0x4b, 1, data)
def WriteHalfWord(Reg,Value):
data = [0 for i in range(3)]
data[0] = Reg
data[1] = Value & 0xff
data[2] = (Value>>8) & 0xff
bus.write_i2c_block_data(0x4b, 1, data)
def WriteOneWord(Reg,Value):
data = [0 for i in range(5)]
data[0] = Reg
data[1] = Value & 0xff
data[2] = (Value>>8) & 0xff
data[3] = (Value>>16) & 0xff
data[4] = (Value>>24) & 0xff
bus.write_i2c_block_data(0x4b, 1, data)
def SetDefault():
WriteOneWord(CONFIG_VALID, 0x00000000)
class MotorBridgeCape:
def __init__(self):
reset_pin.write_text('high')
time.sleep(1)
So, to make this work. We need to perform some specific tasks:
1. change smbus2 lib. on line 304 to handle i2c-2 instead of arbitrary i2c addresses and numbers.
2. https://github.com/kplindegaard/smbus2/blob/master/smbus2/smbus2.py#L304 is where you can find line 304.
filepath="/dev/i2c-2".format(bus)
Line 304 in the pip3 install or python3 -m pip install is located in /.local/lib/python3.7/site-packages/
At least on my board, this is where I have listed smbus2 after the install w/
pip3 install smbus2
or...
python3 -m pip install smbus2
3. Now, used the crimpers. Make sure the quick disconnects do not come completely off when applying high amounts of pressure w/ pulling and grabbing.
4. Use the screwdriver. Loosen the screws w/ the slotted screwdriver on the connectors on your Motor Bridge Cape. Slide in one wire at a time. Actually, let us try a couple of tests real quickly to make sure our wires are aligned correctly...
5. With your DMM (Digital Multimeter) or MM (Multimeter), go to the setting for continuity and test. Go to one wire on the stepper, one out of four wires, and attach your Common GND from your DMM. Now, try another wire w/ Voltage.
One should here the beeping of the DMM/MM when the wires are in the same bracket.
Here is another option if searching for basics and good general info. on stepper motors: https://en.wikipedia.org/wiki/Stepper_motor.
6. Once we have configured our motor(s) correctly and tested which wires go w/ what other wires, then we can place them one by one in the connectors on the Cape to the BBGG. Tighten tightly so that the wires do not come loose when running the software. No one needs a 12v shock or arcs.
7. On the Cape, there is a Working-Standby switch. While not using software, it is in good practice to be in standby mode. Then, when ready, flip the switch and run the source.
8. git clone https://github.com/silver2row/MotorBridgeCapeforBBG_BBB
This way, we can change out all that source from above that I mentioned in the MotorBridge.py file.
9. Prepping never felt so good!
10. Now, the source:
#!/usr/bin/python3
import MotorBridge
from time import sleep
motor = MotorBridge.MotorBridgeCape()
motor.StepperMotorAInit()
class board:
def StepperMotorATest():
core = int(input("Please provide a numerical value between 0 and 100\n "))
if core >= 85:
motor.StepperMotorAMove(2500, 1000)
sleep(5)
motor.StepperMotorAMove(-2500, 1000)
sleep(2)
print("Making 2500 steps in one direction and back again at 100% duty! ")
elif core <= 84:
motor.StepperMotorAMove(1000, 1000)
sleep(5)
motor.StepperMotorAMove(-1000, 1000)
sleep(2)
print("Making 1000 steps and back again at 100% duty! ")
else:
motor.StepperMotorAMove(500, 800)
sleep(5)
motor.StepperMotorAMove(-500, 800)
sleep(2)
print("Making 500 encoder counts and back again at 80% duty! ")
print("Starting the Script, try to press Control-C to quit!\n")
try:
while True:
board.StepperMotorATest()
sleep(5)
except KeyboardInterrupt:
print("You may have just pressed Control-C!\n")
A lot of this source is for aesthetic reasons and to get familiar w/ the BBGG and Motor Bridge Cape. I have not seen a MBCape selling recently. Maybe on Ebay or something, one could attain one. Who knows?
Seth
P.S. But...there is a class, function, if-elif-else statement, and a while loop for testing what we input as a numerical instant.
If I have forgotten anything, please do comment or share what you have learned.
...
Comments