I am trying to get communication between RPi3 Model B and Copley motor driver by using RS232 protocol. Copley driver replies only when ascii codes are sent to it. I want to send an ASCII code but I cannot figure out the problem. I am using TX-RX and GND pins of both devices to communicate them. I control RPi via local network by using ethernet cable (using VNC). Connection diagram like as follows; PC > ethernet cable > RPi3 > TX-RX-GND-5V pins to RS232-TTL converter pins > RX-TX-GND pins of Copley driver. I have tried kermit on Raspberry Pi but I failed. Do you have any idea how I can check whether connection exists between them or how I can send an ASCII character to the driver? Any suggested tiny Python codes?
-
2Based on the lack of real information provided we have NO idea what is actually connected or what you tried, or whether you have even enabled serial on the PI. PS Have you considered trying paragraphs?– MilliwaysCommented Jun 19, 2017 at 10:16
-
Hello Milliways 2, I need really help for this issue and I am ready to provide any information that I should provide. Please just direct me.– Mustafa GülerCommented Jun 19, 2017 at 12:53
-
For starters what is a "Copley motor driver"; what "RS232-TTL converter" (which appears to have no power connection); what code? Look at other questions.– MilliwaysCommented Jun 20, 2017 at 0:09
-
Copley motor driver is just a development tool and it can be controlled via serial communication protocol which is RS232. RS232-TTL Converter: As yo know RPi uses 3.3V level but on the other hand Copley motor driver uses 15V level. Therefore, RS232-TTL converter is used not to burn RPi pins. Of course power connection exists for all equipments but I did not show, sorry for that. I have tried below code but I am not sure about the code because I am noobie for coding.– Mustafa GülerCommented Jun 20, 2017 at 6:36
-
import serial import time receiver = serial.Serial( port='/dev/ttyS0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) while 1: x = receiver.readline() print (x) sender = serial.Serial( port='/dev/ttyS0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) while 1: sender.write('r') time.sleep(1)– Mustafa GülerCommented Jun 20, 2017 at 6:37
2 Answers
Finally I solved the problem by installing gtkterm on Raspberry Pi.sudo apt-get install gtkterm
After installing gtkterm I can send ASCII codes to the serial device (motor controller) and rotate the servo motor :)
Now all I need to write a Python program to control the motor. Can anybody help me about how I can write codes of gtkterm on Python?
This program is written by Mustafa Güler who is Electrical and Electronics Engineer at Teknolus Enerji located in METU Technopolis MET Campus. The main goal of this program is to make communication between RPi 3 Model B and Copley Motor Driver via serial communication protocol to drive a motor.
This tutorial is prepared to learn how to drive a motor by using RPi3 Model B and Copley motor driver.
First of all, cable connections should be prepared properly. The most important issue is to make cross-connection between TX and RX pins of RPi and Copley motor driver. Secondly, LEDs and a breadboard or oscilloscope can be used for debugging. Finally, gtkterm should be installed on Raspberry Pi by using “sudo apt-get install gtkterm” command to make a serial connection between RPi3 and Copley motor driver.
2017.06.28_Motor Control by using RPi3 and Copley_Fritzing 2017.06.28_Motor Control by using RPi3 and Copley_Photo
Here is the Python code to control motor by using RPi3 and Copley motor driver
#This program is written by Mustafa Güler who is Electrical and Electronics Engineer
#at Teknolus Enerji located in METU Technopolis MET Campus.
#The main goal of this program is to make communication between RPi 3 Model B and
#Copley Motor Driver via serial communication protocol
import serial
import time
#Defining the port parameters
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3.0)
#Defining the Copley parameters
#Variables____________________bytearrays____________________________________________Copley Parameters
get_actual_motor_position = [103, 32, 114, 48, 120, 51, 50, 13] # g r0x32
absolute_move = [115, 32, 114, 48, 120, 99, 56, 32, 49, 13] # s r0xc8 1
set_position = [115,32,114,48,120,99,97,32,49,48,48,48,48,48,13] # s r0xca [desired position; i.e. 100000]
abort_move = [116, 32, 48, 13] # t 0
initiate_move = [116, 32, 49, 13] # t 1
initiate_homing = [116, 32, 50, 13] # t 2
amplifier_reset = [114, 13] # r
disable_amplifier = [115, 32, 114, 48, 120, 50, 52, 32, 48, 13] # s r0x24 0
get_bus_voltage = [103, 32, 114, 48, 120, 49, 101, 13] # g r0x1e
get_amplifier_temperature = [103, 32, 114, 48, 120, 50, 48, 13] # g r0x20
get_serial_baud_rate = [103, 32, 114, 48, 120, 57, 48, 13] # g r0x90
set_function_gen_to_sine = [115, 32, 114, 48, 120, 57, 56, 32, 50, 13] # s r0x98 2
set_freq_for_function_gen_Hz= [115, 32, 114, 48, 120, 57, 57, 32, 49, 13] # s r0x99 1
set_current_amplitude = [115, 32, 114, 48, 120, 57, 97, 32, 49, 48, 48, 13] # s r0x9a [current value; 100 for x0.01A]
while True:
#Read the port information
read_port = port.readline()
#"s r0xca 100000" set position
port.write(bytearray(set_position))
print('set_position>>>', read_port)
time.sleep(1)
#"t 1" initiate move
port.write(bytearray(initiate_move))
time.sleep(2)
print('initiate move>>>', read_port)
#"g r0x32" get actual motor position
port.write(bytearray(get_actual_motor_position))
print('actual motor position>>', read_port)
time.sleep(1)
port.close()