Hello Teams!

Hope you are all busy working on your robots for another exciting BEST season!

A few teams have contacted me regarding designing Simulink models to control their servos using the VEX gamepad buttons. So, below is a blog article on this topic for all. 

The servos provided in the BEST competition can be incrementally rotated in the range of -60 to 60 degrees, and this can be done using the buttons on the VEX controller. 

To do this, you can use MATLAB code inside your Simulink model to design a counter that can increment (-60 to 60) or decrement (60 to -60) rotate angle values based on whether a buttons is pressed or not. Below image shows the possible options for achieving this behavior:

Option 1: 
In the first example below, the following MATLAB code was used to take the button as input value (in) and check if its pressed (1) or not (0). If pressed, then increment angle value by a fixed step of 2 until it hits the max possible value, i.e. 127 (this is equal to 60 degrees). mrtVal variable contains the latest motor incremented Val and then it is assigned to 'out' variable at instant of time. So as long as the button is pressed, the incrementation will happen until is maxes out to 127. When the button is not pressed, it stops incrementation and retains the last motor value. To change the increment step, you must change this number 2 on line 12 to any number that you desire. Bigger this number, bigger the incrementation and faster the rotation.

NOTE: See how mrtVal variable is defined as a persistent variable here. This is because MATLAB needs to remember mrtVal for every time this MATLAB function is run (it is run continuously like an endless while loop on the robot). It needs to remember because your robot needs to know if the mtrVal reached a max or not and thus decide if it can increment it further by 2.  Also, note lines 6-8, where we initialize the mrtVal variable as 0 the very first time this MATLAB function is executed. isempty() checks if mrtVal is an empty variable, i.e. it is not defined yet. If yes, then initialize it to 0 else don't do anything. 


Option 2:
Here, the only difference to Option 1 is that you can set the incrementation step size external to the MATLAB function, i.e. in Simulink using a Constant block. See how the MATLAB function block has an extra input port that lets it send in a Constant value as the 'step' input. See below, how the MATLAB code looks for this option:


Option 3:
In this final case, you can use 2 button blocks to increment or decrement the rotation angle values correspondingly. If button 5 up is pressed, then the angles increase towards 60 and if button 5 down is pressed, then angles decrease. The angle change rate depends on the 'step' variable. The MATLAB functions for this option are the same as option 2. Note an 'Add' block is used to combine the input from the 2 functions to determine the input servo value. AN assumption made here is that both buttons are not pressed simultaneously, which otherwise might give you some undesired behavior.  

The Simulink model used in this example is attached here (Zip-file) for your reference.

Hope this helps!


Quote 0 0