Shop OBEX P1 Docs P2 Docs Learn Events
Using Python to control servo on basic stamp 1 project board? - Page 2 — Parallax Forums

Using Python to control servo on basic stamp 1 project board?

2»

Comments

  • dmehling2dmehling2 Posts: 21
    edited 2014-03-19 12:53
    Thanks for that explanation, but I'm kind of lost. I've dabbled in a little bit of programming before, but this is all new to me.

    The purpose of this project is simply to control a servo with a maximum of 90° of rotation. So I only need to send values between 250 and 750.

    Is there some kind of calculator I could use that would take a number larger than 256 and break it into bytes?
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2014-03-19 13:05
    "Is there some kind of calculator I could use that would take a number larger than 256 and break it into bytes?"

    The '/' for division and the '&' are valid to use in the Python code , and as long as you define you Number as a WORD in PBASIC you can directly derive the BYTEs making up the WORD variable.... i.e.

    For W0 you would use B0 and B1
    for W1 you would use B2 and B3
    for W2 you would use B4 and B5
    ... and so on...

    Short of that if you really want a calculator , the calculator in Windows will convert to Binary if you switch it to Scientific mode. .... View --> Scientific ... The radio buttons on the upper left of the Calculator window allow you to select between Hex Dec Oct and Bin
  • dmehling2dmehling2 Posts: 21
    edited 2014-03-19 15:15
    I think I'm starting to grasp it a little. The Windows calculator lets me enter a WORD and then convert it to a BYTE.

    So using an & is like dividing a number by another, and then leaving only a value below 256.

    After playing around with some numbers in the calculator I noticed something strange. Any number below 256 is displayed as 0 when I convert it to a BYTE. Once I reach 256, it shows a 1 and then starts counting up by 1. That part makes sense. When I enter 383 it shows 127 and then 384 is -128. 385 is -127 and so on until it reaches 0 and then starts counting up into the positive. I know this is not really relevant to my project, but what is the reason for this behavior?
  • dmehling2dmehling2 Posts: 21
    edited 2014-03-19 15:24
    I tried to break down 750. I got high byte 2 and low byte 238. Does that seem right to you?

    I'm glad I was able to figure this out, even though I now see that the code you gave me would automatically take care of it.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2014-03-19 15:54
    "The Windows calculator lets me enter a WORD and then convert it to a BYTE." - There is no conversion from WORD to BYTE, what;s happening is that the HIGH byte is truncated .... similar to using the '&'

    The negative behavior is how binary numbers represent a value in " 2's complement " ... See: http://en.wikipedia.org/wiki/Two%27s_complement

    "...high byte 2 and low byte 238" are correct for 750. You can always double check your answer by multiplying the HIGH byte by 256 and then adding the LOW byte.

    2 * 256 + 238 = 750
  • dmehling2dmehling2 Posts: 21
    edited 2014-03-24 15:17
    So now I have my code for receiving serial data just about figured out. Now how would I take my values and use them to adjust the servo position?

    If I have the following code, how should I modify it, and where would I place it in relation to my existing PBASIC code?


    DO
    PULSOUT 0,500
    PAUSE 20
    LOOP
  • dmehling2dmehling2 Posts: 21
    edited 2014-03-27 10:25
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}

    SYMBOL SIn = 0
    SYMBOL Baud = N2400
    SYMBOL Result = W0 ' B1 = High Byte of W0 ; B0 = Low Byte of W0

    Main:
    SERIN SIn, Baud, ("ABCD"), B1 , B0
    DEBUG #Result, CR

    DO
    PULSOUT 0, #Result
    PAUSE 20
    LOOP

    GOTO Main
    END


    Would this work? It looks like there are two different loops in this code, which would seem to be a problem. The tutorial I got the code from indicated that there needs to be a loop for the servo to maintain its position. But how do I do that when I also need the stamp to be continuously requesting serial input?
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2014-03-27 13:08
    The DO LOOP is not available for the BS1, only the BS2, so those two lines need to be removed.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    SYMBOL SIn = 0
    SYMBOL Baud = N2400
    SYMBOL Result = W0                  ' B1 = High Byte of W0 ; B0 = Low Byte of W0
    
    'Note: In order for the servos to update properly, the PC needs to send a continuous data
    '      stream which includes the SYNC bytes and data bytes.
    
    Main:
    SERIN SIn, Baud, ("ABCD"), B1 , B0  'At 2400 Baud, this takes 2.5ms Min to 5ms Max
    
                                        '' 1/2400 x 6 BYTES = 2.5ms ... if the interval is missed
                                        '' the delay takes at MAX a complete additional cycle, thus
                                        '' 5ms
    
    
    DEBUG #Result, CR                   'At 4800 Baud, this can take about 1ms
    
                                        ''4800 is the default serial speed for the BS1
                                        '' 1/4800 x 5 characters = 1.05ms
                                        '' Since the DEBUG is sent with a #, each character is sent
                                        '' as a BYTE.  This encompasses a value ranging from
                                        '' 1000 TO 2000 plus a return character 'CR'
    
    
    Result = Result / 10                'The BS1 can only resolve an increment of 10us for PULSOUT
    PULSOUT 0, Result                   'Send Pulse .. can take from 1ms to 2ms
    
    'PAUSE 20                           'The average time required by the SERIN/DEBUG/PULSOUT
    PAUSE 14                            'and general overhead is about 6ms, so instead of a
                                        'PAUSE 20, a PAUSE 14 will suffice
    GOTO Main
    END
    
  • dmehling2dmehling2 Posts: 21
    edited 2014-03-27 15:05
    Can the servo reach 90° on the BS1? What value would that be? If I have to divide by 10, would that be 75?

    Also, with the code you just shared, the servo should maintain its position? It is still part of a loop?
Sign In or Register to comment.