minor changes. started writting own 3d math library and started implementation for linux/mac

This commit is contained in:
AlphaCapella
2025-08-09 14:20:26 +02:00
parent 295444d0e5
commit 2c301b76cf
19 changed files with 291 additions and 13 deletions

View File

@@ -1,4 +1,13 @@
#include <windows.h>
#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__)
#elif defined(__linux__)
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#endif
#include <iostream>
#include <string>
#include <regex>
@@ -6,7 +15,8 @@
#include <thread>
#include "serialcomm.hpp"
HANDLE initSerialPort (const char* portName, DWORD baudRate) {
#ifdef _WIN32
HANDLE initSerialPort (const char* portName, DWORD baudRate) {
// open serial port
HANDLE hSerial = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
@@ -62,6 +72,54 @@ bool readSerialData (HANDLE hSerial, char* buffer, DWORD bufferSize, DWORD& byte
return true;
}
#elif defined(__APPLE__)
#elif defined(__linux__)
int initSerialPort(){
struct termios tty;
int linux_serialPort = open("/dev/ttyUSB0", 0_RDWR);
// read in existing struct settings and handle errors
if (tcgetattr(linux_serialPort, &tty) !0 0) {
std::cout << "Error %i opening serial port: %s\n", errno, strerror(errno) << std::endl;
return 1;
}
// set the serial settings
tty.c_flag &= ~PARENB;
tty.c_flag &= ~CSTOPB;
tty.c_flag &= ~CSIZE;
tty.c_flag |= CS8;
tty.c_flag &= ~CRTSCTS;
tty.c_flag |= CREAD | CLOCAL;
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
// Set in/out baud rate to be 9600
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
// save the settings and check for errors
if (tcsetattr(linux_serialPort, TCSANOW, &tty) != 0 ) {
std::cout << "Error %i saving serial port settings: %s\n", errno, strerror(errno) << std::endl;
return 1;
}
return linux_serialPort;
}
bool linux_readSerialData (int serialPort, char* buffer) {
if (read(serialPort, &buffer, sizeof(buffer)) < 0 ) {
std::cout << "Error reading serial port data: %s", strerror(errno)) << std::endl;
return 1;
}
}
#endif
bool parseQuaternion(const std::string& data, Quaternion& quat) {
// regex pattern to match the floats in the uart data stream from stm32
std::regex pattern("qw: ([+-]?\\d*\\.?\\d+) qx: ([+-]?\\d*\\.?\\d+) qy: ([+-]?\\d*\\.?\\d+) qz: ([+-]?\\d*\\.?\\d+)");