Skip to content

Commit f987c6c

Browse files
raiden00plxiaoxiang781216
authored andcommitted
examples/foc: make sure that the queue is empty before start the control thread
1 parent 429f5a1 commit f987c6c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

examples/foc/foc_mq.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
int foc_mq_handle(mqd_t mq, FAR struct foc_mq_s *h)
4242
{
4343
int ret = OK;
44-
uint8_t buffer[5];
44+
uint8_t buffer[CONTROL_MQ_MSGSIZE];
4545

4646
/* Get data from AUX */
4747

48-
ret = mq_receive(mq, (char *)buffer, 5, 0);
48+
ret = mq_receive(mq, (char *)buffer, CONTROL_MQ_MSGSIZE, 0);
4949
if (ret < 0)
5050
{
5151
if (errno != EAGAIN)
@@ -65,7 +65,7 @@ int foc_mq_handle(mqd_t mq, FAR struct foc_mq_s *h)
6565

6666
/* Verify message length */
6767

68-
if (ret != 5)
68+
if (ret != CONTROL_MQ_MSGSIZE)
6969
{
7070
PRINTF("ERROR: invalid message length = %d\n", ret);
7171
goto errout;

examples/foc/foc_thr.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static int g_fixed16_thr_cntr = 0;
7070
static FAR void *foc_control_thr(FAR void *arg)
7171
{
7272
FAR struct foc_ctrl_env_s *envp = (FAR struct foc_ctrl_env_s *) arg;
73+
char buffer[CONTROL_MQ_MSGSIZE];
7374
char mqname[10];
7475
int ret = OK;
7576

@@ -116,6 +117,26 @@ static FAR void *foc_control_thr(FAR void *arg)
116117
goto errout;
117118
}
118119

120+
/* Make sure that the queue is empty */
121+
122+
while (1)
123+
{
124+
ret = mq_receive(envp->mqd, buffer, CONTROL_MQ_MSGSIZE, 0);
125+
if (ret < 0)
126+
{
127+
if (errno == EAGAIN)
128+
{
129+
break;
130+
}
131+
else
132+
{
133+
PRINTF("ERROR: mq_receive failed errno=%d\n", errno);
134+
ret = -errno;
135+
goto errout;
136+
}
137+
}
138+
}
139+
119140
/* Select control logic according to FOC device type */
120141

121142
switch (envp->type)

0 commit comments

Comments
 (0)