new java code
WaldoDTD
Posts: 142
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();
}
}
}
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
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
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
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
then yes, lowbyte = int>>>8
To move the lowbyte of an int into a byte,
lowbyte = int&0xFF
regards peter
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)
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
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
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