RoadTest: Trinamic TMCM-0960-MotionPy SBC + Stepper Motor
Author: vav
Creation date:
Evaluation Type: Development Boards & Tools
Did you receive all parts the manufacturer stated would be included in the package?: True
What other parts do you consider comparable to this product?: This would be comparable to Arduino and other micro-controllers like the feather from Adafruit, but running MicroPython. I imagine it is somewhat similar to CircuitPython boards from Adafruit but with a higher voltage tolerance, but I have not worked with CircuitPython so I am uncertain as to the amount of similarities between the two.
What were the biggest problems encountered?: My biggest problems were mainly caused by my lack of experience with the TMCL-IDE, my oxidized Python scripting skills, and my silly groups permissions error.
Detailed Review:
For my Road Test of the Trinamic TMCM-0960-MotionPy single board computer coupled with a PD42-1270 and a stepper motor via CAN bus; I wanted to create a simple motion-tracking “head” using the Pixy2 from CMU and Charmed Labs.
The MotionPy SBC is a marvelous little development board for those who want to work with MicroPython, especially when it is paired with a module like the PD42-1-1270 that came in the test kit. This module is a smart stepper motor controller / driver attached to a 0.27nM, 24Volt NEMA17 stepper motor. Once I got the 0960 and my fresh Kali Linux distro set up, it was a breeze to get the LEDS blinking away with “blinky.”
Just an important note to those of you out there who are working in linux; don’t forget to add your user to necessary groups: tty and dialout. Open the command line and type "groups" to check which groups your user has permissions for. If you do not see tty and dialout you can add yourself to them by:
sudo usermod -a -G tty yourUserName
And/Or
sudo usermod -a -G dialout yourUserName
Of course there are far better resources if you need more information about changing user group permissions in the Linux CLI and I suggest you look into them before just running some code from the internet.
As part of this Road Test, I set up a new linux system on an old friend [the ASUS laptop] that was returned to me, now that my partner has upgraded. Hooray for us both! An old laptop with a new OS is like a new laptop, and I have been itching to build a new linux box. I wanted to tool around in the latest Kali release [for reasons outside of the scope of this write-up], but I needed to add some plain Debian buster into the mix to get some of the peripheral software I use to install correctly. So for the sake of this write-up, I’ll say that I am running a lightly customized xfce version of Debian. In the fun of getting things to run on Kali that were not ported from vanilla Debian, I forgot all about dialout and wasted many hours searching for answers to the wrong questions while simultaneously creating lofi beats by slamming my head against my clacky & classic Fujitsu PS-2 keyboard. Don’t make my mistake and check your groups permissions.
After a bit of code reading [and some trying to remember Python] I was able to modify the main.py and MotionPy.py scripts to create my own shortcuts to other example code. To do this, one must simply modify the MotionPy.py script located in the /PyTrinamicMicro/platforms/motionpy1/ from the root of the SD card to include other scripts inside the _MAP_SCRIPT function. I was impressed by how simple the process of controlling the stepper motor was within Trinamic’s implementation of MicroPython; with the example rotation code as a place to start I was able to quickly implement a simple color tracking protocol that turned the camera to try and center its view on the largest target object. The rotate script provided by Trinamic does most of the heavy lifting here, I have just set up the PA4 GPIO pin as an analog input pulled low awaiting the 0 to 3.3V analog value provided by the Pixy2. I did some digging through the documentation from micropyhton.org and found what I needed to implement the ADC class [Analog / Digital Converter] and the ADC.read_u16 method which returns a 0-65535 integer value. In my code this was saved as a string variable val which I converted to an integer stored in locX and this value was used in a simple if/elif/else inside of a for loop. I essentially have the MotionPy decide if the head should turn one way or the other based upon whether the value read with the ADC.read_u16 method is greater or less than 32,768 which is half of the possible values.
Now, I know that this is a very imprecise [and quite jittery] kludge, but it is at least a starting point for my next project. I would like to take better advantage of Trinamic’s advanced stepper control features once I work out a better algorithm for setting the rotation that takes into account how far out of center the target object is, some ramping, and larger center “stop” soft spot. I am sure there are errors in my code and I am always open to learning. I am certain that I am not even close to taking full advantage of these two brilliant pieces of technology, but I am happy to have had time to play with them. Python has not been in regular rotation in my coding for some time and I have been working in the Arduino IDE with C on my projects, so I am sure to have made some mistakes beyond my overly simplified algorithm. This project has reawakened my desire to hone my Python and MicroPython chops and I am glad for that as well.
I spent quite a while lost in the weeds of trying to set up a working SPI connection between the Pixy2 and the MotionPy, but I was running out of time to complete this Road Test. So, I decided that the solution was to simplify the data that was being communicated between the two devices. The Pixy2 has an output option that transmits an x coordinate value for the largest cluster of a target color while running the Color Connected Components algorithm. I imported this data into my own modified version of the rotate example script for the 1270 module and converted that value to a decision to turn the head one way or another. As a complete newcomer to Trinamic systems, I found the TMCL-IDE more daunting than working in the Python REPL through screen on the command line and my Road Test focused more on that interface and running Python commands live on the MotionPy. I really enjoyed being able to test scripts on the fly from the command line and it was all the interface I needed to get my project to this point, but I'll be digging deeper into the TMCL-IDE in the future.
Just a few notes about the Pixy2, color tracking and my choices seem appropriate here. I used the Color Connected Components algorithm all wrong in this project as I trained it upon my skin tone, when it is intended to track bright and distinctly different colors. It sometimes gets confused between my skin tone and a certain amount of reflected light off of my orange walls, but it managed to follow my hand well enough for this demonstration. I would suggest using something like what is suggested by the Pixy cam makers: a purple dinosaur toy, or some other easily distinguishable solid-colored object. If someone wanted to replicate my project, they will need to set the data out port in the pixymon software provided by CMU and Charmed Labs to “analog/digital x” in the interface tab of the configure settings. The stepper motor is only rated for 24Volts, so I decided to use a power supply comprised of 14 AA batteries as I already had the correct combination of battery holders. The power pack with fresh batteries tests out to just over 22Volts, so within the tolerances for the project and I also imagined AA batteries would be more easily sourced than a custom rechargeable power pack [which is what I plan to construct for the next project involving the MotionPy]. This power supply was sufficient for my testing purposes, as long as I remembered to turn it back off [which I learned to double check after accidentally leaving the batteries on while the blinky script was running for several hours; draining out the first set of batteries. Even with the USB connected to a computer, the battery pack will discharge if left “on”].
I really appreciate the opportunity to participate in this Road Test, my first here. I am still testing this system out and I will post some updates as I make interesting progress. I hope my sharing of this simple project helps you, the community, in some way. Or at least inspires you to jump in headfirst into your own project. I’ve learned quite a bit already, but I have a long way to go before I see this project as a success. It was a successful first step at least. For now I’ll share a plain text version of my python kludge based on the example script found in /PyTrinamicMicro/platforms/motionpy1/examples/modules/TMCM1270 folder:
'''
Rotate the motor with TMCM1270 using CAN interface.
Rotation is loosely controlled by analog output of x-coordinate by pix2
Created on 05.10.2020
Modified on 07.07.2021 by vav
@author: LK
'''
from PyTrinamic.modules.TMCM1270.TMCM_1270 import TMCM_1270
from PyTrinamicMicro.platforms.motionpy1.connections.can_tmcl_interface import can_tmcl_interface
from pyb import Pin
import time
import machine
con = can_tmcl_interface()
module = TMCM_1270(con)
rot=0
a4 = Pin(Pin.cpu.A4, Pin.ANALOG, Pin.PULL_DOWN)
adc = machine.ADC(a4)
for i in range(100):
val = adc.read_u16()
print(val)
locX = int(val)
print(locX)
if(locX>32768):
rot=500
elif(locX<32768):
rot=-500
else:
rot=0
module.rotate(0, rot)
time.sleep(1)
else:
module.stop(0)
con.close()
Top Comments
Nice project vav!
Right, it isn't straight forward to setup SPI connection for custom devices in MotionPy and reworking has to be done with micropython scripts to initialise, communicate and transmit data…
Nice road test report.
DAB