Assignment #6 – Event Driven Modeling Our current models work well when we use sensors to determine specific responses from the robot. For example, when the value returned from the Ultrasonic Sonic Sensor is less than a threshold value, we’ve commanded the robot to stop. When the value is greater than the threshold the robot moves forward according to the other programming rules we’ve devised. Let’s now consider using the touch sensor as a switch to start or stop the robot. We want the robot to start executing the program model when we push it, and to stop when we push it again. The program logic we’ve developed so far would allow us to stop the robot while the sensor is pushed… and to allow it to run when the button isn’t pushed. This isn’t what we want - we haven’t developed logic yet to change the robot behavior each time the button is pushed. We’ll start with a simple start/stop model… then add the logic to more complicated models later. Create the model in Figure 1, and add the code shown on the next page (Figure 2) to the function block (remember, you need to add the code before you can ‘hook up’ the model.) Figure 1 Figure 2 In order to understand how this code works we first need to think about what is happening in the robot as the code runs. The EV3 calls the function over and over. In effect it is running a loop, similar to the MATLAB code you’ve used in chapter 9 of MATLAB for Engineers. Each time the on_off function is called it checks for an input value for touch_sensor and for setspeed. The setspeed is a constant, but the touch_sensor variable has a value of either a 1 (if the physical touch sensor is in the pushed in position) or a zero (if it is in the out position). Skip the first few lines of code for the moment, and look at line 7. This if statement compares the touch_sensor value to 0. If it is greater than 0 (the only other possibility is 1) the value of state is incremented by 1, otherwise nothing happens and the value of state stays the same. Thus, if the physical touch sensor is pushed in when the program checks, the value of state increases. Every time the physical touch sensor is pushed the value of state goes up by 1. On line eleven the rem function is used to check if the state variable is odd or even. The rem function returns the remainder in a division problem (Chapter 3, MATLAB for Engineers), so rem(state,2) is the remainder of state/2. There are only two possible answers, 0 and 1. If the remainder is 0, the value of state is even. If the remainder is one, the value of state is odd. Thus, the if statement from line 11 to line 15 tells the EV3 to set speed equal to setspeed if the value of state is odd, and to set it to 0 if the value of state is even. Now let’s go back to lines 3 to 6. The variables in functions are ‘local’… meaning that they only exist inside the function… and only for the current time you are using the function. That means that every time you call a function you start from scratch… it doesn’t remember anything from previous times it was used. That doesn’t work well for us in this case, because we are trying to increment state.. One way around this is to define state as a persistent variable… which will be remembered, and can therefore be updated with our code logic. But… the first time the on_off function is called state doesn’t have a value yet… it just exists as an empty array. The isempty function checks to see if a variable is an empty array, and returns a value of 1 if it is… and 0 if there are values in the array. Recalling that 1 means ‘true’ in MATLAB and 0 means ‘false’, the first time this function is called the if statement will be true and state will be assigned a value of 0. In subsequent times when the on_off function is called by the EV3, the if statement will be false, and line 5 will be skipped. Save your model as Your_Name_Assignment_6_1.slx, and run it on the EV3. There are other strategies for solving this logic problem. For example function speed = on_off(touch_sensor, setspeed) %#codegen persistent state if isempty(state) state = 0; end if touch_sensor>0 & state==0 state = 1; elseif touch_sensor>0 & state == 1 state = 0; else state = state; end if state>0 speed = setspeed; else speed = 0; end You can probably think of other options yourself. Either use the alternate code or come up with another strategy yourself to implement the on_off function. Save your model as Your_Name_Assignment_6_2.slx. Now try a more complicated model. Save your current model as Assignment_7_3 to use as a starting point. Modify the function to result in 3 possibilities triggered by the touch sensor. (Each time you push the sensor the robot behavior should change.) stop move forward move backward Save your model as Your_Name_Assignment_6_3.slx.
© Copyright 2026 Paperzz