C++ Robot Prelab 7 — Navigation Building Blocks
Table of Contents
Macros versus Subroutines
All compilers take two passes through your code in order to generate the machine instructions uploaded to the MCU. On the first pass macros (#define) are expanded to a list of C++ instructions, which are then compiled on the second pass. In contrast subroutines are compiled on the second pass. So when should you use a macro or a subroutine. Macros run faster than subroutines, because they are in-line with the rest of your code, while a subroutine must be called and subsequently return. Subroutines use less memory than macros because the code is not duplicated each time it is used. In some embedded applications, the microcontroller has limited Flash program memory and timing is not critical, so subroutines make sense. This is also the case if the code to implement a function is long and called many times. In other applications, timing is critical and macros make sense. This is also the case if the code to implement the function is short (one line) and it is only needed a few times. So the answer to which to use at any given time is application specific.
In this prelab, we will look at how to program navigational building blocks using macros and subroutines.
Reading
Lookup Tables
After reading Section 4.3 Digital Encoding The Maze, and studying the look_right[] sample, complete array declarations look_left[]and look_back[].
/* Look around
* look-up tables use direction the robot is facing as index
* index 0 1 2 3
* robot is looking = SOUTH WEST EAST NORTH
*/
const uint8_t look_right[] = {WEST,NORTH,SOUTH,EAST};
const uint8_t look_left[] = {____,_____,_____,____};
const uint8_t look_back[] = {____,_____,_____,____};
Using one of the look-up tables and the direction the robot is looking, (robot.dir), write a C++ statement that will tell us in which direction the robot will be looking if its head turns to the left or right.(EAST,SOUTH,NORTH,WEST).
robot looks right = _______________________;
robot looks left = _______________________;
Test if Bit is Set
After reading Lecture 3 C++ Type of Data and Playing with Bits, answer the following three questions.
All macro definitions evaluate to a value of zero (false) if the bit within the test byte is clear; otherwise evaluate to true. Where true is defined as any number that is not zero.
- What macro is defined as (1 << (bit))?
- What macro (#define) allows you to test if a special function register (sfr) bit is set?
- Write a macro named _TestBit to test if bit in byte_value is set.
#define _TestBit(byte_value, bit) _________________
Test Room Bits
Apply what you learned to write the following macros.
#define _hitWall(room, dir) ______________________________ #define _rightSensor(room, dir) __________________________ #define _leftSensor(room, dir) __________________________
Each macro evaluates to a value of zero (false) if there is no wall in the direction the robot is looking; otherwise evaluates to true. Where true is defined as any number that is not zero.
Hint: While _hitWall directly follows from your answer to question 3, macros _rightSensor and _leftSensor will use arrays look_right and look_left respectively to identify the bit to test.
Macros versus Subroutines
Convert your macros in the last section into subroutines. Please, do not simply add the macro.
bool hitWall(room, dir) { return ______________________________; } bool rightSensor(room, dir) { return ______________________________; } bool leftSensor(room, dir) { return ______________________________; }
Each subroutine returns to Boolean value of 0 (false) if there is no wall in the direction the robot is looking; otherwise returns 1 (true).
Deliverable(s)On page 1 include your name and lab along with the answers to questions in the first two blocks. On page 2, turn in a print-out of your Arduino test script containing the 3 macros and 3 subroutines. All macros should be tested in a new Arduino script and compile without any errors. The prelab should represent your own work – DO NOT COPY. |