CAN通信のシミュレーション用の模擬ECUを作成

CAN送信モジュールを作成し、Arduino同士の通信が可能になったので、今回はArduino Megaに、実際の車両ECUのデータを模したものを送信させるようにプログラムの改修を行った。これにより、クルマのECUの代わりにArduino MegaにCAN送信させることによって、クルマのECUの動作を模擬させ、ガジェットの開発をシミュレーションで行うことができるようになった。

目次

Arduino同士のCAN通信

Arduino Mega+CAN-BUS Shieldを用いてCAN送信し、Arduino Nanoで受信してPCに表示する環境を構築した。

あわせて読みたい
Arduino NanoとArduino Mega間でCAN通信する Arduino Megaに実装したCAN送信プログラムをArduino Nanoに実装したCAN受信プログラムが受信して、PC上のシリアルモニタに表示するテストを行った。ArduinoでCAN通信ガ...

こちらを用いて、今回はArduino Mega側の送信CANを、車両が送信する実際のCAN信号を模擬してArduino Nanoに送信。Arduino Nano側では受信したCAN信号を解析してエンジン回転数として表示するプログラムに変更した。

Arduino Mega側のプログラム(エンジンECUを模擬して回転数をCAN送信)

ベースとなるプログラムはこちら

あわせて読みたい
ArduinoでCANデータを送信する CANデータを読み取ることができる受信モジュールについてはすでに作成したものの、今後のCANを使った車載ガジェットの開発のために、CANを送信できるプログラムを作成し...

この送信部分の信号を生成する。送信する信号は、エンジン回転数。

こちらのサイトにS660のCANデータ解析結果があるので、こちらを参考にして、ID 0x1DCの1,2Byte目に流れるCANデータをArduino Megaで再現して出力することにする。256で割った商を1Byte目に、256で割った余りを2Byte目に格納すればOK。

ちなみに、アルトワークスの場合は、CAN ID0x124に、出力したい回転数を4倍した後、256で割った商を1Byte目に、256で割った余りを2Byte目に格納すればOK。

出力データの作成

Excelで以下のようなデータを作成した。20ms毎に0x1DCのByte1,2に回転数データとして出力する。

作成はExcelで行った。作成したExcelはこちらからダウンロード可能。

これをArduinoのプログラムに組み込む。CAN送信は割り込み関数で行っているため、送信毎にいちいち関数計算をさせると20msの定期送信に支障が出るおそれがあるので、今回はすべての送信予定データを定義した。このデータを配列としてプログラムする(使用メモリ量が増大するので、Arduino Megaじゃないと厳しいかも)。

送信プログラム

以上をArduinoのプログラムとして作成した(殆どがデータ配列…)。

/*
 * File:   CAN-SEND_ArduinoMEGA_CANBUSshield_RPM.ino.c
 * Author: 岡本一車
 * https://kurumashikou.com/
*/
#include <SPI.h>
#include <SD.h>
#include "string.h"
#include "mcp2515_can.h"
#include "MsTimer2.h"

// ピンの設定
// CAN通信用のCSピン(digital 9)
const int SPI_CAN_CS_PIN = 9;
// SDカード通信用のCSピン(digital 4)
const int SPI_SD_CS_PIN = 4;
// SS_defaultPin Arduino unoは10, megaは53
const int PSI_SS_defaultPin = 53;
// 動作確認用のLED
const int LED_PIN = 13;
// CAN送信周期の周期(msec)
#define CAN_SEND_INTERVAL 20
#define CAN_SEND_ID 0x1DC

// グローバル変数 ///////////////////////////////////////////
// タイムスタンプ用変数
unsigned long timestamp = 0;
unsigned long timestamp_old = 0;
// 割り込み関数の定義
void timer_interrapt(void);

