Skip to content

SerialTransfer not being accessed #105

@ericshear26

Description

@ericshear26

Hi, PowerBroker2. I am trying to figure out an issue with pySerialTransfer. I'm on an Apple Silicon Mac and installed the packages with pip3, and my Python was installed from python.org, if that helps.

My intention is to send an array of integers to an Arduino for it to run a stepper motor using that array. The array-generation part of the code works well.

The class SerialTransfer does not work when I run my code. It keeps returning the error message "ImportError: cannot import name 'SerialTransfer' from 'pySerialTransfer' (/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/pySerialTransfer/init.py)".

I looked at the pySerialTransfer.py text file and it does have a class called SerialTransfer so I don't understand what's going on here.

Here's my code.

import numpy as np
import pySerialTransfer
from pySerialTransfer import SerialTransfer
import struct

A = 50.0 # amplitude in mm
r = 19.0 # radius of gear in mm (divide pitch diameter by 2)
f = 0.1 # frequency in Hz (between 0 and 1)
T = 1/f # period of one cycle in seconds
base_step_angle = 0.031416 # base step angle of stepper motor in radians
gear_ratio = 26.85123967 # gear ratio of gearhead
microstepping = 8 # microsteps per step
interval = 100
deltat = 1/interval # time increment in seconds

numberOfSteps = 1/((base_step_angle/gear_ratio)/microstepping) # microsteps per radian
omega = 2.0*np.pi*f # in rad/s

t = np.linspace(0,T,interval)
z = A*np.sin(omega*t) # mathematical position of linear motion in mm
v = A*omega*np.cos(omega*t) # mathematical velocity of linear motion in mm/s
a = (-A*np.square(omega))*np.sin(omega*t) # mathematical acceleration of linear motion in mm/s^2
d = v*deltat + 0.5*a*np.square(deltat) # linear displacement in mm
delta_theta = d/r # angle that motor moves between z positions, in radians
steps = np.round(delta_theta*numberOfSteps) # number of steps to move between positions
int_steps = steps.astype(int)  # Convert data into integers

link = pySerialTransfer.SerialTransfer('/dev/cu.usbmodem83201', 115200)

while True:
    data = struct.pack('<' + 'i' * len(int_steps), *int_steps) # format data with struct
    size = struct.pack('<i', len(int_steps)) # send data size first
    link.send(size)
    link.send(data)

Thanks in advance.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions