-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
278 additions
and
196 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/************************************************************************************************* | ||
OBD-II_PIDs TEST CODE | ||
Loovee, Longan Labs 2022 | ||
Query | ||
send id: 0x7df | ||
dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 | ||
Response | ||
From id: 0x7E9 or 0x7EA or 0x7EB | ||
dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) | ||
https://en.wikipedia.org/wiki/OBD-II_PIDs | ||
***************************************************************************************************/ | ||
|
||
#include <SPI.h> | ||
#include "mcp_can.h" | ||
|
||
/* Please modify SPI_CS_PIN to adapt to different baords. | ||
CANBed V1 - 17 | ||
CANBed M0 - 3 | ||
CAN Bus Shield - 9 | ||
CANBed 2040 - 9 | ||
CANBed Dual - 9 | ||
OBD-2G Dev Kit - 9 | ||
Hud Dev Kit - 9 | ||
*/ | ||
|
||
#define SPI_CS_PIN 9 | ||
|
||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin | ||
|
||
#define PID_ENGIN_PRM 0x0C | ||
#define PID_VEHICLE_SPEED 0x0D | ||
#define PID_COOLANT_TEMP 0x05 | ||
|
||
#define CAN_ID_PID 0x7DF | ||
|
||
void set_mask_filt() | ||
{ | ||
// set mask, set both the mask to 0x3ff | ||
|
||
CAN.init_Mask(0, 0, 0x7FC); | ||
CAN.init_Mask(1, 0, 0x7FC); | ||
|
||
// set filter, we can receive id from 0x04 ~ 0x09 | ||
|
||
CAN.init_Filt(0, 0, 0x7E8); | ||
CAN.init_Filt(1, 0, 0x7E8); | ||
|
||
CAN.init_Filt(2, 0, 0x7E8); | ||
CAN.init_Filt(3, 0, 0x7E8); | ||
CAN.init_Filt(4, 0, 0x7E8); | ||
CAN.init_Filt(5, 0, 0x7E8); | ||
} | ||
|
||
void sendPid(unsigned char __pid) { | ||
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; | ||
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); | ||
} | ||
|
||
bool getRPM(int *r) | ||
{ | ||
sendPid(PID_ENGIN_PRM); | ||
unsigned long __timeout = millis(); | ||
|
||
while(millis()-__timeout < 1000) // 1s time out | ||
{ | ||
unsigned char len = 0; | ||
unsigned char buf[8]; | ||
|
||
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data | ||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf | ||
|
||
if(buf[1] == 0x41) | ||
{ | ||
*r = (256*buf[3]+buf[4])/4; | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while(!Serial); | ||
|
||
// below code need for OBD-II GPS Dev Kit | ||
// pinMode(A3, OUTPUT); | ||
// digitalWrite(A3, HIGH); | ||
|
||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k | ||
Serial.println("CAN init fail, retry..."); | ||
delay(100); | ||
} | ||
Serial.println("CAN init ok!"); | ||
set_mask_filt(); | ||
} | ||
|
||
void loop() { | ||
|
||
int __rpm = 0; | ||
|
||
int ret = getRPM(&__rpm); | ||
|
||
if(ret) | ||
{ | ||
Serial.print("Engin Speed: "); | ||
Serial.print(__rpm); | ||
Serial.println(" rpm"); | ||
}else Serial.println("get Engin Speed Fail..."); | ||
|
||
delay(500); | ||
} | ||
|
||
// END FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/************************************************************************************************* | ||
OBD-II_PIDs TEST CODE | ||
Loovee, Longan Labs 2022 | ||
Query | ||
send id: 0x7df | ||
dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 | ||
Response | ||
From id: 0x7E9 or 0x7EA or 0x7EB | ||
dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) | ||
https://en.wikipedia.org/wiki/OBD-II_PIDs | ||
***************************************************************************************************/ | ||
|
||
#include <SPI.h> | ||
#include "mcp_can.h" | ||
|
||
/* Please modify SPI_CS_PIN to adapt to different baords. | ||
CANBed V1 - 17 | ||
CANBed M0 - 3 | ||
CAN Bus Shield - 9 | ||
CANBed 2040 - 9 | ||
CANBed Dual - 9 | ||
OBD-2G Dev Kit - 9 | ||
Hud Dev Kit - 9 | ||
*/ | ||
|
||
#define SPI_CS_PIN 9 | ||
|
||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin | ||
|
||
#define PID_ENGIN_PRM 0x0C | ||
#define PID_VEHICLE_SPEED 0x0D | ||
#define PID_COOLANT_TEMP 0x05 | ||
|
||
#define CAN_ID_PID 0x7DF | ||
|
||
void set_mask_filt() | ||
{ | ||
// set mask, set both the mask to 0x3ff | ||
|
||
CAN.init_Mask(0, 0, 0x7FC); | ||
CAN.init_Mask(1, 0, 0x7FC); | ||
|
||
// set filter, we can receive id from 0x04 ~ 0x09 | ||
|
||
CAN.init_Filt(0, 0, 0x7E8); | ||
CAN.init_Filt(1, 0, 0x7E8); | ||
|
||
CAN.init_Filt(2, 0, 0x7E8); | ||
CAN.init_Filt(3, 0, 0x7E8); | ||
CAN.init_Filt(4, 0, 0x7E8); | ||
CAN.init_Filt(5, 0, 0x7E8); | ||
} | ||
|
||
void sendPid(unsigned char __pid) { | ||
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; | ||
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); | ||
} | ||
|
||
bool getSpeed(int *s) | ||
{ | ||
sendPid(PID_VEHICLE_SPEED); | ||
unsigned long __timeout = millis(); | ||
|
||
while(millis()-__timeout < 1000) // 1s time out | ||
{ | ||
unsigned char len = 0; | ||
unsigned char buf[8]; | ||
|
||
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data | ||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf | ||
|
||
if(buf[1] == 0x41) | ||
{ | ||
*s = buf[3]; | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while(!Serial); | ||
|
||
// below code need for OBD-II GPS Dev Kit | ||
// pinMode(A3, OUTPUT); | ||
// digitalWrite(A3, HIGH); | ||
|
||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k | ||
Serial.println("CAN init fail, retry..."); | ||
delay(100); | ||
} | ||
Serial.println("CAN init ok!"); | ||
set_mask_filt(); | ||
} | ||
|
||
void loop() { | ||
|
||
int __speed = 0; | ||
int ret = getSpeed(&__speed); | ||
if(ret) | ||
{ | ||
Serial.print("Vehicle Speed: "); | ||
Serial.print(__speed); | ||
Serial.println(" kmh"); | ||
} | ||
delay(500); | ||
} | ||
|
||
// END FILE |
Oops, something went wrong.