Re: Loop problem: CPU usage
Quote:
Originally Posted by shoppinit
but I need to poll at a period of not less more than 0.1 milliseconds
Not sure what this really means. However, I doubt you are going to be able to get that kind of accuracy in Windows. Your best bet is either timers, or sleep for 1 ms.
You could try to "Sleep(0);" as soon as you're done polling. But, I'm not sure how much that'll buy you. Sleeping for 0ms effectively gives up the rest of your timeslice.
Viggy
Re: Loop problem: CPU usage
Thanks for that. I meant that I must poll more frequently than once every 1ms. Probably about 10 times every 1ms.
I'll try the Sleep(0) - didn't occur to me before.
Re: Loop problem: CPU usage
The Sleep(0) doesn't reduce CPU usage, but this isn't surprising since I guess unless it's got some other thread to run it'll stay in this thread.
Interestingly my other threads seem to run better (more predictably), so thanks for the tip, Viggy!
I'd still be interested in finding a way to reduce the frequency of the polling, though. Right now it's entering the loop once every 0.15 µs approx.
I just don't like while(true) loops, but maybe this is necessary in this case.
Ben.
Re: Loop problem: CPU usage
Well, unless you can poll > 1ms, you can't reduce the CPU usage. Windows is not a RTOS.
Viggy
Re: Loop problem: CPU usage
Quote:
Originally Posted by shoppinit
...but I need to poll at a period of not less more than 0.1 milliseconds, so windows timers are out.
Windows in standard user mode can not do it. The fastest available stuff is a multi-media event buth they only go down to order 1ms. If you really have real-time constraints on the order of 100µs, then you need to write a kernel driver or use an external device.
Could you please briefly explain why you need these kinds of timing constraints. Sometimes there can be a design simplification allowing for the use of standard timers and events.
Sincerely, Chris.
Re: Loop problem: CPU usage
Here's about all I can come up with.......
While your resolution is very small (smaller than Windows is designed to handle), perhaps the frequency of release isn't so demanding?
Here's what I mean. Imagine your schedule of the next 1 second were laid out in a linear calendar. Do you have something to be fired off every 0.1ms?
Probably not, but then I don't know. Let's say you don't, at least on occasion.
The resolution I get from waiting on events with release times stated at 1ms is as high as 16ms on my hardware, and perhaps even 20ms on some occasions, meaning the highest resolution I can afford to predict I can release a thread after some kind of hold is about 20ms or so.
If I sorted the schedule (using a tree if the insertion activity is high), then I could tell if the next time for a release is within this 20ms target zone.
If the next release is inside 20ms, then the loop must run full steam.
On the other hand, if there's, say, 40ms before the next release is due, I could schedule a wait (or Sleep or whatever) for 20ms.
I used this approach in an animation scheduling mechanism some time ago. We were simulating a walk through in an artist's installation presentation. We wanted to show how the 'system' - a series of sensors, lights, videos, music - etc.. would work with several people walking through.
I recorded the motion of the 'people' using the mouse drag, noting the time of each motion sample.
The resolution wasn't near your demand, but essentially I could look into the 'next position' of the various 'people' in the simulation, and just Sleep until a short time before that next position was due.
As I recall, every time I came out of a sleep, I checked the remaining time, and slept again (for a shorter duration) if there was enough time. I never 'slept' the full delay to target, always about 60% or so - never missing a schedule release of the next position. It demanded nothing from the CPU.
Obviously if you've got releases constantly inside the margin of your timed release, this can't help - but only you know your data.
Re: Loop problem: CPU usage
Hi JVene,
You're quite right, the frequency of arriving objects is less than the polling frequency and my thread that detects a new object arriving uses Events so I can know fairly precisely the time window that I'd need to scan in. That's an interesting idea and I'm going to implement it to see what the results are like.
Something else also occured to me. My I/O card has an external rate generator that can generate interrupts at a programmable frequency. I've never tried to use this and the SDK is a nightmare, but it might be worth investigating. What do you think?
Thanks!
Ben.
Re: Loop problem: CPU usage
Hi Chris,
Thanks for your reply, I missed it first time round. The app detects objects on a conveyor belt that arrive unpredictably - I use a proximity switch for this. 500 ms after their detection, some action must be taken on them. The window for taking this action is about 2ms.
As soon as the object is detected, the app writes it's detection date into a list as well as the date that action must be taken and this is what's being polled.
I can't see myself writing a Kernel driver - I think that's well outside of my abilities. I have seen that 3rd party realtime plug-ins exist for Windows but I have absolutely no experience of them. I think Venturcom RTX was one I heard of.
Do you know anything about these realtime plug-ins? Are they difficult to implement?
Thanks.
Ben.
Re: Loop problem: CPU usage
Quote:
I use a proximity switch for this. 500 ms after their detection, some action must be taken on them. The window for taking this action is about 2ms.
I have been developing realtime systems professionally for nearly 30 years. Would not even consider using windows to directly attempt this. A simple microcontroller (e.g. PIC) would be much more appropriate.
Re: Loop problem: CPU usage
Quote:
Originally Posted by TheCPUWizard
I have been developing realtime systems professionally for nearly 30 years. Would not even consider using windows to directly attempt this. A simple microcontroller (e.g. PIC) would be much more appropriate.
I just wanted to write back and indicate that I do all of this kind of stuff with an intermediate microcontroller device. But TheCPUWizard beat me to it. Years ago I wrote some kernel mode device drivers. However, for the past 10 years or so I have done all of these kinds of things with simple microcontroller boards. The intermediate board is much simpler to deal with than a kernel level driver. I use 8051, Infineon C167, Motorola HC08 as well as NEC V850.
You can get a simple starter kit with a modern flash microcontroller for around 100 dollars or maybe a few hundred dollars. There will be samples and demo compiler versions.
But just for further information, I wrote a simple sample using a Windows Multi-Media Timer. This is about as close as you can get to 1ms resolution in the Windows User Mode. See the sample below.
But heed the words of TheCPUWizard. Windows will never guarantee real-time response times in user mode.
Sincerely, Chris.
Code:
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
// link with multimedia library
#pragma comment(lib, "winmm.lib")
inline DWORD TickSet(const DWORD t) { return ::GetTickCount() + t; }
inline bool TickTimeout(const DWORD tID) { return ::GetTickCount() - tID < static_cast<DWORD>(0x7FFFFFFFu); }
int main(void)
{
// Create event and periodic 1ms multi-media timer.
const HANDLE mm_event = ::CreateEvent(0, FALSE, FALSE, 0);
const MMRESULT result = ::timeSetEvent(1u,
1u,
static_cast<LPTIMECALLBACK>(mm_event),
static_cast<DWORD_PTR>(0),
TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
// Something just happenend and I want to check something every millisecond.
const DWORD tID = ::TickSet(500u);
int count = 0;
while(!TickTimeout(tID))
{
::WaitForSingleObject(mm_event, 500);
++count;
}
std::cout << "The millisecond count is: " << count << std::endl;
}
Re: Loop problem: CPU usage
Thanks for the support dude :wave: :wave:
One other opportunity (if all the work, MUST be done on "PC Hardware"....) is to check out the QNX operating system. It is a "Unix like" system that is specifically designed for real time operations.
[I was lead architect on one of the first all digital professional audio consoles about a decade ago (when 200Mhz was the fastest Intel processor that money could buy). Some of the timning requirements were on the order of 50uS (0.05 mS) and it was no problem (but did require care). With processor speeds now more than 10 times faster........]
Re: Loop problem: CPU usage
Thanks for all the useful info, guys. I'll definitely consider the microcontroller route. Can you recommend one for a first-time user ? I've never had any experience with them and I'd like to avoid assembler as far as possible.
With a microcontroller would I be able to this:
1. Microcontroller detects object presence (date)
2. It initiates action N°1 at a later date and informs the PC (date + x ms)
3. Upon being informed, the PC carries out analysis on the object and makes a decision whether the action N°2 should be A or B
4. PC informs the microcontroller of the action to carry out (A or B)
5. Microcontroller initiates action N°2 (date + x + y ms)
Is this kind of communication between the PC and microcontoller possible? Since the object arrive asynchronously, this kind of interaction would require object tracking, I guess.
I'm sorry if these sound like silly questions. Like I said, I have no experience of using microcontrollers.
Thanks.
Ben.
Re: Loop problem: CPU usage
You have the basics down 100% :)
It has been a few [3+] years since I have dealt with uControillers. They change even faster than PC's :eek: So I can not really recommend a specific one.
Almost every uC comes with "C" and the majority of them come with "C++". The library functions are sometimes limited (e.g. no-STL) because the intent is to write the highest performance code.
Communication with the PC can be done with the serial port for almost every uC, and some have USB communication.
Re: Loop problem: CPU usage
Quote:
Originally Posted by shoppinit
Thanks for all the useful info, guys. I'll definitely consider the microcontroller route. Can you recommend one for a first-time user ?
For a high-power 32-bit machine with a really good on-chip dubugger, I recomment the NEC V850 line. The first link is a list of microcontroller starter kits. The second link is my recommendation for NEC V850.
http://www.am.necel.com/esales/
http://www.am.necel.com/esales/order...-MINI2-V850/J2
For a low-end controller you can use any 8051 for example the Infineon XC800 series with a demo compiler from Keil (an ARM company). Zilog recently put out a new line of flash micros with a good starter kit. It's called the Z8 Encore. My brother had good results with the starter kit, but I can't find the link right now.
Quote:
Originally Posted by shoppinit
I've never had any experience with them and I'd like to avoid assembler as far as possible.
The NEC starter kits come with demo C/C++ compilers from IAR. The Zilog stuff comes with optimizing C compiler.
Quote:
Originally Posted by shoppinit
With a microcontroller would I be able to this:
1. Microcontroller detects object presence (date)
2. It initiates action N°1 at a later date and informs the PC (date + x ms)
3. Upon being informed, the PC carries out analysis on the object and makes a decision whether the action N°2 should be A or B
4. PC informs the microcontroller of the action to carry out (A or B)
5. Microcontroller initiates action N°2 (date + x + y ms)
Any one you can find. You need to take a bit of time to get accustomed to µC design. The flash tools, build environment, etc. Please note that microcontrollers do not have anything like a CMOS clock. They do not 'know' the date and time, only relative times from the startup.
Quote:
Originally Posted by shoppinit
Is this kind of communication between the PC and microcontoller possible? Since the object arrive asynchronously, this kind of interaction would require object tracking, I guess.
Yes. RS232 can be a very good and simple way to go, as mentioned earlier. I also use some high-end controller boards with USB as well as TCP/IP.
Sincerely, Chris.