A Robotics Project Page
Google
 
Web    www.PhilBot.com
My other sites
  • OurCoolHouse
  • Phil's Resume
  • Web Portfolio

  • [Home] [Projects] [Hardware] [Software] [Books] [Links] [Downloads]


    Examples of using the V6.0 MkIII oUserClass for Sumo

    On my [PBot_MkIII_1.0_VC6] page I describe the inner workings of the OOPic oUserClass I created to control the Mark III Mini-Sumo robot.  This class doesn't actually do Sumo, it just provides all the important interfaces in one central location.  The reason that the UserClass doesn't actually make any Sumo decisions is that I want to try out different strategies, so the "fighting intelligence" is left outside of the class itself.  It also means I can use the class for other functions like Line Following or Maze Solving etc.

    This oUserClass is written for the latest V6.0 version of the OOPic compiler.  If you need one for the 5.0 version, you can find it here.

    The full source code for each of these samples is available on the [downloads] page.  Make sure you pair it with the correct oUserClass program.

    As always, the first thing I do is define some additional constants.  I'll use these for setting the wheel speeds. You can set your own values if you want. 

    // Create motion constants (based on specific Bot)
    Const   FAST_FORWARD    =  10;
    Const   SLOW_FORWARD    =   1;
    Const   FAST_REVERSE    = -10;

    With the OOPic compiler, the way you use an oUserClass is to declare an object and pass it the file name of the program that contains the UserClass.    In this case the Class file is called:  PBot_MkIII_1.0_VC6.osc  The new 6.0 compiler allows you to use a relative path, so I just keep the Class file in the same folder as the sample programs. Here is what the code looks like to reference the user-class.  Keep in mind I'm using the C syntax.

    // Create instance of MkIII Object.  
    oUserClass BOT = New oUserClass("PBot_MkIII_1.0_VC6.osc");

    To use this class I simply need to reference the members and functions by prefixing their names with "BOT.". 
    So what ARE the Members (variables) and Functions?  Well to make it easy to figure out, I have a header section in the class file that explains the various items that are available to be used.
    Tip: A "Header" is a long comment at the top of a program that explains the overall program to a potential user. I use this area to list the Members and Functions available in a user class.

    Here's the relevant section of the UserClass header:

    // Boolean flags indicate when each sensor is detecting its target.
    //
    //  LeftEyeOn               True if Left eye sees opponent
    //  RightEyeOn              True if Right eye sees opponent
    //  LeftLineWhite           True if Left line sensor sees white
    //  CenterLineWhite         True if Center line sensor sees white
    //  RightLineWhite          True if Right line sensor sees white
    //
    // FunctionsSubroutones are provided to calibrate the inputs and set the 
    // MarkIII in motion.
    //
    //  EnableDiagnostics()     Call to power up LEDs
    //  FollowerCalibrate()     Calibrate when centered over a line to be followed
    //  SumoCalibrate()         Calibrate when on black Sumo Ring
    //  Drive(Left Speed, Right Speed, Duration)    Turn wheels as indicated
    //  WaitTenths(Tenths)      Pause for a number of "tenths of seconds"
    //  ObjectTest()            Call this to test the drive and display Status LEDS

    OK, so now we know what's available to be used, let's get down to the business of a basic Sumo program..  Here's what a Sumo has to do: 

    1. Wait five seconds before moving.
    2. Locate it's opponent and push them out of the ring.

    That's pretty straight forward, especially with my MkIII User class.   The UserClass has already set up a timer that we can use, so I'll wait for it to reach one second (to give me time to place the 'Bot on the ring), then I'll calibrate the line sensor and enable the diagnostic LEDs, so I can see what the 'Bot can see.  After that I'll wait for the last second to elapse and then enter an endless loop running my Sumo strategy.

    The Strategy is the key part of the program, and you'll see it can be pretty basic.  Before I start programming, I set up a list of decisions that the 'Bot has to make, and then I assign them priorities (1 being the highest).  My program then has a bunch of If and Else If statements that test each of the conditions in priority order.  If the highest priority test fails, then the program drops down to the next priority.  Only ONE set of actions is taken each loop.

    Before you look at the code, here's the list of actions in priority order:

    Tip: Always list the priorities in the program header, so you can tell your different strategies apart later on.

    Finally here's guts of the code: the Main( ) function....  notice that the If conditions all reference members of the User Class simply by prefixing their name with "BOT."

    // Main program
    Void    Main(Void

        // Calibrate one second before Match  

        While(BOT.ActionTimer > BOT.ONE_SECOND) ;   // Wait for last second
        BOT.SumoCalibrate();                        // Calibrate for Sumo
        BOT.EnableDiagnostics();                    // Turn on status LEDs (optional)

        // Wait for last second to expire
        While(BOT.ActionTimer.NonZero) ;

        // Run main program loop forever.
        While (cvTrue) 
        {
            // Run down the SUMO priority list

            // Priority 1: Back Up from the border
            If (BOT.LeftLineWhite)
            {
                // If we see the left line, backup & Spin right 180
                BOT.Drive(FAST_REVERSE,FAST_REVERSE, 3); 
                BOT.Drive(FAST_FORWARD,FAST_REVERSE, 5); 
            }
            Else If (BOT.RightLineWhite)
            {
                // If we see the right line, backup and spin left 180
                BOT.Drive(FAST_REVERSE,FAST_REVERSE, 3); 
                BOT.Drive(FAST_REVERSE,FAST_FORWARD, 5); 
            }
            
            // Priority 2: PUSH the opponent if BOTH eyes see him
            Else If (BOT.LeftEyeOn && BOT.RightEyeOn)
            {
                // Go forward, no time limit
                BOT.Drive(FAST_FORWARD,FAST_FORWARD, 0); 
            }

            // Priority 3: Turn Towards the opponent if seen in ONE eye     
            Else If (BOT.RightEyeOn)
            {
                // Turn right towards the target, no time limit
                BOT.Drive(FAST_FORWARD,SLOW_FORWARD, 0); 
            }
            Else If (BOT.LeftEyeOn)
            {
                // Turn left towards the target, no time limit
                BOT.Drive(SLOW_FORWARD,FAST_FORWARD, 0); 
            }

            // Priority 4:  Go Straight
            Else
            {
                // Go forward, no time limit
                BOT.Drive(FAST_FORWARD,FAST_FORWARD, 0); 
            }
        }
    }

    There are several other sample programs on the [downloads] page, and they're all fully commented, so why not download thems and see what other strategies you can discover for making your Sumo just a bit smarter than the next guy's.

     

    Web content is copyright © PhilBot.com 2005, Deep Creek Lake, MD.
    Contact: Phil Malone 301.387.2331, webmaster@PhilBot.com