// 送信メッセージ格納用
unsigned char sendMsg[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// 送信するデータ配列
unsigned int msg_index = 0;
unsigned char SimData[][8] = {
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,50,0,0,0,0,0},
	{0,3,67,0,0,0,0,0},
	{0,3,84,0,0,0,0,0},
	{0,3,101,0,0,0,0,0},
	{0,3,119,0,0,0,0,0},
	{0,3,136,0,0,0,0,0},
	{0,3,153,0,0,0,0,0},
	{0,3,170,0,0,0,0,0},
	{0,3,188,0,0,0,0,0},
	{0,3,205,0,0,0,0,0},
	{0,3,222,0,0,0,0,0},
	{0,3,239,0,0,0,0,0},
	{0,4,1,0,0,0,0,0},
	{0,4,18,0,0,0,0,0},
	{0,4,35,0,0,0,0,0},
	{0,4,52,0,0,0,0,0},
	{0,4,70,0,0,0,0,0},
	{0,4,87,0,0,0,0,0},
	{0,4,104,0,0,0,0,0},
	{0,4,121,0,0,0,0,0},
	{0,4,139,0,0,0,0,0},
	{0,4,156,0,0,0,0,0},
	{0,4,173,0,0,0,0,0},
	{0,4,190,0,0,0,0,0},
	{0,4,208,0,0,0,0,0},
	{0,4,225,0,0,0,0,0},
	{0,4,242,0,0,0,0,0},
	{0,5,3,0,0,0,0,0},
	{0,5,21,0,0,0,0,0},
	{0,5,38,0,0,0,0,0},
	{0,5,55,0,0,0,0,0},
	{0,5,72,0,0,0,0,0},
	{0,5,90,0,0,0,0,0},
	{0,5,107,0,0,0,0,0},
	{0,5,124,0,0,0,0,0},
	{0,5,141,0,0,0,0,0},
	{0,5,159,0,0,0,0,0},
	{0,5,176,0,0,0,0,0},
	{0,5,193,0,0,0,0,0},
	{0,5,210,0,0,0,0,0},
	{0,5,228,0,0,0,0,0},
	{0,5,245,0,0,0,0,0},
	{0,6,6,0,0,0,0,0},
	{0,6,23,0,0,0,0,0},
	{0,6,41,0,0,0,0,0},
	{0,6,58,0,0,0,0,0},
	{0,6,75,0,0,0,0,0},
	{0,6,92,0,0,0,0,0},
	{0,6,110,0,0,0,0,0},
	{0,6,127,0,0,0,0,0},
	{0,6,144,0,0,0,0,0},
	{0,6,161,0,0,0,0,0},
	{0,6,179,0,0,0,0,0},
	{0,6,196,0,0,0,0,0},
	{0,6,213,0,0,0,0,0},
	{0,6,230,0,0,0,0,0},
	{0,6,248,0,0,0,0,0},
	{0,7,9,0,0,0,0,0},
	{0,7,26,0,0,0,0,0},
	{0,7,43,0,0,0,0,0},
	{0,7,61,0,0,0,0,0},
	{0,7,78,0,0,0,0,0},
	{0,7,95,0,0,0,0,0},
	{0,7,112,0,0,0,0,0},
	{0,7,130,0,0,0,0,0},
	{0,7,147,0,0,0,0,0},
	{0,7,164,0,0,0,0,0},
	{0,7,181,0,0,0,0,0},
	{0,7,199,0,0,0,0,0},
	{0,7,216,0,0,0,0,0},
	{0,7,233,0,0,0,0,0},
	{0,7,250,0,0,0,0,0},
	{0,8,12,0,0,0,0,0},
	{0,8,29,0,0,0,0,0},
	{0,8,46,0,0,0,0,0},
	{0,8,63,0,0,0,0,0},
	{0,8,81,0,0,0,0,0},
	{0,8,98,0,0,0,0,0},
	{0,8,115,0,0,0,0,0},
	{0,8,132,0,0,0,0,0},
	{0,8,150,0,0,0,0,0},
	{0,8,167,0,0,0,0,0},
	{0,8,184,0,0,0,0,0},
	{0,8,201,0,0,0,0,0},
	{0,8,219,0,0,0,0,0},
	{0,8,236,0,0,0,0,0},
	{0,8,253,0,0,0,0,0},
	{0,9,14,0,0,0,0,0},
	{0,9,32,0,0,0,0,0},
	{0,9,49,0,0,0,0,0},
	{0,9,66,0,0,0,0,0},
	{0,9,83,0,0,0,0,0},
	{0,9,101,0,0,0,0,0},
	{0,9,118,0,0,0,0,0},
	{0,9,135,0,0,0,0,0},
	{0,9,152,0,0,0,0,0},
	{0,9,170,0,0,0,0,0},
	{0,9,187,0,0,0,0,0},
	{0,9,204,0,0,0,0,0},
	{0,9,221,0,0,0,0,0},
	{0,9,239,0,0,0,0,0},
	{0,10,0,0,0,0,0,0},
	{0,10,17,0,0,0,0,0},
	{0,10,34,0,0,0,0,0},
	{0,10,52,0,0,0,0,0},
	{0,10,69,0,0,0,0,0},
	{0,10,86,0,0,0,0,0},
	{0,10,103,0,0,0,0,0},
	{0,10,121,0,0,0,0,0},
	{0,10,138,0,0,0,0,0},
	{0,10,155,0,0,0,0,0},
	{0,10,172,0,0,0,0,0},
	{0,10,190,0,0,0,0,0},
	{0,10,207,0,0,0,0,0},
	{0,10,224,0,0,0,0,0},
	{0,10,241,0,0,0,0,0},
	{0,11,3,0,0,0,0,0},
	{0,11,20,0,0,0,0,0},
	{0,11,37,0,0,0,0,0},
	{0,11,54,0,0,0,0,0},
	{0,11,72,0,0,0,0,0},
	{0,11,89,0,0,0,0,0},
	{0,11,106,0,0,0,0,0},
	{0,11,123,0,0,0,0,0},
	{0,11,141,0,0,0,0,0},
	{0,11,158,0,0,0,0,0},
	{0,11,175,0,0,0,0,0},
	{0,11,192,0,0,0,0,0},
	{0,11,210,0,0,0,0,0},
	{0,11,227,0,0,0,0,0},
	{0,11,244,0,0,0,0,0},
	{0,12,5,0,0,0,0,0},
	{0,12,23,0,0,0,0,0},
	{0,12,40,0,0,0,0,0},
	{0,12,57,0,0,0,0,0},
	{0,12,74,0,0,0,0,0},
	{0,12,92,0,0,0,0,0},
	{0,12,109,0,0,0,0,0},
	{0,12,126,0,0,0,0,0},
	{0,12,143,0,0,0,0,0},
	{0,12,161,0,0,0,0,0},
	{0,12,178,0,0,0,0,0},
	{0,12,195,0,0,0,0,0},
	{0,12,212,0,0,0,0,0},
	{0,12,230,0,0,0,0,0},
	{0,12,247,0,0,0,0,0},
	{0,13,8,0,0,0,0,0},
	{0,13,25,0,0,0,0,0},
	{0,13,43,0,0,0,0,0},
	{0,13,60,0,0,0,0,0},
	{0,13,77,0,0,0,0,0},
	{0,13,94,0,0,0,0,0},
	{0,13,112,0,0,0,0,0},
	{0,13,129,0,0,0,0,0},
	{0,13,146,0,0,0,0,0},
	{0,13,163,0,0,0,0,0},
	{0,13,181,0,0,0,0,0},
	{0,13,198,0,0,0,0,0},
	{0,13,215,0,0,0,0,0},
	{0,13,232,0,0,0,0,0},
	{0,13,250,0,0,0,0,0},
	{0,14,11,0,0,0,0,0},
	{0,14,28,0,0,0,0,0},
	{0,14,45,0,0,0,0,0},
	{0,14,63,0,0,0,0,0},
	{0,14,80,0,0,0,0,0},
	{0,14,97,0,0,0,0,0},
	{0,14,114,0,0,0,0,0},
	{0,14,132,0,0,0,0,0},
	{0,14,149,0,0,0,0,0},
	{0,14,166,0,0,0,0,0},
	{0,14,183,0,0,0,0,0},
	{0,14,201,0,0,0,0,0},
	{0,14,218,0,0,0,0,0},
	{0,14,235,0,0,0,0,0},
	{0,14,252,0,0,0,0,0},
	{0,15,14,0,0,0,0,0},
	{0,15,31,0,0,0,0,0},
	{0,15,48,0,0,0,0,0},
	{0,15,65,0,0,0,0,0},
	{0,15,83,0,0,0,0,0},
	{0,15,100,0,0,0,0,0},
	{0,15,117,0,0,0,0,0},
	{0,15,134,0,0,0,0,0},
	{0,15,152,0,0,0,0,0},
	{0,15,169,0,0,0,0,0},
	{0,15,186,0,0,0,0,0},
	{0,15,203,0,0,0,0,0},
	{0,15,221,0,0,0,0,0},
	{0,15,238,0,0,0,0,0},
	{0,15,255,0,0,0,0,0},
	{0,16,16,0,0,0,0,0},
	{0,16,34,0,0,0,0,0},
	{0,16,51,0,0,0,0,0},
	{0,16,68,0,0,0,0,0},
	{0,16,85,0,0,0,0,0},
	{0,16,103,0,0,0,0,0},
	{0,16,120,0,0,0,0,0},
	{0,16,137,0,0,0,0,0},
	{0,16,154,0,0,0,0,0},
	{0,16,172,0,0,0,0,0},
	{0,16,189,0,0,0,0,0},
	{0,16,206,0,0,0,0,0},
	{0,16,223,0,0,0,0,0},
	{0,16,241,0,0,0,0,0},
	{0,17,2,0,0,0,0,0},
	{0,17,19,0,0,0,0,0},
	{0,17,36,0,0,0,0,0},
	{0,17,54,0,0,0,0,0},
	{0,17,71,0,0,0,0,0},
	{0,17,88,0,0,0,0,0},
	{0,17,105,0,0,0,0,0},
	{0,17,123,0,0,0,0,0},
	{0,17,140,0,0,0,0,0},
	{0,17,157,0,0,0,0,0},
	{0,17,174,0,0,0,0,0},
	{0,17,192,0,0,0,0,0},
	{0,17,209,0,0,0,0,0},
	{0,17,226,0,0,0,0,0},
	{0,17,243,0,0,0,0,0},
	{0,18,5,0,0,0,0,0},
	{0,18,22,0,0,0,0,0},
	{0,18,39,0,0,0,0,0},
	{0,18,56,0,0,0,0,0},
	{0,18,74,0,0,0,0,0},
	{0,18,91,0,0,0,0,0},
	{0,18,108,0,0,0,0,0},
	{0,18,125,0,0,0,0,0},
	{0,18,143,0,0,0,0,0},
	{0,18,160,0,0,0,0,0},
	{0,18,177,0,0,0,0,0},
	{0,18,194,0,0,0,0,0},
	{0,18,212,0,0,0,0,0},
	{0,18,229,0,0,0,0,0},
	{0,18,246,0,0,0,0,0},
	{0,19,7,0,0,0,0,0},
	{0,19,25,0,0,0,0,0},
	{0,19,42,0,0,0,0,0},
	{0,19,59,0,0,0,0,0},
	{0,19,76,0,0,0,0,0},
	{0,19,94,0,0,0,0,0},
	{0,19,111,0,0,0,0,0},
	{0,19,128,0,0,0,0,0},
	{0,19,145,0,0,0,0,0},
	{0,19,163,0,0,0,0,0},
	{0,19,180,0,0,0,0,0},
	{0,19,197,0,0,0,0,0},
	{0,19,214,0,0,0,0,0},
	{0,19,232,0,0,0,0,0},
	{0,19,249,0,0,0,0,0},
	{0,20,10,0,0,0,0,0},
	{0,20,27,0,0,0,0,0},
	{0,20,45,0,0,0,0,0},
	{0,20,62,0,0,0,0,0},
	{0,20,79,0,0,0,0,0},
	{0,20,96,0,0,0,0,0},
	{0,20,114,0,0,0,0,0},
	{0,20,131,0,0,0,0,0},
	{0,20,148,0,0,0,0,0},
	{0,20,165,0,0,0,0,0},
	{0,20,183,0,0,0,0,0},
	{0,20,200,0,0,0,0,0},
	{0,20,217,0,0,0,0,0},
	{0,20,234,0,0,0,0,0},
	{0,20,252,0,0,0,0,0},
	{0,21,13,0,0,0,0,0},
	{0,21,30,0,0,0,0,0},
	{0,21,47,0,0,0,0,0},
	{0,21,65,0,0,0,0,0},
	{0,21,82,0,0,0,0,0},
	{0,21,99,0,0,0,0,0},
	{0,21,116,0,0,0,0,0},
	{0,21,134,0,0,0,0,0},
	{0,21,151,0,0,0,0,0},
	{0,21,168,0,0,0,0,0},
	{0,21,185,0,0,0,0,0},
	{0,21,203,0,0,0,0,0},
	{0,21,220,0,0,0,0,0},
	{0,21,237,0,0,0,0,0},
	{0,21,254,0,0,0,0,0},
	{0,22,16,0,0,0,0,0},
	{0,22,33,0,0,0,0,0},
	{0,22,50,0,0,0,0,0},
	{0,22,67,0,0,0,0,0},
	{0,22,85,0,0,0,0,0},
	{0,22,102,0,0,0,0,0},
	{0,22,119,0,0,0,0,0},
	{0,22,136,0,0,0,0,0},
	{0,22,154,0,0,0,0,0},
	{0,22,171,0,0,0,0,0},
	{0,22,188,0,0,0,0,0},
	{0,22,205,0,0,0,0,0},
	{0,22,223,0,0,0,0,0},
	{0,22,240,0,0,0,0,0},
	{0,23,1,0,0,0,0,0},
	{0,23,18,0,0,0,0,0},
	{0,23,36,0,0,0,0,0},
	{0,23,53,0,0,0,0,0},
	{0,23,70,0,0,0,0,0},
	{0,23,87,0,0,0,0,0},
	{0,23,105,0,0,0,0,0},
	{0,23,122,0,0,0,0,0},
	{0,23,139,0,0,0,0,0},
	{0,23,156,0,0,0,0,0},
	{0,23,174,0,0,0,0,0},
	{0,23,191,0,0,0,0,0},
	{0,23,208,0,0,0,0,0},
	{0,23,225,0,0,0,0,0},
	{0,23,243,0,0,0,0,0},
	{0,24,4,0,0,0,0,0},
	{0,24,21,0,0,0,0,0},
	{0,24,38,0,0,0,0,0},
	{0,24,56,0,0,0,0,0},
	{0,24,73,0,0,0,0,0},
	{0,24,90,0,0,0,0,0},
	{0,24,107,0,0,0,0,0},
	{0,24,125,0,0,0,0,0},
	{0,24,142,0,0,0,0,0},
	{0,24,159,0,0,0,0,0},
	{0,24,176,0,0,0,0,0},
	{0,24,194,0,0,0,0,0},
	{0,24,211,0,0,0,0,0},
	{0,24,228,0,0,0,0,0},
	{0,24,245,0,0,0,0,0},
	{0,25,7,0,0,0,0,0},
	{0,25,24,0,0,0,0,0},
	{0,25,41,0,0,0,0,0},
	{0,25,58,0,0,0,0,0},
	{0,25,76,0,0,0,0,0},
	{0,25,93,0,0,0,0,0},
	{0,25,110,0,0,0,0,0},
	{0,25,127,0,0,0,0,0},
	{0,25,145,0,0,0,0,0},
	{0,25,162,0,0,0,0,0},
	{0,25,179,0,0,0,0,0},
	{0,25,196,0,0,0,0,0},
	{0,25,214,0,0,0,0,0},
	{0,25,231,0,0,0,0,0},
	{0,25,248,0,0,0,0,0},
	{0,26,9,0,0,0,0,0},
	{0,26,27,0,0,0,0,0},
	{0,26,44,0,0,0,0,0},
	{0,26,61,0,0,0,0,0},
	{0,26,78,0,0,0,0,0},
	{0,26,96,0,0,0,0,0},
	{0,26,113,0,0,0,0,0},
	{0,26,130,0,0,0,0,0},
	{0,26,147,0,0,0,0,0},
	{0,26,165,0,0,0,0,0},
	{0,26,182,0,0,0,0,0},
	{0,26,199,0,0,0,0,0},
	{0,26,216,0,0,0,0,0},
	{0,26,234,0,0,0,0,0},
	{0,26,251,0,0,0,0,0},
	{0,27,12,0,0,0,0,0},
	{0,27,29,0,0,0,0,0},
	{0,27,47,0,0,0,0,0},
	{0,27,64,0,0,0,0,0},
	{0,27,81,0,0,0,0,0},
	{0,27,98,0,0,0,0,0},
	{0,27,116,0,0,0,0,0},
	{0,27,133,0,0,0,0,0},
	{0,27,150,0,0,0,0,0},
	{0,27,167,0,0,0,0,0},
	{0,27,185,0,0,0,0,0},
	{0,27,202,0,0,0,0,0},
	{0,27,219,0,0,0,0,0},
	{0,27,236,0,0,0,0,0},
	{0,27,254,0,0,0,0,0},
	{0,28,15,0,0,0,0,0},
	{0,28,32,0,0,0,0,0},
	{0,28,49,0,0,0,0,0},
	{0,28,67,0,0,0,0,0},
	{0,28,84,0,0,0,0,0},
	{0,28,101,0,0,0,0,0},
	{0,28,118,0,0,0,0,0},
	{0,28,136,0,0,0,0,0},
	{0,28,153,0,0,0,0,0},
	{0,28,170,0,0,0,0,0},
	{0,28,187,0,0,0,0,0},
	{0,28,205,0,0,0,0,0},
	{0,28,222,0,0,0,0,0},
	{0,28,239,0,0,0,0,0},
	{0,29,0,0,0,0,0,0},
	{0,29,18,0,0,0,0,0},
	{0,29,35,0,0,0,0,0},
	{0,29,52,0,0,0,0,0},
	{0,29,69,0,0,0,0,0},
	{0,29,87,0,0,0,0,0},
	{0,29,104,0,0,0,0,0},
	{0,29,121,0,0,0,0,0},
	{0,29,138,0,0,0,0,0},
	{0,29,156,0,0,0,0,0},
	{0,29,173,0,0,0,0,0},
	{0,29,190,0,0,0,0,0},
	{0,29,207,0,0,0,0,0},
	{0,29,225,0,0,0,0,0},
	{0,29,242,0,0,0,0,0},
	{0,30,3,0,0,0,0,0},
	{0,30,20,0,0,0,0,0},
	{0,29,242,0,0,0,0,0},
	{0,29,207,0,0,0,0,0},
	{0,29,173,0,0,0,0,0},
	{0,29,138,0,0,0,0,0},
	{0,29,104,0,0,0,0,0},
	{0,29,69,0,0,0,0,0},
	{0,29,35,0,0,0,0,0},
	{0,29,0,0,0,0,0,0},
	{0,28,222,0,0,0,0,0},
	{0,28,187,0,0,0,0,0},
	{0,28,153,0,0,0,0,0},
	{0,28,118,0,0,0,0,0},
	{0,28,84,0,0,0,0,0},
	{0,28,49,0,0,0,0,0},
	{0,28,15,0,0,0,0,0},
	{0,27,236,0,0,0,0,0},
	{0,27,202,0,0,0,0,0},
	{0,27,167,0,0,0,0,0},
	{0,27,133,0,0,0,0,0},
	{0,27,98,0,0,0,0,0},
	{0,27,64,0,0,0,0,0},
	{0,27,29,0,0,0,0,0},
	{0,26,251,0,0,0,0,0},
	{0,26,216,0,0,0,0,0},
	{0,26,182,0,0,0,0,0},
	{0,26,147,0,0,0,0,0},
	{0,26,113,0,0,0,0,0},
	{0,26,78,0,0,0,0,0},
	{0,26,44,0,0,0,0,0},
	{0,26,9,0,0,0,0,0},
	{0,25,231,0,0,0,0,0},
	{0,25,196,0,0,0,0,0},
	{0,25,162,0,0,0,0,0},
	{0,25,127,0,0,0,0,0},
	{0,25,93,0,0,0,0,0},
	{0,25,58,0,0,0,0,0},
	{0,25,24,0,0,0,0,0},
	{0,24,245,0,0,0,0,0},
	{0,24,211,0,0,0,0,0},
	{0,24,176,0,0,0,0,0},
	{0,24,142,0,0,0,0,0},
	{0,24,107,0,0,0,0,0},
	{0,24,73,0,0,0,0,0},
	{0,24,38,0,0,0,0,0},
	{0,24,4,0,0,0,0,0},
	{0,23,225,0,0,0,0,0},
	{0,23,191,0,0,0,0,0},
	{0,23,156,0,0,0,0,0},
	{0,23,122,0,0,0,0,0},
	{0,23,87,0,0,0,0,0},
	{0,23,53,0,0,0,0,0},
	{0,23,18,0,0,0,0,0},
	{0,22,240,0,0,0,0,0},
	{0,22,205,0,0,0,0,0},
	{0,22,171,0,0,0,0,0},
	{0,22,136,0,0,0,0,0},
	{0,22,102,0,0,0,0,0},
	{0,22,67,0,0,0,0,0},
	{0,22,33,0,0,0,0,0},
	{0,21,254,0,0,0,0,0},
	{0,21,220,0,0,0,0,0},
	{0,21,185,0,0,0,0,0},
	{0,21,151,0,0,0,0,0},
	{0,21,116,0,0,0,0,0},
	{0,21,82,0,0,0,0,0},
	{0,21,47,0,0,0,0,0},
	{0,21,13,0,0,0,0,0},
	{0,20,234,0,0,0,0,0},
	{0,20,200,0,0,0,0,0},
	{0,20,165,0,0,0,0,0},
	{0,20,131,0,0,0,0,0},
	{0,20,96,0,0,0,0,0},
	{0,20,62,0,0,0,0,0},
	{0,20,27,0,0,0,0,0},
	{0,19,249,0,0,0,0,0},
	{0,19,214,0,0,0,0,0},
	{0,19,180,0,0,0,0,0},
	{0,19,145,0,0,0,0,0},
	{0,19,111,0,0,0,0,0},
	{0,19,76,0,0,0,0,0},
	{0,19,42,0,0,0,0,0},
	{0,19,7,0,0,0,0,0},
	{0,18,229,0,0,0,0,0},
	{0,18,194,0,0,0,0,0},
	{0,18,160,0,0,0,0,0},
	{0,18,125,0,0,0,0,0},
	{0,18,91,0,0,0,0,0},
	{0,18,56,0,0,0,0,0},
	{0,18,22,0,0,0,0,0},
	{0,17,243,0,0,0,0,0},
	{0,17,209,0,0,0,0,0},
	{0,17,174,0,0,0,0,0},
	{0,17,140,0,0,0,0,0},
	{0,17,105,0,0,0,0,0},
	{0,17,71,0,0,0,0,0},
	{0,17,36,0,0,0,0,0},
	{0,17,2,0,0,0,0,0},
	{0,16,223,0,0,0,0,0},
	{0,16,189,0,0,0,0,0},
	{0,16,154,0,0,0,0,0},
	{0,16,120,0,0,0,0,0},
	{0,16,85,0,0,0,0,0},
	{0,16,51,0,0,0,0,0},
	{0,16,16,0,0,0,0,0},
	{0,15,238,0,0,0,0,0},
	{0,15,203,0,0,0,0,0},
	{0,15,169,0,0,0,0,0},
	{0,15,134,0,0,0,0,0},
	{0,15,100,0,0,0,0,0},
	{0,15,65,0,0,0,0,0},
	{0,15,31,0,0,0,0,0},
	{0,14,252,0,0,0,0,0},
	{0,14,218,0,0,0,0,0},
	{0,14,183,0,0,0,0,0},
	{0,14,149,0,0,0,0,0},
	{0,14,114,0,0,0,0,0},
	{0,14,80,0,0,0,0,0},
	{0,14,45,0,0,0,0,0},
	{0,14,11,0,0,0,0,0},
	{0,13,232,0,0,0,0,0},
	{0,13,198,0,0,0,0,0},
	{0,13,163,0,0,0,0,0},
	{0,13,129,0,0,0,0,0},
	{0,13,94,0,0,0,0,0},
	{0,13,60,0,0,0,0,0},
	{0,13,25,0,0,0,0,0},
	{0,12,247,0,0,0,0,0},
	{0,12,212,0,0,0,0,0},
	{0,12,178,0,0,0,0,0},
	{0,12,143,0,0,0,0,0},
	{0,12,109,0,0,0,0,0},
	{0,12,74,0,0,0,0,0},
	{0,12,40,0,0,0,0,0},
	{0,12,5,0,0,0,0,0},
	{0,11,227,0,0,0,0,0},
	{0,11,192,0,0,0,0,0},
	{0,11,158,0,0,0,0,0},
	{0,11,123,0,0,0,0,0},
	{0,11,89,0,0,0,0,0},
	{0,11,54,0,0,0,0,0},
	{0,11,20,0,0,0,0,0},
	{0,10,241,0,0,0,0,0},
	{0,10,207,0,0,0,0,0},
	{0,10,172,0,0,0,0,0},
	{0,10,138,0,0,0,0,0},
	{0,10,103,0,0,0,0,0},
	{0,10,69,0,0,0,0,0},
	{0,10,34,0,0,0,0,0},
	{0,10,0,0,0,0,0,0},
	{0,9,221,0,0,0,0,0},
	{0,9,187,0,0,0,0,0},
	{0,9,152,0,0,0,0,0},
	{0,9,118,0,0,0,0,0},
	{0,9,83,0,0,0,0,0},
	{0,9,49,0,0,0,0,0},
	{0,9,14,0,0,0,0,0},
	{0,8,236,0,0,0,0,0},
	{0,8,201,0,0,0,0,0},
	{0,8,167,0,0,0,0,0},
	{0,8,132,0,0,0,0,0},
	{0,8,98,0,0,0,0,0},
	{0,8,63,0,0,0,0,0},
	{0,8,29,0,0,0,0,0},
	{0,7,250,0,0,0,0,0},
	{0,7,216,0,0,0,0,0},
	{0,7,181,0,0,0,0,0},
	{0,7,147,0,0,0,0,0},
	{0,7,112,0,0,0,0,0},
	{0,7,78,0,0,0,0,0},
	{0,7,43,0,0,0,0,0},
	{0,7,9,0,0,0,0,0},
	{0,6,230,0,0,0,0,0},
	{0,6,196,0,0,0,0,0},
	{0,6,161,0,0,0,0,0},
	{0,6,127,0,0,0,0,0},
	{0,6,92,0,0,0,0,0},
	{0,6,58,0,0,0,0,0},
	{0,6,23,0,0,0,0,0},
	{0,5,245,0,0,0,0,0},
	{0,5,210,0,0,0,0,0},
	{0,5,176,0,0,0,0,0},
	{0,5,141,0,0,0,0,0},
	{0,5,107,0,0,0,0,0},
	{0,5,72,0,0,0,0,0},
	{0,5,38,0,0,0,0,0},
	{0,5,3,0,0,0,0,0},
	{0,4,225,0,0,0,0,0},
	{0,4,190,0,0,0,0,0},
	{0,4,156,0,0,0,0,0},
	{0,4,121,0,0,0,0,0},
	{0,4,87,0,0,0,0,0},
	{0,4,52,0,0,0,0,0},
	{0,4,18,0,0,0,0,0},
	{0,3,239,0,0,0,0,0},
	{0,3,205,0,0,0,0,0},
	{0,3,170,0,0,0,0,0},
	{0,3,136,0,0,0,0,0},
	{0,3,101,0,0,0,0,0},
	{0,3,67,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0},
	{0,3,32,0,0,0,0,0}
};

