Angefangen zweiten Sensor Readout zu implementieren. Funktioniert auch teils

This commit is contained in:
AlphaCapella
2026-02-01 20:12:22 +01:00
parent 2c301b76cf
commit fd112fe5ce
22 changed files with 164 additions and 35 deletions

0
.gitignore vendored Normal file
View File

View File

@@ -43,7 +43,7 @@
{ {
"directoryIndex" : 0, "directoryIndex" : 0,
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",
"jsonFile" : "target-ALL_BUILD-Debug-337565d1177e62c2d66c.json", "jsonFile" : "target-ALL_BUILD-Debug-ef3c7c685920d9d07655.json",
"name" : "ALL_BUILD", "name" : "ALL_BUILD",
"projectIndex" : 0 "projectIndex" : 0
}, },
@@ -105,7 +105,7 @@
{ {
"directoryIndex" : 0, "directoryIndex" : 0,
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",
"jsonFile" : "target-ALL_BUILD-Release-337565d1177e62c2d66c.json", "jsonFile" : "target-ALL_BUILD-Release-ef3c7c685920d9d07655.json",
"name" : "ALL_BUILD", "name" : "ALL_BUILD",
"projectIndex" : 0 "projectIndex" : 0
}, },
@@ -167,7 +167,7 @@
{ {
"directoryIndex" : 0, "directoryIndex" : 0,
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",
"jsonFile" : "target-ALL_BUILD-MinSizeRel-337565d1177e62c2d66c.json", "jsonFile" : "target-ALL_BUILD-MinSizeRel-ef3c7c685920d9d07655.json",
"name" : "ALL_BUILD", "name" : "ALL_BUILD",
"projectIndex" : 0 "projectIndex" : 0
}, },
@@ -229,7 +229,7 @@
{ {
"directoryIndex" : 0, "directoryIndex" : 0,
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",
"jsonFile" : "target-ALL_BUILD-RelWithDebInfo-337565d1177e62c2d66c.json", "jsonFile" : "target-ALL_BUILD-RelWithDebInfo-ef3c7c685920d9d07655.json",
"name" : "ALL_BUILD", "name" : "ALL_BUILD",
"projectIndex" : 0 "projectIndex" : 0
}, },

View File

