-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan.ino
201 lines (178 loc) · 4.03 KB
/
can.ino
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
int32_t can_init()
{
if(CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) != CAN_OK)
{
DEBUG.println(F("[OBC] CAN INIT FAILED"));
return -1;
}
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
pinMode(CAN_INT, INPUT);
return 0;
}
int32_t get_micl_msg(void)
{
if(!digitalRead(CAN_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&can_rx_id, &can_rx_len, can_rx_msg); // Read data: len = data length, buf = data byte(s)
switch(can_rx_id & 0x00000FFF)
{
case CAN_COM_ID:
{
process_com_micl_msg();
break;
}
default: break;
}
}
return 0;
}
int32_t process_com_hk(can_com_hk_t* msg)
{
int i;
msg->checksum = 0;
memcpy(&(msg->com_temp), can_rx_msg + 2, 2);
memcpy(&(msg->msg_num), can_rx_msg + 4, 2);
for(i = 2; i < 6; i++)
{
msg->checksum ^= can_rx_msg[i];
}
if( msg->checksum != can_rx_msg[6])
{
DEBUG.println(F("[OBC] COM HK CAN CHECKSUM ERROR"));
return -1;
}
return 0;
}
int32_t process_com_micl_msg(void)
{
switch(can_rx_msg[1])
{
case CAN_COM_HK:
{
if( process_com_hk(&can_com_hk_msg) == 0)
{
sleep_task(BUS_PROCESS_TASK);
DEBUG.println(F("[OBC] COM HK DATA RECEIVED ON CAN"));
radio_temp = can_com_hk_msg.com_temp;
radio_msg_id = can_com_hk_msg.msg_num;
DEBUG.print(F("[OBC] COM HK: TEMP: "));
DEBUG.print(radio_temp);
DEBUG.print(F(" | MSG NUM: "));
DEBUG.println(radio_msg_id);
bus_sm(BUS_ACK);
}
break;
}
default: break;
}
return 0;
}
void c_set_ns(can_ltm_t* msg)
{
if(GPS_lati[GPS_valid][0] == '+')
{
msg->latitude1 |= 0x8000;
}
else
{
msg->latitude1 &= 0x7FFF;
}
}
void c_set_we(can_ltm_t* msg)
{
if(GPS_long[GPS_valid][0] == '+')
{
msg->longitude1 |= 0x8000;
}
else
{
msg->longitude1 &= 0x7FFF;
}
}
int32_t can_set_beacon_msg(can_ltm_t* msg)
{
int i, j, IntegerPart;
uint32_t hh;
uint32_t mm;
uint32_t ss;
IntegerPart = 1;
radio_msg_id = 0;
radio_temp = 0;
msg->id = CAN_BEACON_TX;
msg->gps_time = 0;
msg->latitude1 = 0;
msg->latitude2 = 0;
msg->longitude1 = 0;
msg->longitude2 = 0;
msg->altitude = 0;
msg->ext_temp = 0;
msg->obc_temp = 0;
// set time
hh = (unsigned int)(GPS_time[GPS_valid][0] - '0') * 10;
hh += (unsigned int)(GPS_time[GPS_valid][1] - '0');
mm = (unsigned int)(GPS_time[GPS_valid][2] - '0') * 10;
mm += (unsigned int)(GPS_time[GPS_valid][3] - '0');
ss = (unsigned int)(GPS_time[GPS_valid][4] - '0') * 10;
ss += (unsigned int)(GPS_time[GPS_valid][5] - '0');
msg->gps_time = (hh * 3600 ) + (mm * 60 ) + ss;
// set latitude
for( i = 1, j = 0; i < 8; i++)
{
if (GPS_lati[GPS_valid][i] == '.')
{
j++; // Segment index
IntegerPart = 1;
}
else
{
if( j == 0)
{
msg->latitude1 = msg->latitude1 * 10;
msg->latitude1 += (unsigned int)(GPS_lati[GPS_valid][i] - '0');
}
if( j == 1)
{
msg->latitude2 = msg->latitude2 * 10;
msg->latitude2 += (unsigned int)(GPS_lati[GPS_valid][i] - '0');
}
}
}
c_set_ns(msg);
// set longitude
for( i = 1, j = 0; i < 9; i++)
{
if (GPS_long[GPS_valid][i] == '.')
{
j++; // Segment index
IntegerPart = 1;
}
else
{
if( j == 0)
{
msg->longitude1 = msg->longitude1 * 10;
msg->longitude1 += (unsigned int)(GPS_long[GPS_valid][i] - '0');
}
if( j == 1)
{
msg->longitude2 = msg->longitude2 * 10;
msg->longitude2 += (unsigned int)(GPS_long[GPS_valid][i] - '0');
}
}
}
c_set_we(msg);
// set altitude
msg->altitude = GPS_Altitude[GPS_valid];
// set temperatures
msg->ext_temp = ext_temp;
msg->obc_temp = pcb_temp;
}
int32_t can_send_msg(uint32_t id, uint32_t len)
{
byte sndStat = CAN0.sendMsgBuf(id, 0, len, can_tx_msg);
if(sndStat != CAN_OK)
{
return sndStat;
}
return 0;
}