// CANトランシーバにCSピン設定をセットし、CAN通信インスタンスを生成
mcp2515_can CAN(SPI_CAN_CS_PIN);


void setup() {
    // シリアルモニタの設定
    SERIAL_PORT_MONITOR.begin(115200);
    // シリアルポートの準備が整うまで待つ
    while(!Serial);

    // デバッグ用
    pinMode(LED_PIN, OUTPUT);
    
    // タイマー割り込みの設定
    MsTimer2::set(CAN_SEND_INTERVAL, timer_interrapt);
    
    // CANの初期設定 ///////////////////////////////////////////////
    if (CAN.begin(CAN_500KBPS) != CAN_OK) {
      SERIAL_PORT_MONITOR.println("CAN initialized error ..............");
      while(1);
    }else{
      SERIAL_PORT_MONITOR.println("CAN initialized OK !!");
    }

    // 時間計測タイマ開始
    MsTimer2::start();
}

void loop() {
    //割り込み時のみ制御されるので、ここは処理なし
}

// タイマ割り込み
void timer_interrapt(void){
  // 送信するデータを選択する  
  for (int i=0; i<sizeof(sendMsg); i++ ){
    sendMsg[i] = SimData[msg_index][i];
  }  
  
  // https://seeeddoc.github.io/CAN-BUS_Shield/
  // CAN.sendMsgBuf(INT8U id, INT8U ext, INT8U len, data_buf);
  // is a function to send data onto the bus. In which:
  // "id" represents where the data come from.
  // "ext" represents the status of the frame. '0' means standard frame. '1' means extended frame.
  // "len" represents the length of this frame.
  // "data_buf" is the content of this message.
  // CANメッセージを送信
  CAN.sendMsgBuf(CAN_SEND_ID, 0, 0, 8, sendMsg);

  // 送信するデータ配列のインデックスをインクリメント
  msg_index++;
  // 送信するデータ配列の最後まで読み込んだら、インデックスをリセット
  if(msg_index >= sizeof(SimData)/sizeof(SimData[0])){
      msg_index=0;
  }
  // デバッグ用(送信時、LED点灯)
  digitalWrite(LED_PIN, HIGH);
}

