04
-
Topic 4: Functions
- This lab introduces the concept of functions and with it, a concept that is used across all branches of engineering, that of breaking a system down to functional units which go together to make a complete system. The other important concept that is used here is that of “Variables” which are named temporary stores for data that the program is working with. The background for this lab is to create a simple pipeline explorer which is a real industrial robot and is introduced in the linked video. As before, you do NOT need to do the Mini challenges.
-
Video on SWITCH selection structure in Robot C
- This video introduces the SWITCH statement which allows the program to be able to select one of many options depending on the input condition.
-
Video on FOR loop in Robot C
- This video expands on the WHILE loop content to include the last of the repetition structures available in Robot C.
-
Activity 1: More Programming Practice
- These exercises get you to create programs using the SWITCH and FOR structures to practice another form of selection and repetition. The exercises will be found in the Lab Book that you have already started working on. They are in section 4.1. As always, make sure that yu save your work as you go through the lab exercises.
-
Activity 2: Encoders and Variables
- The activity introduces the encoders which are built into the motors and also introduces the concept of Global Variables. These are temporary stores for data used by the program. A Global Variable is available to all parts of the program (hence the term “Global”).
- 🔗 Encoders and Variables
-
Activity 3: Functions
- This online activity introduces how functions are created and used. In this first example there is no particular benefit in using a function as it is only called once but it establishes the principle of how functions operate. Later activities will extend this. The try It exercises may take a few minutes but they are a good test of how well you have understood the creation and use of functions. In particular for the first “Try It” exercise to create a function library you should create functions that do the following: goLeft() : as the name suggests this function should get the robot to turn left by 90 degrees. goRight() : as the name suggests this function should get the robot to turn right by 90 degrees. goStraight() : This should drive the robot straight forward for 2 revolutions at 30% power.
- 🔗 Functions
-
Activity 4: Functions Practice
- These next exercises are contained within the lab booklet which you should already have downloaded. These exercises are in section 4.2 (parts 4.2.4 and 4.2.5) and you will be consolidating your understanding of simple functions before moving on to more advanced functions in the next lab.
-
Summary
- At the end of this lab you should be comfortable with the basic concept of using functions to break programs down into smaller sections of code which are called from task main. This modular approach to building a program is similar to the way that engineering problems are solved by breaking a complex problem down into smaller, more manageable elements and solving each part before integrating them into the complete solution.