Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pressure first commit #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions 20thNSE_Pressure
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <SPI.h>
#include <string.h>
#include <CCP_MCP2515.h>
#include <Adafruit_LPS35HW.h>

Adafruit_LPS35HW lps35hw = Adafruit_LPS35HW();

#define CAN_INT D6
#define CAN_CS D7

// CAN
CCP_MCP2515 CCP(CAN_CS, CAN_INT);
char msgString[128];
char str_buf[7]; // 6+\0

void setup() {
delay(500);
Serial.begin(115200);
pinMode(CAN_CS, OUTPUT);
pinMode(CAN_INT, INPUT);
digitalWrite(CAN_CS, HIGH);

// CAN
CCP.begin();

while (!lps35hw.begin_I2C(0x5C)) {
//if (!lps35hw.begin_SPI(LPS_CS)) {
//if (!lps35hw.begin_SPI(LPS_CS, LPS_SCK, LPS_MISO, LPS_MOSI)) {
Serial.println("Couldn't find LPS35HW chip");
delay(10);
}
delay(10);
lps35hw.setDataRate(LPS35HW_RATE_75_HZ);
}

void loop() {
static uint32_t time = 0;
if (millis() - time >= 10) {
time = millis();
CCP.float_to_device(CCP_surface_pressure1_pressure_pa, lps35hw.readPressure());
}

if (!digitalRead(CAN_INT)) // データ受信確認
{
CCP.read_device();
if (CCP.id < 0x40) {
CCP.string(str_buf, 7);
sprintf(msgString, "%d,ID,%03x,time,%d000,string,%s,,,,", millis(), CCP.id, CCP.time16(), str_buf);
} else if (CCP.id < 0x80) {
sprintf(msgString, "%d,ID,%03x,time,%lu,uint32,%lu,,,,", millis(), CCP.id, CCP.time32(), CCP.data_uint32());
} else if (CCP.id < 0xC0) {
sprintf(msgString, "%d,ID,%03x,time,%lu,float,%8.2f,,,,", millis(), CCP.id, CCP.time32(), CCP.data_float());
} else {
sprintf(msgString, "%d,ID,%03x,time,%d000,fp16_0,%8.2f,fp16_1,%8.2f,fp16_2,%8.2f", millis(), CCP.id, CCP.time16(), CCP.data_fp16_0(), CCP.data_fp16_1(), CCP.data_fp16_2());
}
Serial.println(msgString);
}
}
41 changes: 41 additions & 0 deletions LPS33HW.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <SPI.h> // SPI通信を使用するためのlibraryをinclude
#include <string.h> // 文字列操作用のlibraryをinclude
#include <CCP_MCP2515.h> // CAN通信libraryをinclude
#include <Adafruit_LPS35HW.h> // LPS35HWPressure-sensor-libraryをinclude

Adafruit_LPS35HW lps35hw = Adafruit_LPS35HW(); // LPS35HWsensorのインスタンスを作成

#define CAN_INT D6 // CAN通信の割り込みピンをD6に設定
#define CAN_CS D7 // CAN通信のチップセレクトピンをD7に設定

// CAN通信のinstanceを作成
CCP_MCP2515 CCP(CAN_CS, CAN_INT);
// char msgString[128]; // output-message用の文字列バッファ
// char str_buf[7]; // 文字列データのbuffer(最大6文字+終端文字)

void setup() {
delay(500); // 起動後に500msの遅延を挿入
Serial.begin(115200); // serial通信を115200baudで初期化
pinMode(CAN_CS, OUTPUT); // CAN_CS pinをoutput-modeに設定
pinMode(CAN_INT, INPUT); // CAN_INT pinをinput-modeに設定
digitalWrite(CAN_CS, HIGH); // CAN_C pinをHIGHに設定

// CAN通信の初期化
CCP.begin();

// LPS35HWの初期化
while (!lps35hw.begin_I2C(0x5C)) { // I2C address 0x5Cでsensorを初期化
Serial.println("Couldn't find LPS35HW chip"); // sensorが見つからない場合、error messageを表示
delay(10); // 10msの遅延
}
delay(10); // さらに10msの遅延
lps35hw.setDataRate(LPS35HW_RATE_75_HZ); // sensorのdatalateを75Hzに設定
}

void loop() {
static uint32_t time = 0; // 前回のtimestampを保存するための静的変数
if (millis() - time >= 10) { // 10msごとに処理を実行
time = millis(); // 現在のtime stampを保存
CCP.float_to_device(CCP_surface_pressure1_pressure_pa, lps35hw.readPressure()); // 圧力dataをCAN deviceに送信
}
}
1 change: 0 additions & 1 deletion README.md

This file was deleted.