これで、送信側のプログラムは完成。

Arduino受信側プログラム(CANでエンジン回転数を受信して表示)

ベースとなるプログラムはこちら

あわせて読みたい
ArduinoNanoとMCP2515でCANデータを受信する 以前、Arduino MEGAとCAN-BUS Shieldを用いて車両のCAN情報を取得する事に成功した。→こちら https://kurumashikou.com/gets660can_via_arduino-and-canbus-shield/ Ard...

受信する側のプログラムはシンプルで、受信したデータの1,2Byte目を受信して、rpmに直して表示するだけ。こちらをArduni Nano側に書き込み。

Arduino
¥4,670 (2024/06/20 13:58:27時点 Amazon調べ-詳細)
/*
 * File: CAN-RECEIVE_ArduinoNano_MCP2515_rpm.ino
 * Arduino NANO と MCP2515,TJA1050を用いたCAN受信
 * 必要なライブラリ: MCP_CAN Library ⇒ https://github.com/coryjfowler/MCP_CAN_lib
 * Author: 岡本一車
 * https://kurumashikou.com/
*/
#include <SPI.h>
#include <mcp_can.h>
#include "string.h"

// ピンの設定
// ここではCAN通信用のCSピンとして、digital 9を使用する
const int SPI_CAN_CS_PIN = 9;

