Shop OBEX P1 Docs P2 Docs Learn Events
Parallax Servo Controller (PSC)(Serial) example code — Parallax Forums

Parallax Servo Controller (PSC)(Serial) example code

diafysaldiafysal Posts: 92
edited 2005-12-14 23:36 in General Discussion
Parallax Servo Controller (PSC)(Serial) example code ?

Hi
I read the line “Uart updated to support bidirectional communication using a single I/O pin. “ on the download page (Javelin Stamp IDE Installer - Version 2.0.3 ).
And I have tried to find example code.
Could someone please translate this? (from ServoController.pdf)(The VER? command causes the PSC to reply with its firmware version number.)

'{$PBASIC 2.5}
Sdat PIN 15 ' Serial Data I/O pin
Baud CON 396 ' Constant for 2400 baud
buff VAR Byte(3) ' temporary variable

FindPSC: ' Find and get the version
  DEBUG "Finding PSC", CR ' number of the PSC.
  SEROUT Sdat, Baud+$8000, [noparse][[/noparse]"!SCVER?",CR]
  SERIN Sdat, Baud, 500, FindPSC, [noparse][[/noparse]STR buff\3]
  DEBUG "PSC ver: ", buff(0), buff(1), buff(2), CR
  STOP



(see below for finished code)(2005 08 01)

Post Edited (diafysal) : 7/31/2005 11:45:13 PM GMT

