Announcement

Collapse
No announcement yet.

Gyro Boy Program

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gyro Boy Program

    This is the first in a series of posts I'll be writing about the Gyro Boy robot. The program supplied with the robot works, but is almost impossible to read. Tim challenged my to write a literate Gyro Boy program and I think it is a great opportunity to show of some of the power of visual programming that few teams ever realize. Note that this is more a reinterpretation than a strict translation. You'll never find two programmers who agree 100% on how a project should be implemented.

    Attached is a shot of my top level Gyro Boy program. It took me hours to read the LEGO version of this program. A couple of minutes should be adequate to understand what my version is doing. Comments would have gone a long way in making the LEGO version easier to understand, but I hope to show how hierarchy and modularity are equally, if not more important.

    The most noticeable thing when looking at the image are the large comments. In Literate Programming comments are not used to describe the code, which needs no comments because it is easy to understand, but rather to document the design and usage. The top comment block describes what this program does (how you use it) at a high level. After reading the comment you should be able to use Gyro Boy.

    Below the main comment are two loops; Balance and Command. EV3 lets you label loops for the Loop Interrupt block, but I think the labels are great for documentation too. The top loop balances the robot. The bottom loop uses the color and ultrasonic sensors to tell the robot what to do. Everything is presented at a high level for quick understanding. To find detail you must delve down into the next layer of MyBlocks.

    To be continued...
    Attached Files

  • #2
    Re: Gyro Boy Program

    The Initialize MyBlock

    The first MyBlock we'll open up also implements the first state in Gyro Boy's state machine; Initialize. The Initialize state is where Gyro Boy performs all the setup work that has to happen before the program can run. I like using an Initialize block because it provides a logical grouping of what may first appear to be unrelated activities. By grouping them in the Initialize block I tell my reader that all these activities need to happen first, and I make it easy on myself by providing a way to guarantee that all this setup stuff is done before the program proceeds.

    Again the most noticeable thing in the image is the huge starting comment. The comment describes the purpose of the block and provides some detailed information. It also includes a stream of thought discussion about variable usage and naming conventions in EV3. This provides the reader with insight into how I design software. The insight will hopefully help the reader understand my design decisions. This is how I write comments in my real world programming job. I get teased for being so verbose, but few programmers ever come to me with questions about what a particular programming module does or how it works.

    To be continued.
    Attached Files

    Comment


    • #3
      Re: Gyro Boy Program

      The State MyBlock

      This is one of the more interesting MyBlocks in the program. The LEGO Gyro Boy program had a state variable that was either 0, 1 or 2. Display and sound blocks usually accompanied Write blocks for the state variable. From the display blocks and some code analysis I decided the three states were Sleeping, Waking and Running.

      A problem with the Gyro Boy code was that there was no real connection between the state variable and the display blocks. For all I know my interpretation of the states is completely wrong. What was needed is a mechanism to relate the state variable with the required actions, and it wouldn't hurt if the variable values were more meaningful than 0, 1 and 2. This is what the State MyBlock does.

      The State Myblock is a variable that performs actions when its value is changed. Whereas most MyBlocks are functions or procedures, they process data, the State Myblock is an object, it has both procedures and data. State is set to whatever value I type into its Input plug. The block performs different actions based on what the input value is. To get the current State I use "Get" for the Input. Because "Get" is a common operation, and because it is completely safe to run at any time, I made it the default input value.

      Many programming languages have the concept of user defined ordinal types. Essentially these are numbers that you get to rename. For State I really would prefer to use "Sleep" instead of 0, and "Wake" instead of 1. Unfortunately NXT and EV3 don't have user defined ordinal types. LabVIEW has ordinal types, but I notice there are a lot of programs that don't use them. In a lot of cases text is used instead. I can use text for cases in a switch block, so I decided to see how well text would work in EV3. Other than it being really hard to compare two strings, it works really well. In fact I've started using strings and switch blocks as a way to simulate the Modes that many built in blocks have (i.e. a Math block has a 8 modes going from "+" to "Advanced"). Using text appears to be no slower than using a number, and my only concern is typos. For that I sometimes have an "Error" default case that prints an error message and plays a tone if I provide an invalid input.

      To be continued.
      Attached Files
      Last edited by Dean Hystad; 09-03-2013, 09:37 AM.

      Comment


      • #4
        Re: Gyro Boy Program

        Thanks Dean, it looks great so far!
        I'm trying to restrain an urge to buy an EV3 just to play with the new gyro sensor...

        Comment


        • #5
          Re: Gyro Boy Program

          The GyroOffset MyBlock

          Experiments have shown that the EV3 Gyro sensor can acquire a bias under certain conditions (it doesn't read zero when the sensor is at rest). This bias is not reset by the Gyro Reset mode. The GyroOffset MyBlock measures this bias so it can be removed in the balancing equation. Measuring the gyro bias successfully is what transitions the program from Sleep to Wake mode.

          All my comments so far document design and the design process. This block contains a comment that comments the code. The equation that computes the average gyro rate reading uses the loop counter to get the sample count. This allows changing the CollectData loop count without having to change the code. But the loop counter returns a value one less than the loop count and the equation has to compensate. It is not obvious why there is a "+ 1" in the numerator, and I don't want to force the reader to read the loop block help file, so I use a comment to document the code.

          To be continued.
          Attached Files

          Comment


          • #6
            Re: Gyro Boy Program

            The Balance MyBlock

            This is the block that balances the robot and turns and moves as directed by the Command loop. It doesn't look all that impressive, but it really doesn't do all that much. It computes power commands for the left and right motor and checks to make sure the robot hasn't fallen down. One of my design philosophies is to keep reworking and refactoring until all the modules are unimpressive and completely obvious. A few years ago one of the lads came up with an idea that simplified the sequencer program design. He expressed concern that the resulting code looked too simple and wouldn't impress the design judge. I told him that our design judges are all very knowledgeable and experienced. They would recognize how much work it took to make the program look that simple.
            Attached Files
            Last edited by Dean Hystad; 09-04-2013, 03:03 AM.

            Comment


            • #7
              Re: Gyro Boy Program

              The Power MyBlock

              The power MyBlock evaluates the equation that converts sensor feedbacks and tuning gains into a motor power command that will balance the robot. This equation is pretty much a direct copy of the LEGO Gyro Boy software. In includes a couple of design decisions I don't agree with or even think are valid. I particular I don't like the Motor Rate feedback and gain. I think this is to adjust the motor power for how fast the robot is going. To balance the robot has to accelerate, and to accelerate we need to change the power setting. The motor rate term provides a way to get that moving frame of reference from which our acceleration is applied. I think the design would be cleaner if we computed what the acceleration should be and integrated the acceleration to get a motor rate or power command. Something else to try when I find time.
              Attached Files

              Comment


              • #8
                Re: Gyro Boy Program

                A Redo on the Balance Block

                I found I didn't much like my Balance block. The block does three important things; It balances the robot, it executes commands, and it detects when the robot falls down. I thought the balance and fall down parts were pretty clear, but I couldn't see any connection between the Command loop and the steering and power blocks inside the balance loop. I redesigned the loop to make the relationship more apparent and at the same time got rid of the global variables used to pass the Move and Steer information.
                Attached Files
                Last edited by Dean Hystad; 09-14-2013, 09:12 AM.

                Comment


                • #9
                  Re: Gyro Boy Program

                  Dean,

                  I am newbie to the forum and the labview programming. Your program seems very nice but there are several MyBlock that are not documented. Would it possible to have those detailed as well? Many thanks in advance...(Sorry if you had the intention to do it but had not yet the time to post the missing parts)

                  Comment


                  • #10
                    Re: Gyro Boy Program

                    Alternatively, would it possible that someone post screenshots of the Gyro boy program from the Education package? (I have the non-educational pack but bought the required items to build the gyro boy: I have built it now, but I am not able to write the program to drive it...)

                    Comment


                    • #11
                      Re: Gyro Boy Program

                      Attached is my Gyro Boy program. I had to change the extension to .zip because .ev3s is not one of the extension accepted by the forum. Sorry it took so long, but I made some changes to the software and I needed to rebuild the Gyro Boy robot to test the program before posting.
                      Attached Files

                      Comment


                      • #12
                        Re: Gyro Boy Program

                        Thank you very much for sharing. I remember my 'Modern Control' course back in school, and controlling an inverted pendulum is no trivial task as one need to do a full-state feedback. Obviously, you have put a lot of work/thinking into this, and that is much appreciated. My son has been looking for this for some time now. We'll try it tonight and get back ... Cheers.
                        Last edited by jb63; 10-29-2013, 08:14 PM.

                        Comment


                        • #13
                          Re: Gyro Boy Program

                          There is an error in the design of the "Action" myblock. It doesn't affect performance much, but it could if you decided to give Gyro Boy more complicated actions (instead of just twitching his arms occasionally). I found it when I tried to have Gyro Boy dance to music. Action should be broken up into two MyBlocks, one for setting the action and another for reading the action. Using the same MyBlock in both the command and balance loops causes the balance loop to wait while commands are entered in the control loop.

                          Oops.

                          Comment


                          • #14
                            Re: Gyro Boy Program

                            Works great ... was able to balance itself on a hard table. With a soft carpet on the floor, it did not do too well, most likely it is because the robot could not set a balanced 'zero' as could not keep it vertical/still on the carpet. I'd need to study your program in detail to understand the various dynamics at play. Your program is very elaborate. Any idea how this compares with that on the lego EDU CD?

                            Comment


                            • #15
                              Re: Gyro Boy Program

                              Originally posted by Dean Hystad View Post
                              There is an error in the design of the "Action" myblock. It doesn't affect performance much, but it could if you decided to give Gyro Boy more complicated actions (instead of just twitching his arms occasionally). I found it when I tried to have Gyro Boy dance to music. Action should be broken up into two MyBlocks, one for setting the action and another for reading the action. Using the same MyBlock in both the command and balance loops causes the balance loop to wait while commands are entered in the control loop.

                              Oops.
                              Can you please elaborate a bit on this. A screenshot of the new ACTION block would be appreciated.

                              Comment

                              Working...
                              X