From b36103201cffc0b349b93379da7b3cf10ae83250 Mon Sep 17 00:00:00 2001 From: lulko Date: Sun, 4 May 2025 22:53:42 +0300 Subject: [PATCH] Add flag for CAN --- controller/fw/embed/src/main.cpp | 50 ++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/controller/fw/embed/src/main.cpp b/controller/fw/embed/src/main.cpp index 6554a89..57b8b31 100644 --- a/controller/fw/embed/src/main.cpp +++ b/controller/fw/embed/src/main.cpp @@ -12,6 +12,7 @@ #include "wiring_analog.h" #include "wiring_constants.h" // clang-format on + #include "reg_cah.h" #include "flash.h" @@ -33,6 +34,10 @@ volatile uint8_t msg_ch; volatile uint8_t crc_h; volatile uint8_t crc_l; +/* bool for test CAN */ +volatile bool CAN_GET = false; + + volatile float kt = 0.1; // Torque calculation constant static FLASH_RECORD* flash_rec; @@ -68,9 +73,7 @@ void doMotor(char *cmd) { delayMicroseconds(2); } -// Empty interrupt handler to prevent default handling -void CAN2_RX0_IRQHandler() { -} + void setup_foc(MagneticSensorAS5045 *encoder, BLDCMotor *motor, DRV8313Driver *driver, LowsideCurrentSense *current_sense, @@ -142,12 +145,12 @@ void send_can_with_id_crc(uint32_t id, uint8_t message_type, const void* data, s Can.write(msg); __NOP(); } - -void send_velocity() { - float current_velocity = motor.shaftVelocity(); - flash_rec = load_params(); - if (flash_rec == nullptr) { // Null check - // Error handling: logging, alerts, etc. + +void send_velocity() { + float current_velocity = motor.shaftVelocity(); + flash_rec = load_params(); + if (flash_rec == nullptr) { // Null check + // Error handling: logging, alerts, etc. return; } uint8_t value = flash_rec[vel].value; @@ -372,9 +375,10 @@ void foc_step(BLDCMotor *motor, Commander *commander) { commander->run(); } -void setup(){ + + void setup(){ // Vector table initialization (commented out) - // __set_MSP(*(volatile uint32_t*)0x08008000); + // __set_MSP(*(volatile uintф32_t*)0x08008000); // SCB->VTOR = (volatile uint32_t)0x08008000; Serial.setRx(HARDWARE_SERIAL_RX_PIN); @@ -384,10 +388,11 @@ void setup(){ pinMode(PC11, OUTPUT); pinMode(PC10,OUTPUT); GPIOC->ODR &= ~GPIO_ODR_OD10; - + // Can.enableMBInterrupts(); Can.begin(); Can.setBaudRate(1000000); - + // Настройка прерываний CAN + CAN2->IER |= CAN_IER_FMPIE0; // Load parameters from flash flash_rec = load_params(); for(int i = 0;i < PARAM_COUNT;i++) @@ -396,21 +401,36 @@ void setup(){ // Initialize FOC system setup_foc(&encoder, &motor, &driver, ¤t_sense, &command, doMotor); + CAN2->IER |= CAN_IER_FMPIE0 | // Сообщение в FIFO0 + CAN_IER_FFIE0 | // FIFO0 full + CAN_IER_FOVIE0; // FIFO0 overflow + HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0); + // HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn); + // Default motor configuration GPIOC->ODR |= GPIO_ODR_OD11; motor.torque_controller = TorqueControlType::foc_current; motor.controller = MotionControlType::torque; __enable_irq(); + } + + void loop() { + __enable_irq(); foc_step(&motor, &command); CAN_message_t msg; GPIOC->ODR ^= GPIO_ODR_OD11; // Toggle status LED delay(500); - // Process incoming CAN messages + // // Process incoming CAN messages while (Can.read(msg)) { + CAN_GET = true; + } + /* If receive data from CAN */ + if(CAN_GET) { listen_can(msg); } -} \ No newline at end of file +} +