Page 1 of 8 12345 ... LastLast
Results 1 to 10 of 78

Thread: Gyro Boy Program

  1. #1
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images

  2. #2
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images

  3. #3
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images
    Last edited by Dean Hystad; 09-03-2013 at 09:37 AM.

  4. #4
    Join Date
    Sep 2009
    Location
    Minnesota
    Posts
    1,029

    Default 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...

  5. #5
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images

  6. #6
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images
    Last edited by Dean Hystad; 09-04-2013 at 03:03 AM.

  7. #7
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images

  8. #8
    Join Date
    Sep 2008
    Location
    Minnesota
    Posts
    2,669

    Default 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 Images Attached Images
    Last edited by Dean Hystad; 09-14-2013 at 09:12 AM.

  9. #9
    Join Date
    Oct 2013
    Posts
    8

    Default 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)

  10. #10
    Join Date
    Oct 2013
    Posts
    8

    Default 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...)

Page 1 of 8 12345 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. moving A motor in a master program
    By Maria S in forum Programming
    Replies: 7
    Last Post: 01-08-2013, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •