Shop OBEX P1 Docs P2 Docs Learn Events
Parallax USB Servo avoiding the Javelin Stamp Code using C++ — Parallax Forums

Parallax USB Servo avoiding the Javelin Stamp Code using C++

TyroneTyrone Posts: 7
edited 2010-05-27 11:22 in General Discussion
Hi



I am interfacing C++ code into·a Parallax USB Servo # 28823 avoiding the Javelin Stamp Code. I can control the servohop.gif· but not efficiently from position 250-1250.



I understand that I need to send the following data:

!SC [noparse][[/noparse] SERVO ] [noparse][[/noparse] RAMP ] [noparse][[/noparse] LOW POSITION BYTE ] [noparse][[/noparse] HIGH POSITION BYTE ] [noparse][[/noparse] 0x0D ]



"!SC" I understand I need to send as ASCII in the data type of CHAR. The rest I understand that I have to send as an INT which will be passed on as decimals to the COM Port.



I am using the following code to write the data using Writefile().





(Code to send "!SC" is ASCII)

char szMessage[noparse]/noparse = "!SC";

int nBytesSent = serial.SendData(szMessage, strlen(szMessage));



(Code to send "SERVO")

int szMessageNumber = 00;

nBytesSent = serial.WriteCommByteNumber(szMessageNumber);



(Code the send "RAMP")

szMessageNumber = 00;

nBytesSent = serial.WriteCommByteNumber(szMessageNumber);



(Code to send "LOW POSITION BYTE")

szMessageNumber = 100;

nBytesSent = serial.WriteCommByteNumber(szMessageNumber);



(Code the send "HIGH POSITION BYTE)

szMessageNumber = 1;

nBytesSent = serial.WriteCommByteNumber(szMessageNumber);



(Code to send CARRIAGE RETURN)

szMessageNumber = 13;

nBytesSent = serial.WriteCommByteNumber(szMessageNumber);





I have not been able to move it from position 250-1250 a notch at a time. The only possible way I can make a 180 degree turn is if I change the HIGH POSITION BYTE from 1 - 4. That is if I change the HIGHBYTE from 1 to 2, the servo moves 45 degrees, and then another 45 degrees if the HIGHBYTE is changed to 3 and also 4 eventually making a full 180 degree turn. 5 strains the servo.



What am I doing wrong? Can you clarrify how one would use and/or manipulate the HIGHBYTE·and LOWBYTE.·I eventually burnt an expensive servo.... I'm a junior. How does Javelin pass these values onto the controller.



Thanks

Tyrone

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-25 10:38
    Attached is the class for the Parallax Servo Controller (serial version).

    You should·be able to figure out the send and receive byte sequences from that code.

    I am assumimg the psc usb version uses the same byte sequences.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-25 10:45
    The high byte and low byte are extracted from position value like this:

    int position = 878;
    int lowbyte = position & 255;
    int highbyte = (position >> 8) & 255;

    position range is 250 to 1250, centerposition is 750

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-25 11:00
    Open source for psc control:
    http://www.crustcrawler.com/software/psc/index.php

    regards peter
  • TyroneTyrone Posts: 7
    edited 2009-12-25 13:00
    Thank you Peter for a fast response. Great resource material.

    In your suggestion:

    int position = 878;
    int lowbyte = position & 255;
    int highbyte = (position >> 8)
    & 255;

    What is the & 255 and the >>8 for?

    I haven't got past the 4th chapter of C++.... haha Maybe that is why I burnt my servo!
  • TyroneTyrone Posts: 7
    edited 2009-12-25 14:46
    Okay I see they are BitWise Operators and reading more up on them and playing. Maybe you want to explain the logic behind it so that I can understand what I am doing by performing bitwise operations on the decimal values.
  • TyroneTyrone Posts: 7
    edited 2009-12-25 15:35
    int position = 878;
    int lowbyte = position & 255;
    int highbyte = (position >> 8) & 255;

    1.) 878 (decimal) = 1101101110(binary)
    2.) Performing a BitWise Shift Right 8 places will leave a byte looking like 0000000011 which is 3 in decimal. So this explains why I could only use values 1-4 on the HIGHBYTE without BitWise operators.
    3.) The & "255" takes the binary representation (1101101110) of the decimal value 878 and does a calculation against the binary value of 255 (11111111)
    For example: 878 = 1101101110
    255 = 11111111
    ________
    0001101110 = 110

    OR

    878 = 1101101110
    255 = 11111111
    ________
    1101101100 = 876

    (THIS IS WHERE I GET LOST BECAUSE I AM NOT SURE HOW THE CALCULATION IS DONE WITH A 8-BIT AND 10-BIT BIT-STREAM.)
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-25 16:35
    The &255 means bitwise AND with 0b11111111
    in other words: keep lower 8 bits only
    878 = 1101101110
    255 =·0011111111
    ________
    ······· 0001101110 = 0x6E = 110 decimal

    regards peter
  • TyroneTyrone Posts: 7
    edited 2009-12-25 17:29
    Peter thank you. You have been an awesome help.

    Off to buy another servo and will post the results here.
  • TyroneTyrone Posts: 7
    edited 2010-01-22 17:31
    I have been meaning to post the results here!

    BitWise Operators work like a charm!

    I can control the servo any which way I want to.

    Peter thanks....
  • Rob311Rob311 Posts: 83
    edited 2010-05-27 00:23
    I've been trying to talk to my psc for a little while now, and all i get is an echo. Im using vb express 2010, this is my code

    serialPort.WriteLine("!SCVER?")

    and it responds !SCVER?

    i dont suppose anyone knows what im doing wrong?
  • TyroneTyrone Posts: 7
    edited 2010-05-27 05:49
    It seems the code is not opening the port properly because you should get a response showing the version 1.6 I think?

    Find sample code to open the COM port but it should not be that different from previous versions of Visual C++.

    I dont have any of my code anymore, so I can't help with sample code right now. I'm a beginner in C++.
  • Rob311Rob311 Posts: 83
    edited 2010-05-27 10:32
    the com is opening and sending. the psc echos if it doesnt understand the command. maybe the format of the command?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-05-27 11:22
    Any command should end with a CR character (ascii code 13 decimal).

    regards peter
Sign In or Register to comment.