Shop OBEX P1 Docs P2 Docs Learn Events
how to use python to communicate with the propeller — Parallax Forums

how to use python to communicate with the propeller

tonocastillotonocastillo Posts: 12
edited 2013-01-16 06:09 in Propeller 1
Hello,

I want to control a couple of servos using the PC and python 2.7.3.
I am using pySerial to perform serial communication with the propeller
For debugging, I have been trying to use just 1 servo.

This is the code on the propeller side:


[HTML]CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000

pin = 14 'test servo pin
'width = 1000 'not used for the moment
delay = 300 'not used for the moment


OBJ
servo : "servo32v7"
pst : "parallax serial terminal"

PUB main | width

servo.start
'servo.ramp 'not used for now
pst.start(115200)
waitcnt(clkfreq*2+cnt) 'waits 2 seconds

repeat
width := pst.DecIn 'this gets a value between 500 and 2500 to move the servo to the desired position
servo.set(pin, width)
waitcnt(clkfreq+cnt)
[/HTML]

I think this code is fine, because if I connect to the propeller using the python miniterm tool and send values between 500 and 2500, the servo moves.
This is the python miniterm command I'm using:

[HTML] python -m serial.tools.miniterm -p COM6 -b 115200[/HTML]

As mentioned, with this tool, I can move the servo using the PC.

Now, when I try to do the same using a small python program, the program runs "fine", because I don't get an error, but the servo won't move. (so it's not fine:D )

This is the python code I'm using:


[HTML]import serial
import time
ser = serial.Serial()
ser.port = "COM6" # COM port on the laptop
ser.baudrate = 115200 # same baudrate specified in the propeller
ser.open() # opens port COM6



connected = ser.isOpen()
print "connected to the propeller? %s" % connected #confirms connection to the propeller port

print "a number between 500 and 2500: "
servoval = raw_input() #receives value (the problem might be after this line but not sure what is it

ser.write(servoval)

ser.close()[/HTML]

So, I run this little python program and the servo won't move.
If I do some testing on the python console, this is what I get:

[HTML]Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> import time
>>> ser = serial.Serial()
>>> ser.port = "COM6" # COM port on the laptop
>>> ser.baudrate = 115200 # same baudrate specified in the propeller
>>> ser.open() #opens port COM6
>>> ser.write("2000")
4L
>>>[/HTML]

What does this "4L" means?
What am I missing here?

I have been looking for more information, but seems like other people has been using the same thing without any problems.
Any advice on how to debug what I am actually sending with python?

I appreciate any help.

Thanks.

-Tono

Comments

  • SRLMSRLM Posts: 5,045
    edited 2013-01-15 20:32
    According to the pySerial API, the return value of write is the number of bytes written (http://pyserial.sourceforge.net/pyserial_api.html). So, it looks like you wrote 4 bytes.

    Looking in the ParallaxSerialTerminal file under decIn, we have:
    PUB DecIn : value
    {{Receive carriage return terminated string of characters representing a decimal value.
      Returns: the corresponding decimal value.}}
    
      StrInMax(@str_buffer, MAXSTR_LENGTH)
      value := StrToBase(@str_buffer, 10)
    

    So, it looks like your problem will be resolved if you add a carriage return:
    ser.write(servoval)
    ser.write('\n')
    

    If you want to debug what you are sending with the Python script, I would get an FTDI based USB to serial converter (Prop plug or the like), and hook up it's RX line to the TX line that comes from the USB to serial that is connected to the Propeller. Open up a terminal on the new USB to Serial connector, and try sending data from your python program.
  • tonocastillotonocastillo Posts: 12
    edited 2013-01-15 20:54
    I tried that and didn't work

    But then I tried:


    [HTML]ser.write(servoval + '\r\n')
    time.sleep(1.5)

    #ser.write('\n')
    ser.close()
    [/HTML]

    I had to add that pause and it seems to be working fine now.

    Thanks for the tip :D
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-01-15 23:08
    I don't know python serial, but I think that write is not doing a conversion to an ASCII-string. You have to convert servoval to a string and then send the string to the propeller.

    Or you use raw datatypes on propeller-side as well. This would improve transmission-speed, but you no longer have the possibility to attach with a terminal program and simply test the propeller code manually.
  • tonocastillotonocastillo Posts: 12
    edited 2013-01-16 06:09
    I tried converting the values to string and it did not do the job.
    Once I added the '\r\n', I was able to move the servo.
    I didn't try raw values... it could work as well, will test it and let you know if it works.
Sign In or Register to comment.