Shop OBEX P1 Docs P2 Docs Learn Events
Arduino and Parallax Servo Controller — Parallax Forums

Arduino and Parallax Servo Controller

nitionnition Posts: 5
edited 2008-01-24 17:24 in General Discussion
Hey all,

I'm trying to get my arduino board to communicate with the parallax servo controller, I've read an older topic concerning the same issue, yet it seemed unsolved in the forum. The servo controller accepts a header ("!SC"), followed by a binary number for the servo channel, a binary number for the ramping speed, a 16-bit word for the servo position (250-1250), and a CR as a terminator.

This is the code I'm using for my arduino:

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 6
#define txPin 7

byte chan = 1 + '0'; 
byte ramp = 7 + '0';

int pw = 1250; 
byte lsb =  pw & 0xFF;     
byte msb =  pw >> 8; 

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(2400);
// debug led
  digitalWrite(13, HIGH);
 

    mySerial.print("!SC");
  
    mySerial.print(chan);  
    mySerial.print(ramp);
    mySerial.print(lsb, BYTE);  
    mySerial.print(msb, BYTE);
    mySerial.print(13, BYTE);


}

void loop() {
  // ...
}


Comments

  • JonnyMacJonnyMac Posts: 9,217
    edited 2008-01-23 21:58
  • nitionnition Posts: 5
    edited 2008-01-23 22:17
    Thank you for your (fast!) reply, I've taken a look at your code and, correct me If I'm wrong, saw that the RC-4 doesnt require a 16-bit Word to be sent. This is the main issue I'm having, getting that across. Have you got any idea what would be a good way to do that? I'm not sure if I've handled it correctly in my current code (with the least and most significant byte). Thanks!
  • FranklinFranklin Posts: 4,747
    edited 2008-01-23 22:54
    What are you getting from the setup? It would help if we knew what was happening when you sent your codes. Are you sure it's lsb,msb and not the other way around?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • nitionnition Posts: 5
    edited 2008-01-23 23:10
    Thanks for your reply, lsb, msb should be the correct order. I am now trying it with the following print commands:
        mySerial.print(33, BYTE);  //!
        mySerial.print(83, BYTE);  //S
        mySerial.print(67, BYTE);  //C
        mySerial.print(49, BYTE);  //channel 01
        mySerial.print(48, BYTE);  //rampspeed 0
        mySerial.print(00, HEX);  //LSB for the pw 0
        mySerial.print(03, HEX);  //MSB for the pw 3
        mySerial.print(13, BYTE);
    
    


    When I send it to the serial monitor it outputs !SC1003, which is correct. When I send it to the PSC, the (rx) led on the board lights up for a moment, that's the feedback I'm getting, is there any other way to debug it, a way that gives you more insight?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-01-23 23:49
    It appears you're formatting your word into ASCII characters, but the channel, ramping speed and pulse width are all binary values.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • nitionnition Posts: 5
    edited 2008-01-24 00:03
    Hello Chris, thank you for your answer. What would be the correct format then? Should they all be sent as BIN? I've tried the code below, which didnt do the trick.

    I've declared the vars as follows:
    int pw = 1250; 
    byte lsb = pw;
    //byte lsb =  pw & 0xFF;     
    byte msb =  pw >> 8;
    
    int chan = 9;
    int ramp = 0;
    
        mySerial.print(33, BYTE);  //!
        mySerial.print(83, BYTE);  //S
        mySerial.print(67, BYTE);  //C
        mySerial.print(chan, BIN);  //channel 09
        mySerial.print(ramp, BIN);  //rampspeed 0
        mySerial.print(lsb, BIN);  //LSB
        mySerial.print(msb, BIN);  //MSB
        mySerial.print(13, BYTE);
    
    

    Post Edited (nition) : 1/24/2008 12:08:50 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2008-01-24 03:35
    Make sure your 'int' is indeed 8bits and not 16 or 32 as in some systems

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • nitionnition Posts: 5
    edited 2008-01-24 07:50
    I am finally getting some action, with them all being sent as BYTE. Don't know for sure where the magic happened but I'm happy for now [noparse]:)[/noparse] Thanks to you all for being helpful! [noparse]:)[/noparse]
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-01-24 17:24
    Basically the pulse width is 16-bits (2 bytes) but the serial protocol only handles bytes. So you just send the lowbyte and highbyte as two bytes. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.