Home > Software engineering >  MFC event processing, motor motion control
MFC event processing, motor motion control

Time:10-04

Prospects for description:
In the movement of the control motor, on the one hand, need from the serial port signal movement for motor rotation, on the other hand the motor movement should be feedback after the completion of a signal to tell PC movement has been completed, can receive next movement signals, if no this feedback signal detection, can cause the loss of sports instruction, therefore, motor motion control algorithm should be:
1. Send the movement instruction;
2. Receive feedback signal;
So circulates, until the stop condition,

At present,
Step 1, I use the MSCOMM control used in serial communication, send motor specified movement signal:
m_mscomm. Put_Output (COleVariant (hexdata));//here will hexdata movement data input serial port, and motor sports

Step 2, from a serial port receives the feedback signal, I use the event handling mechanism, firstly defines the global event:
HANDLE hEvent=CreateEvent (NULL, FALSE, FALSE, NULL);
When a serial port receives the motor feedback signal, setting events as a signal:
SetEvent (hEvent);
And wait in the program:
WaitForSingleObject (hEvent, INFINITE);

Hence my motor cycle control for:
the while (1)
{
M_mscomm. Put_Output (COleVariant (hexdata));//send the movement instruction
The WaitForSingleObject (hEvent, INFINITE);//waits to receive feedback signal
}


description:
But the reality is not as I imagined, send sports instruction first, and then wait for receiving feedback signal, after receiving and sending movement instruction, so cycle, makes the motor constantly move forward, I would like to ask, how can I modify to achieve such a motor motion control mode,

CodePudding user response:

To create a manual reset event, debugging debugging

CodePudding user response:

First take a look at your command have it sent to the serial port, received treatment etc, is estimated to be a deadlock

CodePudding user response:

The operation of the first to send signals to an array, the first serial signal sent the first running, set the timer, if the timer time haven't received the feedback (XXX), if you receive send the next command


On a serial port to send and receive can refer to this article, http://blog.csdn.net/cvbtvbwu/article/details/24694739

CodePudding user response:

reference cvbtvbwu reply: 3/f
is going to send a signal to an array of first, first of all, a serial port to send the first run signal, set the timer, if the timer time haven't received the feedback (XXX), if you receive send the next command


On a serial port to send and receive can refer to this article, http://blog.csdn.net/cvbtvbwu/article/details/24694739


Hello! Thank you for your answer! This method should have a shortage is that the timer time is fixed, once the program let motor go a big step rotation time beyond the timer time, wrong will appear; On the other hand, if the motor is go a little steps the rotation time is less than the timer time, motor running will appear a meal,

CodePudding user response:

reference 4 floor xinxindiandeng00 response:
Quote: reference cvbtvbwu reply: 3/f

The operation of the first to send signals to an array, the first serial signal sent the first running, set the timer, if the timer time haven't received the feedback (XXX), if you receive send the next command


On a serial port to send and receive can refer to this article, http://blog.csdn.net/cvbtvbwu/article/details/24694739


Hello! Thank you for your answer! This method should have a shortage is that the timer time is fixed, once the program let motor go a big step rotation time beyond the timer time, wrong will appear; On the other hand, if the motor is go a little steps the rotation time is smaller than the timer length, will appear the motor running a hot meal,


Can't set the timer is small? 500 milliseconds check received data

CodePudding user response:

The crux of the problem is that if the program to write is:
m_mscomm. Put_Output (COleVariant (hexdata));//send the movement instruction
The WaitForSingleObject (hEvent, INFINITE);//waits to receive feedback signal

Found that the program will stuck in the WaitForSingleObject (hEvent, INFINITE); Causes the MSCOMM control message processing function can't response motor feedback signal

To open up a thread, dedicated to wait for receiving feedback signal:
HThread=CreateThread (NULL, 0, ThreadProc, NULL, 0, NULL);
Waiting for a serial port feedback signal in the thread processing function, thus the motor control program for:

void CautoDlg: : OnBnClickedFrontButton ()
{
//TODO: add the control notification handler code
M_mscomm. Put_Output (COleVariant (hexdata));
HThread=CreateThread (NULL, 0, ThreadProc, NULL, 0, NULL);
}
DWORD WINAPI ThreadProc (LPVOID lpParam)
{
DWORD dReturn=WaitForSingleObject (hEvent, INFINITE);
If (WAIT_OBJECT_0==dReturn)
{
AfxMessageBox (_T (" feedback signal has been received!" ));
}
return 0;
}


Program is running normally, but can only press a button, motor rotation, but at the moment, I don't know how to realize the circulation to send? Motor movement signal cycle, feedback signal and detection, a bit not clear, how to implement this in a while loop or other means a cycle,

CodePudding user response:

Position loop control?

CodePudding user response:

To ask this question, I find for a long time, no one pay attention to this thing,
My original idea is to open two threads, a send unlimited movement instruction, then the infinite loop to wait for, another thread to monitor events of the serial port at the same time, as soon as we receive limit signals, then jumped out of the first death cycle, but easy to cause a suspended animation...
No one can show the problem, good care of mulberry

CodePudding user response:

Have certainly need a timeout control response way, timeout way if you can use instead of blocking the way will be better, such as function design is
Input parameters: input data, timeout
Returns: receiving data, successful or not

CodePudding user response:

Is a communication protocol,,,

Upper machine adopts the completion port to do; Your search "completion port",,, single chip microcomputer with interrupt,

You don't is to G command parsing?? CNC numerical control? It is said that there are a lot of open source, call GBLR?? Or GLBR seems, ardnio on pretty much,

CodePudding user response:

To a member variable set?
Void CautoDlg: : OnBnClickedFrontButton ()
{
//TODO: add the control notification handler code
M_mscomm. Put_Output (COleVariant (hexdata));
HThread=CreateThread (NULL, 0, ThreadProc, NULL, 0, NULL);
Set the=1;
While (set)
{
M_mscomm. Put_Output (COleVariant (hexdata));
}
}

In the end to a button control?

CodePudding user response:

Whether need the signal

CodePudding user response:

"On the other hand, motor sports should be feedback after the completion of a signal to tell upper machine movement has been completed,"
Feedback via a serial port, that is to get a character such as' OK '

CodePudding user response:

Be careful, "returned empty error"
  • Related