quarc_i2c_gyroscope_demo.mdl
Open this model

QUARC I2C Gyroscope Demo

This is an example of using an I2C device with QUARC that requires a combined write-read bus transaction. It interfaces with a three-axis gyroscope sensor. Since the gyroscope requires combined write and read operations in a single I2C bus transaction, this example makes use of the Stream Write-Read block.

QUARC I2C Gyroscope Demo Simulink diagram

System Requirements

This demonstration is run using the QUARC Linux DuoVero target. To use QUARC for Linux DuoVero, a Quanser challenge employing this target, such as the QBall-2 or QBot-2, is required, as well as a software license for the Linux DuoVero target.

For a detailed discussion on the QUARC Linux DuoVero target and requirements needed to run models on this target, please refer to the QUARC Linux DuoVero Target reference page.

This example also requires an L3G4200D or L3GD20 three-axis digital output gyroscope sensor from STMicroelectronics Inc., wired to I2C port 0 of the target. A convenient peripheral module called the PmodGYRO is provided by Digilient Inc. that uses the L3G4200D digital output gyroscope. This example was tested using this peripheral module.

The VCC pin of the PmodGYRO J2 connector should be wired to +3.3V and the GND pin should be wired to ground. The SCL/SPC pin should be wired to the serial clock (SCL) of the I2C port and the SDA/SDI/SDO pin should be wired to the serial data line (SDA) of the I2C port. Note that the base slave address of the L3G4200D gyroscope on the PmodGYRO is 0x69 or 0b1101001. See the PmodGYRO Reference Manual and PmodGYRO Schematic for details.

Configuring the Demonstration

Open the QUARC Preferences dialog by selecting Preferences from the QUARC menu of one of the Simulink diagrams.

Select linux_duovero as the Target type on the Model pane. Then change the IP address in the Default model URI parameter to match your Linux DuoVero. For example, if your target has an IP address of 172.16.0.155, then the Default model URI should be tcpip://172.16.0.155:17001?nagle=no . A hostname may be used instead of the IP address if the target is configured with a hostname. The concept of URI's and their use in communications is discussed in Communicating with the Target . In this example, communications between Simulink and the model will occur using TCP/IP. This step configures the default communications scheme used with the Linux DuoVero target and does not have to be repeated unless the address of the target changes.

QUARC Preferences Dialog

The slave address of the URI used by the Stream Call block must match the slave address configured for the gyroscope on its SA0 pin. If this pins is tied high, which is the default for the PmodGYRO, then the address should be 0x69 or 0b1101001. Otherwise the slave address will need to be changed to 0x68 or 0b1101000.

To change the URI of the Stream Call block, double-click on the block to open its configuration dialog, as shown below.

Stream Call parameters

In the URI of host to which to connect field, replace the value of the address option in the URI with the appropriate slave address.

Click OK to close the configuration dialog.

Running the Model Using the QUARC Target for Linux DuoVero

Please note that in order to be able to run this part of the demo, you must have purchased the QUARC Target for Linux ARM as part of your license. In addition you must have satisfied the software and hardware requirements found in the QUARC Installation Guide. This target is used, for example, with the Quanser QBall-2 and QBot-2.

To open the model click on the "Open this model" button found on the top right corner of this page.

Select External from the Simulation/Mode menu of the diagram, or select External from the simulation mode combo box on the toolbar.

Select Build from the QUARC menu of the diagram, or press Ctrl+B while the diagram is the active window. A great deal of output will appear in the Diagnostic Viewer about the progress of the build. If you cannot see the Diagnostic Viewer, you can open it by selecting View/Diagnostic Viewer from the menu of the diagram, or clicking on the View Diagnostics hyperlink at the bottom of the diagram. If you have MATLAB R2013b or earlier then the output will appear in the MATLAB Command Window.

If the build is successful, then the line:

*** Created executable quarc_i2c_gyroscope_demo.rt-linux_duovero

will appear in the Command Window. Finally, QUARC will download the model to the QUARC Target for Linux DuoVero. The line:

### Model quarc_i2c_gyroscope_demo has been downloaded to target 'tcpip://192.168.0.155:7000' (130876 bytes)

will appear in the Command Window indicating that the code was successfully downloaded to the QUARC Target. The IP address will match the IP address of your Linux DuoVero target. If any errors occur, appropriate error messages will appear in the Command Window.

Double click on the Rates (deg/s) block to open the Scope.

Click on the Connect to Target button or select Connect to Target from the Simulation menu of the diagram to connect to the model.

Start the model by clicking on the Run button or selecting Run from the Simulation menu of the diagram. The Start item of the QUARC menu may also be used to both connect and start the model in one operation.

Rotate the gyroscope about its Cartesian axes. The angular velocities in degrees per second will appear in the Scope as shown below. Notice that the Scope trace is moving in actual time. In other words, the trace passes the 10 second mark after 10 seconds have passed since in external mode, QUARC runs the model in real-time.

Scope with angular velocities

Click on the Stop button or select Stop from the Simulation menu of the diagram to stop the model. The Stop item of the QUARC menu may also be used.

Running the example on a different target

To run the example on a different target, refer to the instructions on the Running QUARC Examples on Remote Targets page.