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 LiDAR TFMini distance sensor and Arduino. TFMini Plus is also supported by CASP and can be used in similar manner.

Pre-Requisites

  • Arduino Leonardo 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/07_lidar_tfmini’. These are also available for download at this link.

Description

TFMini 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 send to the serial port. User can see the measured data 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.

By default TFMini gives output in terms of centimeters. To change this and to configure other parameters, perform the following steps on TFMini block:

  • From TFMni block parameters, select Mode as ‘Configuration Mode’. Also, select Detection Mode as 0-5Mtr (15X) from TFMini Configuration Group.
  • With these parameters program the target. After the board is programmed wait for 10secs.
  • Now, from the TFMni block parameters, select Mode as ‘Normal Mode’ and program the target. Now, distance should be measured in millimeter.

CASP Model

Procedure

  • Run CASP and open the project ‘CASP/support/examples/hardware_interface/07_lidar_tfmini/07_lidar_tfmini.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()

Related Links