-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyController.h
210 lines (180 loc) · 6.46 KB
/
MyController.h
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
201
202
203
204
205
206
207
208
209
210
#ifndef MY_CONTROLLER_H
#define MY_CONTROLLER_H
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <chrono>
#include <vector>
#include <sstream>
#include <algorithm>
#include "FloatConverter.h"
#include <limits>
class MyController {
public:
MyController(const char* portName);
~MyController();
bool setupSerialPort();
void clearBuffer();
void sendStopCommand();
void sendBrakeCommand();
void sendRezeroCommand(float float1);
void sendQueryCommand();
void sendWriteCommand(float float1, float float2);
void sendWriteOnlyCommand(float float1, float float2);
void sendCustomCommand();
std::vector<float> sendReadCommand();
void closeSerialPort();
private:
int fd;
std::string portName;
};
// Constructor
MyController::MyController(const char* portName) : portName(portName), fd(-1) {}
// Destructor
MyController::~MyController() {
if (fd != -1) {
closeSerialPort();
}
}
// SERIAL PORT SETUP
bool MyController::setupSerialPort() {
fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
std::cerr << "Error opening " << portName << ": " << strerror(errno) << std::endl;
return false;
}
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(fd, &tty) != 0) {
std::cerr << "Error from tcgetattr: " << strerror(errno) << std::endl;
close(fd);
return false;
}
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
std::cerr << "Error from tcsetattr: " << strerror(errno) << std::endl;
close(fd);
return false;
}
return true;
}
// CLEAR BUFFER
void MyController::clearBuffer() {
char buf[256];
read(fd, buf, sizeof buf);
}
// STOP COMMAND
void MyController::sendStopCommand() {
std::string message = "can send 0001 010000\n";
write(fd, message.c_str(), message.length());
}
// BRAKE COMMAND
void MyController::sendBrakeCommand() {
std::string message = "can send 0001 01000f\n";
write(fd, message.c_str(), message.length());
}
// WRITE COMMAND
void MyController::sendWriteCommand(float float1, float float2) {
std::string commandPrefix = "can send 8001 01000a0c0220";
std::string commandSuffix = "1c0301\n"; //Read 3 registers starting at 0x01
std::vector<unsigned char> float1Bytes = FloatConverter::convertFloat(float1);
std::vector<unsigned char> float2Bytes = FloatConverter::convertFloat(float2);
std::string float1Hex(float1Bytes.begin(), float1Bytes.end());
std::string float2Hex(float2Bytes.begin(), float2Bytes.end());
std::string commandData = float1Hex + float2Hex;
std::string command = commandPrefix + commandData + commandSuffix;
write(fd, command.c_str(), command.length());
}
// WRITE ONLY COMMAND
void MyController::sendWriteOnlyCommand(float float1, float float2) {
std::string commandPrefix = "can send 01 01000a0c0220";
std::string commandSuffix = "\n";
std::vector<unsigned char> float1Bytes = FloatConverter::convertFloat(float1);
std::vector<unsigned char> float2Bytes = FloatConverter::convertFloat(float2);
std::string float1Hex(float1Bytes.begin(), float1Bytes.end());
std::string float2Hex(float2Bytes.begin(), float2Bytes.end());
std::string commandData = float1Hex + float2Hex;
std::string command = commandPrefix + commandData + commandSuffix;
//std::cout << "Command: " << command << std::endl;
write(fd, command.c_str(), command.length());
usleep(5);
//clear buffer
char buf[256];
read(fd, buf, sizeof buf);
}
// CUSTOM COMMAND
void MyController::sendCustomCommand() {
std::string commandPrefix = "can send 0001 ";
std::string commandSuffix = "\n";
std::string commandData = "0db10200004040";
std::string command = commandPrefix + commandData + commandSuffix;
std::cout << "Command: " << command << std::endl;
write(fd, command.c_str(), command.length());
}
// REZERO
void MyController::sendRezeroCommand(float float1) {
std::string commandPrefix = "can send 0001 0db102";
std::string commandSuffix = "\n";
std::vector<unsigned char> float1Bytes = FloatConverter::convertFloat(float1);
std::string float1Hex(float1Bytes.begin(), float1Bytes.end());
std::string commandData = float1Hex;
//std::string commandData = "0db102 00 00 40 40";
std::string command = commandPrefix + commandData + commandSuffix;
//std::cout << "Command: " << command << std::endl;
write(fd, command.c_str(), command.length());
}
// SEND QUERY COMMAND
void MyController::sendQueryCommand() {
std::string commandPrefix = "can send 8001 ";
std::string commandSuffix = "1c0301\n"; //Read 3 register starting at 0x01
std::string command = commandPrefix + commandSuffix;
write(fd, command.c_str(), command.length());
}
// HEX STRING TO FLOAT
float hexStringToFloat(const std::vector<std::string>& hexParts) {
unsigned int x = 0;
std::stringstream ss;
for (const auto& part : hexParts) { ss << part; }
ss >> std::hex >> x;
float f;
std::memcpy(&f, &x, sizeof(x)); // Reinterpret the bits of x as a float
return f;
}
// Send read command and return vector of floats
std::vector<float> MyController::sendReadCommand() {
char buf[256];
read(fd, buf, sizeof buf);
std::vector<float> floats;
// Helper lambda to convert hex in buffer to float
auto convertToFloat = [&](int start) -> float {
std::vector<std::string> parts;
for (int j = 0; j < 4; ++j) {
parts.emplace_back(buf + start + j * 2, 2);
}
std::reverse(parts.begin(), parts.end());
return hexStringToFloat(parts);
};
// Read three floats from specified buffer positions
floats.push_back(convertToFloat(18)); // First float
floats.push_back(convertToFloat(26)); // Second float
floats.push_back(convertToFloat(34)); // Third float
return floats;
}
// Close the serial port
void MyController::closeSerialPort() {
if (fd != -1) {
close(fd);
fd = -1;
}
}
#endif // MY_CONTROLLER_H