-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_version.cpp
More file actions
150 lines (122 loc) · 3.07 KB
/
Copy pathmerge_version.cpp
File metadata and controls
150 lines (122 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <SPI.h>
#include "mcp_can.h"
#define SPI_CS_PIN 9
MCP_CAN CAN(SPI_CS_PIN); // CAN 객체 생성
#define PID_ENGIN_PRM 0x0C
#define PID_VEHICLE_SPEED 0x0D
#define PID_ACCEL_PEDAL_POS 0x49
#define CAN_ID_PID 0x7DF
void set_mask_filt()
{
CAN.init_Mask(0, 0, 0x7FC);
CAN.init_Mask(1, 0, 0x7FC);
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)
{
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
if(buf[1] == 0x41 && buf[2] == PID_VEHICLE_SPEED)
{
*s = buf[3];
return true;
}
}
}
return false;
}
bool getRPM(int *rpm)
{
sendPid(PID_ENGIN_PRM);
unsigned long __timeout = millis();
while(millis() - __timeout < 1000)
{
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
if(buf[1] == 0x41 && buf[2] == PID_ENGIN_PRM)
{
int A = buf[3];
int B = buf[4];
*rpm = ((A * 256) + B) / 4;
return true;
}
}
}
return false;
}
bool getAccelPedalPos(float *pos)
{
sendPid(PID_ACCEL_PEDAL_POS);
unsigned long __timeout = millis();
while(millis() - __timeout < 1000)
{
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
if (buf[1] == 0x41 && buf[2] == PID_ACCEL_PEDAL_POS)
{
int A = buf[3];
*pos = (A * 100.0) / 255.0;
return true;
}
}
}
return false;
}
const int pinPwrCtrl = 12;
void setup() {
Serial.begin(115200);
while(!Serial);
pinMode(pinPwrCtrl, OUTPUT);
digitalWrite(pinPwrCtrl, HIGH);
while (CAN_OK != CAN.begin(CAN_500KBPS)) {
Serial.println("CAN init fail, retry...");
delay(100);
}
Serial.println("CAN init ok!");
set_mask_filt();
}
void loop() {
int __speed = 0;
int __rpm = 0;
float __pedal = 0.0;
if(getSpeed(&__speed))
{
Serial.print("Vehicle Speed: ");
Serial.print(__speed);
Serial.println(" km/h");
}
if(getRPM(&__rpm))
{
Serial.print("Engine RPM: ");
Serial.println(__rpm);
}
if(getAccelPedalPos(&__pedal))
{
Serial.print("Accelerator Pedal Position: ");
Serial.print(__pedal, 1);
Serial.println(" %");
}
delay(500);
}