Hello I'm a newbie with Python but I've been interested on it for 2 years. I want to make robots and I'm trying to use Python with pyserial
on blender. But I found a problem and after 2 hours of looking for the answer in Google and in this site I found that maybe I'm retarded because I can't solve it. I think it isn't asked yet.
I'm using a devantech sd84 servo controller and controling it via a USB port a serial device so I use pyserial
. The problem is that I want Python to take a decimal value fom a user input between 400
-2200
and I know how to do it but I need Python to show it as two bytes and send the low byte first and then the high byte. for example (pseudo code as I don't how to program it yet):
#ask for a decimal number between a range (the range does not really matters)
x = raw_input('\tInsert a number between 400-2200:') #Insert a number between 400-2200: 1500
#show it as hex
hex(x) #5dc
#put it in two bytes and in this case add a zero(?) I don't know how to do that.
0xDC 0x05
#add it to a 16-bit integer to send it to the servo controller via the virtual serial port(?) i also don't know how to do that.
ser.write('\xAA\xA0\x55\x01\x01\x02\xDC\x05')
For the interest of people who have the same controler I'll explane that 16bit integer first three bytes are sync (\xAA\xA0\x55)
then the type of command SET_SERVO (position) (\x01) then the channel 1-84 for 1 is (\x01) then a byte count in this case 2 (\x02) and the servo position low byte and then the high byte (\xDC\x05) which nowadays I calculate with in my iPod with an app and then I insert manually hahaha I want to stop whith that stupid thing and let he compuer do it for me.
Now I will post a code I made for testing purposes it mades a servo in channel 1 go troug it's full range ata different speeds and print funny things in spanish while the servo "dances" I manually calculated the positons and inserted them hahaha sounds like history for me.
# -*- coding: utf-8 -*-
#Pablo P. 2012 "bailecillo"
#mueve un servo en el canal 1 a través de todo su recorido e imprime frases mientras dicho servo "baila"
import serial
import time
# Para cambiar de Sistema Operativo cambiar puerto
#en la siguiente línea: Win COM# linux /dev/ttyS# /dev/ttyUSB#
# #=un número asignado por tu sistema.
port='COM3'
sync='\xAA\xA0\x55'
SET_SERVO='\x01'
GET_SERVO='\x02'
SET_SPEED='\x03'
SET_MODE='\x04'
GET_MODE='\x05'
SET_AD_CNT='\x06' #Controla el número de canales analógicos.
GET_AD_CNT='\x07' #Devuelve el número de canales analógicos actuales.
GET_INPUT='\X08' # Devuelve el estado de una entrada.
GET_ADC='\X09' #Devuelve el valor de un canal analógico.
GET_VERSION='\x0A' #Devuelve la versión del procesador seleccionado.
GET_BULK='\x15' #Usado para test en fábriica.
TEST='\X16' #Usado para test en fábrica.
ser = serial.Serial(port, baudrate=115200, bytesize=8, parity='N', stopbits=2,timeout=1)
print "Hola! me alegro de verte."
time.sleep(2)
if ser.isOpen():
print "Estado del puerto: Correcto."
time.sleep(1)
print "Procedo a enviar modo del canal 1 y posicion(es) del mismo."
time.sleep(3)
print "Comprobando sistemas de baile..."
ser.write(sync+SET_MODE+'\x01\x01\x19')
ser.write(sync+SET_SERVO+'\x01\x02\x98\x08') #izda
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x90\x01') #derecha
time.sleep(1)
ser.write(sync+SET_SPEED+'\x01\x01\x10')
ser.write(sync+SET_SERVO+'\x01\x02\x78\x05') #centro
time.sleep(2)
print "Vamos a bailar!"
time.sleep(2)
print "preparados..."
time.sleep(1)
print "listos..."
time.sleep(1)
print "Yaaaa!!"
ser.write(sync+SET_SERVO+'\x01\x02\x98\x08') #izda
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x90\x01') #derecha
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x98\x08') #izda
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x90\x01') #derecha
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x98\x08') #izda
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x90\x01') #derecha
time.sleep(3)
ser.write(sync+SET_SERVO+'\x01\x02\x78\x05') #centro
time.sleep(3)
ser.write(sync+SET_SPEED+'\x01\x01\x00')
ser.close()
time.sleep(1)
print "Todo el proceso completado correctamente."
else:
print "El puertito está cerrado"
print "Hasta Luego :D"