Test for angle
This commit is contained in:
parent
ee8b011098
commit
7f29caeb76
3 changed files with 59 additions and 28 deletions
2
controller/fw/embed/.gitignore
vendored
2
controller/fw/embed/.gitignore
vendored
|
@ -7,4 +7,4 @@
|
||||||
.metadata/
|
.metadata/
|
||||||
cubemx_config/
|
cubemx_config/
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
../embed.rar/
|
../embed.rar
|
||||||
|
|
|
@ -117,22 +117,22 @@ void setup_foc(MagneticSensorAS5045 *encoder, BLDCMotor *motor,
|
||||||
motor->initFOC();
|
motor->initFOC();
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_can_with_id_crc(uint32_t id, uint8_t message_type, const void* data, size_t data_length) {
|
void send_can_with_id_crc(uint8_t id, uint8_t message_type, const void* data, size_t data_length) {
|
||||||
// Create CAN message
|
// Create CAN message
|
||||||
CAN_message_t msg_l;
|
CAN_message_t msg_l;
|
||||||
msg_l.id = id;
|
msg_l.id = id;
|
||||||
msg_l.len = 8; // Protocol-defined message length
|
// msg_l.len = 8; // Protocol-defined message length
|
||||||
msg_l.buf[0] = message_type;
|
msg_l.buf[0] = message_type;
|
||||||
memcpy(&msg_l.buf[1], data, data_length);
|
memcpy(&msg_l.buf[1], data, 1);
|
||||||
|
|
||||||
// Prepare CRC calculation buffer (ID + data)
|
// Prepare CRC calculation buffer (ID + data)
|
||||||
size_t crc_data_size = sizeof(msg_l.id) + data_length;
|
size_t crc_data_size = sizeof(msg_l.id) + 1;
|
||||||
uint8_t crc_data[crc_data_size];
|
uint8_t crc_data[crc_data_size];
|
||||||
|
|
||||||
// Copy message ID
|
// Copy message ID
|
||||||
memcpy(crc_data, &msg_l.id, sizeof(msg_l.id));
|
memcpy(crc_data, &msg_l.id, sizeof(msg_l.id));
|
||||||
// Copy all data bytes
|
// Copy all data bytes
|
||||||
memcpy(crc_data + sizeof(msg_l.id), data, data_length);
|
memcpy(crc_data + sizeof(msg_l.id), data, 1);
|
||||||
|
|
||||||
// Calculate CRC
|
// Calculate CRC
|
||||||
uint16_t crc_value = validate_crc16(crc_data, crc_data_size);
|
uint16_t crc_value = validate_crc16(crc_data, crc_data_size);
|
||||||
|
@ -192,6 +192,7 @@ void send_id() {
|
||||||
// Error handling: logging, alerts, etc.
|
// Error handling: logging, alerts, etc.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t id = flash_rec[addr_id].value;
|
uint8_t id = flash_rec[addr_id].value;
|
||||||
send_can_with_id_crc(id,'I',&id,sizeof(id));
|
send_can_with_id_crc(id,'I',&id,sizeof(id));
|
||||||
__NOP();
|
__NOP();
|
||||||
|
@ -389,10 +390,10 @@ void foc_step(BLDCMotor *motor, Commander *commander) {
|
||||||
|
|
||||||
flash_rec = load_params();
|
flash_rec = load_params();
|
||||||
/* For test */
|
/* For test */
|
||||||
int value = 73; //addr
|
// int value = 73; //addr
|
||||||
write_param(addr_id,value); //for update address in firmware
|
// write_param(addr_id,value); //for update address in firmware
|
||||||
// Load parameters from flash
|
// // Load parameters from flash
|
||||||
flash_rec = load_params();
|
// flash_rec = load_params();
|
||||||
|
|
||||||
for(int i = 0;i < PARAM_COUNT;i++)
|
for(int i = 0;i < PARAM_COUNT;i++)
|
||||||
flash_buf[i] = flash_rec[i];
|
flash_buf[i] = flash_rec[i];
|
||||||
|
|
|
@ -3,35 +3,65 @@ import struct
|
||||||
import time
|
import time
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
# Function to send the target angle
|
# Константы
|
||||||
|
CAN_INTERFACE = 'can0'
|
||||||
|
DEVICE_ID = 0x00 # ID ADDR for servo
|
||||||
|
REG_WRITE = 0x8
|
||||||
|
REG_POS = 0x72 # MOTOR+ANGLE = 0x72
|
||||||
|
|
||||||
|
def validate_crc16(data):
|
||||||
|
# Calculate CRC16
|
||||||
|
crc = 0xFFFF
|
||||||
|
for byte in data:
|
||||||
|
crc ^= byte
|
||||||
|
for _ in range(8):
|
||||||
|
if crc & 0x0001:
|
||||||
|
crc = (crc >> 1) ^ 0xA001
|
||||||
|
else:
|
||||||
|
crc >>= 1
|
||||||
|
return crc
|
||||||
|
|
||||||
def send_target_angle(bus, target_angle):
|
def send_target_angle(bus, target_angle):
|
||||||
msg = can.Message()
|
# ID and cmd
|
||||||
msg.arbitration_id = 1 # Message ID
|
arbitration_id = (DEVICE_ID << 4) | REG_WRITE
|
||||||
msg.is_extended_id = False
|
id_bytes = list(arbitration_id.to_bytes(2, byteorder='little'))
|
||||||
msg.dlc = 5 # Message length
|
|
||||||
msg.data = [ord('A')] + list(struct.pack('<f', target_angle)) # 'A' for the command identifier, followed by the angle in float format
|
# cmd + parametrs
|
||||||
|
data_write = [REG_POS] + list(struct.pack('<f', target_angle))
|
||||||
|
|
||||||
|
|
||||||
|
full_data_for_crc = id_bytes + data_write
|
||||||
|
crc = validate_crc16(full_data_for_crc)
|
||||||
|
crc_bytes = list(crc.to_bytes(2, byteorder='little'))
|
||||||
|
|
||||||
|
# Full packet
|
||||||
|
packet = data_write + crc_bytes
|
||||||
|
|
||||||
|
|
||||||
|
msg = can.Message(
|
||||||
|
arbitration_id=arbitration_id,
|
||||||
|
is_extended_id=False,
|
||||||
|
data=packet
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bus.send(msg)
|
bus.send(msg)
|
||||||
print(f"Sent message with target angle: {target_angle} degrees")
|
print(f"[Отправка] CAN ID: 0x{arbitration_id:03X}, Угол: {target_angle} rad, Данные: {list(msg.data)}")
|
||||||
print(f"Message data: {msg.data}")
|
|
||||||
except can.CanError:
|
except can.CanError:
|
||||||
print("Message failed to send")
|
print("Ошибка отправки сообщения")
|
||||||
|
|
||||||
# Main function
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Send target angles over CAN bus.")
|
parser = argparse.ArgumentParser(description="Отправка угла позиции по CAN.")
|
||||||
parser.add_argument("--angle", type=float, required=True, help="Target angle to send over the CAN bus")
|
parser.add_argument("--angle", type=float, required=True, help="Угол (в градусах)")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
target_angle = args.angle
|
# Инициализация CAN
|
||||||
|
bus = can.interface.Bus(channel=CAN_INTERFACE, bustype='socketcan')
|
||||||
|
print("CAN шина инициализирована.")
|
||||||
|
|
||||||
# CAN interface setup
|
send_target_angle(bus, args.angle)
|
||||||
bus = can.interface.Bus(channel='can0', bustype='socketcan', bitrate=1000000) # Ensure the bitrate matches the microcontroller settings
|
|
||||||
print("CAN bus initialized, sending target angles...")
|
|
||||||
|
|
||||||
# Loop to send messages
|
bus.shutdown()
|
||||||
send_target_angle(bus, target_angle)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue