diff --git a/controller/fw/embed/test/python_write_pid.py b/controller/fw/embed/test/python_write_pid.py new file mode 100644 index 0000000..103c6a0 --- /dev/null +++ b/controller/fw/embed/test/python_write_pid.py @@ -0,0 +1,122 @@ +import can +import time +import struct +# Конфигурация +CAN_INTERFACE = 'can0' +DEVICE_ID = 0x00 +SET_PID_P = 3.6 +REG_WRITE = 0x8 +REG_READ = 0x7 +REG_ID = 0x30 #REG_MOTOR_POSPID_Kp +PID_P = 0x01 + +def send_can_message(bus, can_id, data): + """Отправка CAN-сообщения""" + try: + msg = can.Message( + arbitration_id=can_id, + data=data, + is_extended_id=False + ) + bus.send(msg) + print(f"[Отправка] CAN ID: 0x{can_id:03X}, Данные: {list(data)}") + return True + except can.CanError as e: + print(f"Ошибка CAN: {e}") + return False + +def receive_response(bus, timeout=1.0): + print("Ожидание ответа") + start_time = time.time() + while time.time() - start_time < timeout: + msg = bus.recv(timeout=0.1) + if msg: + print(f"[Прием] CAN ID: 0x{msg.arbitration_id:03X}, Данные: {list(msg.data)}") + return msg + print("[Ошибка] Таймаут") + return None + +def validate_crc16(data): + """Функция расчета CRC16 (MODBUS)""" + crc = 0xFFFF + for byte in data: + crc ^= byte + for _ in range(8): + if crc & 0x0001: + crc = (crc >> 1) ^ 0xA001 + else: + crc >>= 1 + return crc + +# Инициализация +bus = can.interface.Bus(channel=CAN_INTERFACE, bustype='socketcan') +# Перевод float -> hex -> int +result = (struct.unpack('