Shop OBEX P1 Docs P2 Docs Learn Events
new java code — Parallax Forums

new java code

WaldoDTDWaldoDTD Posts: 142
edited 2007-01-12 05:46 in General Discussion
hey,
I've been working on this code and I don't have the javelin in front of me right now but will soon, does this look good to interpret a control string if the first byte was 0x11 then channel then angle then 0x0D? Thanks!-signol01
import stamp.core.*;
import stamp.peripheral.servo.psc.*;

public class wintermute6 {

final static int SERIAL_TX_PIN = CPU.pin0; // 2
final static int SERIAL_RX_PIN = CPU.pin1; // 3

static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
Uart.speed2400, 8, Uart.stop2 );

static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
Uart.speed9600, Uart.stop1 );

static psc control = new psc(txUart, txUart, 6);

static int[noparse]/noparse buffer = new int[noparse][[/noparse]128];
static int x = 0;
static int index = 0;

public static void setup(){
control.initChannel(0,250,1250,180,45);
control.initChannel(1,250,1250,180,45);
control.initChannel(2,250,1250,180,45);
control.initChannel(3,250,1250,180,45);
control.initChannel(4,250,1250,180,45);
control.initChannel(5,250,1250,180,45);
}
public static void rxMessage(){
index = 0;
while(rxUart.byteAvailable()){
x = (int)rxUart.receiveByte();
x = buffer[noparse][[/noparse]index++];
}
}
public static void main(){
int channel = 0;
int angle = 0;
setup();
rxMessage();
while(buffer[noparse][[/noparse]0] != 0){
channel = buffer;
angle = buffer;
control.setAngle(channel,45,angle);
rxMessage();
}
}
}

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-24 06:17
    inputstring 0x11,channel,angle,0x0D

    public static void main(){
    · int channel = 0;
    · int angle = 0;
    · int i=0;
    · setup();
    · while (true) {
    ··· rxMessage();
    ··· if (buffer[noparse][[/noparse]0] == 0x11){
    ····· channel = buffer[noparse][[/noparse]1];
    ······ angle = buffer[noparse][[/noparse]2];
    ····· control.setAngle(channel,45,angle);
    ··· }
    · }
    }

    regards peter

  • WaldoDTDWaldoDTD Posts: 142
    edited 2006-11-27 19:23
    When I transmit from the computer to the stamp, here is the scheme I use to write to the output stream in the COMM API

    outputStream.write(0x11);
    outputStream.write(angle&0xFF);
    outputStream.write(angle>>>8);
    outputStream.write(0x0D);

    In sending the high and lowbytes of the int value will this mess with the actuall numbers put on the buffer array? How do I assemble an int from the high and low bytes? Also, the numbers returned by a calculation engine that I am sending to the stamp are in double format, I am using this code to transfer to int will this still work? When I get the stamp recieves the number (it is an angle orientation for a servo) do I have to do anything else before I send it to the Servo Controller? Thanks!-Signol

    code to transfer double to int:
    shldr = (int)((ik.shoulder*100+50)/10); //ik.shoulder is the variable that is calculated after the calculation engine completes
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-27 20:11
    If you receive an int as seperate low and high bytes, use
    int r = (low&0xFF)|(high<<8);
    to assemble an int

    The psc class specifies angles to be 0 to 3600,
    which represent 0.0 to 360.0 degrees
    If you specify a negative angle (for counterclock wise rotation)
    just add 360 degrees to make it positive.

    regards peter
  • WaldoDTDWaldoDTD Posts: 142
    edited 2006-11-27 22:44
    low byte is int>>>8 right?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-27 22:53
    To move the highbyte of an int into a byte,
    then yes, lowbyte = int>>>8
    To move the lowbyte of an int into a byte,
    lowbyte = int&0xFF

    regards peter
  • WaldoDTDWaldoDTD Posts: 142
    edited 2006-11-27 23:03
    public static void assembleMessage(){
    int cmd = 0;
    int bufindex= 1;
    int messageindex = 1;
    message[noparse][[/noparse]0] = 1;

    while(bufindex <= 10){
    cmd = (buffer[noparse][[/noparse]bufindex++]&0xFF)|(buffer[noparse][[/noparse]bufindex++]<<8);
    cmd = message[noparse][[/noparse]messageindex++];
    }
    }

    bear in mind that i it will only handle 12 entries from the buffer array 10 of which are angle positions the other two are LF and CR. The array that will be used will be length of 10 with message[noparse][[/noparse]0] as a flag for the other method to tell whether or not there is any data in the array. I just want to know if there will be a problem with the variable incrementation and if I assembled the code right. I believe that the transmission is Highbyte, lowbyte, highbyte low byte (working under the premise that to get the lowbyte of an int you put int>>>8 and to get the high byte you put int&0xFF)
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-27 23:37
    The javelin receives bytes in the order they are sent.
    So if your pc program sends high bytes first, then the javelin
    receives high bytes first.

    cmd = (buffer[noparse][[/noparse]bufindex++]&0xFF)|(buffer[noparse][[/noparse]bufindex++]<<8);
    may give trouble, as it is not specified which part is executed first,
    the term left of the |, or the term right of the |

    int low =·buffer[noparse][[/noparse]bufindex++]&0xFF;
    int high = buffer[noparse][[/noparse]bufindex++]<<8;
    cmd = low | high;

    will exactly do as you intend.
    Note that the code above, implies that low bytes are sent first.

    regards peter
  • WaldoDTDWaldoDTD Posts: 142
    edited 2006-11-28 00:00
    ok well my protocol sends int&0xFF first then int>>>8 so I assume that the highbyte is sent first so I will just put the int high before the int low then. Thanks!-Adam
  • WaldoDTDWaldoDTD Posts: 142
    edited 2007-01-12 02:35
    Hey,
    I have a couple questions,
    1st- is the low/high byte transmission protocol really necessary, can i just send the integer to the stamp and have it pick it up in an int array?
    2nd- How would I go about turning a variable from a double to an int without loosing data? Is it possible to transmit it as a byte then have the stamp reconstruct it? Does the Parallax servo controller and Javelin stamp support double integers?
    Thanks!-Waldo (formerly known as Signol01)

    Post Edited (WaldoDTD) : 1/12/2007 3:02:54 AM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-12 05:46
    A uart connection only transmits bytes. If you want larger data you must reassmble the bytes
    at the receiver. To send double ints (I assume you mean a 32bit integer) then you must
    send the 4 bytes after each other, starting with the lowest byte.
    There is the Int32 class that supports 32bit integers.
    Upon receive, you can assemble the bytes in 2 16bit ints, then assign these
    2 ints to a Int32 variable.
    http://www.parallax.com/javelin/applications.asp#AN011

    regards peter
Sign In or Register to comment.