Assignment #4 Selection structures We saw earlier that MATLAB uses relational operators to determine if a statement is true or false. For example 10<8 is a false statement, so MATLAB returns an answer of 0. (Try it yourself in the MATLAB command window.) Similarly, a true statement returns a value of 1. We saw that Simulink uses the same logic with its relational operators when we took advantage of the output from the relational operator block and created the collision avoidance model in last week’s assignment. (Figure 1) This output is either a 0 or a 1 Figure 1 By combining the relational operator block with the product block we created a selection structure. If the output from the Utrasonic Sensor was greater than the Threshold value, the Set Speed was sent to the motors. If it was less than the Threshold value, zero was sent to the motors. We can do something similar in MATLAB, as shown in Figure 2. This is called an if/else structure, and is covered in Chapter 8 of MATLAB for Engineers. Figure 2. The program in Figure 2 hard codes the ultrasonic_sensor_output variable, but we could modify the code to include an input statement (covered in Chapter 7 of MATLAB for Engineers) so that the user (you) can try out the program for various values of the ultrasonic_sensor_output variable. I’ve also modified the disp inputs (as shown in Figure 3) so that a message is sent to the MATLAB command window describing the results, and a comment line to describe the program. Create a script m-file in MATLAB and try out this program. Can you think of other improvements? Save the program as Your_Name_Assignment_4_1.m and submit it in CANVAS once you’ve completed the rest of the assignment tasks. Figure 3. In our Simulink model (Figure 1) all we cared about was sending either a zero or the set speed to the motors, so the simple relational operator worked well for us. However, if we want to make more complicated decisions another strategy, such as using a switch is required. Consider the model shown in Figure 4. (Create this model in Simulink so you can explore it as you work through these instructions.) The Switch block accepts 3 inputs, ordered from top to bottom as u1, u2, and u3. The center input (u2) is used along with a threshold value (set to 5 in my model) in a decision making process. If the comparison (double click on the block to see your choices) is true, u1 (the top input) is passed through the block and ultimately sent to the display block. If the comparison is false, u3 (the bottom input) is passed through the block. Figure 4 We can use this structure in our collision avoidance model. The set speed is the top constant block, labeled ‘Constant’ , the output from the ultrasonic sensor is Constant 1. We can set the bottom constant (Constant 2) to 0, which is the speed we want if an obstacle is detected. The threshold value is set in the Switch block. See Figure 5 Figure 5 Run the model and adjust the Ultrasonic Detector constant output to confirm that your logic is correct. Now that we understand how the logic works, both in MATLAB and using the Simulink Switch block, modify your model using the actual EV3 Ultrasonic Sensor block and the two motor blocks… and then run it on your robot. Submit the resulting model as Your_Name_Assignment_4_2.slx Figure 6 You may be thinking that just using the relational operator block would have been easier… and you’d be right. But the Switch block is necessary then the logic is more complicated. In the next project we will be combining the Line Following and Collision Avoidance projects we completed previously, into a single model. Start with the incomplete model shown in Figure 7, which is designed to run just on the computer, so that we can explore how it works before involving the robot. Figure 7 Connect the outputs from each of the three sections of code to the switches to support the following logic: a) If there is no obstruction in front of the robot, the robot follows a line b) If there is an obstruction in front of the robot, the robot stops. Now experiment with the slider gains and constant values to observe how the new program works. You should try to figure out how to make the connections yourself, but my solution is on the next page. Figure 8 Once you are convinced the program logic works correctly, substitute the Ultrasonic sensor for the constant and slider gain on the top section of the model Substitute the Color sensor for the constant and slider gain on the bottom section of the model Substitute the motors for the display blocks Send your model to your EV3 robot, and observe its behavior on the track (with appropriate obstacles in place). Save your model and submit it as Your_Name_Assignment_4_3.slx Figure 8 You could simplify this model by eliminating the Compare to constant block, and putting the logic into the switch. Make this change and confirm that it works with the EV3. Save your model as Your_Name_Assignment_4_4.slx.
© Copyright 2026 Paperzz