CASP models can interface with hardware input outputs on embedded micro-controller boards. This example, demonstrates a model that measures accelerometer and gyroscope values from Invensense MPU-6050 & MPU-9250 IMUs from Arduino Uno.

Pre-Requisites

  • Arduino Uno R3 board.
  • Invensense MPU-6050 or MPU-9250 IMUs.
  • CASP software is installed along with CASP Python Libraries.
  • Arduino BSP is installed.
  • The project files are located at CASP installed directory ‘CASP/support/examples/hardware_interface/05_imu_invensense’. These are also available for download at this link.

Description

IMU MPU-6050 is connected to the target board as shown in the circuit diagram. A model is developed to communicate with the IMU and read the accelerometer and gyroscope values along x, y, z axes. The measured values are then send to the serial port for remote access from the host PC. A logic is also developed such that if the IMU is kept vertical (i.e. Z value of the accelerometer is less than 100) the on-board LED will be continuously ON.

User can see the measured data (6 integer values) from the ‘Configure Simulation Hardware’ interface window by clicking on Simulation->Configure Simulation IO menu item from the CASP main tool bar. A Python script is also provided that plots the data from IMU. Please go through the individual block parameters used in the model along with respective documentation for better understanding.

CASP Model

Procedure

  • Run CASP and open the project ‘CASP/support/examples/hardware_interface/05_imu_invensense/05_imu_invensense.prj’ from CASP installed directory.
  • Open ‘Setup Simulation’ window and change the ‘Target Programmer Port’ to which the board is connected.
  • Click Run button to build the model and program the board.

Python Script

The Python script that plots the data from the Arduino board is shown below. The script file is available in the project directory. Within the script, change the serial port in the method ‘obj.InitSerialPort()’ to which the board is connected and run the script from Python terminal.

import casp
import casp_targets
import casp_defs
from models import casp_plot_n_control

#Initialize CASP
casp.Init()

# Create and open default project.
casp.ProjectOpenDefault()

# Instantiate class object
obj = casp_plot_n_control.Model(6,6)

# Initiate serial port. Change serial port with actual port to which the board is connected
obj.InitSerialPort("COM35") #serial_port_name, master
#obj.InitEthernet(False, False, "0.0.0.0", "0.0.0.0", 52001, "127.0.0.1", 42001) #server, tcp, gateway_ip, local_ip, local_port, remote_ip, remote_port

# Add plot
obj.AddPlotter(False, 0, 6, "imu_data") #data_type_real, data_index, data_size, title

# Finalize plotter
obj.Finalize()

# Set simulation to real time (by setting simulation speed factor to 1).
casp.SimSetupSpeedFactor(1)

# Build and program the board.
casp.SimStart()

# Open project folder. Go through the log files (log_build_wnd.txt and log_msg_wnd.txt) in the project folder for any error/warning messages.
#casp.FolderProject()

# Promptly close CASP model editor program. simulation panel program will still be running.
casp.Close()

Related Links