Comments

  • dandreaedandreae Posts: 1,375
    edited 2005-07-30 16:34
    Here is a link to the forums that may help:· http://forums.parallax.com/showthread.php?p=464950


    Dave

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave Andreae

    Tech Support
    dandreae@parallax.com
    Http://www.parallax.com

    ·
  • diafysaldiafysal Posts: 92
    edited 2005-07-30 16:39
    That's "transmit only" code isn't it ?
  • dandreaedandreae Posts: 1,375
    edited 2005-07-30 16:46
    True, that this is·transmit only.· I don't have a set up here (at home)·to test, but you may be able to test it using RX instead of TX?· Of course you will have to modify the code, but it might give you an idea of how to communicate with the PSC?

    Dave

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave Andreae

    Tech Support
    dandreae@parallax.com
    Http://www.parallax.com

    ·
  • diafysaldiafysal Posts: 92
    edited 2005-07-30 16:55
    Yes I think that I might manage to send commands to the PSC. It is the "bidirectional on one IO pin" I'm not so sure about.

    I have been programming Java for only a few days/hours (and Javelin stamp for a few days/hours).
    Personally, if I don't find example code it is (for the moment) really difficult for me to write programs.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-30 16:56
    The bidirectional use is done by restarting your uart.

    See the restart method in class Uart. Restart allows you to change direction, pins, baud etc

    without the need to create a new uart (remember there is no garbage collection,

    so this restarting is very economic regarding resources)

    regards peter
  • diafysaldiafysal Posts: 92
    edited 2005-07-30 17:30
    But I have to check that the transmit is complete first ?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-30 17:39
    Yes,

    eg.

    Uart myUart = new Uart(Uart.dirTransmit,CPU.pin0,Uart.dontInvert,Uart.speed1200,Uart.stop1);

    //send command

    myUart.sendByte(aByte);

    //multiple sendByte() if necessary

    //wait for buffer empty

    while (!myUart.sendBufferEmpty()) ;

    //change direction

    myUart.restart(Uart.dirReceive,CPU.pin0,Uart.dontInvert,Uart.speed1200,Uart.stop1);

    //receive response

    response = myUart.receiveByte();

    //multiple bytes receive if necessary



    regards peter






    Post Edited (Peter Verkaik) : 7/30/2005 5:43:40 PM GMT
  • diafysaldiafysal Posts: 92
    edited 2005-07-30 19:19
    Is this wrong?
    I get stuck on receiveByte.


    import stamp.core.*;
    
    public class test {
    
    
      public static void main() {
    
        int [noparse][[/noparse]] buff;
        buff = new int;            // data from PSC
    
        Uart myUart = new Uart(Uart.dirTransmit,
                               CPU.pin15,          // Serial Data I/O pin
                               Uart.dontInvert,
                               Uart.speed2400,
                               Uart.stop1);
    
        while(true) {
    
          System.out.println("Finding PSC");
    
          myUart.sendString("!SCVER?");         //send command
          myUart.sendByte(0x0D);                //end of command
    System.out.print("1");
          while (!myUart.sendBufferEmpty()) ;   //wait for buffer empty
    System.out.print("2");
          myUart.setDirection(Uart.dirReceive);      //change direction
    System.out.print("3");
          buff[noparse][[/noparse]0] = myUart.receiveByte();       //receive response
          buff = myUart.receiveByte();
          buff = myUart.receiveByte();
    System.out.print("4");
          myUart.setDirection(Uart.dirTransmit);     //change direction
    
          System.out.println("PSC ver: ");
          System.out.print(buff[noparse][[/noparse]0]);
          System.out.print(buff);
          System.out.print(buff);
    
          CPU.delay( 30000 );  // Long delay
    
          System.out.print('\u0010');  // Clears the terminal
    
        }                                                // end while
      }                                                  // end main
    }                                                    // end class declaration
    



    Dont mind the missing [noparse]/noparse (not my fault]
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-30 20:17
    Code looks good.
    You probably get stuck on receiveByte because you missed a byte.

    Comment out the System.out.print("1") etc,
    as these consume time, and you don't want to consume
    time between sending and receiving.
    If that still doesn't work, try replacing the while (!sendBufferEmpty())
    with just CPU.delay(105); //105 equals 10 msec, increase if not delay enough
    to tune time before direction change.
    At 2400 baud, bytetime is 10 * 1 / 2400 sec = 4.17 millisec
    so 10 msec should allow for two bytes send
    Also note that the restart method consumes alot of time also.

    regards peter
    ·
  • diafysaldiafysal Posts: 92
    edited 2005-07-30 22:07
    I think it was my fault smile.gif
    A combination of:

    I have the habit of using a current limiting resistor in series with the CPU.pin
    Did NOT work well in this case, with the pull up resistor(led) on the PSC smile.gif

    PSC had a tendensy to hang when I started the servo(powered on the servo)




    And I get out 3 numbers instead of 3 characters ! smile.gif

    So.. how sensitive is the PSC regarding voltage supply?
    And the servo power on/off switch. Am I NOT supposed to switch it on/off while running stamp and PSC?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-31 21:03
    Just curious, does it work now?

    regards peter
  • diafysaldiafysal Posts: 92
    edited 2005-07-31 21:30
    Yes!

    The "system out 1 2 3 4" was just something I tried with to see where it stopped.
    I have not fixed the display "1.4" instead of "494652" yet.
    Should fix that first and then post the code here.
    I read the "AN012 - The Text Format Library" but I'm not wiser.
    How do I get System.out.print to display the asci character "1" instead of integer "49" ?
    myUart.receiveByte() receives 3 integers "49 46 52", how do I convert it to character/string "1.4" ?
    How do I store one byte from "public int receiveByte()" in a char ?

    Post Edited (diafysal) : 7/31/2005 11:14:35 PM GMT
  • diafysaldiafysal Posts: 92
    edited 2005-07-31 23:34
    This works!

    /* Simple test program to Identify Firmware Version Number.
     * From "ServoController.pdf"
     *
     *(please improve the coding)
     *
     * diafysal
     */
    import stamp.core.*;
    
    public class test_ver {
    
    
      public static void main() {
    
        char [noparse][[/noparse]] buff;
        buff = new char;            // data from PSC
    
        Uart myUart = new Uart(Uart.dirTransmit,
                               CPU.pin15,          // Serial Data I/O pin
                               Uart.dontInvert,
                               Uart.speed2400,
                               Uart.stop2);
    
        while(true) {
    
          System.out.println("Finding PSC");
    
          myUart.sendString("!SCVER?");         //send command
          myUart.sendByte(0x0D);                //end of command
    
          while (!myUart.sendBufferEmpty()) ;   //wait for buffer empty
    
          myUart.setDirection(Uart.dirReceive);      //change direction
    
          buff[noparse][[/noparse]0] = (char)myUart.receiveByte();       //receive response
          buff = (char)myUart.receiveByte();
          buff = (char)myUart.receiveByte();
    
          myUart.setDirection(Uart.dirTransmit);     //change direction
    
          System.out.print("PSC ver: ");
          System.out.print(buff[noparse][[/noparse]0]);
          System.out.print(buff);
          System.out.print(buff);
    
          CPU.delay( 30000 );  // Long delay
    
          System.out.print('\u0010');  // Clears the terminal
    
        }                                                // end while
      }                                                  // end main
    }                                                    // end class declaration
    
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-08-01 06:34
    quote:
    I read the "AN012 - The Text Format Library" but I'm not wiser.
    How do I get System.out.print to display the asci character "1" instead of integer "49" ?
    myUart.receiveByte() receives 3 integers "49 46 52", how do I convert it to character/string "1.4" ?

    You receive the ascii values of the characters you want to print.
    If data holds such a value, then
    · Format.printf("%c",data);
    will print that character.

    System.out.print(data) should print the character also, if data is of type char.
    If data is of type int, this will print the decimal number.

    regards peter
    ·
  • diafysaldiafysal Posts: 92
    edited 2005-08-02 12:45
    I forgot to say "thank you" smile.gif
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-12-13 00:30
    I have uploaded a PSC class and test programs here:
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/servo/psc/

    The class allows servo control using 0.1 degree resolution angles.
    It supports bidirectional data over a single I/O line or seperate transmit/receive·lines.

    I do not have the PSC board and servos, so if you like to test it,
    let me know the results.

    The PSC class requires the UnsignedIntMath class here:
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/math/

    regards peter
    ·
  • diafysaldiafysal Posts: 92
    edited 2005-12-14 23:29
    Thank you, again !
    But I do not have the time at the moment to test it.
    Maybe someone else has?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-12-14 23:36
    Someone already is.

    http://forums.parallax.com/showthread.php?p=556854

    regards peter
Sign In or Register to comment.