// グローバル変数 ///////////////////////////////////////////
// タイムスタンプ用変数
unsigned long timestamp = 0;
unsigned long timestamp_old = 0;
// 受信メッセージ格納用
unsigned long can_ID = 0;
unsigned char len = 0;
unsigned char buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// 回転数格納用変数
unsigned int rpm = 0;

// CANデータ書き込み値を保持する
String logDataStr; 

// CANトランシーバにCSピン設定をセットし、CAN通信インスタンスを生成
MCP_CAN CAN(SPI_CAN_CS_PIN);


// 初期設定 ////////////////////////////////////////////////
void setup() {

  // シリアルモニタの設定
  SERIAL_PORT_MONITOR.begin(115200);
  // シリアルポートの準備が整うまで待つ
  while(!Serial);
  
  // CANの初期設定 ///////////////////////////////////////////////
  if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) != CAN_OK) {
    SERIAL_PORT_MONITOR.println("CAN-BUS initiliased error ..............");
    while(1);
  }else{
    CAN.setMode(MCP_NORMAL);
    SERIAL_PORT_MONITOR.println("CAN-BUS initiliased OK !!");
  }
}


// メイン処理 ////////////////////////////////////////////////
void loop() {

  // CANメッセージ受信したときの動作
  if (CAN_MSGAVAIL == CAN.checkReceive()) {

    // バッファの初期化
    for(int i=0; i<8; i++){
      buf[i] = 0;
    }
    
    // 受信したデータをシリアルモニタに送信 ////////////////////////////////////
    if(timestamp_old == 0){
      timestamp_old = millis();
    }
    // CANデータを取得
    //CAN.readMsgBufID(&can_ID, &len, buf);
    CAN.readMsgBuf(&can_ID, &len, buf);

    // タイムスタンプの取得
    timestamp = millis() - timestamp_old;

    rpm = buf[1]*256 + buf[2];
    
    // デバッグ用出力
    logDataStr  = "";
    logDataStr += String(rpm);
    logDataStr += String(" rpm :  ");
    logDataStr += "timestamp ";
    logDataStr += String(timestamp);
    logDataStr += String(" / ID ");
    logDataStr += String(can_ID);
    logDataStr += String(" / ");
    logDataStr += String(len);
    logDataStr += String(" / ");
    logDataStr += String(buf[0]);
    logDataStr += String(" ");
    logDataStr += String(buf[1]);
    logDataStr += String(" ");
    logDataStr += String(buf[2]);
    logDataStr += String(" ");
    logDataStr += String(buf[3]);
    logDataStr += String(" ");
    logDataStr += String(buf[4]);
    logDataStr += String(" ");
    logDataStr += String(buf[5]);
    logDataStr += String(" ");
    logDataStr += String(buf[6]);
    logDataStr += String(" ");
    logDataStr += String(buf[7]);
    logDataStr += "\r\n";
    
    SERIAL_PORT_MONITOR.print(logDataStr);  
  }
}

