CASP models can interface with hardware input outputs on embedded micro-controller boards. This example, demonstrates a model that measures distance of an object with ultrasonic distance sensor and Arduino Uno.
Pre-Requisites
- Arduino Uno R3 board.
- Ultrasonic Distance sensor module
- 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/06_ultrasonic_distance’. These are also available for download at this link.
Description
An ultrasonic distance sensor module HC-SR04 is connected to the target board as shown in the circuit diagram. A model is developed to measure the distance of the target object from the sensor and vary the brightness of the on-board LED such that as the object comes closer to the sensor LED brightness increases.
The measured data is also sent through the serial port. User can see the measured 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 the board. 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/06_ultrasonic_distance/06_ultrasonic_distance.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(4,4)
# 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, 1, "dist0") #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()