@@ -27,7 +27,7 @@
"objects" : "objects" :
[ [
{ {
"jsonFile" : "codemodel-v2-698401d937ca015c3200.json", "jsonFile" : "codemodel-v2-55cefbb714e39f287e6c.json",
"kind" : "codemodel", "kind" : "codemodel",
"version" : "version" :
{ {
@@ -100,7 +100,7 @@
} }
}, },
{ {
"jsonFile" : "codemodel-v2-698401d937ca015c3200.json", "jsonFile" : "codemodel-v2-55cefbb714e39f287e6c.json",
"kind" : "codemodel", "kind" : "codemodel",
"version" : "version" :
{ {

View File

@@ -17,10 +17,10 @@
"dependencies" : "dependencies" :
[ [
{ {
"id" : "ZERO_CHECK::@6890427a1f51a3e7e1df" "id" : "visualizer::@6890427a1f51a3e7e1df"
}, },
{ {
"id" : "visualizer::@6890427a1f51a3e7e1df" "id" : "ZERO_CHECK::@6890427a1f51a3e7e1df"
} }
], ],
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",

View File

@@ -17,10 +17,10 @@
"dependencies" : "dependencies" :
[ [
{ {
"id" : "ZERO_CHECK::@6890427a1f51a3e7e1df" "id" : "visualizer::@6890427a1f51a3e7e1df"
}, },
{ {
"id" : "visualizer::@6890427a1f51a3e7e1df" "id" : "ZERO_CHECK::@6890427a1f51a3e7e1df"
} }
], ],
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",

View File

@@ -17,10 +17,10 @@
"dependencies" : "dependencies" :
[ [
{ {
"id" : "ZERO_CHECK::@6890427a1f51a3e7e1df" "id" : "visualizer::@6890427a1f51a3e7e1df"
}, },
{ {
"id" : "visualizer::@6890427a1f51a3e7e1df" "id" : "ZERO_CHECK::@6890427a1f51a3e7e1df"
} }
], ],
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",

View File

@@ -17,10 +17,10 @@
"dependencies" : "dependencies" :
[ [
{ {
"id" : "ZERO_CHECK::@6890427a1f51a3e7e1df" "id" : "visualizer::@6890427a1f51a3e7e1df"
}, },
{ {
"id" : "visualizer::@6890427a1f51a3e7e1df" "id" : "ZERO_CHECK::@6890427a1f51a3e7e1df"
} }
], ],
"id" : "ALL_BUILD::@6890427a1f51a3e7e1df", "id" : "ALL_BUILD::@6890427a1f51a3e7e1df",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,6 @@
[Window][Debug##Default] [Window][Debug##Default]
Pos=22,29 Pos=22,51
Size=326,393 Size=432,537
[Window][Hello, ImGui!] [Window][Hello, ImGui!]
Pos=60,60 Pos=60,60

View File

@@ -14,6 +14,9 @@
#include <string> #include <string>
#include <mutex> #include <mutex>
#include "visualization.hpp" #include "visualization.hpp"
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector> #include <vector>
#include "include/glm/glm.hpp" #include "include/glm/glm.hpp"
#include "include/glm/gtc/matrix_transform.hpp" #include "include/glm/gtc/matrix_transform.hpp"
@@ -65,9 +68,11 @@ void serialParserThread() {
// define communication variables // define communication variables
const char* portName = "COM3"; const char* portName = "COM3";
DWORD baudRate = CBR_115200; DWORD baudRate = CBR_115200;
char buffer[64];
DWORD bytesRead; DWORD bytesRead;
std::string incompleteDataBuffer; //char buffer[64];
//std::string incompleteDataBuffer;
uint8_t buffer[64];
std::vector<uint8_t> binaryPacketBuffer;
// establish communication to stm32 // establish communication to stm32
g_serialHandle = initSerialPort(portName, baudRate); g_serialHandle = initSerialPort(portName, baudRate);
@@ -77,24 +82,34 @@ void serialParserThread() {
return; return;
} else {std::cout << "Serial connection succesfully established" << std::endl;} } else {std::cout << "Serial connection succesfully established" << std::endl;}
// reinterpret_cast<char*>(buffer)
// serial reading loop // serial reading loop
while (g_running) { while (g_running) {
if (readSerialData(g_serialHandle, buffer, sizeof(buffer), bytesRead)) { if (readSerialData(g_serialHandle, reinterpret_cast<uint8_t*>(buffer), sizeof(buffer), bytesRead)) {
if (bytesRead > 0) { if (bytesRead > 0) {
// add newly arrived data to buffer /*std::cout << "Received " << bytesRead << " bytes: ";
buffer[bytesRead] = '\0'; for (DWORD i = 0; i < bytesRead && i < 10; ++i) { // Print first 10 bytes
incompleteDataBuffer += buffer; std::cout << std::hex << std::setfill('0') << std::setw(2) << (int)buffer[i] << " ";
}
std::cout << std::dec << std::endl; */
// add newly arrived binary data to buffer
//buffer[bytesRead] = '\0';
//incompleteDataBuffer += buffer;
binaryPacketBuffer.insert(binaryPacketBuffer.end(), buffer, buffer+bytesRead);
// process any complete lines in the buffer // process any complete packets
size_t pos; /* size_t pos; */
while ((pos = incompleteDataBuffer.find('\n')) != std::string::npos) { while (true) {
// create substring of completed line and erase it from incompleteDataBuffer /* // create substring of completed line and erase it from incompleteDataBuffer
std::string completeLine = incompleteDataBuffer.substr(0, pos); std::string completeLine = incompleteDataBuffer.substr(0, pos);
incompleteDataBuffer.erase(0, pos + 1); incompleteDataBuffer.erase(0, pos + 1); */
std::vector<uint8_t> packet = extractPacket(binaryPacketBuffer);
if (packet.empty()) {break;}
// now parse the quaternions from line // now parse the quaternions from line
Quaternion quat; Quaternion quat;
if (parseQuaternion(completeLine, quat)) { if (parseBinaryPacket(packet, quat)) {
// lock mutex while updating it // lock mutex while updating it
std::lock_guard<std::mutex> lock(g_mutex); std::lock_guard<std::mutex> lock(g_mutex);
g_currQuat = quat; g_currQuat = quat;
@@ -109,6 +124,7 @@ void serialParserThread() {
} }
} }
//std::this_thread::sleep_for(std::chrono::milliseconds(5));
} }

View File

@@ -1,3 +1,5 @@
#include <cstdint>
#include <vector>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#elif defined(__APPLE__) #elif defined(__APPLE__)
@@ -14,6 +16,14 @@
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include "serialcomm.hpp" #include "serialcomm.hpp"
#include <iomanip>
#include <algorithm>
// constants for the uart packet of quaterniondata
constexpr uint8_t PACKET_START_BYTE = 0xDE;
constexpr uint8_t PACKET_END_BYTE = 0xAD;
constexpr size_t PACKET_SIZE = sizeof(QuaternionData);
#ifdef _WIN32 #ifdef _WIN32
HANDLE initSerialPort (const char* portName, DWORD baudRate) { HANDLE initSerialPort (const char* portName, DWORD baudRate) {
@@ -62,13 +72,13 @@
} }
bool readSerialData (HANDLE hSerial, char* buffer, DWORD bufferSize, DWORD& bytesRead) { bool readSerialData (HANDLE hSerial, uint8_t* buffer, DWORD bufferSize, DWORD& bytesRead) {
if (!ReadFile(hSerial, buffer, bufferSize - 1, &bytesRead, NULL)){ if (!ReadFile(hSerial, buffer, bufferSize - 1, &bytesRead, NULL)){
std::cerr << "Error reading from serial port: " << GetLastError() << std::endl; std::cerr << "Error reading from serial port: " << GetLastError() << std::endl;
return false; return false;
} }
// dont needed für binary stream denke ich
buffer[bytesRead] = '\0'; //buffer[bytesRead] = '\0';
return true; return true;
} }
@@ -136,4 +146,85 @@ bool parseQuaternion(const std::string& data, Quaternion& quat) {
} else {return false;} } else {return false;}
} }
bool parseBinaryPacket(std::vector<uint8_t> &packetData, Quaternion &quat) {
if (packetData.size() != PACKET_SIZE) {
std::cout << "Wrong packet size detected" << std::endl;
return false;
}
// cast raw bytes to struct
QuaternionData* packet = reinterpret_cast<QuaternionData*>(packetData.data());
// verify start and end bytes
if (packet->StartByte != PACKET_START_BYTE || packet->EndByte != PACKET_END_BYTE) {
std::cout << "Mismatch in packet end- or startbyte size" << std::endl;
return false;
}
quat.w = packet->qw;
quat.x = packet->qx;
quat.y = packet->qy;
quat.z = packet->qz;
return true;
}
std::vector<uint8_t> extractPacket(std::vector<uint8_t> &buffer) {
while (buffer.size() >= PACKET_SIZE) {
// find start byte
auto startIT = std::find(buffer.begin(), buffer.end(), PACKET_START_BYTE);
if (startIT == buffer.end()) {
// clear buffer because no start byte found
buffer.clear();
std::cout << "Buffer cleared - no start byte found" << std::endl;
return {};
}
//remove data before the start byte
if (startIT != buffer.begin()){
buffer.erase(buffer.begin(), startIT);
return {};
}
// check for enough enough bytes for whole packet
if (buffer.size() < PACKET_SIZE) {
return {};
}
// check for end byte at correct position
if (buffer[PACKET_SIZE -1] != PACKET_END_BYTE) {
buffer.erase((buffer.begin()));
return {};
}
// now extract the valid packet
std::vector<uint8_t> validPacket(buffer.begin(), buffer.begin() + PACKET_SIZE);
buffer.erase(buffer.begin(), buffer.begin() + PACKET_SIZE);
return validPacket;
}
return {};
}
void checkPacketSize() {
std::cout << "QuaternionData struct size: " << sizeof(QuaternionData) << " bytes" << std::endl;
std::cout << "Expected layout:" << std::endl;
std::cout << " StartByte: " << sizeof(uint8_t) << " byte" << std::endl;
std::cout << " SensorAddress: " << sizeof(uint8_t) << " byte" << std::endl;
std::cout << " qw: " << sizeof(float) << " bytes" << std::endl;
std::cout << " qx: " << sizeof(float) << " bytes" << std::endl;
std::cout << " qy: " << sizeof(float) << " bytes" << std::endl;
std::cout << " qz: " << sizeof(float) << " bytes" << std::endl;
std::cout << " EndByte: " << sizeof(uint8_t) << " byte" << std::endl;
std::cout << " Total expected: " << (3 * sizeof(uint8_t) + 4 * sizeof(float)) << " bytes" << std::endl;
}
// Add this debug function to serialcomm.cpp
void printBufferHex(const std::vector<uint8_t>& buffer, const std::string& label) {
std::cout << label << " (" << buffer.size() << " bytes): ";
for (size_t i = 0; i < buffer.size() && i < 60; ++i) { // Print first 20 bytes
std::cout << std::hex << std::setfill('0') << std::setw(2) << (int)buffer[i] << " ";
}
if (buffer.size() > 20) std::cout << "...";
std::cout << std::dec << std::endl;
}

View File

@@ -1,13 +1,33 @@
#include <vector>
#include <cstdint>
#include <windows.h>
#include <iostream>
struct Quaternion { struct Quaternion {
float w, x, y, z; float w, x, y, z;
}; };
#pragma pack(push, 1)
typedef struct {
uint8_t StartByte;
uint8_t SensorAddress;
float qw;
float qx;
float qy;
float qz;
uint8_t EndByte;
} QuaternionData;
#pragma pack(pop)
HANDLE initSerialPort (const char* portName, DWORD baudRate); HANDLE initSerialPort (const char* portName, DWORD baudRate);
bool readSerialData (HANDLE hSerial, char* buffer, DWORD bufferSize, DWORD& bytesRead); bool readSerialData (HANDLE hSerial, uint8_t* buffer, DWORD bufferSize, DWORD& bytesRead);
bool parseQuaternion(const std::string& data, Quaternion& quat); bool parseQuaternion(const std::string& data, Quaternion& quat);
void checkPacketSize();
void printBufferHex(const std::vector<uint8_t>& buffer, const std::string& label);
int linux_initSerialPort(); int linux_initSerialPort();
bool linux_readSerialData(int serialPort, char* buffer); bool linux_readSerialData(int serialPort, char* buffer);
std::vector<uint8_t> extractPacket(std::vector<uint8_t> &buffer);
bool parseBinaryPacket(std::vector<uint8_t> &packetData, Quaternion &quat);

2
tempCodeRunnerFile.cpp Normal file
View File

@@ -0,0 +1,2 @@
buffer[bytesRead] = '\0';
incompleteDataBuffer += buffer;