実行結果

こんな感じで、送信したCANデータを正しく受信し、解釈できていることが確認できた。

まとめ

CAN送信モジュールを作成し、Arduino MegaとArduni Nanoの通信が可能になったので、今回はArduino Megaに、実際の車両ECUのデータを模したものを送信させ、Arduni Nanoで表示するプログラムを作成した。

Arduino
¥4,670 (2024/06/20 13:58:27時点 Amazon調べ-詳細)

これにより、クルマのECUの代わりにArduino MegaにCAN送信させることによって、クルマのECUの動作を模擬させ、ガジェットの開発をシミュレーションで行うことができるようになった。次はガジェットの開発。続きはこちら

あわせて読みたい
ArduinoとCAN通信で作るレブインジケータ S660やアルトワークスでサーキットを走行すると、ついついアツくなってしまい、レブリミッターを作動させてしまう事があった。レブリミッターはその通り、上限回転数に...

関連記事

あわせて読みたい
ArduinoでCANデータを送信する CANデータを読み取ることができる受信モジュールについてはすでに作成したものの、今後のCANを使った車載ガジェットの開発のために、CANを送信できるプログラムを作成し...
あわせて読みたい
ArduinoNanoとMCP2515でCANデータを受信する 以前、Arduino MEGAとCAN-BUS Shieldを用いて車両のCAN情報を取得する事に成功した。→こちら https://kurumashikou.com/gets660can_via_arduino-and-canbus-shield/ Ard...
あわせて読みたい
Arduino NanoとArduino Mega間でCAN通信する Arduino Megaに実装したCAN送信プログラムをArduino Nanoに実装したCAN受信プログラムが受信して、PC上のシリアルモニタに表示するテストを行った。ArduinoでCAN通信ガ...
あわせて読みたい
ArduinoとCAN通信で作るレブインジケータ S660やアルトワークスでサーキットを走行すると、ついついアツくなってしまい、レブリミッターを作動させてしまう事があった。レブリミッターはその通り、上限回転数に...
あわせて読みたい
ArduinoとCAN通信で制御する振動アクセルペダル(試作) S660やアルトワークスでサーキットを走行すると、ついついアツくなってしまい、レブリミッターを作動させてしまう事があった。対策として、CANから車両の回転数を取得し...
この記事をSNSでシェアする
  • URLをコピーしました!

コメント

コメントする

目次