Can msg send

This commit is contained in:
2021-07-02 16:13:59 +03:00
parent f9368c434b
commit 88c6b06061
8 changed files with 90 additions and 25 deletions

52
kx.py
View File

@@ -3,6 +3,8 @@ import can
import cantools
import os
import numpy as np
import struct
import time
K = np.zeros(protocol_kx.KX_K_COUNT)
@@ -21,21 +23,55 @@ class KX:
def reset(self):
resetMsg = can.Message(arbitration_id=0xA, data=[99, self.sbl_id], is_extended_id=False)
self.can0.send(resetMsg)
try:
self.can0.send(resetMsg)
print(f"Message sent on{self.can0.channel_info}")
except self.can0.CanError:
print("Message NOT sent")
def sendK(self):
msg = protocol_kx.CANMsgK(self.sbl_id, 0, protocol_kx.KX_FLAG_SEND, K[0])
i = 0
for element in K:
msg.k = element
msg.index = i
ba = bytearray(struct.pack("f", element))
messageK = can.Message(arbitration_id=0x2BB, data=[self.sbl_id, i, 0,
protocol_kx.KX_FLAG_SEND, ba[0], ba[1], ba[2], ba[3]], is_extended_id=False)
try:
self.can0.send(messageK)
time.sleep(0.01)
print(f"Message sent on{self.can0.channel_info}")
except self.can0.CanError:
print("Message NOT sent")
i = i + 1
messageK = can.Message(protocol_kx.KX_FLAG_SEND, data=msg, is_extended_id=False)
self.can0.send(messageK)
def writeToFlashK(self):
msg = protocol_kx.CANMsgK(self.sbl_id, 0, protocol_kx.KX_FLAG_WRITE_REQ, K)
messageK = can.Message(protocol_kx.KX_FLAG_READ, data=msg, is_extended_id=False)
self.can0.send(messageK)
messageK = can.Message(arbitration_id=0x2BB, data=[self.sbl_id, 0, 0,
protocol_kx.KX_FLAG_WRITE_REQ], is_extended_id=False)
try:
self.can0.send(messageK)
time.sleep(0.01)
print(f"Flash sent on{self.can0.channel_info}")
except self.can0.CanError:
print("Flash NOT sent")
def send_test_source_enable(self, x, y, xVel, yVel):
resetMsg = can.Message(
arbitration_id=0xA,
data=[
0x53,
0xFF,
struct.pack('b', int(x))[0],
struct.pack('B', int(y))[0],
struct.pack('b', int(xVel))[0],
struct.pack('b', int(yVel))[0],
0x68,
0xA4
],
is_extended_id=False
)
self.can0.send(resetMsg)