Shop OBEX P1 Docs P2 Docs Learn Events
Can use SERIN to read PIN 2 of Javelin Stamp? — Parallax Forums

Can use SERIN to read PIN 2 of Javelin Stamp?

StereoPonyzStereoPonyz Posts: 82
edited 2010-01-26 20:43 in General Discussion
I am using localizer - stargazer hagisonic on the robot to detect its location. It was programmed using Bs2sx stamp previously and the command used was:

SERIN 2,16624,[noparse][[/noparse]WAIT ("~^I"),SDEC storeangle,WAIT("|"), SDEC BB3x,WAIT ("|") ,SDEC BB3y]

where SERIN 2, 16624, means to receive input thru pin 2 of Bs2sx and '16624' means 9600 baud rate. while SDEC means to retreive negative decimal.

I would like to know if to use Javelin Stamp can i use the same command? I search through the manual and knew that Javelin Stamp suport SERIN command, however, what command can i use for SDEC? Also, if the same pin can be used for reading?
«1

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-13 13:27
    The SERIN 2,...
    means pin P2 (not physical pin2 which is SIN, which would use SERIN 16,...)

    On the javelin you therefor just use an Uart.

    Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.dontInvert,Uart.speed9600,Uart.stop1);
    while (true) { //wait for "~^I"
    · do {
    ··· int c = myUart.receiveByte();
    · while (c != '~');
    ··c = myUart.receiveByte();
    · if (c != '^') continue;
    ··c = myUart.receiveByte();
    · if (c == 'I') break;
    }

    You can expand on this to mimic the BS line.

    Or use·a generic wait method
    void wait(Uart rx, String toWaitFor) {
    · int index = 0;
    · while (index < toWaitFor.length) {
    ····int c = rx.receiveByte();
    ··· if·(c != toWaitFor.charAt(index)) {
    ····· index= 0;
    ····· if (c == toWaitFor.charAt(0)) index=1; //for example wait for "~^I" but receive "~~^I"
    ····· continue;
    ··· }
    ··· index++;
    · }
    }

    This is not quite working. It would fail if you wait for "~~^I" but receive "~~~^I"
    but you get the idea.

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2009-12-31 09:45
    Hi, i have tried to implement UART to read data from my localizer and print out watever data it shows. Below are my codes
    import stamp.core.*;


    public class Stargazer {

    static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.dontInvert,Uart.speed9600,Uart.stop1);
    static int a;
    static String b = "";

    public static void main() {

    for (int i = 0; i < 11; i++)
    {
    a = myUart.receiveByte();
    System.out.println(a);

    if(a == '+' || a == '-')
    {
    a = myUart.receiveByte();
    b = b + Integer.toString(a);
    System.out.println(b);
    } // end of if

    } // end of for loop

    } // end of main()

    }

    I realised that the results can only print one time and i have to restart the boe-bot to detect the next coordinate. Besides that, restarting the boe-bot too early, the results will show all zeroes, i need to wait for a few seconds before i can get the whole data. Is it cos, the UART cannot read continously?
    837 x 644 - 53K
  • StereoPonyzStereoPonyz Posts: 82
    edited 2009-12-31 10:21
    I would like to add on. My localizer baudrate is 9600, and will the javelin stamp auto-adjust itself to 9600 Baud rate too? or do i have to configure it? or have i already specified in the codes --> uart.speed9600 ?

    Post Edited (StereoPonyz) : 12/31/2009 10:32:49 AM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-12-31 10:37
    Try this

    import stamp.core.*;
    import stamp.util.text.*;
    
    public class Stargazer {
    
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.dontInvert,Uart.speed9600,Uart.stop1);
      static int a,i;
    
      static void main() {
        i = 0;
        while (true) {
          a = myUart.receiveByte();
          Format.printf("%02x ",a); //print hexadecimal value of received byte
          i = (i+1) & 0x0F;
          if (i == 0) Format.printf("\n");
        }    
      }
    }
    

    This will print continuesly the received bytes as hexadecimal values.

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2009-12-31 11:06
    thank you peter... i have tried out the codes. However, when i convert the Hexadecimal back into decimal using calculators, i noticed that the numbers are v huge for instances FFD6 (65494), there are many zeroes in between too. The zeros are caused by the localizer itself or javelin chip?

    I flipped through the javelin manual and found some information on PWN stating that

    "PWM will accept values from 0 to 65535. The Javelin's int field can hold values from -32768 to 32767. To enter PWN values abv 32767 use the following maps"

    is this the map i need to look at to see what are exactly are the hexadecimal values are?
    776 x 406 - 80K
  • StereoPonyzStereoPonyz Posts: 82
    edited 2009-12-31 15:55
    Ah...... HELP ME~~~~~~~~~ i had tried and changed the codes countless of times but the results produced were still not right. The attached was the application i used which comes tgt with the localizer, stargazer.

    The localizer will output the data in the format circle in red. Actually i require only the X and Y coordinates, but the results produced by the Javelin is so different, since i am using UART class that will only accept Integer. Thus, i am unable to tell which is the starting integer and which is the ending ones.

    Can i know if there is way to output the data in the Stargazer format --> ~^I+150.23|-33.12|+12.00|64` which means ( ~^I : Map Mode, +150.23:Angle, -33.12:X, +12,00:Y, 64 : IDNumber )
    469 x 646 - 55K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-02 07:21
    The large FF?? values are probably due to promoting byte to int.

    Change the line
    a = myUart.receiveByte();
    into
    a = myUart.receiveByte() & 255;

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-02 08:38
    Thank you peter i have replaced that line. But can i know why are there zeroes in between the data? Also, is it possible to print the exact numbers instead of hexadecimals?
    454 x 500 - 87K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-02 11:35
    Try this

    import stamp.core.*;
    import stamp.util.text.*;
    
    public class Stargazer {
    
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.dontInvert,Uart.speed9600,Uart.stop1);
      static int a,i;
      static int buf[noparse][[/noparse]16];
    
      static void main() {
        i = 0;
        while (true) {
          a = myUart.receiveByte() & 255;
          buf[noparse][[/noparse]i] = a;
          Format.printf("%02x ",a); //print hexadecimal value of received byte
          i = (i+1) & 0x0F;
          if (i == 0) {
            for (int j=0; j<16; j++) {
              if ((buf[noparse][[/noparse]j] < 32) || (buf[noparse][[/noparse]j] >= 127)) {
                Format.printf(".");
              }
              else {
                Format.printf((char)buf[noparse][[/noparse]j]);
              }
            }
            Format.printf("\n");
          }
        }    
      }
    }
    

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-03 12:59
    Thank you Peter, but i got compiling error. Is it cos i have not dl all the necessary classes?
    776 x 814 - 76K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-03 13:07
    Try this

    import stamp.core.*;
    import stamp.util.text.*;
     
    public class Stargazer {
     
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.dontInvert,Uart.speed9600,Uart.stop1);
    
      static int a,i;
      static int buf[noparse][[/noparse]] = new int[noparse][[/noparse]16];
     
      static void main() {
        i = 0;
        while (true) {
          a = myUart.receiveByte() & 255;
          buf[noparse][[/noparse]i] = a;
          Format.printf("%02x ",a); //print hexadecimal value of received byte
          i = (i+1) & 0x0F;
          if (i == 0) {
            for (int j=0; j<16; j++) {
              if ((buf[noparse][[/noparse]j] < 32) || (buf[noparse][[/noparse]j] >= 127)) {
                Format.printf(".");
              }
              else {
                Format.printf("%c",buf[noparse][[/noparse]j]);
              }
            }
            Format.printf("\n");
          }
        }
      }
    }
    
    

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-03 13:59
    Thank You Peter, sorry to keep bothering u... However, the results are still in HEXdecimal... Now, the results consist of dots besides Zeroes.
    456 x 834 - 106K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-04 07:43
    Make the message window wider.
    What you get per line are 16 hexvalues and 16 characters (readable text) of those values.
    Unprintable values (0-31, 127-255) are replaced by dots.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-04 08:02
    According to the manual
    http://www.robotshop.ca/content/PDF/StarGazer_User_Manual_Ver_04_080417%28English%29.pdf

    the stargazer communicates at baudrate 115200, which is not supported by the javelin.
    You could try a Propeller SpinStamp which has identical 24pins footprint.

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-05 02:04
    Hi Peter, i have re-attached the results in wider screen. The stargazer are working at 9600 Baud rate, i had sent back to the manufacturer to re-configure.
    The stargazer can only output from a range of values from -180 to 180. What can i do to extract the values, since (0-31, 127-255) are unprintable?
    793 x 833 - 199K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-05 10:24
    All the binary 0's don't make sense.
    Try using invert mode

    · static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.invert,Uart.speed9600,Uart.stop1);

    and see if that generates more readable output

    The stargazor outputs TTL levels 0-3.3V which is ok for the javelin

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-05 15:22
    WOW THANK YOU PETER. The Javelin is able to produce the output from the stargazer in a more similar format. The format of the Stargazer is shown in StarGazerMonitor (right) Circled in Red. Am i right to say, only the data Circled in Red in the Javelin Message (left) are the useful data, while the rest of the Hexadecimal are not? Since only those that are circled looks almost like the output format when compared with StarGazerMonitor software. The data will start with ~ and end with '

    When using the StarGazerMonitor software, the measurement output has a constant +/- 1.something changes. Could this be the reason why there are many hexadecimal?

    I noticed that, the results does not always produce the 6 information (Map, IDNum, Angle, X, Y, Height)
    ~^I16|+81.35|-17.53|-25.59|231.09' --> (~^I:Map 16:ID Num | +81.35:Angle | -17.53:X | -25.59:Y| 231.09:Height')
    There is Only ONE set of data (highlighted in organge) which consists of the right information and in the right format. So, it means i will only have one set of data for feedback usage?

    Post Edited (StereoPonyz) : 1/5/2010 4:05:32 PM GMT
    1036 x 612 - 168K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-06 13:09
    Try this

    //SERIN 2,16624,[noparse][[/noparse]WAIT ("~^I"),SDEC storeangle,WAIT("|"), SDEC BB3x,WAIT ("|") ,SDEC BB3y]
    //where SERIN 2, 16624, means to receive input thru pin 2 of Bs2sx and '16624' means 9600 baud rate.
    //while SDEC means to retreive negative decimal.
    
    import stamp.core.*;
    import stamp.util.text.*;
     
    public class Stargazer1 {
     
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.invert,Uart.speed9600,Uart.stop1);
      static int a,i;
      static int buf[noparse][[/noparse]] = new int[noparse][[/noparse]16];
      static int state = 0;
      static int index = 0;
      static int value[noparse][[/noparse]] = new int[noparse][[/noparse]1];
    
      static int storeangle, BB3x, BB3y;
    
      static void main() {
        i = 0;
        while (true) {
          switch (state) {
            case 0: //wait for ~
                    a = myUart.receiveByte() & 255;
                    if (a == '~') state = 1;
                    break;
            case 1: //wait for ^
                    a = myUart.receiveByte() & 255;
                    if (a == '^') state = 2;
                    else state = 0;
                    break;
            case 2: //wait for I
                    a = myUart.receiveByte() & 255;
                    if (a == 'I') {
                      index = 0;
                      state = 3;
                    }
                    else state = 0;
                    break;
            case 3: //wait for |, storing +,-,digits
                    a = myUart.receiveByte() & 255;
                    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
                    else {
                      buf[noparse][[/noparse]index] = 0;
                      Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
                      storeangle = value[noparse][[/noparse]0];
                      Format.printf("storeangle = %d :",storeangle);
                      index = 0;
                      state = 4;
                    }
                    break;
            case 4: //wait for |, storing +,-,digits
                    a = myUart.receiveByte() & 255;
                    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
                    else {
                      buf[noparse][[/noparse]index] = 0;
                      Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
                      BB3x = value[noparse][[/noparse]0];
                      Format.printf("BB3x = %d :",BB3x);
                      index = 0;
                      state = 5;
                    }
                    break;
            case 5: //storing +,-,digits
                    a = myUart.receiveByte() & 255;
                    if ((a == '-') || (a == '+') || ((a >= '0') && (a <= '9'))) buf[noparse][[/noparse]index++] = (char)a;
                    else {
                      buf[noparse][[/noparse]index] = 0;
                      Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
                      BB3y = value[noparse][[/noparse]0];
                      Format.printf("BB3y = %d\n",BB3y);
                      index = 0;
                      state = 0;
                    }
                    break;
          }
        }
      }
    }
    

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-06 13:44
    Thanks Peter. There is 3 errors pertaining to sscanf --> sscanf(int[noparse]/noparse, java.lang.String, int[noparse]/noparse). Sorry, i had look up on the web to try and solve the errors myself but i dont quite understand...
    Thank you once again....
    776 x 810 - 93K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-06 14:44
    Change
    · static int buf[noparse]/noparse = new int[noparse][[/noparse]16];
    into
    · static char buf[noparse]/noparse = new char[noparse][[/noparse]16];

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-06 15:40
    THANK YOU PETER!!! a million thanks haha....
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-07 16:08
    Hi, Peter i just noticed that every 4th line of the stargazer output is wrong which affects my boe-bot movement [noparse]:([/noparse]
    I have added in extra codes to output X, Y using the same codes as in case 3, while Height output use the same codes as in case 5. Is it that i need to change the if condition

    May I know, the IF CONDITION in case 7 codes is to prevent OutofMemory Exception error? i had tried without that IF CONDITION and used the same codes like in CASE 3 to 6, it resulted OutofMemory Exception.

    import stamp.core.*;
    import stamp.util.text.*;

    public class StargazerFinal {

    static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.invert,Uart.speed9600,Uart.stop1);
    static int a,i;
    static char buf[noparse]/noparse = new char[noparse][[/noparse]16];
    static int state = 0;
    static int index = 0;
    static int value[noparse]/noparse = new int;

    static int MapID, Angle, Boebot3x, Boebot3y, Height;

    static void main() {
    i = 0;
    while (true) {
    switch (state) {
    case 0: //wait for ~
    a = myUart.receiveByte() & 255;
    if (a == '~') state = 1;
    break;
    case 1: //wait for ^
    a = myUart.receiveByte() & 255;
    if (a == '^') state = 2;
    else state = 0;
    break;
    case 2: //wait for I
    a = myUart.receiveByte() & 255;
    if (a == 'I') {
    index = 0;
    state = 3;
    }
    else state = 0;
    break;
    case 3: //wait for |, storing +,-, Landmark Map ID
    a = myUart.receiveByte() & 255;
    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    MapID = value[noparse][[/noparse]0];
    Format.printf("Landmark Map ID = %d :", MapID);
    index = 0;
    state = 4;
    }
    break;
    case 4: //wait for |, storing +,-,Angle
    a = myUart.receiveByte() & 255;
    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    Angle = value[noparse][[/noparse]0];
    Format.printf("Angle = %d :",Angle);
    index = 0;
    state = 5;
    }
    break;

    case 5: //wait for |, storing +,-,X-Coordinates
    a = myUart.receiveByte() & 255;
    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    Boebot3x = value[noparse][[/noparse]0];
    Format.printf("Boebot3x = %d :",Boebot3x);
    index = 0;
    state = 6;
    }
    break;

    case 6: //wait for |, storing +,-,Y-Coordinates
    a = myUart.receiveByte() & 255;
    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    Boebot3y = value[noparse][[/noparse]0];
    Format.printf("Boebot3y = %d :",Boebot3y);
    index = 0;
    state = 7;
    }
    break;


    /* case 7: //wait for |, storing +,-,Height
    a = myUart.receiveByte() & 255;
    if (a != '|') buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    Height = value[noparse][[/noparse]0];
    Format.printf("Height = %d :",Height);
    index = 0;
    state = 0;
    }
    break;
    */
    case 7: //storing +,-,Height
    a = myUart.receiveByte() & 255;
    if ((a == '-') || (a == '+') || ((a >= '0') && (a <= '9'))) buf[noparse][[/noparse]index++] = (char)a;
    else {
    buf[noparse][[/noparse]index] = 0;
    Format.sscanf(buf,"%d",value); //convert signed number in buf to binary value
    Height = value[noparse][[/noparse]0];
    Format.printf("Height = %d\n",Height);
    index = 0;
    state = 0;
    }
    break;

    }
    }
    }
    }
    932 x 835 - 242K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-07 17:04
    The if condition in case 7 is to ensure only -,+ and digits are stored in buf.
    Storing stops at any other character.
    Note that in the other cases there is a simple test for not | ,assuming
    there will only·be -,+ or digits before |
    That may however be not the case. You really should study the protocol
    description·in the manual and create a decode routine that catches all
    possible outputs.
    The code·you have now contains all the logic·you·need to implement
    such a decode routine.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-08 14:21
    This should do it.

    import stamp.core.*;
    import stamp.util.text.*;
     
    public class Stargazer {
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.invert,Uart.speed9600,Uart.stop1);
     
      static char[noparse][[/noparse]] responseBuf = new char[noparse][[/noparse]128];
      static int[noparse][[/noparse]] scanvalue = new int[noparse][[/noparse]1];
     
    /*
      10. Format of Received Data
      The format of data received from StarGazer for the command ~#CalcStart` are as follows:
           F
       ~ ^ I ±aaaa.aa|±xxxx.xx|±yyyy.yy|iiii `
           Z
       ^  Means the result data
       F  Indicates the Map-Building Mode
       I Indicates the Map Mode
       Z Indicates the Height Calculation Mode
       ±aaaa.aa Value of Angle (degrees; -180~+180)
       ±xxxx.xx Position on X axis (cm)
       ±yyyy.yy Position on Y axis (cm)
       iiii The number of an ID
    */
     
      static int mode;  // 'F', 'I', 'Z'
      static int angle; // in 0.01 deg units (-18000 to +18000)
      static int xpos;  // in 0.01 cm units
      static int ypos;  // in 0.01 cm units
      static int id;
     
      static int abs(int value) {
        return (value < 0) ? -value : value;
      }
     
      //receive reponse from StarGazer
      static void response() {
        char c;
        int k=0;
        do { //wait for ~
          c = (char)myUart.receiveByte();
        } while (c != '~');
        responseBuf[noparse][[/noparse]k++] = c;
        do { //receive until `
          c = (char)myUart.receiveByte();
          responseBuf[noparse][[/noparse]k++] = c;
        } while (c != '`');
        responseBuf[noparse][[/noparse]k] = 0; //closing null
        //get mode
        k = Format.bscanf(responseBuf,0,"~^%c",scanvalue);
        mode = scanvalue[noparse][[/noparse]0] & 255;
        //get angle integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        angle = scanvalue[noparse][[/noparse]0];
        //get angle fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        angle = (angle*100);
        if (angle < 0) angle -= scanvalue[noparse][[/noparse]0];
        else angle += scanvalue[noparse][[/noparse]0];
        //get xpos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        xpos = scanvalue[noparse][[/noparse]0];
        //get xpos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        xpos = (xpos*100);
        if (xpos < 0) xpos -= scanvalue[noparse][[/noparse]0];
        else xpos += scanvalue[noparse][[/noparse]0];
        //get ypos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        ypos = scanvalue[noparse][[/noparse]0];
        //get ypos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        ypos = (ypos*100);
        if (ypos < 0) ypos -= scanvalue[noparse][[/noparse]0];
        else ypos += scanvalue[noparse][[/noparse]0];
        //get id
        k = Format.bscanf(responseBuf,k,"%d`",scanvalue);
        id = scanvalue[noparse][[/noparse]0];
        //print full response
        Format.printf("%s\n",responseBuf);
      }
     
      static void main() {
        Format.printf("StarGazer test program\n");
        while (true) {
          response(); //get a full response, extract values and print response
          //rebuild response output fron extracted values, should be identical to response
          Format.printf("~^");
          Format.printf("%c",(char)mode);
          if (angle >= 0) Format.printf("+");
          Format.printf("%d.",angle/100);
          Format.printf("%02d|",abs(angle)%100);
          if (xpos >= 0) Format.printf("+");
          Format.printf("%d.",xpos/100);
          Format.printf("%02d|",abs(xpos)%100);
          if (ypos >= 0) Format.printf("+");
          Format.printf("%d.",ypos/100);
          Format.printf("%02d|",abs(ypos)%100);
          Format.printf("%d",id);
        }
      }
    
    }
    
    

    regards peter

    Post Edited (Peter Verkaik) : 1/8/2010 3:02:12 PM GMT
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-10 07:42
    Thank you so much Peter, you have helped me tremendously. I will try to implement the earlier codes you had, as only the highlighted outputs are correct. Thank You.
    506 x 645 - 130K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-11 08:22
    According to your latest picture the Stargazer output looks like

    ~^I16|+84.89|-26.51|-47.19|181.79`
    which is different from chapter of the manual I found

    · 10a . New format of Received Data · The format of data received from StarGazer for the command ~#CalcStart` are as follows:
    ········ F
    ·· ~ ^ I iiii | ±aaaa.aa | ±xxxx.xx | ±yyyy.yy | zzzz.zz `
    ········ Z
    ·· ^· Means the result data
    ·· F· Indicates the Map-Building Mode
    ·· I Indicates the Map Mode
    ·· Z Indicates the Height Calculation Mode
    ·· iiii The number of an ID
    ·· ±aaaa.aa Value of Angle (degrees; -180~+180)
    ·· ±xxxx.xx Position on X axis (cm)
    ·· ±yyyy.yy Position on Y axis (cm)
    ·· zzzz.zz Last value (what parameter??)

    It looks iiii now follows mode letter

    Try this

    [size=2][code]
    import stamp.core.*;
    import stamp.util.text.*;
     
    public class Stargazer2 {
      static Uart myUart = new Uart(Uart.dirReceive,CPU.pin2,Uart.invert,Uart.speed9600,Uart.stop1);
     
      static char[noparse][[/noparse]] responseBuf = new char[noparse][[/noparse]128];
      static int[noparse][[/noparse]] scanvalue = new int[noparse][[/noparse]1];
     
    /*
      10a. Format of Received Data
      The format of data received from StarGazer for the command ~#CalcStart` are as follows:
           F
       ~ ^ I iiii|±aaaa.aa|±xxxx.xx|±yyyy.yy|zzzz.zz `
           Z
       ^  Means the result data
       F  Indicates the Map-Building Mode
       I Indicates the Map Mode
       Z Indicates the Height Calculation Mode
       iiii The number of an ID
       ±aaaa.aa Value of Angle (degrees; -180~+180)
       ±xxxx.xx Position on X axis (cm)
       ±yyyy.yy Position on Y axis (cm)
       zzzz.zz What parameter ??
    */
     
      static int mode;  // 'F', 'I', 'Z'
      static int id;
      static int angle; // in 0.01 deg units (-18000 to +18000)
      static int xpos;  // in 0.01 cm units
      static int ypos;  // in 0.01 cm units
    
      static int LastValue;  // what parameter ??
     
      static int abs(int value) {
        return (value < 0) ? -value : value;
      }
     
      //receive reponse from StarGazer
      static void response() {
        char c;
        int k=0;
        do { //wait for ~
          c = (char)myUart.receiveByte();
        } while (c != '~');
        responseBuf[noparse][[/noparse]k++] = c;
        do { //receive until `
          c = (char)myUart.receiveByte();
          responseBuf[noparse][[/noparse]k++] = c;
        } while (c != '`');
        responseBuf[noparse][[/noparse]k] = 0; //closing null
        //get mode
        k = Format.bscanf(responseBuf,0,"~^%c",scanvalue);
        mode = scanvalue[noparse][[/noparse]0] & 255;
        //get id
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        id = scanvalue[noparse][[/noparse]0];
        //get angle integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        angle = scanvalue[noparse][[/noparse]0];
        //get angle fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        angle = (angle*100);
        if (angle < 0) angle -= scanvalue[noparse][[/noparse]0];
        else angle += scanvalue[noparse][[/noparse]0];
        //get xpos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        xpos = scanvalue[noparse][[/noparse]0];
        //get xpos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        xpos = (xpos*100);
        if (xpos < 0) xpos -= scanvalue[noparse][[/noparse]0];
        else xpos += scanvalue[noparse][[/noparse]0];
        //get ypos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        ypos = scanvalue[noparse][[/noparse]0];
        //get ypos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        ypos = (ypos*100);
        if (ypos < 0) ypos -= scanvalue[noparse][[/noparse]0];
        else ypos += scanvalue[noparse][[/noparse]0];
        //get LastValue integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        LastValue = scanvalue[noparse][[/noparse]0];
        //get LastValue fractional part
        k = Format.bscanf(responseBuf,k,"%d`",scanvalue);
        LastValue = (LastValue*100);
        if (LastValue < 0) LastValue -= scanvalue[noparse][[/noparse]0];
        else LastValue += scanvalue[noparse][[/noparse]0];
        //print full response
        Format.printf("received: %s\n",responseBuf);
      }
     
      static void main() {
        Format.printf("StarGazer test program\n");
        while (true) {
          response(); //get a full response, extract values and print response
          //recreate response output fron extracted values, should be identical to response
          Format.printf("recreate: ~^");
          Format.printf("%c",(char)mode);
          Format.printf("%d|",id);
          if (angle >= 0) Format.printf("+");
          Format.printf("%d.",angle/100);
          Format.printf("%02d|",abs(angle)%100);
          if (xpos >= 0) Format.printf("+");
          Format.printf("%d.",xpos/100);
          Format.printf("%02d|",abs(xpos)%100);
          if (ypos >= 0) Format.printf("+");
          Format.printf("%d.",ypos/100);
          Format.printf("%02d|",abs(ypos)%100);
          //if (LastValue >= 0) Format.printf("+");
          Format.printf("%d.",LastValue/100);
          Format.printf("%02d`\n",abs(LastValue)%100);
        }
      }
    
    }
    
    


    [/code][/size]
    Now you should get identical lines for receive and recreate

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-11 14:45
    Hi Peter,

    10a . New format of Received Data
    The format of data received from StarGazer for the command ~#CalcStart` are as follows:
    F
    ~ ^ I iiii | ±aaaa.aa | ±xxxx.xx | ±yyyy.yy | zzzz.zz `
    Z
    ^ Means the result data
    F Indicates the Map-Building Mode
    I Indicates the Map Mode
    Z Indicates the Height Calculation Mode
    iiii The number of an ID
    ±aaaa.aa Value of Angle (degrees; -180~+180)
    ±xxxx.xx Position on X axis (cm)
    ±yyyy.yy Position on Y axis (cm)
    zzzz.zz Last value (what parameter??)

    zzzz.zz --> was the height parameter.
    In my case, i configure the localizer to be Map Mode, so it indicates a "I"

    The output is in this sequence: ~^MapMode IDNum | Angle | X | Y | Height ` (~^I16|+84.89|-26.51|-47.19|181.79`)

    Thank You Very Much for your help. I have attached the output using your latest codes. The localizer got this weird prob of giving a wrong output after every few correct ones.

    Post Edited (StereoPonyz) : 1/11/2010 2:57:51 PM GMT
    454 x 823 - 153K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-11 16:17
    It looks you are occasionally have buffer overruns.
    You can add a check for proper message

    //receive reponse from StarGazer
    static void response() {
      char c;
      int k,num;
      while (true) {
        k = 0;
        do { //wait for ~
          c = (char)myUart.receiveByte();
        } while (c != '~');
        responseBuf[noparse][[/noparse]k++] = c;
        do { //receive until `
          c = (char)myUart.receiveByte();
          responseBuf[noparse][[/noparse]k++] = c;
        } while (c != '`');
        responseBuf[noparse][[/noparse]k] = 0; //closing null
    [color=red]    num=0
        for (int i=0; i<k; i++) { //check for 4 '|'
          if (responseBuf[noparse][[/noparse]i] == '|') num++;
        }
        if (num != 4) continue; //skip invalid message
    [/color]    //get mode
        k = Format.bscanf(responseBuf,0,"~^%c",scanvalue);
        mode = scanvalue[noparse][[/noparse]0] & 255;
        //get id
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        id = scanvalue[noparse][[/noparse]0];
        //get angle integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        angle = scanvalue[noparse][[/noparse]0];
        //get angle fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        angle = (angle*100);
        if (angle < 0) angle -= scanvalue[noparse][[/noparse]0];
        else angle += scanvalue[noparse][[/noparse]0];
        //get xpos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        xpos = scanvalue[noparse][[/noparse]0];
        //get xpos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        xpos = (xpos*100);
        if (xpos < 0) xpos -= scanvalue[noparse][[/noparse]0];
        else xpos += scanvalue[noparse][[/noparse]0];
        //get ypos integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        ypos = scanvalue[noparse][[/noparse]0];
        //get ypos fractional part
        k = Format.bscanf(responseBuf,k,"%d|",scanvalue);
        ypos = (ypos*100);
        if (ypos < 0) ypos -= scanvalue[noparse][[/noparse]0];
        else ypos += scanvalue[noparse][[/noparse]0];
        //get LastValue integer part
        k = Format.bscanf(responseBuf,k,"%d.",scanvalue);
        LastValue = scanvalue[noparse][[/noparse]0];
        //get LastValue fractional part
        k = Format.bscanf(responseBuf,k,"%d`",scanvalue);
        LastValue = (LastValue*100);
        if (LastValue < 0) LastValue -= scanvalue[noparse][[/noparse]0];
        else LastValue += scanvalue[noparse][[/noparse]0];
        //print full response
        Format.printf("received: %s\n",responseBuf);
        break;
    
      }
    }
    
    

    regards peter
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-11 16:45
    Wow~ thanks peter, that problem is solved [noparse]:D[/noparse]
  • StereoPonyzStereoPonyz Posts: 82
    edited 2010-01-11 18:25
    Hi Peter, sorry for troubling again. I actually need the StarGazer output to do some tasks and calculation. However, my robot cannot move as i want it to. What i wish my robot will do is that,
    it will: turn Clockwise till it meet the condition (89<angle<91) --> move straight --> (if angle>91) --> turn anti-Clockwise --> move straight.
    I would like my robot to move within the angle and so if it is greater den 91, turn anti-clockwise to lesser than 91 before moving off again.

    However, my robot output is: Turn clockwise -> meet cond (89<angle<91) -> go straight -> angle>91 -> STILL Clockwise.

    I am not sure where got have gone wrong, whether it is my placement of subroutine or even the codes itself. I tried placing my Anticlockwise at other places too but din work.

    The attachments are the flow of the terminal output, start from movement 1 --> movement 2

    static void moveStraight() {
    System.out.println("move straight");
    pwmR.update ( 171, 2304 ) ; //A-CW < 173 < CW
    pwmL.update ( 175, 2304 ) ; //move Straight
    pwmR.start () ;
    pwmL.start () ;
    CPU.delay(100);
    pwmR.stop () ;
    pwmL.stop () ;
    if (angle/100 > 91) turnAntiClockwise ();
    }

    static void turnClockwise() {
    System.out.println("turnClockwise");
    pwmR.update ( 175, 2304 ) ; //A-CW < 173 < CW
    pwmL.update ( 175, 2304 ) ; //turn CW
    pwmR.start () ;
    pwmL.start () ;
    CPU.delay(100);
    pwmR.stop () ;
    pwmL.stop () ;
    }

    static void turnAntiClockwise() {
    System.out.println("turnAntiClockwise");
    pwmR.update ( 171, 2304 ) ; //A-CW < 173 < CW
    pwmL.update ( 171, 2304 ) ; //turn CW
    pwmR.start () ;
    pwmL.start () ;
    CPU.delay(100);
    pwmR.stop () ;
    pwmL.stop () ;
    }


    static void main() {
    Format.printf("StarGazer test program\n");
    Format.printf("~^MapMode IDNum | Angle | X | Y | Height `");
    while (true) {
    response(); //get a full response, extract values and print response
    //recreate response output from extracted values, should be identical to response



    if ((angle/100 >= 89) && (angle/100 <= 91)) {
    System.out.println ("Correct Angle");
    moveStraight();
    }

    else {
    System.out.println ("Turn Robot");
    turnClockwise();
    }



    Format.printf("\nRecreate: ~^");
    Format.printf("%c",(char)mode);
    Format.printf("%d|",id);
    if (angle >= 0) Format.printf("+");
    Format.printf("%d.",angle/100);
    Format.printf("%02d|",abs(angle)%100);
    if (xpos >= 0) Format.printf("+");
    Format.printf("%d.",xpos/100);
    Format.printf("%02d|",abs(xpos)%100);
    if (ypos >= 0) Format.printf("+");
    Format.printf("%d.",ypos/100);
    Format.printf("%02d|",abs(ypos)%100);
    //if (LastValue >= 0) Format.printf("+");
    Format.printf("%d.",height/100);
    Format.printf("%02d`\n",abs(height)%100);
    }
    }
    }
    801 x 815 - 100K
    801 x 815 - 96K
Sign In or Register to comment.