From 6f1608742f112680c1b151cea465acce9332bc31 Mon Sep 17 00:00:00 2001 From: samuko-things Date: Wed, 31 Dec 2025 02:58:11 +0100 Subject: [PATCH 1/2] update serial communication code --- eimu.py | 279 +++++++++++++++------------------------------------- read_imu.py | 27 +++-- 2 files changed, 98 insertions(+), 208 deletions(-) diff --git a/eimu.py b/eimu.py index e3682a1..cf5ad2b 100644 --- a/eimu.py +++ b/eimu.py @@ -1,29 +1,49 @@ import serial -import struct - -class EIMUSerialError(Exception): - """Custom exception for for EIMU Comm failure""" - pass - -START_BYTE = 0xBB -READ_QUAT = 0x01 -READ_RPY = 0x02 -READ_RPY_VAR = 0x03 -READ_ACC = 0x05 -READ_ACC_VAR = 0x09 -READ_GYRO = 0x0B -READ_GYRO_VAR = 0x0F -READ_MAG = 0x11 -SET_FILTER_GAIN = 0x1D -GET_FILTER_GAIN = 0x1E -SET_FRAME_ID = 0x1F -GET_FRAME_ID = 0x20 -READ_QUAT_RPY = 0x22 -READ_ACC_GYRO = 0x23 -CLEAR_DATA_BUFFER = 0x27 -READ_IMU_DATA = 0x28 -READ_LIN_ACC_RAW = 0x2B -READ_LIN_ACC = 0x2C +import time + +# Serial Protocol Command IDs ------------- +READ_RPY = 10 +READ_RPY_VAR = 11 +WRITE_RPY_VAR = 12 + +READ_GYRO = 13 +READ_GYRO_RAW = 14 +READ_GYRO_OFF = 15 +WRITE_GYRO_OFF = 16 +READ_GYRO_VAR = 17 +WRITE_GYRO_VAR = 18 + +READ_ACC = 19 +READ_ACC_RAW = 20 +READ_ACC_OFF = 21 +WRITE_ACC_OFF = 22 +READ_ACC_VAR = 23 +WRITE_ACC_VAR = 24 +READ_LIN_ACC_RAW = 25 +READ_LIN_ACC = 26 +SET_ACC_LPF_CUT_FREQ = 27 +GET_ACC_LPF_CUT_FREQ = 28 + +READ_MAG = 29 +READ_MAG_RAW = 30 +READ_MAG_H_OFF = 31 +WRITE_MAG_H_OFF = 32 +READ_MAG_S_OFF0 = 33 +WRITE_MAG_S_OFF0 = 34 +READ_MAG_S_OFF1 = 35 +WRITE_MAG_S_OFF1 = 36 +READ_MAG_S_OFF2 = 37 +WRITE_MAG_S_OFF2 = 38 + +SET_I2C_ADDR = 39 +GET_I2C_ADDR = 40 +SET_FILTER_GAIN = 41 +GET_FILTER_GAIN = 42 +SET_FRAME_ID = 43 +GET_FRAME_ID = 44 +RESET = 45 +CLEAR = 46 +#--------------------------------------------- @@ -31,221 +51,80 @@ class EIMU: def __init__(self): pass - def connect(self, port, baud=56700, timeOut=0.1): + def connect(self, port, baud=115200, timeOut=0.1): self.ser = serial.Serial(port, baud, timeout=timeOut) - + time.sleep(0.1) + self.ser.reset_input_buffer() + self.ser.reset_output_buffer() + def disconnect(self): if self.ser.is_open: self.ser.close() - - #------------------------------------------------------------------------ - def send_packet_without_payload(self, cmd): - length = 0 - packet = bytearray([START_BYTE, cmd, length]) - checksum = sum(packet) & 0xFF - packet.append(checksum) - self.ser.write(packet) - self.ser.flush() - def send_packet_with_payload(self, cmd, payload_bytes): - length = len(payload_bytes) - packet = bytearray([START_BYTE, cmd, length]) + payload_bytes - checksum = sum(packet) & 0xFF - packet.append(checksum) - self.ser.write(packet) - self.ser.flush() - - def read_packet1(self): - """ - Reads 4 bytes from the serial port and converts to a float (little-endian). - Returns (success, value-array) - """ - try: - payload = self.ser.read(4) - if len(payload) != 4: - # print("[EPMC SERIAL ERROR]: Timeout while reading 1 values") - return False, 0.0 + #------------------------------------------------------------------------ + def send(self, cmd, arg1=0.0, arg2=0.0, arg3=0.0): + send_str = str(float(cmd))+" "+str(float(arg1))+" "+str(float(arg2))+" "+str(float(arg3))+"\r" + self.ser.write(send_str.encode()) - # Unpack 4 bytes as little-endian float - (val,) = struct.unpack(' sampleTime: - success, r, p, y, ax, ay, az, gx, gy, gz = eimu.readImuData() + success, valx, valy, valz = eimu.readRPY() + if success: + r=valx; p=valy; y=valz + success, valx, valy, valz = eimu.readLinearAcc() if success: - print(f"r: {r}\tp: {p}\ty: {y}") - print(f"ax: {ax}\tay: {ay}\taz: {az}") - print(f"gx: {gx}\tgy: {gy}\tgz: {gz}") - print() - else: - print("Error reading IMU data") + ax=valx; ay=valy; az=valz + + success, valx, valy, valz = eimu.readGyro() + if success: + gx=valx; gy=valy; gz=valz + + print(f"r: {r}\tp: {p}\ty: {y}") + print(f"ax: {ax}\tay: {ay}\taz: {az}") + print(f"gx: {gx}\tgy: {gy}\tgz: {gz}") + print() prevTime = time.time() if __name__ == "__main__": From 9e5db740736cbad5854054524ef31058198db5c4 Mon Sep 17 00:00:00 2001 From: samuko-things Date: Wed, 31 Dec 2025 14:23:27 +0100 Subject: [PATCH 2/2] update communication code --- eimu.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/eimu.py b/eimu.py index cf5ad2b..f704c52 100644 --- a/eimu.py +++ b/eimu.py @@ -63,14 +63,14 @@ def disconnect(self): #------------------------------------------------------------------------ def send(self, cmd, arg1=0.0, arg2=0.0, arg3=0.0): - send_str = str(float(cmd))+" "+str(float(arg1))+" "+str(float(arg2))+" "+str(float(arg3))+"\r" + send_str = str(round(float(cmd),6))+" "+str(round(float(arg1),6))+" "+str(round(float(arg2),6))+" "+str(round(float(arg3),6))+"\r" self.ser.write(send_str.encode()) def recv(self, cmd, arg1=0): try: self.send(cmd, arg1) data = self.ser.readline().decode().strip().split(' ') - return True, float(data[0]), float(data[1]), float(data[2]) + return True, round(float(data[0]),6), round(float(data[1]),6), round(float(data[2]),6) except: # self.ser.reset_input_buffer() # self.ser.reset_output_buffer() @@ -105,26 +105,26 @@ def getAccFilterCF(self): def readRPY(self): success, r, p, y = self.recv(READ_RPY) - return success, round(r, 6), round(p, 6), round(y, 6) + return success, r, p, y def readRPYVariance(self): success, r, p, y = self.recv(READ_RPY_VAR) - return success, round(r, 6), round(p, 6), round(y, 6) + return success, r, p, y def readLinearAcc(self): success, ax, ay, az = self.recv(READ_LIN_ACC) - return success, round(ax, 6), round(ay, 6), round(az, 6) + return success, ax, ay, az def readAccVariance(self): success, ax, ay, az = self.recv(READ_ACC_VAR) - return success, round(ax, 6), round(ay, 6), round(az, 6) + return success, ax, ay, az def readGyro(self): success, gx, gy, gz = self.recv(READ_GYRO) - return success, round(gx, 6), round(gy, 6), round(gz, 6) + return success, gx, gy, gz def readGyroVariance(self): success, gx, gy, gz = self.recv(READ_GYRO_VAR) - return success, round(gx, 6), round(gy, 6), round(gz, 6) + return success, gx, gy, gz #--------------------------------------------------------------------- \ No newline at end of file