-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathECU_main.cpp
169 lines (138 loc) · 3.79 KB
/
ECU_main.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/////////////////////////////////////////////////////////////////////
// This is normal C++ code so you can do all C++ stuff like:
// - define and derive classes
// - define and overload functions
// - include headers
// - use math.h functions like sin(), sqrt(), pow(), log10() ...
// etc.
/////////////////////////////////////////////////////////////////////
#include "canllib.h"
// global variables
int counter = 0;
unsigned int currentRaw;
unsigned char byteVal;
unsigned int canId;
// timer based on seconds
Timer myTimer(onMyTimer);
// timer based on mili seconds
MsTimer myMsTimer(onMyMsTimer);
////////////////////////////////////////
// start function (optionally)
////////////////////////////////////////
void onStart()
{
print("onStart() called\n");
// start 1 second timer
setTimer(myTimer, 1);
// start 100 ms timer
setTimer(myMsTimer, 100);
}
////////////////////////////////////////
// stop function (optionally)
////////////////////////////////////////
void onStop()
{
print("onStop() called\n");
}
////////////////////////////////////////
// actions triggered by key press in
// CAN++ main window (optionally)
////////////////////////////////////////
void onKey(char key)
{
if (key == 'a')
{
message m(0xaa);
m.DLC = 8;
send(m);
}
else if (key == 'b')
{
message m(0xbb);
m.DLC = 8;
send(m);
}
}
////////////////////////////////////////
// message RX handler for message RadarControl
////////////////////////////////////////
void RadarControl::onMessage()
{
// look on CAN 1 only
if (CAN != 1)
return;
// define message BatteryUsage
BatteryUsage m;
////////////////////////////////////
// signal assignments
// note: "this->" can be omitted
////////////////////////////////////
// set signal raw value by "="
m.BU_Count = counter;
// set signal physical value
m.BU_Volatage.setPhys(20.5 / counter );
// set signal raw value from RX message by "="
m.BU_CRC = this->RC_CRC;
// set signal raw value from RX message by set()
currentRaw = this->RC_Accerlation.getRaw();
m.BU_Current.setRaw(currentRaw); // also: set()
// set message byte at index 6 from RX message
byteVal = this->getByte(5);
m.setByte(6, byteVal); // also: m.byte(6) = byte
////////////////////////////////////
// send on CAN
////////////////////////////////////
// output BatteryUsage on default channel
send(m);
// send message with CAN ID 0x123 on channel 2
message m2(0x123);
m2.CAN = 2;
m2.DLC = 8;
send(m2);
// print in can++ print window
print("Received message with Id = %x", this->ID);
// handle counter variable
counter = counter + 1;
if (counter == 16)
counter = 0;
}
/////////////////////////////////////////////////////////////////////
// signal change handler for signal BatteryUsage::BatteryUsage_Count
/////////////////////////////////////////////////////////////////////
void BatteryUsage::onMessage()
{
// @BatteryUsage::BatteryUsage_Count changed code here:
if (BU_Count.changed())
{
print("New BU_Count = %d", BU_Count.getRaw());
}
}
/////////////////////////////////////////////////////////////////////
// Following function may be defined to be called for all RX messages
// (except messages which are handeled by onMessage() like above).
// If not needed just remove it.
/////////////////////////////////////////////////////////////////////
void onAllMessages(message &m)
{
canId = m.ID;
}
/////////////////////////////////////////////////////////////////////
// timer handlers
/////////////////////////////////////////////////////////////////////
void onMyTimer()
{
print("1 sec timer");
BatteryState m;
m.BS_Count = counter;
send(m);
// restart timer for peridoc occurrence
setTimer(myTimer, 1);
}
void onMyMsTimer()
{
LockAttempts m1;
m1.LA_Count = counter;
send(m1);
// restart timer for peridoc occurrence
setTimer(myMsTimer, 100);
}