C 如何在步进电机运行时拉住线

问题描述 投票:0回答:1

我有 CMSIS RTOS 在 ARM 控制器上运行。一个线程用于执行步进电机运动。 步进电机向一个方向移动 2000 步并返回到起点。

目前,执行run命令后,线程会被循环阻止执行

returnCommand()
函数。循环由由中断调用的步进计数器设置的标志结束。

我尝试使用互斥体解决阻塞问题,但要么没有正确理解该函数,要么没有正确实现它。

感谢您的帮助。

旧代码:

void stepperMotor_thread(void *arg)
{
  runCommand();
  while(!movementDone)
  {
    osDelay(100);
  }
  returnCommand();
} 
void exti_handler()
{
  if (stepsreached)
  {
    stopCommand();
    movementDone = true;
  }  
}

新代码:

osMutexId move_Mutex_ID ;

const osMutexAttr_t Thread_Mutex_attr = {
    "myThreadMutex",    // human readable mutex name
    osMutexPrioInherit, // attr_bits
    NULL,               // memory for control block
    0U                  // size for control block
};

void stepperMotor_thread(void *arg)
{
  move_Mutex_ID = osMutexNew(&Thread_Mutex_attr);
  runCommand();
  osMutexAcquire(move_Mutex_ID , osWaitForever);
  returnCommand();
} 
void exti_handler()
{
  steps++;
  if (stepsreached())
  {
    stopCommand();
    osMutexRelease(move_Mutex_ID );
  } 
}
c multithreading mutex blocking cmsis
1个回答
0
投票

我不太确定这是否是您想要实现的目标。

osSemaphoreId_t movementSemaphore;
const osSemaphoreAttr_t movementSemaphoreAttr = {
    .name = "MovementSemaphore"
};

// Create the semaphore with an initial count of 0 (not available)
movementSemaphore = osSemaphoreNew(1, 0, &movementSemaphoreAttr);

void stepperMotor_thread(void *arg)
{
    runCommand();

    // Wait indefinitely for the semaphore to be signaled
    osSemaphoreAcquire(movementSemaphore, osWaitForever);

    returnCommand();
}

void exti_handler()
{
    if (stepsreached)
    {
        stopCommand();
        osSemaphoreRelease(movementSemaphore); // Signal the semaphore
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.