Shop OBEX P1 Docs P2 Docs Learn Events
Main Method won't run — Parallax Forums

Main Method won't run

GibithGibith Posts: 18
edited 2008-02-06 04:54 in General Discussion
The main method is completely skipped when it hits the first for statement. I have tried to debug it and it just skips to the end. I get bizarre Error and debug messages like: Error IDE-0010 Error Communicating over the serial port (corrupt packet $00);
[noparse][[/noparse]Debug] Discarding packet (<-- I get this one multiple times.); [noparse][[/noparse]Debug] Unknown message from Javelin.
public static void main(){
  Rover a = new Rover(2);
  ChromoAmount = DNA.length;
  Timer min = new Timer();
  min.mark();
  System.out.println("Program RUnning");
  for(int i = 0; i > 10; i ++){




    a.DNA = new String[noparse][[/noparse]15];


  for( i = 0; i < DNA.length; i ++){


      if (DNA[i] == null){
        continue;
        }
      if (DNA[i] == StartServosEx){
        a.StartServos();
        if (CheckTime(min)){

          break;
          }
        else continue;
        }
      if (DNA[i] == StopLeftEx){
        a.StopLeft();
        if (CheckTime(min)){
          break;
          }
        else continue;
        }

      if (DNA[i] == StopRightEx){
        a.StopRight();
        if (CheckTime(min)){
          break;
          }
        else continue;
        }

      if (DNA[i] == ChangeRightEx){
        a.ChangeRight();
        if (CheckTime(min)){
          break;
          }
        else continue;

        }
      if (DNA[i] == ChangeLeftEx){
        a.ChangeLeft();
        if (CheckTime(min)){
          break;
          }
        else continue;

        }
      if (DNA[i] == DelayEx){
        a.Delay();
        if (CheckTime(min)){
          break;
          }
        else continue;

        }
      if (DNA[i] == RandomizeRightEx){
        a.RandomizeRight();
        if (CheckTime(min)){
          break;
          }
        else continue;

        }
      if (DNA[i] == RandomizeLeftEx){
        a.RandomizeLeft();
        if (CheckTime(min)){
          break;
          }
        else continue;

        }
        } /* End of for loop */
        if (RightUsed == true){
          RightUsed = false;
          PWM11 = InPWM11;
          }
        else if  (LeftUsed == true){
          LeftUsed = false;
          PWM22 = InPWM22;
          }
        else{
        System.out.println("Else!");
        }
        System.out.println("Pre Mutation");
        a.MutationMarker(a);
        a.Mutate(a);
        lstDNA = a.DNA;
        GenerationPop.add(lstDNA);
        System.out.println("Post Mutation");
        while(true)
          {
          switch(Terminal.getChar()){
          case 'c':

          System.out.println(begin);
          for(int k = 0; k < GenerationPop.size(); k ++){
            for(int j = 0; j < ((String [noparse][[/noparse]])GenerationPop.get(k)).length; j++){
            System.out.println(((String [noparse][[/noparse]])GenerationPop.get(k))[noparse][[/noparse]j]);[/i][/i][/i][/i][/i][/i][/i][/i][/i]


This is the main method. The whole thing compiles fine. I need to figure this out ASAP. So please don't hesitate to help. I am running the latest IDE from parallax and I'm using a Serial port on my windows XP PC.

Post Edited (Gibith) : 2/5/2008 5:30:32 AM GMT

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-05 06:45
    This is not the complete code. Please post your code as an attachement so I
    can load it into the IDE to figure out what happens.

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-05 15:08
    Here is the complete code. I know it's inefficient and there may be some unused variables but I wrote the program quickly plus it was late.

    import stamp.core.*;
    //import stamp.core.PWM;
    //import stamp.core.Timer;
    import java.util.*;
    import stamp.util.*;
    public class RetardRover{
      public static int MutMarker [noparse][[/noparse]];
      public static List GenerationPop = new List(10);
      public static String [noparse][[/noparse]] lstDNA;
      public static String [noparse][[/noparse]] DNA;
      public static Random rDelay = new Random(17);
      public static Random rPWM11 = new Random(4);
      public static Random rPWM22 = new Random(16);
      public static int rate;
      public static int ChromoAmount;
      public static int PWM11 = 173;
      public static int PWM22 = 173;
      public static String Possible [noparse][[/noparse]] = new String[noparse][[/noparse]7];
      public static int DelayTime;
      public static PWM Right = new PWM(CPU.pin12, 173, 2304);
      public static PWM Left = new PWM(CPU.pin13, 173, 2304);
      public static String StartServosEx = "StartServos";
      public static String StopLeftEx = "StopLeft";
      public static String StopRightEx = "StopRight";
      public static String ChangeRightEx = "ChangeRight";
      public static String ChangeLeftEx = "ChangeLeft";
      public static String DelayEx = "Delay";
      public static String RandomizeRightEx = "RandomizeRight";
      public static String RandomizeLeftEx = "RandomizeLeft";
      public static boolean OutoTime = false;
      public static final int TimeLimit = 360; /* This could be a value that I would want to add into the constructor */
      public static boolean RightUsed = false;
      public static boolean LeftUsed = false;
      public static int InPWM11;
      public static int InPWM22;
      public static String begin = "Begin";
      public static String end  = "End";
    
    
    
      public RetardRover(int MutationRate){
        rate = MutationRate;
        }
    
    
    
      public static void main(){
      RetardRover a = new RetardRover(2);
      ChromoAmount = DNA.length;
      Timer min = new Timer();
      min.mark();
      System.out.println("Program RUnning");
      for(int i = 0; i > 10; i ++){
    
    
    
    
      a.DNA = new String[noparse][[/noparse]15];
    
    
      for( i = 0; i < DNA.length; i ++){
    
    
          if (DNA[i] == null){
            continue;
            }
          if (DNA[i] == StartServosEx){
            a.StartServos();
            if (CheckTime(min)){
    
              break;
              }
            else continue;
            }
          if (DNA[i] == StopLeftEx){
            a.StopLeft();
            if (CheckTime(min)){
              break;
              }
            else continue;
            }
    
          if (DNA[i] == StopRightEx){
            a.StopRight();
            if (CheckTime(min)){
              break;
              }
            else continue;
            }
    
          if (DNA[i] == ChangeRightEx){
            a.ChangeRight();
            if (CheckTime(min)){
              break;
              }
            else continue;
    
            }
          if (DNA[i] == ChangeLeftEx){
            a.ChangeLeft();
            if (CheckTime(min)){
              break;
              }
            else continue;
    
            }
          if (DNA[i] == DelayEx){
            a.Delay();
            if (CheckTime(min)){
              break;
              }
            else continue;
    
            }
          if (DNA[i] == RandomizeRightEx){
            a.RandomizeRight();
            if (CheckTime(min)){
              break;
              }
            else continue;
    
            }
          if (DNA[i] == RandomizeLeftEx){
            a.RandomizeLeft();
            if (CheckTime(min)){
              break;
              }
            else continue;
    
            }
            } /* End of for loop */
            if (RightUsed == true){
              RightUsed = false;
              PWM11 = InPWM11;
              }
            else if  (LeftUsed == true){
              LeftUsed = false;
              PWM22 = InPWM22;
              }
            else{
            System.out.println("Else!");
            }
            System.out.println("Pre Mutation");
            a.MutationMarker(a);
            a.Mutate(a);
            lstDNA = a.DNA;
            GenerationPop.add(lstDNA);
            System.out.println("Shet sum Mewtant!");
            while(true)
              {
              switch(Terminal.getChar()){
              case 'c':
    
              System.out.println(begin);
              for(int k = 0; k < GenerationPop.size(); k ++){
                for(int j = 0; j < ((String [noparse][[/noparse]])GenerationPop.get(k)).length; j++){
                System.out.println(((String [noparse][[/noparse]])GenerationPop.get(k))[noparse][[/noparse]j]);
              }
              }
              }
              }
            }/* End of for loop */
            }/* End of main method */
    
    
    
      public static boolean CheckTime(Timer TimerName){
        if (TimerName.timeoutSec(TimeLimit));  /*Check to see if 1 minute has passed*/
          return OutoTime = true;
    
      }
    
      //public static void LIVE(RobotRetard){
        //System.out.println("sup");/* Put DNA Strand Together */
    
    
        //}
    
    
      public static String StartServos(){
        Right.start();
        Left.start();
        return StartServosEx;
        }
      public static String StopLeft(){
        Left.update(173, 2304);
        return StopLeftEx;
        }
      public static String StopRight(){
        Right.update(173, 2304);
        return StopRightEx;
        }
      public static String ChangeRight(){
        Right.update(PWM11, 2304);
        return ChangeRightEx;
        }
      public static String ChangeLeft(){
        Left.update(PWM22, 2304);
        return ChangeLeftEx;
        }
      public static String Delay(){
        CPU.delay(DelayTime);
        return DelayEx;
        }
      public static String RandomizeRight(){
        InPWM11 = PWM11;
        PWM11 = ((rPWM11.next() % 130)+110);
        RightUsed = true;
        return RandomizeRightEx;
        }
      public static String RandomizeLeft(){
        InPWM22 = PWM22;
        PWM22 = ((rPWM22.next() % 130)+110);
        LeftUsed = true;
        return RandomizeLeftEx;
        }
    
    
    
      public static void BuildLists(RetardRover a){
    
        Possible[noparse][[/noparse]0] = StartServosEx;
        Possible = StopLeftEx;
        Possible = StopRightEx;
        Possible = ChangeRightEx;
        Possible = ChangeLeftEx;
        Possible = DelayEx;
        Possible[noparse][[/noparse]6] = RandomizeRightEx;
        Possible[noparse][[/noparse]7] = RandomizeLeftEx;
    
        }
    
    
      public static void MutationMarker(RetardRover a){
              Random RandRate = new Random(144);
              ChromoAmount = DNA.length;
              int MutMarker [noparse][[/noparse]]= new int[noparse][[/noparse]ChromoAmount]; /* Make MutMarker as long as the number of chromosomes a 1 signifies a mutation*/
              for(int i = 0; i < ChromoAmount; i ++){ /* iterate through MutMarker*/
                if((RandRate.next() % rate) == rate){         /* if a mutation occurs on a specific chromosome */
                  MutMarker [i] = 1;                  /* mark that chromosome */
                  Random coinFlip = new Random(23);
                  int MutType = ((coinFlip.next() % 2) + 1);
                  if (MutType == 1){
    
                  }       /* End if statement */
                  }       /* End if statement */
                  }       /* End for loop */
                  }
      public static void Mutate (RetardRover a){
        Random coinFlip = new Random(23);
        Random RandPoss = new Random(42);
        Random MutRight = new Random(99);
        Random MutLeft = new Random(91);
        int MutChromoType;
        int MutType = ((coinFlip.next() % 2) + 1);    /*is it a point mutation or chromosome mutation? */
        if (MutType == 1){                            /*if it is a chromosome Mutation */
          MutChromoType = ((coinFlip.next() % 2) +1); /* what type of chromosome mutation is it? */
          if (MutChromoType == 1){                    /* If it is a insertion Chromsome Mutation */
            a.DNA[noparse][[/noparse]-1]=(Possible[noparse][[/noparse](((RandPoss.next() % 7) + 1))]); /* Add a random Chromosome */
            }
          else if (MutChromoType == 2){   /* if it is a chromosome deletion */
            a.DNA[noparse][[/noparse]((RandPoss.next() % ChromoAmount) + 1)] = null;
          } /* end else if */
          } /* end if */
        if (MutType == 2){
          for(int i = 0; i >= MutMarker.length; i ++){
            if (MutMarker[i] == 1){
              if (DNA[i] == null){
                continue;
                }
              if (DNA[i] == DelayEx){
                DelayTime = rDelay.next();
                }
              if  (DNA[i] == ChangeRightEx){
                PWM11 = ((MutRight.next() % 130) + 110);
                }
              if  (DNA[i] == ChangeLeftEx){
                PWM22 = ((MutLeft.next() % 130) + 110);
                }
    
          }
          }
          }
          }
          } /*End of class */[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    


    There are also some inside jokes pretaining to some of the System.out.println lines. These are just to test if a piece of code has executed.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-02-05 15:15
    Gibith, please post the code as an attachment (instead os pasting it into the message) so it can be loaded directly into the editor.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-05 16:11
    Do post code as an attachement. You can upload the java files.
    From what I see, you declare a class RetardRover, that has a constructor,
    and it has a static void main() method, and from main() you create
    RetardRover instances. Maybe that works on the PC (and the compiler allows
    it because it is a PC compiler), but it is bad programming.
    Put the method main() into its own class, say RetardRover_test,
    and import the RetardRover class. That also has the benefit
    that you can reuse the RetardRover class in other projects.

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-05 16:48
    I'm having trouble importing the class. I have it in the projects folder in another folder called gene. I looked in global options and here are my class paths :
    C:\Program Files\Parallax Inc\Javelin Stamp IDE\lib;C:\Program Files\Parallax Inc\Javelin Stamp IDE\Projects
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-05 16:52
    If you put your RetardRover class in ...\Projects\gene folder, then use
    import gene.*;
    If that doesn't work, move your gene folder to
    ...\lib\stamp
    and use
    import stamp.gene.*;

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-05 16:56
    if I do that I get Error saying that there is no type gene/RetardRover found. But import is working I don't get errors there.
    I thought all you had to do was delcare something public and when you imported it you could manipulate it directly.

    Post Edited (Gibith) : 2/5/2008 5:01:25 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-05 17:15
    The best way to do things is:

    //file RetardRover.java
    package stamp.gene;
    import stamp.core.*;

    public class RetardRover {

    · //your class code

    }

    Save the file RetardRover.java in folder ...\lib\stamp\gene

    //file RetardRover_test.java
    import stamp.core.*;
    import stamp.gene.*

    public class RetardRover_test {

    · //your class code here (remember to use static for variables)

    · static void main() {
    ··· //your main method
    · }

    }

    Save the file RetardRover_test.java either in folder Projects or folder ...\lib\stamp\gene
    When you program the javelin, make sure you have this file active (on top)

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-05 18:25
    It still seems to skip the main method, I tried debugging it and when I hit step into to step into the for loop it skips the the last bracket in the main method. When I get to the end of main method while debugging I don't get any errors.
  • GibithGibith Posts: 18
    edited 2008-02-05 18:42
    I found the cause of my first problem. I wanted something to repeat ten times and instead of writing:

    for(int i = 0; i < 10; i++){code}
    
    


    I wrote it like this:
    for(int i = 0; i < 10; i++){code}
    
    



    But I have more issues pertaining to this program. I will post them soon.
  • GibithGibith Posts: 18
    edited 2008-02-06 02:51
    This is the final barrier between me and my completed project yeah.gif. when I try and return a String array I get this error: [noparse][[/noparse]Java Error] RetardRover.java(190): Syntax: Invalid Type. I just need it to compile not run on it's own. I have attached the code.

    Thanks,
    Gibith

    Nevermind I fixed that problem however I have a new one. I keep getting index out of bounds error but i don't understand why it's out of bounds. I appears to be in bounds.

    Post Edited (Gibith) : 2/6/2008 3:50:36 AM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-06 03:36
    I edited your files. Both files go into folder ...\lib\stamp\gene
    and both files compile now without error.
    You need to check them for possible logical errors.
    I also think you need to think about how to setup this particular project,
    as it seems to me that RetardRover could be split in several more logical units.

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-06 03:57
    I'm sorry that I am double posting but I edited my last post and I wanted to make it clear that I had a new issue. This one is my last issue (hopefully).
    I keep getting index out of bounds error but i don't understand why it's out of bounds. It appears to be in bounds.

    Post Edited (Gibith) : 2/6/2008 4:06:23 AM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-06 04:06
    Please start using the RetardRover.java I uploaded. That one compiles.
    If you take it from there you will have less issues.
    An index out of bounds exception·means you are indexing an array
    beyond its limits, for example if you declare
    · int[noparse]/noparse p = new int[noparse][[/noparse]5];
    you can only use p[noparse][[/noparse]0] to p[noparse][[/noparse]4]
    Using p[noparse][[/noparse]5] will generate an index out of bounds exception.

    regards peter
  • GibithGibith Posts: 18
    edited 2008-02-06 04:24
    Thanks I'm using your code now Peter. I have a problem as far as Randomizing goes. If I use the same seed on each block of code I'm going to get the same results.
    How can I randomize the seed values? I thought that maybe I could use some external stimulus. At this point I want something that works and something I can quickly incorporate into my code.

    Thanks,
    Gibith
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-02-06 04:54
    If you store blocks in an array (like DNA[noparse]/noparse), you can use the index as seed value.
    Then the blocks will get different random values.

    regards peter
Sign In or Register to comment.