lego_rip_prog_simulink
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lego_rip_prog_simulink [2016/04/17 23:59] – joaomatos | lego_rip_prog_simulink [2016/04/18 22:08] (current) – joaomatos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | **Getting the angles and calculating the velocities from the encoders: ** | + | **Following this tutorial you will be able to make the system balance like this:** |
+ | |||
+ | {{youtube> | ||
+ | ---- | ||
+ | |||
+ | **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. | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | |||
+ | * Following the tutorial , you will need to update the firmware of your Brick , just click next and it will auto update.However , many times an error can occur when updating for the first time. If your update fail and your brick don't want to turn on again and keep " | ||
+ | |||
+ | |||
+ | {{:: | ||
+ | |||
+ | 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: ** | ||
* **Motor Encoder Logic:** We can get the angle from the encoder using the “Encoder” block, the encoder block returns an integer value in degrees.However for our calculations is better to use a variable with better precision, so we use a “Data Type Conversion” block to convert the angle from integer to double precision. After , we need to convert this angle in degrees to radians , so a “Gain” block is used for this task. Using a “Discrete State – Space” block we can derive the velocity , using the angle as input. | * **Motor Encoder Logic:** We can get the angle from the encoder using the “Encoder” block, the encoder block returns an integer value in degrees.However for our calculations is better to use a variable with better precision, so we use a “Data Type Conversion” block to convert the angle from integer to double precision. After , we need to convert this angle in degrees to radians , so a “Gain” block is used for this task. Using a “Discrete State – Space” block we can derive the velocity , using the angle as input. | ||
- | **1)** Drag the following blocks to the work area: 1x Encoder / 1x Data Type Conversion / 1x Gain / 1x Discrete State Space / 2x Output (Out1). | + | **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: | + | **2) Double click on the following blocks to modify its parameters: |
* **Encoder** : Change the port to port A, reset mode as no reset, and sample time to -1. | * **Encoder** : Change the port to port A, reset mode as no reset, and sample time to -1. | ||
* **Data Type Conversion** : Output type to double , Integer rounding mode to Simplest. | * **Data Type Conversion** : Output type to double , Integer rounding mode to Simplest. | ||
Line 11: | Line 34: | ||
* **Discrete State Space**: Change the A,B,C,D matrix to the name of your A,B,C,D matrix that represents your discrete state space model stored on the workspace. Use the Sample time as you want ,can be 0.010. ( Be sure to use the same Sample time on the pendulum encoder and on the model configuration – will be explained later) | * **Discrete State Space**: Change the A,B,C,D matrix to the name of your A,B,C,D matrix that represents your discrete state space model stored on the workspace. Use the Sample time as you want ,can be 0.010. ( Be sure to use the same Sample time on the pendulum encoder and on the model configuration – will be explained later) | ||
* **Output:** Change the name for Motor Angle and Motor Velocity. | * **Output:** Change the name for Motor Angle and Motor Velocity. | ||
+ | |||
+ | **3)Connections: | ||
+ | {{:: | ||
+ | |||
+ | |||
+ | * **Pendulum Encoder Logic: | ||
+ | |||
+ | **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: | ||
+ | |||
+ | * **Encoder**: | ||
+ | * **Data Type Conversion**: | ||
+ | * **Gain**: | ||
+ | * **Discrete State Space**: | ||
+ | * **Output**: | ||
+ | * **Constants**: | ||
+ | * **Switch**: Change the criteria to u2 >= Threshold and Threshold to 0. | ||
+ | * **SUM: | ||
+ | |||
+ | **3) Connections: | ||
+ | {{:: | ||
+ | |||
+ | **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 ** | ||
+ | |||
+ | * **Input Calculation Logic:**Now that we have our “[x]” matrix with the states of our system , we can calculate the motor input as [K]*[u] , where [K] is our optimal gain matrix from LQR (Can be calculated using Matlab). We just need to do a matrix multiplication and its done. | ||
+ | |||
+ | **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: | ||
+ | * **Mux**: | ||
+ | * **Gain**: Change the gain value to the name of your Optimal Gain matrix stored on your workspace, and the multiplication to matrix. | ||
+ | * **Output**: Change the name to power input. | ||
+ | |||
+ | **3) Connections: | ||
+ | {{:: | ||
+ | |||
+ | **4)Organizing: | ||
+ | {{:: | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | **Part III) Sending the input to the motor ** | ||
+ | |||
+ | * **Motor Input Logic:**Now that we have our input calculated, we just need to send it to the motor. As the Lego motors only takes -100 to +100 values, we can use a saturation block to limit the input. For the convenience, | ||
+ | |||
+ | **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: | ||
+ | * **Constant**: | ||
+ | * **Motor**: | ||
+ | * **Switch: | ||
+ | * **Saturation**: | ||
+ | * **LCD: | ||
+ | |||
+ | **3) Connections: | ||
+ | |||
+ | **4) Organizing: | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | **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. | ||
+ | |||
+ | {{:: |
lego_rip_prog_simulink.1460962775.txt.gz · Last modified: 2016/04/17 23:59 by joaomatos