Following this tutorial you will be able to make the system balance like this:


Hardware And Software requirements:

You will need a NXT Brick and Matlab with Simulink version 2015 or higher. 

Follow the step-by-step tutorial to install this ADD-ON on your Matlab provided by MathWorks.

Download and Tutorial Link

Simulink Files

To run the model , first run the script on matlab and then deploy the model to the hardware on the Simulink.


Part I) Getting the angles and calculating the velocities from the encoders:

1) Drag the following blocks to the work area: 1x Encoder / 1x Data Type Conversion / 1x Gain / 1x Discrete State Space / 2x Output (Out1).

2) Double click on the following blocks to modify its parameters:

3)Connections: Connect the blocks as shown in the picture. After connecting, select all the blocks and create a subsystem.

1) Drag the following blocks to the work area:1x Encoder / 1x Data Type Conversion / 1x Gain / 2x Constant /2x Sum/ 1x Switch / 1x Discrete State Space / 2x Output (Out1).

2)Double click on the following blocks to modify its parameters:

3) Connections:Connect the blocks as shown in the picture. After connecting, select all the blocks and create a subsystem.

End of Part I): For better understanding ,organize your subsystem as shown in the picture.Pay attention to the output order (OUTPUT NUMBER) , you should follow the same order as shown in the picture (to create the right [u] matrix).


Part II) Calculating the input to be sent to the motor

1)Drag the following blocks to the work area: 1x Mux / 1x Gain / 1x Output(out1).

2)Double click on the following blocks to modify its parameters:

3) Connections:Connect the blocks as shown in the picture. After connecting, select all the blocks and create a subsystem.

4)Organizing: Change the name of your subsystem and the name of input and output to representative names , as shown in the picture:


Part III) Sending the input to the motor

1) Drag the following blocks to the work area: 1x Switch / 2x Clock / 1x Constant / 1x Saturation /1x Motor/1x LCD

2)Double click on the following blocks to modify its parameters:

3) Connections:Connect the blocks as shown in the picture. No need to create a subsystem.

4) Organizing:You can add a Clock block and connect to a LCD block to see on the screen the running time.


Part IV) Configurations and running the model

1) Click on the Simulation tab on the top of the window and then click on Model Configuration Parameters. Change the Solver tab and the Hardware implementation tab as shown in the pictures. The step size can be typed on the tab or can be a variable stored on your Matlab workspace (I'm using 0.010s).

2)Running the Model: The run the model , click on the Deploy to Hardware buttom , on the right top corner . If you did everything OK , the system will load a little bit and then your brick will start running the program.