Communicating over serial to a stepper motor

asked 2016-07-12 02:21:16 -0600

Kjemu gravatar image

Hi,

I want to make a simple node to subscribe to a topic and publish/write these to a serial port. I have managed to send what i want and get the response i need but i can't implement this in a ROS node script. All the rosserial guides i have read is about arduino. However i only want to read and write to this port. Not edit whats on the other side of the ports software.

So basically i have a python script that send commands which gives me the wanted response. (Positioning of a stepper motor of type Nanotec PD2-n) But how can i implement this into a node?

Appreciate help in python and ROS (new to both)

import serial, fcntl, struc, time

#establish connection with serial port

ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)

# commands as following: "absolute mode" , "move to position 'angle' " , "execute"

commands = ['#1p2\r',
'#1s' + str(angle) + '\r',
'#1A\r',
]

if ser.isOpen():
    ser.flushOutput()
    ser.flushInput()
    for data in commands
        ser.write(data)
        response = ser.readline()
        print("read data: " + response)
ser.Close()
else:
    print "cannot open serial port"
edit retag flag offensive close merge delete

Comments

Not an answer, but some clarification: rosserial is not meant as a generic interface to a (set of) serial port(s), but rather as a convenience library that provides a minimal implementation of the ROS C++ client library that works on Arduinos (and other embedded systems).

gvdhoorn gravatar imagegvdhoorn ( 2016-07-12 02:47:24 -0600 )edit

It's called rosserial because most of the times, such systems will be connected to a more powerful 'host' via some sort of serial interface (RS232 or similar). To allow communication, it comes with a comms protocol that supports concepts similar to TCPROS, allowing pub/sub etc from an Arduino.

gvdhoorn gravatar imagegvdhoorn ( 2016-07-12 02:49:27 -0600 )edit

Yes, so rosserial will not be used in my case. Thanks for the clarification. I found this example: serial-example which i assume is something i want but in python.

Kjemu gravatar imageKjemu ( 2016-07-12 03:24:17 -0600 )edit

You want a python serial port library? something like https://pythonhosted.org/pyserial/ ?

If your code is already running the serial part you are missing the ROS communication part. Have you seen the ROS Python tutorials? http://wiki.ros.org/ROS/Tutorials/Wri... ?

fsteinhardt gravatar imagefsteinhardt ( 2016-07-12 09:25:22 -0600 )edit