ST意法半导体
直播中

算一挂

9年用户 962经验值
私信 关注
[问答]

为什么每次使用类方法操作交通灯时程序就会崩溃呢?

我在使用类方法操作交通灯时遇到问题 - 每次程序在那个时候崩溃。
这是一个简化的示例,用于理解我正在写的内容:
班级:
  • class IO_control
  • {
  • int num;
  • public:
  •    osSemaphoreId semaphoreHandle;
  •    int *outputs;
  •    IO_control(int num,int outputs,osSemaphoreId semaphoreHandle);
  •    virtual ~IO_control();
  •    void SetOutputs();
  •    void UnsetOutputs();
  • };
  • /***********************************************/
  • IO_control::IO_control(int num, int *outputs,osSemaphoreId semaphoreHandle)
  • {
  •   this->num = num;
  •   this->outputs = outputs;
  •   this->semaphoreHandle = semaphoreHandle;
  • }
  • IO_control::~IO_control(){}
  • void IO_control::SetOutputs()
  • {
  •   osSemaphoreWait(semaphoreHandle, osWaitForever);
  •   *outputs |= num;
  •   osSemaphoreRelease(semaphoreHandle);
  • }
  • void IO_control::UnsetOutputs()
  • {
  •   osSemaphoreWait(semaphoreHandle, osWaitForever);
  •   *outputs &= ~(num);
  •   osSemaphoreRelease(semaphoreHandle);
  • }
我有三个任务争夺一个变量 i_o_CAN.data_out,特别是该类的四个实例 + 发送一条消息
两次设置 - 第三次将其复制到 CAN 消息中
  • typedef struct can_I_O_struct
  •   {
  •     union
  •     {
  •       int data_out;
  •       uint8_t out_arr[4];
  •     };
  •     union
  •     {
  •       int data_in;
  •       uint8_t in_arr[4];
  •     };
  •   } I_O_struct_can;
  •   I_O_struct_can i_o_CAN;
  •   osSemaphoreId CAN_IO_semaphoreHandle;
  •   IO_control out1(1,&i_o_CAN.data_out,CAN_IO_semaphoreHandle);
  •   IO_control out2(2,&i_o_CAN.data_out,CAN_IO_semaphoreHandle);
  •   IO_control out3(3,&i_o_CAN.data_out,CAN_IO_semaphoreHandle);
  •   IO_control out4(4,&i_o_CAN.data_out,CAN_IO_semaphoreHandle);
  •   void Task01(void const * argument)
  •   {
  •     for(;;)
  •     {
  •       out1.SetOutputs();
  •       osDelay(50);
  •       out1.UnsetOutputs();
  •       osDelay(1000);
  •       out2.SetOutputs();
  •       osDelay(50);
  •       out2.UnsetOutputs();
  •       osDelay(1000);
  •     }
  •   }
  •   void Task02(void const * argument)
  •   {
  •     for(;;)
  •     {
  •       out3.SetOutputs();
  •       osDelay(50);
  •       out3.UnsetOutputs();
  •       osDelay(2000);
  •       out4.SetOutputs();
  •       osDelay(50);
  •       out4.UnsetOutputs();
  •       osDelay(2000);
  •     }
  •   }
  •   void TaskCAN_Tx(void const * argument)
  •   {
  •     int tmp = 0;
  •       TxHeader2.StdId = 0x180;
  •     TxHeader2.DLC = 4;
  •     for(;;)
  •     {
  •       osSemaphoreWait(CAN_IO_semaphoreHandle, osWaitForever);
  •       if (i_o_CAN.data_out != tmp)
  •       {
  •       tmp = i_o_regs_can.data_out;
  •       TxData2[0] = i_o_CAN.out_arr[0];
  •       TxData2[1] = i_o_CAN.out_arr[1];
  •       TxData2[2] = i_o_CAN.out_arr[2];
  •       TxData2[3] = i_o_CAN.out_arr[3];
  •       if (HAL_CAN_AddTxMessage (&hcan2, &TxHeader2, TxData2, &TxMailbox2)!= HAL_OK)
  •         {
  •           Error_Handler ();
  •         }
  •       }
  •           osSemaphoreRelease(CAN_IO_semaphoreHandle);
  •     }
  •   }
当我调整它以便我直接从任务中操作红绿灯时,它起作用了
  • void IO_control::SetOutputs()
  • {
  •   *outputs |= num;
  • }
  • void IO_control::UnsetOutputs()
  • {
  •   *outputs &= ~(num);
  • }
  • //////////////////////////////////////////////////////////////////////
  •   void Task01(void const * argument)
  •   {
  •     for(;;)
  •     {
  •       osSemaphoreWait(CAN_IO_semaphoreHandle, osWaitForever);
  •       out1.SetOutputs();
  •       osSemaphoreRelease(CAN_IO_semaphoreHandle);
  •       osDelay(50);
  •       osSemaphoreWait(CAN_IO_semaphoreHandle, osWaitForever);
  •       out1.UnsetOutputs();
  •       osSemaphoreRelease(CAN_IO_semaphoreHandle);
  •       osDelay(1000);
  •       osSemaphoreWait(CAN_IO_semaphoreHandle, osWaitForever);
  •       out2.SetOutputs();
  •       osSemaphoreRelease(CAN_IO_semaphoreHandle);
  •       osDelay(50);
  •       osSemaphoreWait(CAN_IO_semaphoreHandle, osWaitForever);
  •       out2.UnsetOutputs();
  •       osSemaphoreRelease(CAN_IO_semaphoreHandle);
  •       osDelay(1000);
  •     }
  •   }
但实际上并没有那么简单,我需要从 insatnaci 访问红绿灯——此外,还有很多无用的代码

回帖(1)

李艺银

2023-1-5 15:27:40
但问题与硬件或 freertos 无关
我发现这是 C++ 的问题,并且错误地传递了对类的引用。
这是一个功能性的解决方案:


  • class IO_control {
  •     int num;

  •   public:
  •     osSemaphoreId & semaphoreHandle;
  •     int * outputs;
  •     IO_control (int num, int * outputs, osSemaphoreId & semaphoreHandle);
  •     virtual ~IO_control();

  •     void SetOutputs();
  •     void UnsetOutputs();
  • };
  • /***********************************************/
  • IO_control::IO_control (int _num, int * _outputs, osSemaphoreId & _semaphoreHandle) :
  •   num(_num), semaphoreHandle(_semaphoreHandle), outputs(_outputs) {
  • }
举报

更多回帖

发帖
×
20
完善资料,
赚取积分