Monitoring Engine RPM Using Raspberry Pi CAN Blocks

This example shows how to use Simulink® Support Package for Raspberry Pi™ Hardware to monitor vehicle engine RPM and read the data on a web browser.

Introduction

The model in this example uses CAN Transmit, CAN Receive, Scope, and Websocket Publish blocks. The Read Engine RPM subsystem in the model extracts and validates engine RPM details from the data received.

Prerequisites

Required Hardware

To run this example, you must have the following hardware:

  • Supported Raspberry Pi board

  • Connecting wires

  • OBD (on-board diagnostics) connector

  • MCP2515 based CAN shield

Configure CAN hardware

1. Connect the CAN shield to the Raspberry Pi board.

2. Connect the OBD connector from the vehicle to the MCP2515 based CAN shield. For more information on MCP2515, see MCP2515.

Engine RPM Monitoring Model

In this example, request is first sent from the CAN Transmit block to the vehicle for engine RPM details. The vehicle responds and sends the RPM details to the CAN Receive block. The model settings are preconfigured in this example. For information on setting or modifying the CAN properties, see Model Configuration Simulink Support Package for Raspberry Pi Hardware.

open_system('raspberrypi_CAN_engine_RPM');

The model contains a Constant block, which has a preconfigured Constant value of [2 1 12 0 0 0 0 0]. This value is required to send a request to receive engine RPM details.

The constant value vector [2 1 12 0 0 0 0 0] represents a byte. Each element in the vector is a bit that is interpreted as described in the table.

Set IP Address for Raspberry Pi Hardware

1. In your Simulink model, click the Modeling tab and then select Model Settings to open Configuration Parameters dialog box.

2. Select the Hardware Implementation pane and select your Raspberry Pi hardware from the Hardware board parameter list. Do not change any other settings. The parameter values under the Hardware board settings are automatically populated to their default values.

3. Select the Target hardware resources pane and enter the IP address for the Raspberry Pi hardware in the Device Address field.

4. Click OK.

Send Request for Engine RPM Using CAN Transmit Block

Double-click the CAN Transmit block to open the block mask and set the CAN Transmit block parameters as shown. This sends a request to the engine to send the RPM details. The request is sent to the vehicle on the CAN bus at message ID 7DFh, using 8 data bytes.

  • Data is input as : Data input type used for transmitting messages. You can change this value, if required.

  • Identifier Type: Message identifier type. Use Standard (11-bitidentifier) type.

  • Message ID: Value of message identifier. Specify the value as hex2dec('7df').

  • Message Length: Message length, in bytes. Specify the value as 8.

Receive Engine RPM Information in CAN Receive Block

The vehicle responds to the request on the CAN bus at message ID 7E8h. The CAN Receive block receives the engine RPM details.

The hex2dec('7df') message received and its usage is as shown in the table.

The Scope block displays the data received in the target hardware. The Read Engine RPM subsystem extracts and validates engine RPM details from the data received.

Deploy Simulink Model

In your Simulink model, click the Hardware tab and then click Build, Deploy & Start to deploy the model or click Monitor & Tune to deploy and run the model in external mode.

Publish Engine RPM Details on a Web Page

The Websocket Publish block is used to publish the engine RPM details on a webpage. The engine RPM details can be viewed on the webpage in a computer or a mobile device. To publish the engine RPM details on a webpage, set the block parameters as required. The IP address should be the same as the IP address of the Raspberry Pi hardware specified in the Configuration Parameters dialog. For more information, see the Publish data from Raspberry Pi Sense HAT to a Websocket Server in Publish data from Raspberry Pi Sense HAT to a WebSocket Server.

Other Things to Try