-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc_comm.cpp
More file actions
150 lines (124 loc) · 3.58 KB
/
pc_comm.cpp
File metadata and controls
150 lines (124 loc) · 3.58 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 "pc_comm.h"
PcComm::PcComm() {}
void PcComm::begin(HardwareSerial& serial) {
this->stream = &serial;
stream->begin(115200);
this->is_connected = true;
this->is_alive_on = true;
}
void PcComm::sendPacket(byte AorM, byte ESTOP, byte GEAR, int SPEED, int OMEGA, byte BRAKE, int OMEGA_L, int OMEGA_R, byte BATTERY_V) {
byte packet[19] = {
STX[0], STX[1], STX[2], // STX (3 bytes)
AorM,
ESTOP,
GEAR, // 단일 바이트 값들
(byte)(SPEED >> 8), (byte)SPEED, // 2-byte int
(byte)(OMEGA >> 8), (byte)OMEGA, // 2-byte int
BRAKE,
(byte)(OMEGA_L >> 8), (byte)OMEGA_L, // 2-byte int
(byte)(OMEGA_R >> 8), (byte)OMEGA_R, // 2-byte int
BATTERY_V, ALIVE,
ETX[0], ETX[1] // ETX (2 bytes)
};
stream->write(packet, 19);
}
void PcComm::processPacket(byte *receivedPacket) {
PcComm::AorM = receivedPacket[3];
PcComm::ESTOP = receivedPacket[4];
PcComm::GEAR = receivedPacket[5];
PcComm::SPEED = (int16_t)((receivedPacket[6] << 8) | receivedPacket[7]);
PcComm::OMEGA = (int16_t)((receivedPacket[8] << 8) | receivedPacket[9]);
PcComm::BRAKE = receivedPacket[10];
PcComm::ALIVE = receivedPacket[11];
this->is_alive_on = (alive_prev != ALIVE);
this->alive_prev = ALIVE;
}
void PcComm::getCommandDD(CommandDDRobot &cmd) {
cmd.velocity = PcComm::SPEED;
cmd.omega = PcComm::OMEGA;
}
void PcComm::getCommandSteer(CommandSteeringRobot &cmd) {
// velocity
uint16_t vel = PcComm::SPEED;
if (vel > 5) {
cmd.velocity = 5;
}
else {cmd.velocity = vel;}
// brake
cmd.brake = PcComm::BRAKE;
// omega
cmd.steer = PcComm::OMEGA;
if (cmd.steer > 20)
{
cmd.steer = 20;
}
else if (cmd.steer < -20)
{
cmd.steer = -20;
}
// gear
switch (PcComm::GEAR) {
case 1:
cmd.gear = GEAR_FORWARD;
break;
case 0:
cmd.gear = GEAR_NEUTRAL;
break;
case -1:
cmd.gear = GEAR_REVERSE;
break;
case -100:
default:
cmd.gear = GEAR_NONE;
break;
}
// estop
cmd.estop = PcComm::ESTOP;
}
void PcComm::loop() {
processIncomingData();
checkConnectionTimeout();
updateConnectionStatus();
}
void PcComm::processIncomingData() {
while (stream->available()) {
byte incomingByte = stream->read();
lastReceivedTime = millis();
if (bufferIndex >= BUFFER_SIZE) {
bufferIndex = 0; // 버퍼 오버플로 방지
}
buffer[bufferIndex++] = incomingByte;
// 최소한 패킷의 기본 길이(14바이트) 이상 수신됐을 때 처리
if (bufferIndex >= 14) {
for (int i = 0; i <= bufferIndex - 14; i++) {
if (isValidPacket(i)) {
processPacket(&buffer[i]);
// 버퍼에서 해당 패킷을 제거
memmove(buffer, buffer + i + 14, bufferIndex - (i + 14));
bufferIndex -= (i + 14);
break;
}
}
}
}
}
bool PcComm::isValidPacket(byte index) {
return buffer[index] == STX[0] && buffer[index + 1] == STX[1] && buffer[index + 2] == STX[2] &&
buffer[index + 12] == ETX[0] && buffer[index + 13] == ETX[1];
}
void PcComm::checkConnectionTimeout() {
if (millis() - lastReceivedTime > TIMEOUT_MS) {
is_upper_on = false;
}
else{
is_upper_on = true;
}
}
void PcComm::updateConnectionStatus() {
if (!this->is_alive_on || !this->is_upper_on) {
this->is_connected = false;
}
else {
this->is_connected = true;
}
}